@matboks/utilities 0.0.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.
Files changed (177) hide show
  1. package/dist/constants.d.ts +1 -0
  2. package/dist/constants.js +1 -0
  3. package/dist/geometry/aabb.d.ts +40 -0
  4. package/dist/geometry/aabb.js +143 -0
  5. package/dist/geometry/cut-polygon.d.ts +7 -0
  6. package/dist/geometry/cut-polygon.js +116 -0
  7. package/dist/geometry/hollowgon.d.ts +24 -0
  8. package/dist/geometry/hollowgon.js +156 -0
  9. package/dist/geometry/index.d.ts +13 -0
  10. package/dist/geometry/index.js +14 -0
  11. package/dist/geometry/line.d.ts +10 -0
  12. package/dist/geometry/line.js +26 -0
  13. package/dist/geometry/polygon-operations.d.ts +31 -0
  14. package/dist/geometry/polygon-operations.js +159 -0
  15. package/dist/geometry/polygon.d.ts +55 -0
  16. package/dist/geometry/polygon.js +353 -0
  17. package/dist/geometry/polyline.d.ts +23 -0
  18. package/dist/geometry/polyline.js +100 -0
  19. package/dist/geometry/ray.d.ts +6 -0
  20. package/dist/geometry/ray.js +6 -0
  21. package/dist/geometry/ray3.d.ts +7 -0
  22. package/dist/geometry/ray3.js +10 -0
  23. package/dist/geometry/segment.d.ts +16 -0
  24. package/dist/geometry/segment.js +50 -0
  25. package/dist/geometry/shared.d.ts +5 -0
  26. package/dist/geometry/shared.js +60 -0
  27. package/dist/geometry/transformations.d.ts +7 -0
  28. package/dist/geometry/transformations.js +28 -0
  29. package/dist/geometry/vec2.d.ts +71 -0
  30. package/dist/geometry/vec2.js +225 -0
  31. package/dist/geometry/vec3.d.ts +102 -0
  32. package/dist/geometry/vec3.js +256 -0
  33. package/dist/geometry/vec4.d.ts +71 -0
  34. package/dist/geometry/vec4.js +238 -0
  35. package/dist/index.d.ts +11 -0
  36. package/dist/index.js +11 -0
  37. package/dist/math/clamp.d.ts +1 -0
  38. package/dist/math/clamp.js +5 -0
  39. package/dist/math/fract.d.ts +1 -0
  40. package/dist/math/fract.js +3 -0
  41. package/dist/math/index.d.ts +8 -0
  42. package/dist/math/index.js +8 -0
  43. package/dist/math/mix.d.ts +1 -0
  44. package/dist/math/mix.js +3 -0
  45. package/dist/math/mod.d.ts +1 -0
  46. package/dist/math/mod.js +5 -0
  47. package/dist/math/signed.d.ts +1 -0
  48. package/dist/math/signed.js +3 -0
  49. package/dist/math/smoothstep.d.ts +3 -0
  50. package/dist/math/smoothstep.js +24 -0
  51. package/dist/math/split-into-int-and-fract.d.ts +1 -0
  52. package/dist/math/split-into-int-and-fract.js +5 -0
  53. package/dist/math/units.d.ts +2 -0
  54. package/dist/math/units.js +6 -0
  55. package/dist/noise/idw.d.ts +11 -0
  56. package/dist/noise/idw.js +10 -0
  57. package/dist/noise/index.d.ts +6 -0
  58. package/dist/noise/index.js +6 -0
  59. package/dist/noise/perlin.d.ts +5 -0
  60. package/dist/noise/perlin.js +29 -0
  61. package/dist/noise/random.d.ts +13 -0
  62. package/dist/noise/random.js +39 -0
  63. package/dist/noise/value.d.ts +4 -0
  64. package/dist/noise/value.js +20 -0
  65. package/dist/noise/voronoise.d.ts +10 -0
  66. package/dist/noise/voronoise.js +26 -0
  67. package/dist/noise/worley.d.ts +4 -0
  68. package/dist/noise/worley.js +39 -0
  69. package/dist/shader-modules/index.d.ts +2 -0
  70. package/dist/shader-modules/index.js +2 -0
  71. package/dist/shader-modules/library.d.ts +2 -0
  72. package/dist/shader-modules/library.js +36 -0
  73. package/dist/shader-modules/modules/camera.d.ts +1 -0
  74. package/dist/shader-modules/modules/camera.js +43 -0
  75. package/dist/shader-modules/modules/color/blend.d.ts +1 -0
  76. package/dist/shader-modules/modules/color/blend.js +108 -0
  77. package/dist/shader-modules/modules/color/index.d.ts +1 -0
  78. package/dist/shader-modules/modules/color/index.js +135 -0
  79. package/dist/shader-modules/modules/constants.d.ts +1 -0
  80. package/dist/shader-modules/modules/constants.js +14 -0
  81. package/dist/shader-modules/modules/geometry.d.ts +1 -0
  82. package/dist/shader-modules/modules/geometry.js +110 -0
  83. package/dist/shader-modules/modules/math.d.ts +1 -0
  84. package/dist/shader-modules/modules/math.js +19 -0
  85. package/dist/shader-modules/modules/noise.d.ts +1 -0
  86. package/dist/shader-modules/modules/noise.js +410 -0
  87. package/dist/shader-modules/modules/random.d.ts +1 -0
  88. package/dist/shader-modules/modules/random.js +147 -0
  89. package/dist/shader-modules/modules/ray-marching.d.ts +1 -0
  90. package/dist/shader-modules/modules/ray-marching.js +54 -0
  91. package/dist/shader-modules/modules/sdf/index.d.ts +1 -0
  92. package/dist/shader-modules/modules/sdf/index.js +183 -0
  93. package/dist/shader-modules/modules/sdf/operations.d.ts +1 -0
  94. package/dist/shader-modules/modules/sdf/operations.js +77 -0
  95. package/dist/shader-modules/modules/utils.d.ts +1 -0
  96. package/dist/shader-modules/modules/utils.js +115 -0
  97. package/dist/shader-modules/registry.d.ts +2 -0
  98. package/dist/shader-modules/registry.js +7 -0
  99. package/dist/shader-modules/shaders.d.ts +1 -0
  100. package/dist/shader-modules/shaders.js +109 -0
  101. package/dist/shader-renderer/helpers.d.ts +10 -0
  102. package/dist/shader-renderer/helpers.js +19 -0
  103. package/dist/shader-renderer/index.d.ts +2 -0
  104. package/dist/shader-renderer/index.js +2 -0
  105. package/dist/shader-renderer/pixel-shader.d.ts +32 -0
  106. package/dist/shader-renderer/pixel-shader.js +172 -0
  107. package/dist/shader-renderer/pixel-vertex-shader.d.ts +1 -0
  108. package/dist/shader-renderer/pixel-vertex-shader.js +14 -0
  109. package/dist/shader-renderer/texture.d.ts +28 -0
  110. package/dist/shader-renderer/texture.js +97 -0
  111. package/dist/shader-renderer/types.d.ts +17 -0
  112. package/dist/shader-renderer/types.js +4 -0
  113. package/dist/shader-renderer/uniform.d.ts +16 -0
  114. package/dist/shader-renderer/uniform.js +134 -0
  115. package/dist/tilings/delaunay.d.ts +11 -0
  116. package/dist/tilings/delaunay.js +28 -0
  117. package/dist/tilings/grid.d.ts +5 -0
  118. package/dist/tilings/grid.js +29 -0
  119. package/dist/tilings/hexagononal.d.ts +4 -0
  120. package/dist/tilings/hexagononal.js +40 -0
  121. package/dist/tilings/index.d.ts +12 -0
  122. package/dist/tilings/index.js +12 -0
  123. package/dist/tilings/line.d.ts +5 -0
  124. package/dist/tilings/line.js +19 -0
  125. package/dist/tilings/mediterranean.d.ts +3 -0
  126. package/dist/tilings/mediterranean.js +49 -0
  127. package/dist/tilings/pythagorean.d.ts +3 -0
  128. package/dist/tilings/pythagorean.js +40 -0
  129. package/dist/tilings/random-cuts.d.ts +6 -0
  130. package/dist/tilings/random-cuts.js +16 -0
  131. package/dist/tilings/recursive-cuts.d.ts +5 -0
  132. package/dist/tilings/recursive-cuts.js +11 -0
  133. package/dist/tilings/rhombille.d.ts +3 -0
  134. package/dist/tilings/rhombille.js +16 -0
  135. package/dist/tilings/rightangled-triangle.d.ts +3 -0
  136. package/dist/tilings/rightangled-triangle.js +29 -0
  137. package/dist/tilings/triangle.d.ts +3 -0
  138. package/dist/tilings/triangle.js +17 -0
  139. package/dist/tilings/voronoi.d.ts +11 -0
  140. package/dist/tilings/voronoi.js +29 -0
  141. package/dist/tilings/weaving.d.ts +1 -0
  142. package/dist/tilings/weaving.js +8 -0
  143. package/dist/transforms/camera/camera.d.ts +32 -0
  144. package/dist/transforms/camera/camera.js +60 -0
  145. package/dist/transforms/camera/index.d.ts +3 -0
  146. package/dist/transforms/camera/index.js +3 -0
  147. package/dist/transforms/camera/orthographic.d.ts +15 -0
  148. package/dist/transforms/camera/orthographic.js +31 -0
  149. package/dist/transforms/camera/perspective.d.ts +16 -0
  150. package/dist/transforms/camera/perspective.js +40 -0
  151. package/dist/transforms/index.d.ts +2 -0
  152. package/dist/transforms/index.js +2 -0
  153. package/dist/transforms/normalize-transform.d.ts +12 -0
  154. package/dist/transforms/normalize-transform.js +20 -0
  155. package/dist/types.d.ts +1 -0
  156. package/dist/types.js +1 -0
  157. package/dist/utilities/create-frame.d.ts +2 -0
  158. package/dist/utilities/create-frame.js +6 -0
  159. package/dist/utilities/ensure-array.d.ts +1 -0
  160. package/dist/utilities/ensure-array.js +3 -0
  161. package/dist/utilities/idw-interpolator.d.ts +9 -0
  162. package/dist/utilities/idw-interpolator.js +18 -0
  163. package/dist/utilities/index.d.ts +8 -0
  164. package/dist/utilities/index.js +8 -0
  165. package/dist/utilities/marching-squares.d.ts +51 -0
  166. package/dist/utilities/marching-squares.js +177 -0
  167. package/dist/utilities/point-sampler.d.ts +12 -0
  168. package/dist/utilities/point-sampler.js +24 -0
  169. package/dist/utilities/poisson/grid.d.ts +21 -0
  170. package/dist/utilities/poisson/grid.js +51 -0
  171. package/dist/utilities/poisson/poissonnier.d.ts +50 -0
  172. package/dist/utilities/poisson/poissonnier.js +118 -0
  173. package/dist/utilities/resolution.d.ts +10 -0
  174. package/dist/utilities/resolution.js +14 -0
  175. package/dist/utilities/rng.d.ts +13 -0
  176. package/dist/utilities/rng.js +80 -0
  177. package/package.json +28 -0
@@ -0,0 +1,183 @@
1
+ export const sdfDefinition = /*glsl*/ `
2
+
3
+ import { barycentric, transformToZAxis, rotate } from "@/geometry";
4
+ import { lerp, signed, dot2 } from "@/utils";
5
+ import { PI } from "@/constants";
6
+
7
+ export {
8
+ sphere, box, rectangularCylinder,
9
+ discoBall, donut, boxFrame,
10
+ triangleSDFSquared, meshSDF, regularPolygon,
11
+ polygon, lineSegment
12
+ }
13
+
14
+ float sphere(vec3 p, vec3 center, float radius) {
15
+ return length(p - center) - radius;
16
+ }
17
+
18
+ float box(vec3 p, vec3 center, vec3 direction, vec3 size) {
19
+ p = transformToZAxis(p, center, direction, 0.0);
20
+ p = abs(p) - size/2.0;
21
+
22
+ // First part gives distance inside box, second part outside
23
+ return min(max(max(p.x, p.y), p.z), 0.0) + length(max(p, vec3(0)));
24
+ }
25
+
26
+ float rectangularCylinder(vec3 p, vec3 center, vec3 direction, vec2 size, float height, bool offsetCenter, float rotation) {
27
+ if (offsetCenter) center += 0.5*height*normalize(direction);
28
+ p = transformToZAxis(p, center, direction, rotation);
29
+ return box(p, vec3(0), vec3(0, 0, 1), vec3(size, height));
30
+ }
31
+
32
+ float discoBall(vec3 p, vec3 center, float radius, float n, float o) {
33
+ p -= center;
34
+ float phi = -PI + 2.0*PI*(round(lerp(atan(p.y, p.x), -PI, PI)*n)/n + o);
35
+ float theta = PI*(round(lerp(acos(p.z/length(p)), 0.0, PI)*n)/n + o);
36
+
37
+ vec3 bp = vec3(sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta));
38
+
39
+ return dot(bp, p - radius*bp);
40
+ }
41
+
42
+ float discoBall(vec3 p, vec3 center, float radius, float n) {
43
+ return discoBall(p, center, radius, n, 0.0);
44
+ }
45
+
46
+ float regularPolygon(vec2 p, float radius, float n) {
47
+ float theta = PI*signed((floor(lerp(atan(p.y, p.x), -PI, PI)*n) + .5)/n);
48
+ vec2 pp = vec2(cos(theta), sin(theta));
49
+ return dot(pp, p - radius*pp);
50
+ }
51
+
52
+ float polygon(vec2 p, sampler2D vertices, int startIndex, int endIndex, int total) {
53
+ float nf = float(total);
54
+ float sf = float(startIndex);
55
+ float ef = float(endIndex);
56
+ float minDistSquared = 1e9;
57
+ float textureStep = 1.0 / nf;
58
+ float winding = 0.0; // >0 → inside for CCW, <0 → inside for CW
59
+
60
+ vec2 start = texture(vertices, vec2((sf + 0.5) * textureStep, 0.5)).xy;
61
+
62
+ for (float i = sf + 1.0; i <= ef; i++) {
63
+ float idx = mod(i + 0.5, nf);
64
+ vec2 end = texture(vertices, vec2(idx * textureStep, 0.5)).xy;
65
+
66
+ // Distance to segment (start to end)
67
+ vec2 seg = end - start;
68
+ vec2 rel = p - start;
69
+ float t = clamp(dot(rel, seg) / dot(seg, seg), 0.0, 1.0);
70
+ float d2 = dot2(rel - seg*t);
71
+ minDistSquared = min(minDistSquared, d2);
72
+
73
+ // Portion for finding out if point is inside polygon
74
+ // Equivalent to classic point-in-polygon test:
75
+ // Does segment cross horizontal ray to the right of p?
76
+ bool aboveA = start.y > p.y;
77
+ bool aboveB = end.y > p.y;
78
+ // Sign: +1 when edge crosses in one direction, -1 in the opposite
79
+ if (aboveA != aboveB) {
80
+ float xCross = (end.x - start.x) * (p.y - start.y) / (end.y - start.y) + start.x;
81
+ if (xCross > p.x) {
82
+ winding += aboveB ? -1.0 : 1.0;
83
+ }
84
+ }
85
+
86
+ start = end;
87
+ }
88
+
89
+ float signValue = (winding == 0.0 ? 1.0 : -1.0);
90
+ return signValue * sqrt(minDistSquared);
91
+ }
92
+
93
+
94
+ float donut(vec3 p, vec3 center, vec3 direction, float innerRadius, float outerRadius, float rotation, float sections, float sides) {
95
+ p = transformToZAxis(p, center, direction, 0.0);
96
+
97
+ vec3 xy = normalize(vec3(p.xy, 0));
98
+ float angle = mod(atan(xy.y, xy.x), 2.*PI);
99
+
100
+ angle = 2.*PI*round(sections*angle/(2.*PI))/sections;
101
+ innerRadius *= sqrt(PI/(sections*tan(PI/sections)));
102
+ outerRadius *= sqrt(PI/(sides*tan(PI/sides)));
103
+ return regularPolygon(rotate(vec2(dot(p, vec3(cos(angle), sin(angle), 0)) - innerRadius, p.z), rotation + 0.25*PI), outerRadius, sides);
104
+ }
105
+
106
+ float boxFrame(vec3 p, vec3 center, vec3 direction, vec3 size, float thickness) {
107
+ float dBox = box(p, center, direction, size);
108
+
109
+ p = transformToZAxis(p, center, direction, 0.0);
110
+
111
+ vec3 o = 2.0*vec3(thickness, thickness, -1e9);
112
+ float dX = box(p, vec3(0), vec3(1, 0, 0), size.yzx - o);
113
+ float dY = box(p, vec3(0), vec3(0, 1, 0), size.zxy - o);
114
+ float dZ = box(p, vec3(0), vec3(0, 0, 1), size.xyz - o);
115
+
116
+ float dInner = min(dX, min(dY, dZ));
117
+ return max(dBox, -dInner);
118
+ }
119
+
120
+ // x is distance squared, y indicates which side of triangle p is on
121
+ // (relative to normal: positive if p is above, negative if below)
122
+ vec2 triangleSDFSquared(vec3 p, vec3 a, vec3 b, vec3 c) {
123
+ vec3 ab = b - a;
124
+ vec3 ca = a - c;
125
+ vec3 bc = c - b;
126
+
127
+ vec3 ap = p - a;
128
+ vec3 bp = p - b;
129
+ vec3 cp = p - c;
130
+
131
+ vec3 normal = normalize(cross(ab, -ca)); // Negate to make cross(ab, ac), follows right-hand rule assuming abc is CCW
132
+
133
+ bool isInsideTriangle = all(greaterThan(barycentric(p, a, b, c), vec3(0)));
134
+ bool isInsideXYProjection = abs(normal.z) > 1e-5 && all(greaterThan(barycentric(p.xy, a.xy, b.xy, c.xy), vec3(0)));
135
+
136
+ float distSquared;
137
+ bool isAboveXYProjection;
138
+
139
+ // If p is above XY projection, then dist = dot(ap, normal) > 0 when normal points upwards
140
+ // and dist < 0 when it points downwards. sign(normal.z)*dist > 0.0 then tells us if p is above
141
+ if (isInsideTriangle) {
142
+ float dist = dot(ap, normal);
143
+ distSquared = dist*dist;
144
+ isAboveXYProjection = isInsideXYProjection && sign(normal.z)*dist > 0.0;
145
+ } else {
146
+ distSquared = min(min(
147
+ dot2(ap - ab*clamp(dot(ap, ab) / dot2(ab), 0.0, 1.0)),
148
+ dot2(bp - bc*clamp(dot(bp, bc) / dot2(bc), 0.0, 1.0))),
149
+ dot2(cp - ca*clamp(dot(cp, ca) / dot2(ca), 0.0, 1.0))
150
+ );
151
+ isAboveXYProjection = isInsideXYProjection && sign(normal.z)*dot(ap, normal) > 0.0;
152
+ }
153
+
154
+ return vec2(distSquared, isAboveXYProjection ? -1.0 : 1.0);
155
+ }
156
+
157
+ float lineSegment(vec2 p, vec2 a, vec2 b, float o) {
158
+ vec2 ap = p - a;
159
+ vec2 ab = b - a;
160
+
161
+ float t = clamp(dot(ap, ab)/dot(ab, ab), 0.0, 1.0);
162
+
163
+ return length(ap - ab*t) - o;
164
+ }
165
+
166
+ float lineSegment(vec2 p, vec2 a, vec2 b) {
167
+ return lineSegment(p, a, b, 0.0);
168
+ }
169
+
170
+ float lineSegment(vec3 p, vec2 a, vec2 b, float o) {
171
+ vec3 ap = p - a;
172
+ vec3 ab = b - a;
173
+
174
+ float t = clamp(dot(ap, ab)/dot(ab, ab), 0.0, 1.0);
175
+
176
+ return length(ap - ab*t) - o;
177
+ }
178
+
179
+ float lineSegment(vec3 p, vec2 a, vec2 b) {
180
+ return lineSegment(p, a, b, 0.0);
181
+ }
182
+
183
+ `;
@@ -0,0 +1 @@
1
+ export declare const operationsDefinition = "\nimport { tile } from \"@/utils\";\n\nexport {\n sdfNormal,\n combine, intersect, subtract, xor,\n smoothCombine, smoothIntersect, smoothSubtract, smoothXOR,\n repeatGrid\n}\n\nconst float DEFAULT_SMOOTH = 0.2;\n\nvec3 sdfNormal(vec3 p, float sdf(vec3), float h) {\n vec2 o = vec2(1, -1)*h;\n return normalize( \n o.xyy*sdf(p + o.xyy) + \n o.yyx*sdf(p + o.yyx) + \n o.yxy*sdf(p + o.yxy) + \n o.xxx*sdf(p + o.xxx)\n );\n}\n\nfloat combine(float a, float b) {\n return min(a, b);\n}\n\nfloat intersect(float a, float b) {\n return max(a, b);\n}\n\nfloat subtract(float a, float b) {\n return max(a, -b);\n}\n\nfloat xor(float a, float b) {\n return subtract(combine(a, b), intersect(a, b));\n}\n\n\nfloat smoothCombine(float d1, float d2, float k) {\n float h = clamp(0.5 + 0.5*(d2 - d1)/k, 0.0, 1.0);\n return mix(d2, d1, h) - k*h*(1.0 - h);\n}\n\nfloat smoothCombine(float d1, float d2) {\n return smoothCombine(d1, d2, DEFAULT_SMOOTH);\n}\n\nfloat smoothIntersect(float d1, float d2, float k) {\n return -smoothCombine(-d1, -d2, k);\n}\n\nfloat smoothIntersect(float d1, float d2) {\n return smoothIntersect(d1, d2, DEFAULT_SMOOTH);\n}\n\nfloat smoothSubtract(float d1, float d2, float k) {\n return -smoothCombine(-d1, d2, k);\n}\n\nfloat smoothSubtract(float d1, float d2) {\n return smoothSubtract(d1, d2, DEFAULT_SMOOTH);\n}\n\nfloat smoothXOR(float d1, float d2, float k) {\n return smoothSubtract(smoothCombine(d1, d2, k), smoothIntersect(d1, d2, k), k);\n}\n\nfloat smoothXOR(float d1, float d2) {\n return smoothXOR(d1, d2, DEFAULT_SMOOTH);\n}\n\nfloat repeatGrid(vec3 p, float size, float sdf(vec3)) {\n return size*sdf(tile(p, size)/size);\n}\n\n";
@@ -0,0 +1,77 @@
1
+ export const operationsDefinition = /*glsl*/ `
2
+ import { tile } from "@/utils";
3
+
4
+ export {
5
+ sdfNormal,
6
+ combine, intersect, subtract, xor,
7
+ smoothCombine, smoothIntersect, smoothSubtract, smoothXOR,
8
+ repeatGrid
9
+ }
10
+
11
+ const float DEFAULT_SMOOTH = 0.2;
12
+
13
+ vec3 sdfNormal(vec3 p, float sdf(vec3), float h) {
14
+ vec2 o = vec2(1, -1)*h;
15
+ return normalize(
16
+ o.xyy*sdf(p + o.xyy) +
17
+ o.yyx*sdf(p + o.yyx) +
18
+ o.yxy*sdf(p + o.yxy) +
19
+ o.xxx*sdf(p + o.xxx)
20
+ );
21
+ }
22
+
23
+ float combine(float a, float b) {
24
+ return min(a, b);
25
+ }
26
+
27
+ float intersect(float a, float b) {
28
+ return max(a, b);
29
+ }
30
+
31
+ float subtract(float a, float b) {
32
+ return max(a, -b);
33
+ }
34
+
35
+ float xor(float a, float b) {
36
+ return subtract(combine(a, b), intersect(a, b));
37
+ }
38
+
39
+
40
+ float smoothCombine(float d1, float d2, float k) {
41
+ float h = clamp(0.5 + 0.5*(d2 - d1)/k, 0.0, 1.0);
42
+ return mix(d2, d1, h) - k*h*(1.0 - h);
43
+ }
44
+
45
+ float smoothCombine(float d1, float d2) {
46
+ return smoothCombine(d1, d2, DEFAULT_SMOOTH);
47
+ }
48
+
49
+ float smoothIntersect(float d1, float d2, float k) {
50
+ return -smoothCombine(-d1, -d2, k);
51
+ }
52
+
53
+ float smoothIntersect(float d1, float d2) {
54
+ return smoothIntersect(d1, d2, DEFAULT_SMOOTH);
55
+ }
56
+
57
+ float smoothSubtract(float d1, float d2, float k) {
58
+ return -smoothCombine(-d1, d2, k);
59
+ }
60
+
61
+ float smoothSubtract(float d1, float d2) {
62
+ return smoothSubtract(d1, d2, DEFAULT_SMOOTH);
63
+ }
64
+
65
+ float smoothXOR(float d1, float d2, float k) {
66
+ return smoothSubtract(smoothCombine(d1, d2, k), smoothIntersect(d1, d2, k), k);
67
+ }
68
+
69
+ float smoothXOR(float d1, float d2) {
70
+ return smoothXOR(d1, d2, DEFAULT_SMOOTH);
71
+ }
72
+
73
+ float repeatGrid(vec3 p, float size, float sdf(vec3)) {
74
+ return size*sdf(tile(p, size)/size);
75
+ }
76
+
77
+ `;
@@ -0,0 +1 @@
1
+ export declare const utilsDefinition = "\n\nimport { EPS } from \"@/constants\";\nimport { rotate } from \"@/geometry\";\n\nexport {\n aastep,\n triangleWave,\n lerp,\n signed,\n tile,\n dot2,\n smoothsteps,\n triplanarTexture\n}\n\nfloat aastep(float threshold, float value) {\n float d = length(vec2(dFdx(value), dFdy(value))) * 0.707;\n return smoothstep(-d, d, value - threshold);\n}\n\nfloat triangleWave(float p) {\n return 2.*abs(p - floor(p + .5));\n}\n\nfloat triangleWave(vec2 p) {\n return triangleWave(p.x);\n}\n\nfloat triangleWave(vec2 p, float o) {\n return triangleWave(rotate(p, o));\n}\n\nfloat lerp(float x, float a, float b) {\n return clamp((x - a)/(b - a), 0.0, 1.0);\n}\n\nfloat signed(float t) {\n return -1.0 + 2.0*t;\n}\n\nvec2 signed(vec2 t) {\n return -1.0 + 2.0*t;\n}\n\nvec3 signed(vec3 t) {\n return -1.0 + 2.0*t;\n}\n\nvec4 signed(vec4 t) {\n return -1.0 + 2.0*t;\n}\n\nfloat tile(float p, float s) {\n return mod(p, 2.0*s) - s;\n}\n\nvec2 tile(vec2 p, vec2 s) {\n return mod(p, 2.0*s) - s;\n}\n\nvec2 tile(vec2 p, float s) {\n return tile(p, vec2(s));\n}\n\nvec3 tile(vec3 p, vec3 s) {\n return mod(p, 2.0*s) - s;\n}\n\nvec3 tile(vec3 p, float s) {\n return tile(p, vec3(s));\n}\n\nvec4 tile(vec4 p, vec4 s) {\n return mod(p, 2.0*s) - s;\n}\n\nvec4 tile(vec4 p, float s) {\n return tile(p, vec4(s));\n}\n\nfloat dot2(vec2 v) {\n return dot(v, v);\n}\n\nfloat dot2(vec3 v) {\n return dot(v, v);\n}\n\nfloat dot2(vec4 v) {\n return dot(v, v);\n}\n\nfloat smoothsteps(float t, int steps, float flatness) {\n if (t < 0.0) return 0.0;\n if (t > 1.0) return 1.0;\n\n float sf = float(steps);\n float value;\n if (flatness == 1.0) {\n value = floor(t*(sf + 1.0))/sf;\n } else {\n float l = 1.0/sf*(1.0 - flatness/(sf + 1.0));\n value = (floor(t/l) + smoothstep(flatness, 1.0, fract(t/l)))/sf;\n }\n return clamp(value, 0.0, 1.0); \n}\n\nfloat triplanarTexture(vec3 p, vec3 normal, float power, float texture(vec2)) {\n vec3 w = pow(abs(normal), vec3(power));\n vec3 v = vec3(texture(p.yz), texture(p.xz), texture(p.xy));\n return dot(w, v) / dot(w, vec3(1));\n}\n\n";
@@ -0,0 +1,115 @@
1
+ export const utilsDefinition = /*glsl*/ `
2
+
3
+ import { EPS } from "@/constants";
4
+ import { rotate } from "@/geometry";
5
+
6
+ export {
7
+ aastep,
8
+ triangleWave,
9
+ lerp,
10
+ signed,
11
+ tile,
12
+ dot2,
13
+ smoothsteps,
14
+ triplanarTexture
15
+ }
16
+
17
+ float aastep(float threshold, float value) {
18
+ float d = length(vec2(dFdx(value), dFdy(value))) * 0.707;
19
+ return smoothstep(-d, d, value - threshold);
20
+ }
21
+
22
+ float triangleWave(float p) {
23
+ return 2.*abs(p - floor(p + .5));
24
+ }
25
+
26
+ float triangleWave(vec2 p) {
27
+ return triangleWave(p.x);
28
+ }
29
+
30
+ float triangleWave(vec2 p, float o) {
31
+ return triangleWave(rotate(p, o));
32
+ }
33
+
34
+ float lerp(float x, float a, float b) {
35
+ return clamp((x - a)/(b - a), 0.0, 1.0);
36
+ }
37
+
38
+ float signed(float t) {
39
+ return -1.0 + 2.0*t;
40
+ }
41
+
42
+ vec2 signed(vec2 t) {
43
+ return -1.0 + 2.0*t;
44
+ }
45
+
46
+ vec3 signed(vec3 t) {
47
+ return -1.0 + 2.0*t;
48
+ }
49
+
50
+ vec4 signed(vec4 t) {
51
+ return -1.0 + 2.0*t;
52
+ }
53
+
54
+ float tile(float p, float s) {
55
+ return mod(p, 2.0*s) - s;
56
+ }
57
+
58
+ vec2 tile(vec2 p, vec2 s) {
59
+ return mod(p, 2.0*s) - s;
60
+ }
61
+
62
+ vec2 tile(vec2 p, float s) {
63
+ return tile(p, vec2(s));
64
+ }
65
+
66
+ vec3 tile(vec3 p, vec3 s) {
67
+ return mod(p, 2.0*s) - s;
68
+ }
69
+
70
+ vec3 tile(vec3 p, float s) {
71
+ return tile(p, vec3(s));
72
+ }
73
+
74
+ vec4 tile(vec4 p, vec4 s) {
75
+ return mod(p, 2.0*s) - s;
76
+ }
77
+
78
+ vec4 tile(vec4 p, float s) {
79
+ return tile(p, vec4(s));
80
+ }
81
+
82
+ float dot2(vec2 v) {
83
+ return dot(v, v);
84
+ }
85
+
86
+ float dot2(vec3 v) {
87
+ return dot(v, v);
88
+ }
89
+
90
+ float dot2(vec4 v) {
91
+ return dot(v, v);
92
+ }
93
+
94
+ float smoothsteps(float t, int steps, float flatness) {
95
+ if (t < 0.0) return 0.0;
96
+ if (t > 1.0) return 1.0;
97
+
98
+ float sf = float(steps);
99
+ float value;
100
+ if (flatness == 1.0) {
101
+ value = floor(t*(sf + 1.0))/sf;
102
+ } else {
103
+ float l = 1.0/sf*(1.0 - flatness/(sf + 1.0));
104
+ value = (floor(t/l) + smoothstep(flatness, 1.0, fract(t/l)))/sf;
105
+ }
106
+ return clamp(value, 0.0, 1.0);
107
+ }
108
+
109
+ float triplanarTexture(vec3 p, vec3 normal, float power, float texture(vec2)) {
110
+ vec3 w = pow(abs(normal), vec3(power));
111
+ vec3 v = vec3(texture(p.yz), texture(p.xz), texture(p.xy));
112
+ return dot(w, v) / dot(w, vec3(1));
113
+ }
114
+
115
+ `;
@@ -0,0 +1,2 @@
1
+ import { GLSLRegistry } from "glsl-modules";
2
+ export declare const glslRegistry: GLSLRegistry;
@@ -0,0 +1,7 @@
1
+ import { GLSLRegistry } from "glsl-modules";
2
+ import { glslkitLibrary } from "./library.js";
3
+ import { arrowFunctionsPlugin, cssColorsPlugin, functionAsArgumentPlugin, namedArgumentsPlugin, namespacedImportsPlugin } from "glsl-modules/plugins";
4
+ export const glslRegistry = new GLSLRegistry({
5
+ libraries: [glslkitLibrary],
6
+ plugins: [namespacedImportsPlugin(), cssColorsPlugin(), namedArgumentsPlugin(), arrowFunctionsPlugin(), functionAsArgumentPlugin()]
7
+ });
@@ -0,0 +1 @@
1
+ export declare const testFragmentShaderDefinition: string;
@@ -0,0 +1,109 @@
1
+ import { glslRegistry } from "./registry.js";
2
+ export const testFragmentShaderDefinition = glslRegistry.resolve(/*glsl*/ `
3
+ #version 300 es
4
+
5
+ import { meshSDF, sdfNormal, discoBall } from "glslkit/sdf";
6
+ import { rayMarch, RayMarchResult as RMR, Ray } from "glslkit/ray-marching";
7
+ import { orthographic } from "glslkit/camera";
8
+ import { axisAngle, create2DBasisVectors, rotate } from "glslkit/geometry";
9
+ import { PI } from "glslkit/constants";
10
+ import { aastep, signed } from "glslkit/utils";
11
+ import { perlinNoise, valueNoise } from "glslkit/noise";
12
+
13
+ precision highp float;
14
+
15
+ in vec2 uv;
16
+ out vec4 color;
17
+
18
+ struct Camera {
19
+ vec3 position;
20
+ vec3 lookAt;
21
+ float viewport;
22
+ float rotation;
23
+ };
24
+
25
+ uniform int u_id;
26
+ uniform vec2 u_resolution;
27
+ uniform float u_time;
28
+ uniform sampler2D u_vertices;
29
+ uniform int u_numVertices;
30
+ uniform vec3 u_cameraPosition;
31
+ uniform vec3 u_lookAt;
32
+ uniform float u_viewport;
33
+ uniform float u_rotation;
34
+ uniform float u_values[10];
35
+
36
+ void main() {
37
+ Camera;
38
+ float aspectRatio = u_resolution.x / u_resolution.y;
39
+ vec2 p = 2.0*vec2(aspectRatio, 1)*(uv - 0.5);
40
+
41
+ Ray ray = orthographic({
42
+ uv,
43
+ cameraPosition: u_cameraPosition,
44
+ lookAt: u_lookAt,
45
+ viewport: u_viewport,
46
+ aspectRatio,
47
+ rotation: u_rotation
48
+ });
49
+
50
+ // float sdf = (vec3 p) => length(p) - 0.5;
51
+
52
+ float sdf = (vec3 p) => {
53
+ // p += 0.01*signed(valueNoise(p, 100.0, 0.0));
54
+ // p = axisAngle(p, vec3(0, 1, 0), 0.);
55
+ float d0 = meshSDF(p, u_vertices, u_numVertices);
56
+ // float d0 = length(p) - 20.0;
57
+ // float d1 = length(p.xy) - 0.5;
58
+
59
+ // float d1 = min(min(length(p.xz), length(p.yz)), length(p.xy)) - 0.5;
60
+ float d1 = discoBall(p, vec3(0, 0, 1), 0.25, 8.0, 0.);
61
+ // float d1 = length(p - vec3(0, 0, 1)) - 1.;
62
+
63
+ float d = d0;
64
+ // d = max(d, -d1);
65
+ // d = min(d, d1);
66
+ // d = min(d, length(p) - 0.3);
67
+ // d = min(d, length(p.xy) - 0.25);
68
+ // d = d0;
69
+ return d;
70
+ };
71
+
72
+ RMR rmr = rayMarch(ray, sdf);
73
+
74
+ if (!rmr.hit) {
75
+ color = vec4(0, 0, 0, 0);
76
+ return;
77
+ }
78
+
79
+ vec3 position = rmr.position;
80
+
81
+ vec3 normal = sdfNormal(position + vec3(0, 0, 0.000), sdf, 0.0005);
82
+
83
+ mat2x3 basis2D = create2DBasisVectors(normal);
84
+ vec2 pn = position*basis2D;
85
+
86
+ vec3 lightDirection = normalize(vec3(1, -1, -1));
87
+ // vec3 lightDirection = ray.direction;
88
+ float light = clamp(-dot(normal, lightDirection), 0.0, 1.0);
89
+
90
+ // float value = smoothstep(0.45, 0.55, light);
91
+ float value;
92
+
93
+ // value = light;
94
+ value = aastep(1.0 - (-0.1 + 1.2*light), 0.5 + 0.5*sin(20.0*2.*PI*pn.x));
95
+ // value = aastep(1.0 - light, perlinNoise(pn, 0.01, 0.0));
96
+ // if (p.y > 0.0) {
97
+ // } else {
98
+ // }
99
+
100
+ // vec3 c = vec3(value);
101
+ vec3 c = mix(vec3(0, 0, 0), vec3(1, 1, 1), value);
102
+
103
+ // c = texture(u_vertices, uv).rgb;
104
+
105
+ // color = vec4(1, 0, 0, 1);
106
+ color = vec4(c, 1);
107
+ }
108
+
109
+ `.trim());
@@ -0,0 +1,10 @@
1
+ export type ParsedUniform = {
2
+ type: string;
3
+ name: string;
4
+ } & ({
5
+ isArray: true;
6
+ arraySize: number;
7
+ } | {
8
+ isArray: false;
9
+ });
10
+ export declare function parseShaderUniforms(shader: string): ParsedUniform[];
@@ -0,0 +1,19 @@
1
+ const uniformRegex = /uniform\s+([0-9a-z_]+)\s+([0-9a-z_]+)(\[(.*)\])?\s*;/i;
2
+ export function parseShaderUniforms(shader) {
3
+ let index = 0;
4
+ let uniforms = [];
5
+ while (true) {
6
+ const match = shader.slice(index).match(uniformRegex);
7
+ if (!match)
8
+ break;
9
+ const [, type, name, array, arraySize] = match;
10
+ uniforms.push({
11
+ type, name,
12
+ ...array ?
13
+ { isArray: true, arraySize: parseInt(arraySize) } :
14
+ { isArray: false }
15
+ });
16
+ index += match.index + 1;
17
+ }
18
+ return uniforms;
19
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./pixel-shader.js";
2
+ export * from "./texture.js";
@@ -0,0 +1,2 @@
1
+ export * from "./pixel-shader.js";
2
+ export * from "./texture.js";
@@ -0,0 +1,32 @@
1
+ import { Resolution } from "../utilities/resolution.js";
2
+ import { ShaderTexture, TextureData, TextureSpec } from "./texture.js";
3
+ import { ProgramData, UniformData, UniformValue } from "./types.js";
4
+ import { UniformHandler } from "./uniform.js";
5
+ export type PixelShaderOptions = {
6
+ fragmentShader: string;
7
+ resolution?: Resolution;
8
+ attributes?: WebGLContextAttributes;
9
+ gl?: WebGL2RenderingContext;
10
+ };
11
+ export declare class PixelShader {
12
+ canvas: HTMLCanvasElement | OffscreenCanvas;
13
+ resolution: Resolution;
14
+ gl: WebGL2RenderingContext;
15
+ programData: ProgramData;
16
+ uniforms: UniformHandler;
17
+ textures: Record<string, ShaderTexture>;
18
+ textureUnitCounter: number;
19
+ constructor(options: PixelShaderOptions);
20
+ setUniform(data: UniformData): PixelShader;
21
+ setUniform(name: string, value: UniformValue): PixelShader;
22
+ createTexture(name: string, spec: TextureSpec): ShaderTexture;
23
+ setTextureData(name: string, width: number, height: number, data: TextureData): void;
24
+ updateTextureSpec(name: string, partial: Partial<TextureSpec>): void;
25
+ private ensureCurrentProgram;
26
+ render(target?: HTMLCanvasElement | OffscreenCanvas | CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D): void;
27
+ resize(resolution: Resolution): void;
28
+ }
29
+ export declare function setupWebGL(target: HTMLCanvasElement | OffscreenCanvas | WebGL2RenderingContext, vertexShaderDefinition: string, fragmentShaderDefinition: string, attributes?: WebGLContextAttributes): {
30
+ gl: WebGL2RenderingContext;
31
+ programData: ProgramData;
32
+ };