-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrose_curve.tscn
More file actions
134 lines (105 loc) · 2.75 KB
/
Copy pathrose_curve.tscn
File metadata and controls
134 lines (105 loc) · 2.75 KB
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
[gd_scene load_steps=4 format=2]
[ext_resource path="res://rose_curve.gd" type="Script" id=1]
[sub_resource type="Shader" id=1]
code = "shader_type canvas_item;
// TODO: Proper denominator handling
// TODO: Automatic iteration count calculation
uniform float scale: hint_range(0.0, 10.0) = 1.0;
uniform float offset = 0.0;
uniform float numerator: hint_range(1, 7);
uniform float denominator: hint_range(1, 9);
uniform int max_iters: hint_range(1, 4096) = 8;
uniform bool use_time = false;
uniform float time = 0.0;
const float PI = 3.14159;
const float TAU = 6.28318;
const float INF = 1.0 / 0.0;
vec2 center_uv(vec2 uv) {
return (uv * 2.0) - 1.0;
}
vec2 cartesian_to_polar(vec2 cartesian) {
// Radius
float r = length(cartesian);
// Angle
float theta = atan(cartesian.y, cartesian.x);
if(cartesian.y < 0.0) {
theta += TAU;
}
return vec2(theta, r);
}
// Normalized sine - takes input in 0..1 range instead of 0..PI
float sin_norm(float k) {
return sin(k * PI);
}
// Normalized cosine - takes input in 0..1 range instead of 0..PI
float cos_norm(float k) {
return cos(k * PI);
}
void fragment() {
vec2 uv = center_uv(UV);
vec2 polar = cartesian_to_polar(uv);
float o = polar.x;
float r = polar.y;
float k = numerator / denominator;
float dist = INF;
float cycle_fac = 0.5 * cos_norm(k) + 0.5;
float denom = 1.0 / k;
float denom_fac = 0.5 * cos_norm(denom) + 0.5;
float curve_length;
float curve_step = curve_length / k;
if (k >= 1.0) {
curve_length = PI * cycle_fac;
curve_step = curve_length / k;
}
else {
curve_length = PI / (denom + denom_fac);
curve_step = PI;
}
int iters;
if (k >= 1.0) {
iters = max_iters;
}
else {
iters = max_iters;
}
for(int i = 0; i < iters; ++i) {
float lo = o - (float(i) * curve_step);
float ko = k * lo;
float c = cos(ko);
float lr = scale * c + offset;
float d = abs(r - lr);
dist = min(dist, d);
}
vec2 frag_size = fwidth(UV);
float frag_width = max(frag_size.x, frag_size.y);
frag_width /= polar.y;
frag_width *= scale;
frag_width *= max(1.0, k);
frag_width *= 2.0;
float fac = smoothstep(frag_width, 0.0, dist);
COLOR = vec4(vec3(fac), 1);
}"
[sub_resource type="ShaderMaterial" id=2]
shader = SubResource( 1 )
shader_param/scale = 1.0
shader_param/offset = 0.0
shader_param/numerator = 5.0
shader_param/denominator = 7.0
shader_param/max_iters = 10
shader_param/use_time = false
shader_param/time = 0.0
[node name="Control" type="ColorRect"]
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0, 0, 0, 1 )
script = ExtResource( 1 )
[node name="ColorRect" type="ColorRect" parent="."]
material = SubResource( 2 )
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -254.311
margin_top = -255.5
margin_right = 256.689
margin_bottom = 255.5