yum-archive/2ner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/2ner

yumadd hypotrochoid gimmickf1fc3bd

master
3.7 KiB117 linesraw
1#ifndef __TROCHOID_MATH
2#define __TROCHOID_MATH
3
4#if defined(_TROCHOID)
5
6#include "globals.cginc"
7
8#define PI 3.14159265
9#define TAU PI * 2.0
10
11float3 _trochoid_map(float theta, float r0, float vert_z)
12{
13  r0 = pow(r0, _Trochoid_r_Power);
14  r0 *= 100;
15
16  float R = _Trochoid_R;
17  float r = _Trochoid_r;
18  float d = _Trochoid_d;
19
20  theta *= max(R, r) * _Trochoid_theta_k;
21  float theta_t = theta + _Time[2] * _Trochoid_t_k;
22
23  float x = (R - r) * cos(theta_t) + d * cos((R - r) * theta_t / r);
24  float y = (R - r) * sin(theta_t) - d * sin((R - r) * theta_t / r);
25  float z = vert_z + cos(theta_t * 5) * .1 + theta * .0002;
26
27  float3 result = float3(x, y, z) * r0;
28  float3 trochoid_scale = float3(_Trochoid_X_Scale, _Trochoid_Y_Scale, _Trochoid_Z_Scale);
29  result *= trochoid_scale;
30  return result;
31}
32
33float3 trochoid_map(float3 pos)
34{
35  float theta = atan2(pos.y, pos.x);
36  float r0 = length(pos.xy) * theta;
37  float z = pos.z;
38  float3 result = _trochoid_map(theta, r0, z);
39  return result;
40}
41
42float3 trochoid_normal(float3 pos)
43{
44    // Parameters of the trochoid
45    float R = _Trochoid_R;
46    float r = _Trochoid_r;
47    float d = _Trochoid_d;
48    float theta_k = max(R, r) * _Trochoid_theta_k;
49    float t_k = _Trochoid_t_k;
50    float time = _Time[2];
51    float r_power = _Trochoid_r_Power;
52
53    // Intermediate variables based on input position
54    float r_orig = max(length(pos.xy), 1e-5); // Avoid division by zero
55    float theta = atan2(pos.y, pos.x);
56    
57    float r0 = r_orig * theta;
58    float theta_scaled = theta * theta_k;
59    float theta_t = theta_scaled + time * t_k;
60
61    // Components of the trochoid function before final scaling
62    float C1 = cos(theta_t);
63    float S1 = sin(theta_t);
64    float common_factor = (R - r) / r;
65    float C2 = cos(common_factor * theta_t);
66    float S2 = sin(common_factor * theta_t);
67
68    float p_x = (R - r) * C1 + d * C2;
69    float p_y = (R - r) * S1 - d * S2;
70    float p_z = pos.z + cos(theta_t * 5.0) * 0.1 + theta * 0.0002;
71
72    // Partial derivatives of the components with respect to theta_t
73    float dp_x_dt = -(R - r) * S1 - d * common_factor * S2;
74    float dp_y_dt =  (R - r) * C1 - d * common_factor * C2;
75    float dp_z_dt = -sin(theta_t * 5.0) * 5.0 * 0.1;
76
77    // Partial derivatives of intermediate variables w.r.t. pos.x and pos.y
78    float d_theta_dx = -pos.y / (r_orig * r_orig);
79    float d_theta_dy =  pos.x / (r_orig * r_orig);
80    float d_r_orig_dx = pos.x / r_orig;
81    float d_r_orig_dy = pos.y / r_orig;
82
83    // The r0-dependent scaling factor and its derivative
84    float F = 100.0 * pow(r0, r_power);
85    float dF_dr0 = 100.0 * r_power * pow(r0, r_power - 1.0);
86
87    // Chain rule for tangents
88    // Tangent with respect to pos.x
89    float d_r0_dx = d_r_orig_dx * theta + r_orig * d_theta_dx;
90    float d_theta_t_dx = d_theta_dx * theta_k;
91    float dF_dx = dF_dr0 * d_r0_dx;
92
93    float dp_x_dx = dp_x_dt * d_theta_t_dx;
94    float dp_y_dx = dp_y_dt * d_theta_t_dx;
95    float dp_z_dx = dp_z_dt * d_theta_t_dx + 0.0002 * d_theta_dx;
96    
97    float3 trochoid_scale = float3(_Trochoid_X_Scale, _Trochoid_Y_Scale, _Trochoid_Z_Scale);
98    float3 T_x = trochoid_scale * (dF_dx * float3(p_x, p_y, p_z) + F * float3(dp_x_dx, dp_y_dx, dp_z_dx));
99
100    // Tangent with respect to pos.y
101    float d_r0_dy = d_r_orig_dy * theta + r_orig * d_theta_dy;
102    float d_theta_t_dy = d_theta_dy * theta_k;
103    float dF_dy = dF_dr0 * d_r0_dy;
104    
105    float dp_x_dy = dp_x_dt * d_theta_t_dy;
106    float dp_y_dy = dp_y_dt * d_theta_t_dy;
107    float dp_z_dy = dp_z_dt * d_theta_t_dy + 0.0002 * d_theta_dy;
108
109    float3 T_y = trochoid_scale * (dF_dy * float3(p_x, p_y, p_z) + F * float3(dp_x_dy, dp_y_dy, dp_z_dy));
110    
111    return normalize(cross(T_x, T_y));
112}
113
114#endif  // _TROCHOID
115
116#endif  // __TROCHOID_MATH
117