@bigmistqke/view.gl 0.2.0 → 0.3.0

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 (43) hide show
  1. package/README.md +82 -27
  2. package/dev/dist/404.html +19 -0
  3. package/dev/dist/assets/bezier-DrA4cU_H.js +292 -0
  4. package/dev/dist/assets/game-of-life-BYlzWhEL.js +186 -0
  5. package/dev/dist/assets/glsl-composition-D_BaFFij.js +97 -0
  6. package/dev/dist/assets/grid-DfXHNLBm.js +181 -0
  7. package/dev/dist/assets/index-C2N3u8wj.css +12 -0
  8. package/dev/dist/assets/index-D-N0FDYs.js +110 -0
  9. package/dev/dist/assets/main-CSE2fki3.js +32 -0
  10. package/dev/dist/assets/overview-IeUhUflE.js +80 -0
  11. package/dev/dist/assets/pix-sim-DMJJA_WO.js +500 -0
  12. package/dev/dist/assets/ray-casting-DTQetDPd.js +411 -0
  13. package/dev/dist/assets/ray-casting-modules-Dkfw4tdc.js +765 -0
  14. package/dev/dist/assets/sdf-CS0uBonx.js +66 -0
  15. package/dev/dist/assets/symbol-BL_7GJ0M.js +67 -0
  16. package/dev/dist/assets/tag-C0Vskpp8.js +833 -0
  17. package/dev/dist/assets/tetris-D8mokodr.js +344 -0
  18. package/dev/dist/assets/utils-2dzuv_bW.js +62 -0
  19. package/dev/dist/index.html +51 -0
  20. package/dist/chunk-BZDGEIOT.mjs +278 -0
  21. package/dist/chunk-BZDGEIOT.mjs.map +1 -0
  22. package/dist/{chunk-EYXOSM7B.mjs → chunk-X4M3LV4R.mjs} +60 -133
  23. package/dist/chunk-X4M3LV4R.mjs.map +1 -0
  24. package/dist/gl.d.mts +2 -2
  25. package/dist/gl.mjs +1 -1
  26. package/dist/index.d.mts +13 -4
  27. package/dist/index.mjs +7 -2
  28. package/dist/tag.d.mts +6 -1
  29. package/dist/tag.mjs +13 -5
  30. package/dist/tag.mjs.map +1 -1
  31. package/dist/{types-CyGyGi18.d.mts → types-CNdqh7tB.d.mts} +34 -4
  32. package/package.json +1 -1
  33. package/src/gl.ts +13 -2
  34. package/src/index.ts +55 -39
  35. package/src/tag.ts +18 -2
  36. package/src/types.ts +33 -3
  37. package/src/utils.ts +45 -10
  38. package/test/index.test.ts +111 -29
  39. package/test/mocks/webgl.ts +14 -0
  40. package/test/tag.test.ts +41 -0
  41. package/dist/chunk-CJTT7WPV.mjs +0 -75
  42. package/dist/chunk-CJTT7WPV.mjs.map +0 -1
  43. package/dist/chunk-EYXOSM7B.mjs.map +0 -1
@@ -0,0 +1,411 @@
1
+ import { u as uniform, g as glsl, c as compile } from './tag-C0Vskpp8.js';
2
+ import { d as dom } from './utils-2dzuv_bW.js';
3
+
4
+ const sphereData = [
5
+ [0, 0, 0, 1],
6
+ // Center sphere
7
+ [3, 1, -2, 0.8],
8
+ // Right sphere
9
+ [-3, -1, 1, 0.9],
10
+ // Left sphere
11
+ [1, 3, -1, 0.7],
12
+ // Top sphere
13
+ [-2, -3, 2, 0.6],
14
+ // Bottom sphere
15
+ [4, -2, -3, 0.5],
16
+ // Far right sphere
17
+ [-4, 2, 3, 0.8],
18
+ // Far left sphere
19
+ [0, -1, -4, 1.2]
20
+ // Back sphere
21
+ ];
22
+ const sphereColors = [
23
+ [1, 0.3, 0.3],
24
+ // Red
25
+ [0.3, 1, 0.3],
26
+ // Green
27
+ [0.3, 0.3, 1],
28
+ // Blue
29
+ [1, 1, 0.3],
30
+ // Yellow
31
+ [1, 0.3, 1],
32
+ // Magenta
33
+ [0.3, 1, 1],
34
+ // Cyan
35
+ [1, 0.7, 0.3],
36
+ // Orange
37
+ [0.7, 0.3, 1]
38
+ // Purple
39
+ ];
40
+ const boxData = [
41
+ [2, 0, -1, 0.8],
42
+ // Right box
43
+ [-2, 0, 1, 0.6],
44
+ // Left box
45
+ [0, -2, -2, 0.7]
46
+ // Bottom box
47
+ ];
48
+ const boxColors = [
49
+ [0.8, 0.8, 0.2],
50
+ // Yellow-green
51
+ [0.2, 0.8, 0.8],
52
+ // Cyan
53
+ [0.8, 0.2, 0.8]
54
+ // Magenta
55
+ ];
56
+ let SHAPE_COUNTER = 0;
57
+ const controller = new AbortController();
58
+ const canvas = dom("canvas", { parentElement: document.body });
59
+ const gl = canvas.getContext("webgl2");
60
+ const NUM_SPHERES = 8;
61
+ const NUM_BOXES = 3;
62
+ const SHAPE_SPHERE = Symbol("SHAPE_SPHERE");
63
+ const sphereModule = glsl`
64
+ #define NUM_SPHERES ${NUM_SPHERES}
65
+ #define ${SHAPE_SPHERE} ${SHAPE_COUNTER++}
66
+
67
+ struct Sphere {
68
+ vec3 position;
69
+ float radius;
70
+ vec3 color;
71
+ };
72
+
73
+ ${uniform.vec4("sphereData", { size: NUM_SPHERES })}
74
+ ${uniform.vec3("sphereColors", { size: NUM_SPHERES })}
75
+
76
+ float sphereIntersection(in vec3 rayOrigin, in vec3 rayDirection, in Sphere sphere)
77
+ {
78
+ // We're shooting a ray from the camera and checking if it hits a sphere
79
+ // Think of it like shining a laser pointer at a ball
80
+
81
+ vec3 rayToSphere = rayOrigin - sphere.position;
82
+
83
+ // Step 1: Find the closest point on the ray to the sphere's position
84
+ // This is like finding where the laser beam passes nearest to the ball's position
85
+ float closestApproach = dot(rayToSphere, rayDirection);
86
+
87
+ // Step 2: Check if we're already inside the sphere
88
+ // If negative, we're outside and pointing away from sphere position
89
+ float rayStartDistanceSquared = dot(rayToSphere, rayToSphere) - sphere.radius * sphere.radius;
90
+
91
+ // Step 3: Use geometry to check if the ray can reach the sphere
92
+ // If this value is negative, the ray misses the sphere entirely
93
+ float canReachSphere = closestApproach * closestApproach - rayStartDistanceSquared;
94
+
95
+ if(canReachSphere < 0.0) {
96
+ return -1.0; // Ray misses the sphere
97
+ }
98
+
99
+ // Step 4: Calculate how far along the ray we hit the sphere
100
+ // We want the nearest intersection point (there can be two when ray goes through)
101
+ float hitDistance = -closestApproach - sqrt(canReachSphere);
102
+
103
+ return hitDistance;
104
+ }
105
+
106
+ // Calculate normal vector at hit point on sphere surface
107
+ vec3 sphereNormal(in vec3 hitPosition, in Sphere sphere)
108
+ {
109
+ return (hitPosition - sphere.position) / sphere.radius;
110
+ }
111
+
112
+ // Find closest sphere intersection, returns distance and sets hitIndex and hitType
113
+ float findClosestSphere(in vec3 rayOrigin, in vec3 rayDirection, in float maxDistance, out int hitIndex, out int hitType)
114
+ {
115
+ float closestDistance = maxDistance;
116
+ hitIndex = -1;
117
+ hitType = -1;
118
+
119
+ for(int i = 0; i < NUM_SPHERES; i++) {
120
+ Sphere sphere = Sphere(sphereData[i].xyz, sphereData[i].w, sphereColors[i]);
121
+ float distance = sphereIntersection(rayOrigin, rayDirection, sphere);
122
+
123
+ if(distance > 0.0 && distance < closestDistance) {
124
+ closestDistance = distance;
125
+ hitIndex = i;
126
+ hitType = ${SHAPE_SPHERE};
127
+ }
128
+ }
129
+
130
+ return hitIndex >= 0 ? closestDistance : -1.0;
131
+ }
132
+
133
+ // Calculate sphere normal and color at hit point
134
+ void calculateSphereHit(in vec3 hitPosition, in int hitIndex, out vec3 normal, out vec3 color)
135
+ {
136
+ Sphere hitSphere = Sphere(sphereData[hitIndex].xyz, sphereData[hitIndex].w, sphereColors[hitIndex]);
137
+ normal = sphereNormal(hitPosition, hitSphere);
138
+ color = sphereColors[hitIndex];
139
+ }`;
140
+ const SHAPE_BOX = Symbol("SHAPE_BOX");
141
+ const boxModule = glsl`
142
+ #define ${SHAPE_BOX} ${SHAPE_COUNTER++}
143
+ #define NUM_BOXES ${NUM_BOXES}
144
+
145
+ struct Box {
146
+ vec3 position;
147
+ float size;
148
+ vec3 color;
149
+ };
150
+
151
+ ${uniform.vec4("boxData", { size: NUM_BOXES })}
152
+ ${uniform.vec3("boxColors", { size: NUM_BOXES })}
153
+
154
+ float boxIntersection(in vec3 rayOrigin, in vec3 rayDirection, in Box box)
155
+ {
156
+ // Ray-box intersection using slab method
157
+ // A box is defined by 6 planes, we find where the ray enters and exits each pair of parallel planes
158
+
159
+ vec3 boxMin = box.position - vec3(box.size);
160
+ vec3 boxMax = box.position + vec3(box.size);
161
+
162
+ // Calculate intersection distances for each axis
163
+ vec3 invDir = 1.0 / rayDirection;
164
+ vec3 t1 = (boxMin - rayOrigin) * invDir;
165
+ vec3 t2 = (boxMax - rayOrigin) * invDir;
166
+
167
+ // Find the min and max for each axis (handles negative direction)
168
+ vec3 tMin = min(t1, t2);
169
+ vec3 tMax = max(t1, t2);
170
+
171
+ // The ray enters the box at the maximum of all tMin values
172
+ float tNear = max(max(tMin.x, tMin.y), tMin.z);
173
+
174
+ // The ray exits the box at the minimum of all tMax values
175
+ float tFar = min(min(tMax.x, tMax.y), tMax.z);
176
+
177
+ // If tNear > tFar, ray misses the box
178
+ // If tFar < 0, box is behind the ray
179
+ if(tNear > tFar || tFar < 0.0) {
180
+ return -1.0;
181
+ }
182
+
183
+ // Return the nearest intersection (tNear, unless we're inside the box)
184
+ return tNear > 0.0 ? tNear : tFar;
185
+ }
186
+
187
+ // Calculate normal vector at hit point on box surface
188
+ vec3 boxNormal(in vec3 hitPosition, in Box box)
189
+ {
190
+ // Find which face of the box we hit by checking which coordinate is closest to the box boundary
191
+ vec3 localPos = hitPosition - box.position;
192
+ vec3 absLocal = abs(localPos);
193
+
194
+ // Return normal for the face that's closest to the box size
195
+ if(absLocal.x > absLocal.y && absLocal.x > absLocal.z) {
196
+ return vec3(sign(localPos.x), 0.0, 0.0);
197
+ } else if(absLocal.y > absLocal.z) {
198
+ return vec3(0.0, sign(localPos.y), 0.0);
199
+ } else {
200
+ return vec3(0.0, 0.0, sign(localPos.z));
201
+ }
202
+ }
203
+
204
+ // Find closest box intersection, returns distance and sets hitIndex and hitType
205
+ float findClosestBox(in vec3 rayOrigin, in vec3 rayDirection, in float maxDistance, out int hitIndex, out int hitType)
206
+ {
207
+ float closestDistance = maxDistance;
208
+ hitIndex = -1;
209
+ hitType = -1;
210
+
211
+ for(int i = 0; i < NUM_BOXES; i++) {
212
+ Box box = Box(boxData[i].xyz, boxData[i].w, boxColors[i]);
213
+ float distance = boxIntersection(rayOrigin, rayDirection, box);
214
+
215
+ if(distance > 0.0 && distance < closestDistance) {
216
+ closestDistance = distance;
217
+ hitIndex = i;
218
+ hitType = ${SHAPE_BOX};
219
+ }
220
+ }
221
+
222
+ return hitIndex >= 0 ? closestDistance : -1.0;
223
+ }
224
+
225
+ // Calculate box normal and color at hit point
226
+ void calculateBoxHit(in vec3 hitPosition, in int hitIndex, out vec3 normal, out vec3 color)
227
+ {
228
+ Box hitBox = Box(boxData[hitIndex].xyz, boxData[hitIndex].w, boxColors[hitIndex]);
229
+ normal = boxNormal(hitPosition, hitBox);
230
+ color = boxColors[hitIndex];
231
+ }
232
+
233
+ `;
234
+ const fragment = glsl`#version 300 es
235
+ precision mediump float;
236
+
237
+ struct HitInfo {
238
+ bool hit;
239
+ float distance;
240
+ vec3 position;
241
+ vec3 normal;
242
+ vec3 color;
243
+ };
244
+
245
+ ${sphereModule}
246
+ ${boxModule}
247
+
248
+ ${uniform.float("aspectRatio")}
249
+
250
+ in vec2 v_uv;
251
+ out vec4 fragColor;
252
+
253
+ vec3 lightPos = vec3(5.0, 5.0, 5.0);
254
+
255
+ vec3 calculateLighting(in vec3 position, in vec3 normal, in vec3 color)
256
+ {
257
+ vec3 lightDirection = normalize(lightPos - position);
258
+ float diffuse = max(0.0, dot(normal, lightDirection));
259
+ return color * (diffuse * 0.8 + 0.2); // 80% diffuse + 20% ambient
260
+ }
261
+
262
+ void main()
263
+ {
264
+ // Ray from camera through pixel
265
+ vec3 rayOrigin = vec3(0.0, 0.0, 4.0);
266
+ vec3 rayDirection = normalize(vec3(v_uv.x * aspectRatio, v_uv.y, -1.0));
267
+
268
+ // Find closest intersection across all shapes
269
+ float closestDistance = 1000.0;
270
+ int hitType = -1;
271
+ int hitIndex = -1;
272
+
273
+ // Check spheres
274
+ int sphereIndex, sphereType;
275
+ float sphereDistance = findClosestSphere(rayOrigin, rayDirection, closestDistance, sphereIndex, sphereType);
276
+ if(sphereDistance > 0.0) {
277
+ closestDistance = sphereDistance;
278
+ hitType = sphereType;
279
+ hitIndex = sphereIndex;
280
+ }
281
+
282
+ // Check boxes
283
+ int boxIndex, boxType;
284
+ float boxDistance = findClosestBox(rayOrigin, rayDirection, closestDistance, boxIndex, boxType);
285
+ if(boxDistance > 0.0) {
286
+ closestDistance = boxDistance;
287
+ hitType = boxType;
288
+ hitIndex = boxIndex;
289
+ }
290
+
291
+ vec3 color = vec3(0.1, 0.1, 0.2); // Background color
292
+
293
+ // Calculate normal and lighting only for the closest hit
294
+ if(hitType >= 0) {
295
+ vec3 hitPosition = rayOrigin + closestDistance * rayDirection;
296
+ vec3 normal;
297
+ vec3 hitColor;
298
+
299
+ switch(hitType) {
300
+ case ${SHAPE_SPHERE}:
301
+ calculateSphereHit(hitPosition, hitIndex, normal, hitColor);
302
+ color = calculateLighting(hitPosition, normal, hitColor);
303
+ break;
304
+ case ${SHAPE_BOX}:
305
+ calculateBoxHit(hitPosition, hitIndex, normal, hitColor);
306
+ color = calculateLighting(hitPosition, normal, hitColor);
307
+ break;
308
+ }
309
+ }
310
+
311
+ // Gamma correction
312
+ color = pow(color, vec3(1.0 / 2.2));
313
+
314
+ fragColor = vec4(color, 1.0);
315
+ }`;
316
+ const { program, view } = compile.toQuad(gl, fragment);
317
+ gl.useProgram(program);
318
+ view.attributes.a_quad.bind();
319
+ const spheresArray = new Float32Array(
320
+ function* () {
321
+ for (const sphere of sphereData) {
322
+ yield sphere[0];
323
+ yield sphere[1];
324
+ yield sphere[2];
325
+ yield sphere[3];
326
+ }
327
+ }()
328
+ );
329
+ const sphereColorsArray = new Float32Array(
330
+ function* () {
331
+ for (const color of sphereColors) {
332
+ yield color[0];
333
+ yield color[1];
334
+ yield color[2];
335
+ }
336
+ }()
337
+ );
338
+ const boxesArray = new Float32Array(
339
+ function* () {
340
+ for (const box of boxData) {
341
+ yield box[0];
342
+ yield box[1];
343
+ yield box[2];
344
+ yield box[3];
345
+ }
346
+ }()
347
+ );
348
+ const boxColorsArray = new Float32Array(
349
+ function* () {
350
+ for (const color of boxColors) {
351
+ yield color[0];
352
+ yield color[1];
353
+ yield color[2];
354
+ }
355
+ }()
356
+ );
357
+ view.uniforms.sphereData.set(spheresArray);
358
+ view.uniforms.sphereColors.set(sphereColorsArray);
359
+ view.uniforms.boxData.set(boxesArray);
360
+ view.uniforms.boxColors.set(boxColorsArray);
361
+ view.uniforms.aspectRatio.set(canvas.width / canvas.height);
362
+ function animate(timestamp) {
363
+ const time = timestamp / 1e3;
364
+ const pulseFactor = 0.8 + 0.4 * Math.sin(time * 2);
365
+ spheresArray[3] = pulseFactor;
366
+ const orbitRadius = 2.5;
367
+ spheresArray[4] = Math.cos(time * 0.8) * orbitRadius;
368
+ spheresArray[5] = Math.sin(time * 1.2) * 1.5;
369
+ spheresArray[6] = Math.sin(time * 0.8) * orbitRadius;
370
+ spheresArray[8] = Math.cos(time * 0.6 + Math.PI) * orbitRadius;
371
+ spheresArray[9] = Math.cos(time * 0.9) * 1.2;
372
+ spheresArray[10] = Math.sin(time * 0.6 + Math.PI) * orbitRadius;
373
+ spheresArray[13] = 1 + Math.sin(time * 1.5) * 2;
374
+ spheresArray[16] = Math.sin(time * 0.7) * 2.5;
375
+ spheresArray[17] = Math.sin(time * 1.4) * 1.5;
376
+ spheresArray[18] = -2 + Math.cos(time * 0.7);
377
+ spheresArray[20] = Math.cos(time) * 3.5;
378
+ spheresArray[22] = Math.sin(time) * 3.5;
379
+ spheresArray[23] = 0.4 + 0.2 * Math.sin(time * 3);
380
+ spheresArray[24] = -4 + Math.sin(time * 0.8) * 1.5;
381
+ spheresArray[25] = 2 + Math.sin(time * 1.2) * 1;
382
+ spheresArray[28] = Math.cos(time * 0.5) * 2;
383
+ spheresArray[29] = -1 + Math.sin(time * 0.5) * 0.5;
384
+ sphereColorsArray[0] = 0.5 + 0.5 * Math.sin(time * 3);
385
+ sphereColorsArray[4] = 0.5 + 0.5 * Math.cos(time * 2);
386
+ sphereColorsArray[9] = 0.5 + 0.5 * Math.sin(time * 2.5);
387
+ sphereColorsArray[15] = 0.3 + 0.7 * Math.abs(Math.sin(time * 1.8));
388
+ sphereColorsArray[16] = 0.3 + 0.7 * Math.abs(Math.sin(time * 1.8));
389
+ sphereColorsArray[17] = 0.3 + 0.7 * Math.abs(Math.sin(time * 1.8));
390
+ boxesArray[0] = 2 + Math.cos(time * 0.7) * 0.5;
391
+ boxesArray[2] = -1 + Math.sin(time * 0.7) * 0.5;
392
+ boxesArray[7] = 0.6 + 0.3 * Math.sin(time * 2);
393
+ view.uniforms.sphereData?.set(spheresArray);
394
+ view.uniforms.sphereColors?.set(sphereColorsArray);
395
+ view.uniforms.boxData?.set(boxesArray);
396
+ view.uniforms.boxColors?.set(boxColorsArray);
397
+ }
398
+ function draw(timestamp) {
399
+ if (controller.signal.aborted) return;
400
+ animate(timestamp);
401
+ gl.drawArrays(gl.TRIANGLES, 0, 6);
402
+ requestAnimationFrame(draw);
403
+ }
404
+ requestAnimationFrame(draw);
405
+ new ResizeObserver(() => {
406
+ canvas.width = window.innerWidth;
407
+ canvas.height = window.innerHeight;
408
+ gl.viewport(0, 0, canvas.width, canvas.height);
409
+ view.uniforms.aspectRatio.set(canvas.width / canvas.height);
410
+ draw(performance.now());
411
+ }).observe(document.body);