@threenative/three-native 0.0.0-stage → 0.3.5

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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +24 -2
  3. package/package.json +42 -3
  4. package/src/addons/buffer-geometry-utils.ts +14 -0
  5. package/src/addons/data-utils.ts +63 -0
  6. package/src/addons/decal-geometry.ts +25 -0
  7. package/src/addons/decal.ts +218 -0
  8. package/src/addons/gltf-loader-web.ts +323 -0
  9. package/src/addons/hdr-loader.ts +71 -0
  10. package/src/addons/hdr.ts +139 -0
  11. package/src/addons/lut-cube-loader-web.ts +17 -0
  12. package/src/addons/lut-cube-loader.ts +149 -0
  13. package/src/addons/merge-geometries.ts +374 -0
  14. package/src/addons/mesh-bvh-webgpu.ts +13 -0
  15. package/src/addons/mesh-bvh.ts +50 -0
  16. package/src/addons/post-effects-web.ts +22 -0
  17. package/src/addons/post-effects.ts +173 -0
  18. package/src/audio.ts +564 -0
  19. package/src/blend-mode.ts +33 -0
  20. package/src/browser-backend.ts +2156 -0
  21. package/src/browser-entry.ts +305 -0
  22. package/src/browser-math.ts +448 -0
  23. package/src/browser-renderer.ts +527 -0
  24. package/src/browser-tsl.ts +650 -0
  25. package/src/catalog.ts +497 -0
  26. package/src/fixture-format.ts +721 -0
  27. package/src/fixture-native.ts +426 -0
  28. package/src/fixture-protocol.ts +315 -0
  29. package/src/math-utils.ts +65 -0
  30. package/src/object-surface.ts +250 -0
  31. package/src/pass-node.ts +286 -0
  32. package/src/property-binding.ts +93 -0
  33. package/src/quad-mesh.ts +54 -0
  34. package/src/reflector.ts +89 -0
  35. package/src/refused.ts +27 -0
  36. package/src/render-target.ts +107 -0
  37. package/src/rtt.ts +158 -0
  38. package/src/shadow-map.ts +21 -0
  39. package/src/storage.ts +206 -0
  40. package/src/texture-sources.ts +252 -0
  41. package/src/tsl-extras.ts +51 -0
  42. package/src/tsl-struct.ts +20 -0
  43. package/src/tsl-uniforms.ts +234 -0
  44. package/src/update-ranges.ts +109 -0
  45. package/src/wgsl-fn.ts +55 -0
@@ -0,0 +1,448 @@
1
+ /**
2
+ * three's math value classes in JavaScript (three r185's Vector3, MIT), for the browser back end.
3
+ *
4
+ * A `new Vector3()` is a plain JS value: its arithmetic never calls the engine, which cost Midway
5
+ * hundreds of thousands of engine calls at load. A vector the engine owns (`mesh.position`) is the
6
+ * same class over the engine's memory (see `defineBrowserClasses`), so `copy`, `add` and the rest run
7
+ * the same code on both. A value crosses to the engine by value only when an engine call takes it.
8
+ */
9
+
10
+ interface IVec3 {
11
+ x: number;
12
+ y: number;
13
+ z: number;
14
+ }
15
+ interface IQuat extends IVec3 {
16
+ w: number;
17
+ }
18
+ // A Matrix3 reads the first nine.
19
+ type Elements = readonly [
20
+ number,
21
+ number,
22
+ number,
23
+ number,
24
+ number,
25
+ number,
26
+ number,
27
+ number,
28
+ number,
29
+ number,
30
+ number,
31
+ number,
32
+ number,
33
+ number,
34
+ number,
35
+ number,
36
+ ];
37
+ interface IElements {
38
+ readonly elements: Elements;
39
+ }
40
+ interface IQuaternionScratch extends IQuat {
41
+ setFromEuler(euler: unknown): IQuat;
42
+ setFromAxisAngle(axis: IVec3, angle: number): IQuat;
43
+ }
44
+
45
+ const clamp = (value: number, min: number, max: number): number =>
46
+ Math.max(min, Math.min(max, value));
47
+
48
+ /** The JS classes; `quaternion` makes the engine Quaternion `applyEuler` and `applyAxisAngle` use. */
49
+ export function defineMath(quaternion: () => IQuaternionScratch) {
50
+ let rotation: IQuaternionScratch | undefined;
51
+ const scratchQuaternion = (): IQuaternionScratch => {
52
+ rotation ??= quaternion();
53
+ return rotation;
54
+ };
55
+
56
+ class Vector3 implements IVec3 {
57
+ declare x: number;
58
+ declare y: number;
59
+ declare z: number;
60
+ constructor(x = 0, y = 0, z = 0) {
61
+ this.x = x;
62
+ this.y = y;
63
+ this.z = z;
64
+ }
65
+ set(x: number, y: number, z?: number): this {
66
+ this.x = x;
67
+ this.y = y;
68
+ this.z = z === undefined ? this.z : z;
69
+ return this;
70
+ }
71
+ setScalar(scalar: number): this {
72
+ this.x = scalar;
73
+ this.y = scalar;
74
+ this.z = scalar;
75
+ return this;
76
+ }
77
+ setX(x: number): this {
78
+ this.x = x;
79
+ return this;
80
+ }
81
+ setY(y: number): this {
82
+ this.y = y;
83
+ return this;
84
+ }
85
+ setZ(z: number): this {
86
+ this.z = z;
87
+ return this;
88
+ }
89
+ setComponent(index: number, value: number): this {
90
+ if (index === 0) this.x = value;
91
+ else if (index === 1) this.y = value;
92
+ else if (index === 2) this.z = value;
93
+ else throw new Error(`THREE.Vector3: index is out of range: ${index}`);
94
+ return this;
95
+ }
96
+ getComponent(index: number): number {
97
+ if (index === 0) return this.x;
98
+ if (index === 1) return this.y;
99
+ if (index === 2) return this.z;
100
+ throw new Error(`THREE.Vector3: index is out of range: ${index}`);
101
+ }
102
+ clone(): Vector3 {
103
+ return new Vector3(this.x, this.y, this.z);
104
+ }
105
+ copy(v: IVec3): this {
106
+ this.x = v.x;
107
+ this.y = v.y;
108
+ this.z = v.z;
109
+ return this;
110
+ }
111
+ add(v: IVec3): this {
112
+ this.x += v.x;
113
+ this.y += v.y;
114
+ this.z += v.z;
115
+ return this;
116
+ }
117
+ addScalar(s: number): this {
118
+ this.x += s;
119
+ this.y += s;
120
+ this.z += s;
121
+ return this;
122
+ }
123
+ addVectors(a: IVec3, b: IVec3): this {
124
+ this.x = a.x + b.x;
125
+ this.y = a.y + b.y;
126
+ this.z = a.z + b.z;
127
+ return this;
128
+ }
129
+ addScaledVector(v: IVec3, s: number): this {
130
+ this.x += v.x * s;
131
+ this.y += v.y * s;
132
+ this.z += v.z * s;
133
+ return this;
134
+ }
135
+ sub(v: IVec3): this {
136
+ this.x -= v.x;
137
+ this.y -= v.y;
138
+ this.z -= v.z;
139
+ return this;
140
+ }
141
+ subScalar(s: number): this {
142
+ this.x -= s;
143
+ this.y -= s;
144
+ this.z -= s;
145
+ return this;
146
+ }
147
+ subVectors(a: IVec3, b: IVec3): this {
148
+ this.x = a.x - b.x;
149
+ this.y = a.y - b.y;
150
+ this.z = a.z - b.z;
151
+ return this;
152
+ }
153
+ multiply(v: IVec3): this {
154
+ this.x *= v.x;
155
+ this.y *= v.y;
156
+ this.z *= v.z;
157
+ return this;
158
+ }
159
+ multiplyScalar(scalar: number): this {
160
+ this.x *= scalar;
161
+ this.y *= scalar;
162
+ this.z *= scalar;
163
+ return this;
164
+ }
165
+ multiplyVectors(a: IVec3, b: IVec3): this {
166
+ this.x = a.x * b.x;
167
+ this.y = a.y * b.y;
168
+ this.z = a.z * b.z;
169
+ return this;
170
+ }
171
+ applyEuler(euler: unknown): this {
172
+ return this.applyQuaternion(scratchQuaternion().setFromEuler(euler));
173
+ }
174
+ applyAxisAngle(axis: IVec3, angle: number): this {
175
+ return this.applyQuaternion(scratchQuaternion().setFromAxisAngle(axis, angle));
176
+ }
177
+ applyMatrix3(m: IElements): this {
178
+ const { x, y, z } = this;
179
+ const e = m.elements;
180
+ this.x = e[0] * x + e[3] * y + e[6] * z;
181
+ this.y = e[1] * x + e[4] * y + e[7] * z;
182
+ this.z = e[2] * x + e[5] * y + e[8] * z;
183
+ return this;
184
+ }
185
+ applyNormalMatrix(m: IElements): this {
186
+ return this.applyMatrix3(m).normalize();
187
+ }
188
+ applyMatrix4(m: IElements): this {
189
+ const { x, y, z } = this;
190
+ const e = m.elements;
191
+ const w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
192
+ this.x = (e[0] * x + e[4] * y + e[8] * z + e[12]) * w;
193
+ this.y = (e[1] * x + e[5] * y + e[9] * z + e[13]) * w;
194
+ this.z = (e[2] * x + e[6] * y + e[10] * z + e[14]) * w;
195
+ return this;
196
+ }
197
+ applyQuaternion(q: IQuat): this {
198
+ const vx = this.x;
199
+ const vy = this.y;
200
+ const vz = this.z;
201
+ const { x: qx, y: qy, z: qz, w: qw } = q;
202
+ const tx = 2 * (qy * vz - qz * vy);
203
+ const ty = 2 * (qz * vx - qx * vz);
204
+ const tz = 2 * (qx * vy - qy * vx);
205
+ this.x = vx + qw * tx + qy * tz - qz * ty;
206
+ this.y = vy + qw * ty + qz * tx - qx * tz;
207
+ this.z = vz + qw * tz + qx * ty - qy * tx;
208
+ return this;
209
+ }
210
+ project(camera: { matrixWorldInverse: IElements; projectionMatrix: IElements }): this {
211
+ return this.applyMatrix4(camera.matrixWorldInverse).applyMatrix4(camera.projectionMatrix);
212
+ }
213
+ unproject(camera: { projectionMatrixInverse: IElements; matrixWorld: IElements }): this {
214
+ return this.applyMatrix4(camera.projectionMatrixInverse).applyMatrix4(camera.matrixWorld);
215
+ }
216
+ transformDirection(m: IElements): this {
217
+ const { x, y, z } = this;
218
+ const e = m.elements;
219
+ this.x = e[0] * x + e[4] * y + e[8] * z;
220
+ this.y = e[1] * x + e[5] * y + e[9] * z;
221
+ this.z = e[2] * x + e[6] * y + e[10] * z;
222
+ return this.normalize();
223
+ }
224
+ divide(v: IVec3): this {
225
+ this.x /= v.x;
226
+ this.y /= v.y;
227
+ this.z /= v.z;
228
+ return this;
229
+ }
230
+ divideScalar(scalar: number): this {
231
+ return this.multiplyScalar(1 / scalar);
232
+ }
233
+ min(v: IVec3): this {
234
+ this.x = Math.min(this.x, v.x);
235
+ this.y = Math.min(this.y, v.y);
236
+ this.z = Math.min(this.z, v.z);
237
+ return this;
238
+ }
239
+ max(v: IVec3): this {
240
+ this.x = Math.max(this.x, v.x);
241
+ this.y = Math.max(this.y, v.y);
242
+ this.z = Math.max(this.z, v.z);
243
+ return this;
244
+ }
245
+ clamp(min: IVec3, max: IVec3): this {
246
+ this.x = clamp(this.x, min.x, max.x);
247
+ this.y = clamp(this.y, min.y, max.y);
248
+ this.z = clamp(this.z, min.z, max.z);
249
+ return this;
250
+ }
251
+ clampScalar(minVal: number, maxVal: number): this {
252
+ this.x = clamp(this.x, minVal, maxVal);
253
+ this.y = clamp(this.y, minVal, maxVal);
254
+ this.z = clamp(this.z, minVal, maxVal);
255
+ return this;
256
+ }
257
+ clampLength(min: number, max: number): this {
258
+ const length = this.length();
259
+ return this.divideScalar(length || 1).multiplyScalar(clamp(length, min, max));
260
+ }
261
+ floor(): this {
262
+ this.x = Math.floor(this.x);
263
+ this.y = Math.floor(this.y);
264
+ this.z = Math.floor(this.z);
265
+ return this;
266
+ }
267
+ ceil(): this {
268
+ this.x = Math.ceil(this.x);
269
+ this.y = Math.ceil(this.y);
270
+ this.z = Math.ceil(this.z);
271
+ return this;
272
+ }
273
+ round(): this {
274
+ this.x = Math.round(this.x);
275
+ this.y = Math.round(this.y);
276
+ this.z = Math.round(this.z);
277
+ return this;
278
+ }
279
+ roundToZero(): this {
280
+ this.x = Math.trunc(this.x);
281
+ this.y = Math.trunc(this.y);
282
+ this.z = Math.trunc(this.z);
283
+ return this;
284
+ }
285
+ negate(): this {
286
+ this.x = -this.x;
287
+ this.y = -this.y;
288
+ this.z = -this.z;
289
+ return this;
290
+ }
291
+ dot(v: IVec3): number {
292
+ return this.x * v.x + this.y * v.y + this.z * v.z;
293
+ }
294
+ lengthSq(): number {
295
+ return this.x * this.x + this.y * this.y + this.z * this.z;
296
+ }
297
+ length(): number {
298
+ return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
299
+ }
300
+ manhattanLength(): number {
301
+ return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z);
302
+ }
303
+ normalize(): this {
304
+ return this.divideScalar(this.length() || 1);
305
+ }
306
+ setLength(length: number): this {
307
+ return this.normalize().multiplyScalar(length);
308
+ }
309
+ lerp(v: IVec3, alpha: number): this {
310
+ this.x += (v.x - this.x) * alpha;
311
+ this.y += (v.y - this.y) * alpha;
312
+ this.z += (v.z - this.z) * alpha;
313
+ return this;
314
+ }
315
+ lerpVectors(v1: IVec3, v2: IVec3, alpha: number): this {
316
+ this.x = v1.x + (v2.x - v1.x) * alpha;
317
+ this.y = v1.y + (v2.y - v1.y) * alpha;
318
+ this.z = v1.z + (v2.z - v1.z) * alpha;
319
+ return this;
320
+ }
321
+ cross(v: IVec3): this {
322
+ return this.crossVectors(this, v);
323
+ }
324
+ crossVectors(a: IVec3, b: IVec3): this {
325
+ const { x: ax, y: ay, z: az } = a;
326
+ const { x: bx, y: by, z: bz } = b;
327
+ this.x = ay * bz - az * by;
328
+ this.y = az * bx - ax * bz;
329
+ this.z = ax * by - ay * bx;
330
+ return this;
331
+ }
332
+ projectOnVector(v: Vector3): this {
333
+ const denominator = v.lengthSq();
334
+ if (denominator === 0) return this.set(0, 0, 0);
335
+ const scalar = v.dot(this) / denominator;
336
+ return this.copy(v).multiplyScalar(scalar);
337
+ }
338
+ projectOnPlane(planeNormal: Vector3): this {
339
+ return this.sub(new Vector3().copy(this).projectOnVector(planeNormal));
340
+ }
341
+ reflect(normal: IVec3): this {
342
+ return this.sub(new Vector3().copy(normal).multiplyScalar(2 * this.dot(normal)));
343
+ }
344
+ angleTo(v: Vector3): number {
345
+ const denominator = Math.sqrt(this.lengthSq() * v.lengthSq());
346
+ if (denominator === 0) return Math.PI / 2;
347
+ return Math.acos(clamp(this.dot(v) / denominator, -1, 1));
348
+ }
349
+ distanceTo(v: IVec3): number {
350
+ return Math.sqrt(this.distanceToSquared(v));
351
+ }
352
+ distanceToSquared(v: IVec3): number {
353
+ const dx = this.x - v.x;
354
+ const dy = this.y - v.y;
355
+ const dz = this.z - v.z;
356
+ return dx * dx + dy * dy + dz * dz;
357
+ }
358
+ manhattanDistanceTo(v: IVec3): number {
359
+ return Math.abs(this.x - v.x) + Math.abs(this.y - v.y) + Math.abs(this.z - v.z);
360
+ }
361
+ setFromSphericalCoords(radius: number, phi: number, theta: number): this {
362
+ const sinPhiRadius = Math.sin(phi) * radius;
363
+ this.x = sinPhiRadius * Math.sin(theta);
364
+ this.y = Math.cos(phi) * radius;
365
+ this.z = sinPhiRadius * Math.cos(theta);
366
+ return this;
367
+ }
368
+ setFromCylindricalCoords(radius: number, theta: number, y: number): this {
369
+ this.x = radius * Math.sin(theta);
370
+ this.y = y;
371
+ this.z = radius * Math.cos(theta);
372
+ return this;
373
+ }
374
+ setFromMatrixPosition(m: IElements): this {
375
+ const e = m.elements;
376
+ this.x = e[12];
377
+ this.y = e[13];
378
+ this.z = e[14];
379
+ return this;
380
+ }
381
+ setFromMatrixScale(m: IElements): this {
382
+ const e = m.elements;
383
+ const sx = Math.sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]);
384
+ const sy = Math.sqrt(e[4] * e[4] + e[5] * e[5] + e[6] * e[6]);
385
+ const sz = Math.sqrt(e[8] * e[8] + e[9] * e[9] + e[10] * e[10]);
386
+ return this.set(sx, sy, sz);
387
+ }
388
+ setFromMatrixColumn(m: IElements, index: number): this {
389
+ return this.fromArray(m.elements, index * 4);
390
+ }
391
+ setFromMatrix3Column(m: IElements, index: number): this {
392
+ return this.fromArray(m.elements, index * 3);
393
+ }
394
+ setFromEuler(e: IVec3): this {
395
+ return this.copy(e);
396
+ }
397
+ setFromColor(c: { r: number; g: number; b: number }): this {
398
+ return this.set(c.r, c.g, c.b);
399
+ }
400
+ equals(v: IVec3): boolean {
401
+ return v.x === this.x && v.y === this.y && v.z === this.z;
402
+ }
403
+ fromArray(array: ArrayLike<number>, offset = 0): this {
404
+ this.x = array[offset] as number;
405
+ this.y = array[offset + 1] as number;
406
+ this.z = array[offset + 2] as number;
407
+ return this;
408
+ }
409
+ toArray(array: number[] = [], offset = 0): number[] {
410
+ array[offset] = this.x;
411
+ array[offset + 1] = this.y;
412
+ array[offset + 2] = this.z;
413
+ return array;
414
+ }
415
+ fromBufferAttribute(
416
+ attribute: { getX(i: number): number; getY(i: number): number; getZ(i: number): number },
417
+ index: number,
418
+ ): this {
419
+ this.x = attribute.getX(index);
420
+ this.y = attribute.getY(index);
421
+ this.z = attribute.getZ(index);
422
+ return this;
423
+ }
424
+ *[Symbol.iterator](): Generator<number> {
425
+ yield this.x;
426
+ yield this.y;
427
+ yield this.z;
428
+ }
429
+ }
430
+
431
+ /** three's MathUtils functions the engine binds, as JS: a clamp is not worth an engine call. */
432
+ class MathUtils {
433
+ clamp(value: number, min: number, max: number): number {
434
+ return clamp(value, min, max);
435
+ }
436
+ degToRad(degrees: number): number {
437
+ return degrees * (Math.PI / 180);
438
+ }
439
+ euclideanModulo(n: number, m: number): number {
440
+ return ((n % m) + m) % m;
441
+ }
442
+ lerp(x: number, y: number, t: number): number {
443
+ return (1 - t) * x + t * y;
444
+ }
445
+ }
446
+
447
+ return { Vector3, MathUtils };
448
+ }