forked from rodrigomas/DCTLs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gear.dctl
166 lines (144 loc) · 3.55 KB
/
Gear.dctl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Gear DCTL
#define PI 3.1415926536f
typedef struct
{
float t;
float gearR;
float teethH;
float teethR;
float teethCount;
float diskR;
float3 color;
} Gear;
__DEVICE__ Gear make_gear(float A, float B, float C, float D, float E, float F, float3 G)
{
Gear H;
H.t = A;
H.gearR = B;
H.teethH = C;
H.teethR = D;
H.teethCount = E;
H.diskR = F;
H.color = G;
return H;
}
__DEVICE__ float2 add(float2 A, float2 B)
{
float2 C;
C.x = A.x + B.x;
C.y = A.y + B.y;
return C;
}
__DEVICE__ float2 divide(float2 A, float B)
{
float2 C;
C.x = A.x / B;
C.y = A.y / B;
return C;
}
__DEVICE__ float length(float2 A)
{
float B = _sqrtf(A.x * A.x + A.y * A.y);
return B;
}
__DEVICE__ float2 make_float2(float A)
{
return make_float2(A, A);
}
__DEVICE__ float3 make_float3(float A)
{
return make_float3(A, A, A);
}
__DEVICE__ float2 minus(float2 A, float2 B)
{
float2 C;
C.x = A.x - B.x;
C.y = A.y - B.y;
return C;
}
__DEVICE__ float3 mix(float3 A, float3 B, float C)
{
float3 D;
D.x = A.x * (1.0f - C) + B.x * C;
D.y = A.y * (1.0f - C) + B.y * C;
D.z = A.z * (1.0f - C) + B.z * C;
return D;
}
__DEVICE__ float2 multi(float A, float2 B)
{
float2 C;
C.x = A * B.x;
C.y = A * B.y;
return C;
}
__DEVICE__ float3 multi(float3 A, float B)
{
float3 C;
C.x = A.x * B;
C.y = A.y * B;
C.z = A.z * B;
return C;
}
__DEVICE__ float smoothstep(float edge0, float edge1, float x)
{
float t = _saturatef((x - edge0) / (edge1 - edge0));
return t * t * (3.0f - 2.0f * t);
}
__DEVICE__ float GearFunction(float2 uv, Gear g)
{
float r = length(uv);
float a = _atan2f(uv.y, uv.x);
float p = g.gearR - 0.5f * g.teethH + g.teethH / (1.0f + _expf(g.teethR * _sinf(g.t + g.teethCount * a)));
float gear = r - p;
float disk = r - g.diskR;
return g.gearR > g.diskR ? _fmaxf(-disk, gear) : _fmaxf(disk, -gear);
}
__DEVICE__ float GearDe(float2 uv, Gear g)
{
float f = GearFunction(uv, g);
float2 eps = make_float2(0.0001f, 0.0f);
float2 grad = divide(make_float2(
GearFunction(add(uv, eps), g) - GearFunction(minus(uv, eps), g),
GearFunction(add(uv, make_float2(eps.y, eps.x)), g) - GearFunction(minus(uv, make_float2(eps.y, eps.x)), g)), (2.0f * eps.x));
return (f)/length(grad);
}
__DEVICE__ float GearShadow(float2 uv, Gear g)
{
float r = length(add(uv, make_float2(0.1f)));
float de = r - g.diskR + 0.0f * (g.diskR - g.gearR);
float eps = 0.4f * g.diskR;
return smoothstep(eps, 0.0f, _fabs(de));
}
__DEVICE__ void DrawGear(float3 *color, float2 uv, Gear g, float eps)
{
float d = smoothstep(eps, -eps, GearDe(uv, g));
float s = 1.0f - 0.7f * GearShadow(uv, g);
*color = mix(multi(*color, s), g.color, d);
}
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, __TEXTURE__ p_TexR, __TEXTURE__ p_TexG, __TEXTURE__ p_TexB)
{
//float iTime = _tex2D(p_TexR, 500, 500) / 24;
float iTime = _tex2D(p_TexR, 500, 500) * 10;
float X = (float)p_X;
float Y = (float)(p_Height - p_Y);
float2 resolution = make_float2((float)p_Width, (float)p_Height);
float t = 0.5f * iTime;
float2 uv;
uv.x = 2.0f * (X - 0.5f * resolution.x) / resolution.y;
uv.y = 2.0f * (Y - 0.5f * resolution.y) / resolution.y;
float eps = 2.0f / resolution.y;
float3 base = make_float3(0.95f, 0.7f, 0.2f);
float count = 8.0f;
Gear outer = make_gear(0.0f, 0.8f, 0.08f, 4.0f, 32.0f, 0.9f, base);
Gear inner = make_gear(0.0f, 0.4f, 0.08f, 4.0f, 16.0f, 0.3f, base);
float3 color = make_float3(0.0f);
for(float i = 0.0f; i < count; i++)
{
t += 2.0f * PI / count;
inner.t = 16.0f * t;
inner.color = multi(base, (0.35f + 0.6f * i / (count - 1.0f)));
DrawGear(&color, add(uv, multi(0.4f, make_float2(_cosf(t), _sinf(t)))), inner, eps);
}
DrawGear(&color, uv, outer, eps);
return color;
}