@xort/cga 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/THIRD_PARTY_NOTICES.md +29 -0
- package/build/@xort_cga.js +14908 -8876
- package/build/cga.d.ts +1266 -448
- package/build/cga.esm.js +14891 -8895
- package/build/shaders/cga.glsl +204 -0
- package/build/shaders/common.glsl +36 -0
- package/build/shaders/noise/GLSL-Noise.md +1108 -0
- package/build/shaders/noise/LICENSE +20 -0
- package/build/shaders/noise/cellular2D.glsl +69 -0
- package/build/shaders/noise/cellular2x2.glsl +64 -0
- package/build/shaders/noise/cellular2x2x2.glsl +88 -0
- package/build/shaders/noise/cellular3D.glsl +187 -0
- package/build/shaders/noise/classicnoise2D.glsl +113 -0
- package/build/shaders/noise/classicnoise3D.glsl +177 -0
- package/build/shaders/noise/classicnoise4D.glsl +302 -0
- package/build/shaders/noise/noise2D.glsl +71 -0
- package/build/shaders/noise/noise3D.glsl +103 -0
- package/build/shaders/noise/noise3Dgrad.glsl +111 -0
- package/build/shaders/noise/noise4D.glsl +129 -0
- package/build/shaders/noise/psrdnoise2D.glsl +463 -0
- package/dist/THIRD_PARTY_NOTICES.md +29 -0
- package/dist/index.cjs +14908 -8876
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1266 -448
- package/dist/index.d.ts +1266 -448
- package/dist/index.js +14891 -8895
- package/dist/index.js.map +1 -1
- package/dist/shaders.cjs +21 -0
- package/dist/shaders.d.cts +20 -0
- package/dist/shaders.d.ts +20 -0
- package/dist/shaders.js +20 -0
- package/dist/three.cjs +3034 -0
- package/dist/three.cjs.map +1 -0
- package/dist/three.d.cts +308 -0
- package/dist/three.d.ts +308 -0
- package/dist/three.js +3007 -0
- package/dist/three.js.map +1 -0
- package/package.json +42 -5
- package/readme.md +68 -119
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Host declares u_time, u_resolution and u_mouse before this chunk.
|
|
2
|
+
#ifndef PI
|
|
3
|
+
#define PI 3.141592653589793
|
|
4
|
+
#endif
|
|
5
|
+
#ifndef TWO_PI
|
|
6
|
+
#define TWO_PI 6.283185307179586
|
|
7
|
+
#endif
|
|
8
|
+
#define rx (1.0 / min(u_resolution.x, u_resolution.y))
|
|
9
|
+
#define st coord(gl_FragCoord.xy)
|
|
10
|
+
vec2 coord(in vec2 p);
|
|
11
|
+
/* Pixel unit conversion function */
|
|
12
|
+
vec2 pos(in float x, in float y) { return st + vec2(x * rx, y * rx); }
|
|
13
|
+
vec2 pos(in float x) { return pos(x, x); }
|
|
14
|
+
vec2 pos(in vec2 p) { return pos(p.x, p.y); }
|
|
15
|
+
float size(in float x) { return x * rx; }
|
|
16
|
+
vec2 size(in float x, in float y) { return vec2(x * rx, y * rx); }
|
|
17
|
+
|
|
18
|
+
/* Staggered animation */
|
|
19
|
+
struct Animation { float time; float pow; };
|
|
20
|
+
Animation animation = Animation(0.0, 0.0);
|
|
21
|
+
void totalTime(in float t, in float offset) { animation.time = mod(u_time + offset, t); }
|
|
22
|
+
void totalTime(in float t) { totalTime(t, 0.0); }
|
|
23
|
+
bool between(in float duration, in float offset) {
|
|
24
|
+
float p = (animation.time - offset) / duration;
|
|
25
|
+
animation.pow = p;
|
|
26
|
+
animation.time -= (duration + offset);
|
|
27
|
+
return (p >= 0.0 && p <= 1.0);
|
|
28
|
+
}
|
|
29
|
+
bool between(in float duration) { return between(duration, 0.0); }
|
|
30
|
+
|
|
31
|
+
/* Color palette */
|
|
32
|
+
#define BLACK vec3(0.0, 0.0, 0.0)
|
|
33
|
+
#define WHITE vec3(1.0, 1.0, 1.0)
|
|
34
|
+
#define RED vec3(1.0, 0.0, 0.0)
|
|
35
|
+
#define GREEN vec3(0.0, 1.0, 0.0)
|
|
36
|
+
#define BLUE vec3(0.0, 0.0, 1.0)
|
|
37
|
+
#define YELLOW vec3(1.0, 1.0, 0.0)
|
|
38
|
+
#define CYAN vec3(0.0, 1.0, 1.0)
|
|
39
|
+
#define MAGENTA vec3(1.0, 0.0, 1.0)
|
|
40
|
+
#define ORANGE vec3(1.0, 0.5, 0.0)
|
|
41
|
+
#define PURPLE vec3(1.0, 0.0, 0.5)
|
|
42
|
+
#define LIME vec3(0.5, 1.0, 0.0)
|
|
43
|
+
#define ACQUA vec3(0.0, 1.0, 0.5)
|
|
44
|
+
#define VIOLET vec3(0.5, 0.0, 1.0)
|
|
45
|
+
#define AZUR vec3(0.0, 0.5, 1.0)
|
|
46
|
+
|
|
47
|
+
/* Coordinate and unit utils */
|
|
48
|
+
vec2 coord(in vec2 p) {
|
|
49
|
+
p = p / u_resolution.xy;
|
|
50
|
+
// correct aspect ratio
|
|
51
|
+
if (u_resolution.x > u_resolution.y) {
|
|
52
|
+
p.x *= u_resolution.x / u_resolution.y;
|
|
53
|
+
p.x += (u_resolution.y - u_resolution.x) / u_resolution.y / 2.0;
|
|
54
|
+
} else {
|
|
55
|
+
p.y *= u_resolution.y / u_resolution.x;
|
|
56
|
+
p.y += (u_resolution.x - u_resolution.y) / u_resolution.x / 2.0;
|
|
57
|
+
}
|
|
58
|
+
// centering
|
|
59
|
+
p -= 0.5;
|
|
60
|
+
p *= vec2(-1.0, 1.0);
|
|
61
|
+
return p;
|
|
62
|
+
}
|
|
63
|
+
#define uv gl_FragCoord.xy / u_resolution.xy
|
|
64
|
+
#define mx coord(u_mouse)
|
|
65
|
+
|
|
66
|
+
/* Signed distance drawing methods */
|
|
67
|
+
float fill(in float d) { return 1.0 - smoothstep(0.0, rx * 2.0, d); }
|
|
68
|
+
float stroke(in float d, in float t) { return 1.0 - smoothstep(t - rx * 1.5, t + rx * 1.5, abs(d)); }
|
|
69
|
+
vec3 draw(in sampler2D t, in vec2 pos, in vec2 w) { vec2 s = w / 1.0; s.x *= -1.0; return texture2D(t, pos / s + 0.5).rgb; }
|
|
70
|
+
/* Field Adapted from https://www.shadertoy.com/view/XsyGRW */
|
|
71
|
+
vec3 field(float d) {
|
|
72
|
+
const vec3 c1 = mix(WHITE, YELLOW, 0.4);
|
|
73
|
+
const vec3 c2 = mix(WHITE, AZUR, 0.7);
|
|
74
|
+
const vec3 c3 = mix(WHITE, ORANGE, 0.9);
|
|
75
|
+
const vec3 c4 = BLACK;
|
|
76
|
+
float d0 = abs(stroke(mod(d + 0.1, 0.2) - 0.1, 0.004));
|
|
77
|
+
float d1 = abs(stroke(mod(d + 0.025, 0.05) - 0.025, 0.004));
|
|
78
|
+
float d2 = abs(stroke(d, 0.004));
|
|
79
|
+
float f = clamp(d * 0.85, 0.0, 1.0);
|
|
80
|
+
vec3 gradient = mix(c1, c2, f);
|
|
81
|
+
gradient = mix(gradient, c4, 1.0 - clamp(1.25 - d * 0.25, 0.0, 1.0));
|
|
82
|
+
gradient = mix(gradient, c3, fill(d));
|
|
83
|
+
gradient = mix(gradient, c4, max(d2 * 0.85, max(d0 * 0.25, d1 * 0.06125)) * clamp(1.25 - d, 0.0, 1.0));
|
|
84
|
+
return gradient;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* Shape 2D arc */
|
|
88
|
+
float sArc(in vec2 p,in float w,in float s,in float e){
|
|
89
|
+
float a=distance(p,w*.5*vec2(cos(s),sin(s)));
|
|
90
|
+
float x=-PI;
|
|
91
|
+
p*=mat2(cos(x-s),-sin(x-s),sin(x-s),cos(x-s));
|
|
92
|
+
float b=clamp(atan(p.y,p.x),x,x+e);
|
|
93
|
+
b=distance(p,w*.5*vec2(cos(b),sin(b)));
|
|
94
|
+
return min(a,b)*2.;
|
|
95
|
+
}
|
|
96
|
+
float arc(in vec2 p,in float w,in float s,in float e,in float t){
|
|
97
|
+
float d=sArc(p,w,s,e);
|
|
98
|
+
return stroke(d,t);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* Shape 2D poly */
|
|
102
|
+
float sPoly(in vec2 p, in float w, in int sides) {
|
|
103
|
+
float a = atan(p.x, p.y) + PI;
|
|
104
|
+
float r = TWO_PI / float(sides);
|
|
105
|
+
float d = cos(floor(0.5 + a / r) * r - a) * length(max(abs(p) * 1.0, 0.0));
|
|
106
|
+
return d * 2.0 - w;
|
|
107
|
+
}
|
|
108
|
+
float poly(in vec2 p, in float w, in int sides) {
|
|
109
|
+
float d = sPoly(p, w, sides);
|
|
110
|
+
return fill(d);
|
|
111
|
+
}
|
|
112
|
+
float poly(in vec2 p, in float w, in int sides, in float t) {
|
|
113
|
+
float d = sPoly(p, w, sides);
|
|
114
|
+
return stroke(d, t);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/* Tiling function */
|
|
118
|
+
vec2 tile(in vec2 p, vec2 w) { return fract(mod(p + w / 2.0, w)) - (w / 2.0); }
|
|
119
|
+
vec2 tile(in vec2 p, float w) { return tile(p, vec2(w)); }
|
|
120
|
+
|
|
121
|
+
/* Boolean functions */
|
|
122
|
+
#ifndef CGA_SDF_BOOLEAN
|
|
123
|
+
#define CGA_SDF_BOOLEAN
|
|
124
|
+
float sUnion(float a, float b) {
|
|
125
|
+
return min(a, b);
|
|
126
|
+
}
|
|
127
|
+
float sIntersect(float a, float b) {
|
|
128
|
+
return max(a, b);
|
|
129
|
+
}
|
|
130
|
+
float sDifference(float a, float b) {
|
|
131
|
+
return max(a, -b);
|
|
132
|
+
}
|
|
133
|
+
#endif
|
|
134
|
+
|
|
135
|
+
/* Math 2D Transformations */
|
|
136
|
+
mat2 rotate2d(in float angle){
|
|
137
|
+
return mat2(cos(angle),-sin(angle), sin(angle), cos(angle));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/* Math 3D Transformations */
|
|
141
|
+
|
|
142
|
+
const mat4 projection = mat4(
|
|
143
|
+
vec4(3.0 / 4.0, 0.0, 0.0, 0.0),
|
|
144
|
+
vec4( 0.0, 1.0, 0.0, 0.0),
|
|
145
|
+
vec4( 0.0, 0.0, 0.5, 0.5),
|
|
146
|
+
vec4( 0.0, 0.0, 0.0, 1.0)
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
mat4 scale = mat4(
|
|
150
|
+
vec4(4.0 / 3.0, 0.0, 0.0, 0.0),
|
|
151
|
+
vec4( 0.0, 1.0, 0.0, 0.0),
|
|
152
|
+
vec4( 0.0, 0.0, 1.0, 0.0),
|
|
153
|
+
vec4( 0.0, 0.0, 0.0, 1.0)
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
mat4 rotationMatrix(float time) { return mat4(
|
|
157
|
+
vec4(1.0, 0.0, 0.0, 0.0),
|
|
158
|
+
vec4(0.0, cos(time), sin(time), 0.0),
|
|
159
|
+
vec4(0.0, -sin(time), cos(time), 0.0),
|
|
160
|
+
vec4(0.0, 0.0, 0.0, 1.0)
|
|
161
|
+
); }
|
|
162
|
+
#define rotation rotationMatrix(u_time)
|
|
163
|
+
|
|
164
|
+
mat4 rotationAxis(float angle, vec3 axis) {
|
|
165
|
+
axis = normalize(axis);
|
|
166
|
+
float s = sin(angle);
|
|
167
|
+
float c = cos(angle);
|
|
168
|
+
float oc = 1.0 - c;
|
|
169
|
+
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
|
|
170
|
+
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
|
|
171
|
+
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
|
|
172
|
+
0.0, 0.0, 0.0, 1.0);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
vec3 rotateX(vec3 p, float angle) {
|
|
176
|
+
mat4 rmy = rotationAxis(angle, vec3(1.0, 0.0, 0.0));
|
|
177
|
+
return (vec4(p, 1.0) * rmy).xyz;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
vec3 rotateY_(vec3 p, float angle) {
|
|
181
|
+
mat4 rmy = rotationAxis(angle, vec3(0.0, 1.0, 0.0));
|
|
182
|
+
return (vec4(p, 1.0) * rmy).xyz;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
vec3 rotateZ(vec3 p, float angle) {
|
|
186
|
+
mat4 rmy = rotationAxis(angle, vec3(0.0, 0.0, 1.0));
|
|
187
|
+
return (vec4(p, 1.0) * rmy).xyz;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
vec3 rotateY(vec3 p, float angle) {
|
|
191
|
+
float c = cos(angle);
|
|
192
|
+
float s = sin(angle);
|
|
193
|
+
mat4 r = mat4(
|
|
194
|
+
vec4(c, 0, s, 0),
|
|
195
|
+
vec4(0, 1, 0, 0),
|
|
196
|
+
vec4(-s, 0, c, 0),
|
|
197
|
+
vec4(0, 0, 0, 1)
|
|
198
|
+
);
|
|
199
|
+
return (vec4(p, 1.0) * r).xyz;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* Object struct */
|
|
203
|
+
struct Object { float distance; vec3 color; };
|
|
204
|
+
Object object = Object(0.0, vec3(0.0));
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Distances in model units. dss returns squared distance; the other queries return distance.
|
|
2
|
+
float dss(vec3 a, vec3 b) { vec3 d = b - a; return dot(d, d); }
|
|
3
|
+
float dssps(vec3 p, vec3 a, vec3 b, out int endpoint) {
|
|
4
|
+
vec3 direction = b - a;
|
|
5
|
+
float squaredLength = dot(direction, direction);
|
|
6
|
+
float t = squaredLength == 0.0 ? 0.0 : clamp(dot(p - a, direction) / squaredLength, 0.0, 1.0);
|
|
7
|
+
endpoint = t <= 0.0 ? 0 : (t >= 1.0 ? 1 : 2);
|
|
8
|
+
return length(p - (a + t * direction));
|
|
9
|
+
}
|
|
10
|
+
// Explicit count avoids sentinel coordinates. A count below two has no segment.
|
|
11
|
+
float dsspss(vec3 p, in vec3 points[100], int count, out bool atPathEnd) {
|
|
12
|
+
float best = 1.0e30;
|
|
13
|
+
atPathEnd = false;
|
|
14
|
+
for (int i = 0; i < 99; i++) {
|
|
15
|
+
if (i + 1 >= count) break;
|
|
16
|
+
int endpoint;
|
|
17
|
+
float candidate = dssps(p, points[i], points[i + 1], endpoint);
|
|
18
|
+
if (candidate < best) {
|
|
19
|
+
best = candidate;
|
|
20
|
+
atPathEnd = (i == 0 && endpoint == 0) || (i == count - 2 && endpoint == 1);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return best;
|
|
24
|
+
}
|
|
25
|
+
// Compatibility: the historical array terminates at the first point with y < -99.
|
|
26
|
+
float dsspss(vec3 p, in vec3 points[100], out bool atPathEnd) {
|
|
27
|
+
int count = 100;
|
|
28
|
+
for (int i = 0; i < 100; i++) { if (points[i].y < -99.0) { count = i; break; } }
|
|
29
|
+
return dsspss(p, points, count, atPathEnd);
|
|
30
|
+
}
|
|
31
|
+
#ifndef CGA_SDF_BOOLEAN
|
|
32
|
+
#define CGA_SDF_BOOLEAN
|
|
33
|
+
float sUnion(float a, float b) { return min(a, b); }
|
|
34
|
+
float sIntersect(float a, float b) { return max(a, b); }
|
|
35
|
+
float sDifference(float a, float b) { return max(a, -b); }
|
|
36
|
+
#endif
|