@needle-tools/three-mesh-effects 0.1.0-alpha → 0.1.0-alpha.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 (86) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +94 -88
  3. package/build/deformation-runtime.js.map +1 -1
  4. package/build/impostor-runtime-needle-engine.js.map +1 -1
  5. package/build/impostor-runtime.js.map +1 -1
  6. package/build/internal/deformation/cloth-surface.js.map +1 -1
  7. package/build/internal/deformation/fur-surface.js.map +1 -1
  8. package/build/internal/deformation/ik-constraints.js.map +1 -1
  9. package/build/internal/deformation/joints-json.js.map +1 -1
  10. package/build/internal/deformation/limits.js.map +1 -1
  11. package/build/internal/deformation/motion-settings.js.map +1 -1
  12. package/build/internal/deformation/pin-cloth.js.map +1 -1
  13. package/build/internal/deformation/pin-ik.js.map +1 -1
  14. package/build/internal/deformation/pin-types.js.map +1 -1
  15. package/build/internal/deformation/pin-wiggle.js.map +1 -1
  16. package/build/internal/deformation/pose-slots.js.map +1 -1
  17. package/build/internal/deformation/pose-timeline.js.map +1 -1
  18. package/build/internal/deformation/rig-drivers.js.map +1 -1
  19. package/build/internal/deformation/rig-extension.js.map +1 -1
  20. package/build/internal/deformation/rig-runtime.js.map +1 -1
  21. package/build/internal/deformation/wind.js.map +1 -1
  22. package/build/internal/impostor/impostor-metadata.js.map +1 -1
  23. package/build/internal/impostor/impostor-node-material.js.map +1 -1
  24. package/build/internal/impostor/impostor-runtime-support.js.map +1 -1
  25. package/build/internal/impostor/octahedral.js.map +1 -1
  26. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +1 -1
  27. package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +1 -1
  28. package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +1 -1
  29. package/build/internal/rendering/skin-projection.js +52 -52
  30. package/build/internal/rendering/skin-projection.js.map +2 -2
  31. package/build/internal/rendering/surface-query.js +52 -52
  32. package/build/internal/rendering/surface-query.js.map +2 -2
  33. package/build/internal/rendering/wireframe.js.map +1 -1
  34. package/build/internal/rendering/wrap-preview-settings.js.map +1 -1
  35. package/build/internal/rendering/wrap-preview.js +52 -52
  36. package/build/internal/rendering/wrap-preview.js.map +2 -2
  37. package/build/internal/rendering/wrap-skin.js +52 -52
  38. package/build/internal/rendering/wrap-skin.js.map +2 -2
  39. package/build/pixel-impostor-runtime.js.map +1 -1
  40. package/build/wrap-effects.js +52 -52
  41. package/build/wrap-effects.js.map +2 -2
  42. package/package.json +107 -94
  43. package/src/deformation/cloth-surface.ts +223 -223
  44. package/src/deformation/fur-surface.ts +129 -129
  45. package/src/deformation/ik-constraints.ts +37 -37
  46. package/src/deformation/joints-json.ts +94 -94
  47. package/src/deformation/limits.ts +25 -25
  48. package/src/deformation/motion-settings.ts +9 -9
  49. package/src/deformation/pin-cloth.ts +149 -149
  50. package/src/deformation/pin-ik.ts +582 -582
  51. package/src/deformation/pin-types.ts +22 -22
  52. package/src/deformation/pin-wiggle.ts +119 -119
  53. package/src/deformation/pose-slots.ts +106 -106
  54. package/src/deformation/pose-timeline.ts +242 -242
  55. package/src/deformation/rig-drivers.ts +32 -32
  56. package/src/deformation/rig-extension.ts +87 -87
  57. package/src/deformation/rig-runtime.ts +416 -416
  58. package/src/deformation/wind.ts +36 -36
  59. package/src/impostor/impostor-metadata.js +137 -137
  60. package/src/impostor/impostor-node-material.js +1581 -1581
  61. package/src/impostor/impostor-runtime-support.js +139 -139
  62. package/src/impostor/octahedral.js +280 -280
  63. package/src/pixel-impostor/pixel-impostor-display.js +794 -794
  64. package/src/pixel-impostor/pixel-impostor-metadata.js +74 -74
  65. package/src/pixel-impostor/pixel-impostor-runtime.js +78 -78
  66. package/src/public/deformation-runtime.ts +3 -3
  67. package/src/public/impostor-runtime-needle-engine.js +56 -56
  68. package/src/public/impostor-runtime.js +21 -21
  69. package/src/public/pixel-impostor-runtime.js +5 -5
  70. package/src/public/wrap-effects.js +21 -21
  71. package/src/rendering/skin-projection.js +113 -113
  72. package/src/rendering/surface-query.js +88 -88
  73. package/src/rendering/wireframe.js +115 -115
  74. package/src/rendering/wrap-preview-settings.js +123 -123
  75. package/src/rendering/wrap-preview.js +645 -645
  76. package/src/rendering/wrap-skin.js +109 -109
  77. package/build/internal/public/deformation-runtime.js +0 -2143
  78. package/build/internal/public/deformation-runtime.js.map +0 -7
  79. package/build/internal/public/impostor-runtime-needle-engine.js +0 -42
  80. package/build/internal/public/impostor-runtime-needle-engine.js.map +0 -7
  81. package/build/internal/public/impostor-runtime.js +0 -1333
  82. package/build/internal/public/impostor-runtime.js.map +0 -7
  83. package/build/internal/public/pixel-impostor-runtime.js +0 -876
  84. package/build/internal/public/pixel-impostor-runtime.js.map +0 -7
  85. package/build/internal/public/wrap-effects.js +0 -5640
  86. package/build/internal/public/wrap-effects.js.map +0 -7
@@ -1,280 +1,280 @@
1
- // @ts-check
2
-
3
- // A billboard point samples the capture camera along point -> camera, so its
4
- // local plane offset is subtracted from the object-center camera vector.
5
- export const PERSPECTIVE_RAY_PLANE_SIGN = -1;
6
-
7
- /**
8
- * Encodes a local-space point into the orthographic capture depth used by the
9
- * normal/depth atlas. Capture cameras sit at `direction * 2r` and cover the
10
- * interval from +r to -r along their forward axis.
11
- *
12
- * @param {[number, number, number]} point
13
- * @param {[number, number, number]} direction Unit capture direction.
14
- * @param {number} radius
15
- */
16
- export function captureDepthFromPoint(point, direction, radius) {
17
- if (!(radius > 0) || !Number.isFinite(radius)) {
18
- throw new RangeError('Capture radius must be positive and finite.');
19
- }
20
- const normalizedDirection = normalize3(direction);
21
- return (radius - dot3(point, normalizedDirection)) / (2 * radius);
22
- }
23
-
24
- /**
25
- * Intersects a runtime point-to-camera ray with the capture-depth plane
26
- * `dot(P, captureDirection) = radius - 2 * radius * depth`.
27
- *
28
- * This is the fixed-point step solved by the runtime parallax shader. It is
29
- * deliberately expressed independently here so sign and scale regressions are
30
- * caught by browserless tests.
31
- *
32
- * @param {[number, number, number]} rayOrigin
33
- * @param {[number, number, number]} rayDirection Unit point-to-camera ray.
34
- * @param {[number, number, number]} captureDirection Unit capture direction.
35
- * @param {number} depth
36
- * @param {number} radius
37
- * @returns {[number, number, number]}
38
- */
39
- export function intersectRayWithCaptureDepth(
40
- rayOrigin,
41
- rayDirection,
42
- captureDirection,
43
- depth,
44
- radius,
45
- ) {
46
- if (!(radius > 0) || !Number.isFinite(radius) || !Number.isFinite(depth)) {
47
- throw new RangeError('Capture depth and radius must be finite, with a positive radius.');
48
- }
49
- const ray = normalize3(rayDirection);
50
- const capture = normalize3(captureDirection);
51
- const denominator = dot3(ray, capture);
52
- if (Math.abs(denominator) < 1e-8) {
53
- throw new RangeError('Runtime ray is parallel to the capture-depth plane.');
54
- }
55
- const planeDepth = radius - 2 * radius * depth;
56
- const distance = (planeDepth - dot3(rayOrigin, capture)) / denominator;
57
- return /** @type {[number, number, number]} */ (rayOrigin.map((value, index) => (
58
- value + ray[index] * distance
59
- )));
60
- }
61
-
62
- /**
63
- * Decodes a regular or upper-hemisphere octahedral grid coordinate.
64
- * Y is the up axis used throughout the baker and preview runtime.
65
- *
66
- * @param {[number, number]} coordinate normalized 0..1 grid coordinate
67
- * @param {'hemisphere' | 'full'} mapping
68
- * @returns {[number, number, number]}
69
- */
70
- export function gridToDirection(coordinate, mapping) {
71
- const [u, v] = coordinate;
72
- let x;
73
- let y;
74
- let z;
75
- if (mapping === 'hemisphere') {
76
- x = u - v;
77
- z = -1 + u + v;
78
- y = 1 - Math.abs(x) - Math.abs(z);
79
- } else {
80
- x = u * 2 - 1;
81
- z = v * 2 - 1;
82
- y = 1 - Math.abs(x) - Math.abs(z);
83
- if (y < 0) {
84
- const oldX = x;
85
- x = nonZeroSign(x) * (1 - Math.abs(z));
86
- z = nonZeroSign(z) * (1 - Math.abs(oldX));
87
- }
88
- }
89
- return normalize3([x, y, z]);
90
- }
91
-
92
- /**
93
- * @param {[number, number, number]} input
94
- * @param {'hemisphere' | 'full'} mapping
95
- * @returns {[number, number]}
96
- */
97
- export function directionToGrid(input, mapping) {
98
- let [x, y, z] = normalize3(input);
99
- if (mapping === 'hemisphere') y = Math.max(y, 1e-6);
100
- const inverseL1 = 1 / Math.max(1e-12, Math.abs(x) + Math.abs(y) + Math.abs(z));
101
- x *= inverseL1;
102
- y *= inverseL1;
103
- z *= inverseL1;
104
- if (mapping === 'hemisphere') {
105
- return clamp2([(x + z) * 0.5 + 0.5, (z - x) * 0.5 + 0.5]);
106
- }
107
- if (y < 0) {
108
- const oldX = x;
109
- x = nonZeroSign(x) * (1 - Math.abs(z));
110
- z = nonZeroSign(z) * (1 - Math.abs(oldX));
111
- }
112
- return clamp2([x * 0.5 + 0.5, z * 0.5 + 0.5]);
113
- }
114
-
115
- /**
116
- * Finds the authored grid direction with the smallest spherical angle to an
117
- * arbitrary view. Octahedral coordinates are not equal-area, so independently
118
- * rounding U/V is not guaranteed to choose the closest capture near folds.
119
- * The production grids contain at most 24² views; an exact dot-product search
120
- * is both cheap and unambiguous for an interaction that runs on orbit release.
121
- *
122
- * @param {[number, number, number]} inputDirection
123
- * @param {'hemisphere' | 'full'} mapping
124
- * @param {number} frames
125
- */
126
- export function nearestOctahedralCapture(inputDirection, mapping, frames) {
127
- if (mapping !== 'hemisphere' && mapping !== 'full') {
128
- throw new RangeError('Impostor mapping must be hemisphere or full.');
129
- }
130
- if (!Number.isInteger(frames) || frames < 2) {
131
- throw new RangeError('Impostor capture search requires at least two frames per axis.');
132
- }
133
- const direction = normalize3(inputDirection);
134
- let bestFrame = /** @type {[number, number]} */ ([0, 0]);
135
- let bestDirection = gridToDirection([0, 0], mapping);
136
- let bestDot = -Number.POSITIVE_INFINITY;
137
- for (let y = 0; y < frames; y++) {
138
- for (let x = 0; x < frames; x++) {
139
- const candidate = gridToDirection([x / (frames - 1), y / (frames - 1)], mapping);
140
- const similarity = dot3(direction, candidate);
141
- if (similarity <= bestDot) continue;
142
- bestDot = similarity;
143
- bestFrame = [x, y];
144
- bestDirection = candidate;
145
- }
146
- }
147
- return {
148
- frame: bestFrame,
149
- direction: bestDirection,
150
- dot: Math.max(-1, Math.min(1, bestDot)),
151
- angleDegrees: Math.acos(Math.max(-1, Math.min(1, bestDot))) * 180 / Math.PI,
152
- };
153
- }
154
-
155
- /**
156
- * Projects a spherical direction beyond the latest sample. OrbitControls'
157
- * residual release motion supplies the two samples; values above one look
158
- * ahead in the same direction, so a fast release cannot select the capture
159
- * behind its visible trajectory.
160
- *
161
- * @param {[number, number, number]} startDirection
162
- * @param {[number, number, number]} endDirection
163
- * @param {number} multiplier 1 returns the end sample, 1.5 projects half of
164
- * the sampled angular motion beyond it.
165
- * @returns {[number, number, number]}
166
- */
167
- export function extrapolateSphericalDirection(startDirection, endDirection, multiplier) {
168
- if (!Number.isFinite(multiplier) || multiplier < 0) {
169
- throw new RangeError('Spherical extrapolation multiplier must be finite and non-negative.');
170
- }
171
- const start = normalize3(startDirection);
172
- const end = normalize3(endDirection);
173
- const similarity = Math.max(-1, Math.min(1, dot3(start, end)));
174
- const angle = Math.acos(similarity);
175
- const axis = cross3(start, end);
176
- const axisLength = Math.hypot(...axis);
177
- if (angle < 1e-8 || axisLength < 1e-8) return end;
178
- const unitAxis = /** @type {[number, number, number]} */ (
179
- axis.map((component) => component / axisLength)
180
- );
181
- const projectedAngle = angle * multiplier;
182
- const cosine = Math.cos(projectedAngle);
183
- const sine = Math.sin(projectedAngle);
184
- const axisCrossStart = cross3(unitAxis, start);
185
- const axisDotStart = dot3(unitAxis, start);
186
- return normalize3(/** @type {[number, number, number]} */ (start.map((component, index) => (
187
- component * cosine
188
- + axisCrossStart[index] * sine
189
- + unitAxis[index] * axisDotStart * (1 - cosine)
190
- ))));
191
- }
192
-
193
- /**
194
- * Returns the three vertices and barycentric weights of the grid triangle
195
- * containing a direction. The diagonal is chosen consistently on CPU and GPU.
196
- *
197
- * @param {[number, number, number]} direction
198
- * @param {'hemisphere' | 'full'} mapping
199
- * @param {number} frames
200
- */
201
- export function selectOctahedralFrames(direction, mapping, frames) {
202
- const scale = frames - 1;
203
- const grid = directionToGrid(direction, mapping);
204
- const gx = grid[0] * scale;
205
- const gy = grid[1] * scale;
206
- const x = Math.min(scale - 1, Math.floor(gx));
207
- const y = Math.min(scale - 1, Math.floor(gy));
208
- const fx = gx - x;
209
- const fy = gy - y;
210
- const base = gridToDirection([x / scale, y / scale], mapping);
211
- const right = gridToDirection([(x + 1) / scale, y / scale], mapping);
212
- const up = gridToDirection([x / scale, (y + 1) / scale], mapping);
213
- const diagonal = gridToDirection([(x + 1) / scale, (y + 1) / scale], mapping);
214
- // Octahedral grid quads are strongly warped on the sphere, especially at
215
- // low view counts. Use the shorter of their two spherical diagonals instead
216
- // of always connecting base -> diagonal. Fold cells still necessarily use
217
- // the anti-diagonal because it is the captured z=0 view edge.
218
- const antiDiagonal = mapping === 'hemisphere' && x + y === scale - 1
219
- || dot3(right, up) > dot3(base, diagonal);
220
- if (antiDiagonal) {
221
- if (fx + fy <= 1) {
222
- return {
223
- frames: /** @type {[[number, number], [number, number], [number, number]]} */ ([
224
- [x, y], [x + 1, y], [x, y + 1],
225
- ]),
226
- weights: /** @type {[number, number, number]} */ ([1 - fx - fy, fx, fy]),
227
- };
228
- }
229
- return {
230
- frames: /** @type {[[number, number], [number, number], [number, number]]} */ ([
231
- [x + 1, y + 1], [x, y + 1], [x + 1, y],
232
- ]),
233
- weights: /** @type {[number, number, number]} */ ([fx + fy - 1, 1 - fx, 1 - fy]),
234
- };
235
- }
236
- const horizontal = fx > fy;
237
- return {
238
- frames: /** @type {[[number, number], [number, number], [number, number]]} */ ([
239
- [x, y],
240
- horizontal ? [x + 1, y] : [x, y + 1],
241
- [x + 1, y + 1],
242
- ]),
243
- weights: /** @type {[number, number, number]} */ ([
244
- Math.min(1 - fx, 1 - fy),
245
- Math.abs(fx - fy),
246
- Math.min(fx, fy),
247
- ]),
248
- };
249
- }
250
-
251
- /** @param {[number, number, number]} a @param {[number, number, number]} b */
252
- function dot3(a, b) {
253
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
254
- }
255
-
256
- /** @param {[number, number, number]} a @param {[number, number, number]} b */
257
- function cross3(a, b) {
258
- return /** @type {[number, number, number]} */ ([
259
- a[1] * b[2] - a[2] * b[1],
260
- a[2] * b[0] - a[0] * b[2],
261
- a[0] * b[1] - a[1] * b[0],
262
- ]);
263
- }
264
-
265
- /** @param {[number, number, number]} value */
266
- function normalize3(value) {
267
- const length = Math.hypot(...value);
268
- if (length < 1e-12) return /** @type {[number, number, number]} */ ([0, 1, 0]);
269
- return /** @type {[number, number, number]} */ (value.map((component) => component / length));
270
- }
271
-
272
- /** @param {[number, number]} value */
273
- function clamp2(value) {
274
- return /** @type {[number, number]} */ (value.map((component) => Math.max(0, Math.min(1, component))));
275
- }
276
-
277
- /** @param {number} value */
278
- function nonZeroSign(value) {
279
- return value < 0 ? -1 : 1;
280
- }
1
+ // @ts-check
2
+
3
+ // A billboard point samples the capture camera along point -> camera, so its
4
+ // local plane offset is subtracted from the object-center camera vector.
5
+ export const PERSPECTIVE_RAY_PLANE_SIGN = -1;
6
+
7
+ /**
8
+ * Encodes a local-space point into the orthographic capture depth used by the
9
+ * normal/depth atlas. Capture cameras sit at `direction * 2r` and cover the
10
+ * interval from +r to -r along their forward axis.
11
+ *
12
+ * @param {[number, number, number]} point
13
+ * @param {[number, number, number]} direction Unit capture direction.
14
+ * @param {number} radius
15
+ */
16
+ export function captureDepthFromPoint(point, direction, radius) {
17
+ if (!(radius > 0) || !Number.isFinite(radius)) {
18
+ throw new RangeError('Capture radius must be positive and finite.');
19
+ }
20
+ const normalizedDirection = normalize3(direction);
21
+ return (radius - dot3(point, normalizedDirection)) / (2 * radius);
22
+ }
23
+
24
+ /**
25
+ * Intersects a runtime point-to-camera ray with the capture-depth plane
26
+ * `dot(P, captureDirection) = radius - 2 * radius * depth`.
27
+ *
28
+ * This is the fixed-point step solved by the runtime parallax shader. It is
29
+ * deliberately expressed independently here so sign and scale regressions are
30
+ * caught by browserless tests.
31
+ *
32
+ * @param {[number, number, number]} rayOrigin
33
+ * @param {[number, number, number]} rayDirection Unit point-to-camera ray.
34
+ * @param {[number, number, number]} captureDirection Unit capture direction.
35
+ * @param {number} depth
36
+ * @param {number} radius
37
+ * @returns {[number, number, number]}
38
+ */
39
+ export function intersectRayWithCaptureDepth(
40
+ rayOrigin,
41
+ rayDirection,
42
+ captureDirection,
43
+ depth,
44
+ radius,
45
+ ) {
46
+ if (!(radius > 0) || !Number.isFinite(radius) || !Number.isFinite(depth)) {
47
+ throw new RangeError('Capture depth and radius must be finite, with a positive radius.');
48
+ }
49
+ const ray = normalize3(rayDirection);
50
+ const capture = normalize3(captureDirection);
51
+ const denominator = dot3(ray, capture);
52
+ if (Math.abs(denominator) < 1e-8) {
53
+ throw new RangeError('Runtime ray is parallel to the capture-depth plane.');
54
+ }
55
+ const planeDepth = radius - 2 * radius * depth;
56
+ const distance = (planeDepth - dot3(rayOrigin, capture)) / denominator;
57
+ return /** @type {[number, number, number]} */ (rayOrigin.map((value, index) => (
58
+ value + ray[index] * distance
59
+ )));
60
+ }
61
+
62
+ /**
63
+ * Decodes a regular or upper-hemisphere octahedral grid coordinate.
64
+ * Y is the up axis used throughout the baker and preview runtime.
65
+ *
66
+ * @param {[number, number]} coordinate normalized 0..1 grid coordinate
67
+ * @param {'hemisphere' | 'full'} mapping
68
+ * @returns {[number, number, number]}
69
+ */
70
+ export function gridToDirection(coordinate, mapping) {
71
+ const [u, v] = coordinate;
72
+ let x;
73
+ let y;
74
+ let z;
75
+ if (mapping === 'hemisphere') {
76
+ x = u - v;
77
+ z = -1 + u + v;
78
+ y = 1 - Math.abs(x) - Math.abs(z);
79
+ } else {
80
+ x = u * 2 - 1;
81
+ z = v * 2 - 1;
82
+ y = 1 - Math.abs(x) - Math.abs(z);
83
+ if (y < 0) {
84
+ const oldX = x;
85
+ x = nonZeroSign(x) * (1 - Math.abs(z));
86
+ z = nonZeroSign(z) * (1 - Math.abs(oldX));
87
+ }
88
+ }
89
+ return normalize3([x, y, z]);
90
+ }
91
+
92
+ /**
93
+ * @param {[number, number, number]} input
94
+ * @param {'hemisphere' | 'full'} mapping
95
+ * @returns {[number, number]}
96
+ */
97
+ export function directionToGrid(input, mapping) {
98
+ let [x, y, z] = normalize3(input);
99
+ if (mapping === 'hemisphere') y = Math.max(y, 1e-6);
100
+ const inverseL1 = 1 / Math.max(1e-12, Math.abs(x) + Math.abs(y) + Math.abs(z));
101
+ x *= inverseL1;
102
+ y *= inverseL1;
103
+ z *= inverseL1;
104
+ if (mapping === 'hemisphere') {
105
+ return clamp2([(x + z) * 0.5 + 0.5, (z - x) * 0.5 + 0.5]);
106
+ }
107
+ if (y < 0) {
108
+ const oldX = x;
109
+ x = nonZeroSign(x) * (1 - Math.abs(z));
110
+ z = nonZeroSign(z) * (1 - Math.abs(oldX));
111
+ }
112
+ return clamp2([x * 0.5 + 0.5, z * 0.5 + 0.5]);
113
+ }
114
+
115
+ /**
116
+ * Finds the authored grid direction with the smallest spherical angle to an
117
+ * arbitrary view. Octahedral coordinates are not equal-area, so independently
118
+ * rounding U/V is not guaranteed to choose the closest capture near folds.
119
+ * The production grids contain at most 24² views; an exact dot-product search
120
+ * is both cheap and unambiguous for an interaction that runs on orbit release.
121
+ *
122
+ * @param {[number, number, number]} inputDirection
123
+ * @param {'hemisphere' | 'full'} mapping
124
+ * @param {number} frames
125
+ */
126
+ export function nearestOctahedralCapture(inputDirection, mapping, frames) {
127
+ if (mapping !== 'hemisphere' && mapping !== 'full') {
128
+ throw new RangeError('Impostor mapping must be hemisphere or full.');
129
+ }
130
+ if (!Number.isInteger(frames) || frames < 2) {
131
+ throw new RangeError('Impostor capture search requires at least two frames per axis.');
132
+ }
133
+ const direction = normalize3(inputDirection);
134
+ let bestFrame = /** @type {[number, number]} */ ([0, 0]);
135
+ let bestDirection = gridToDirection([0, 0], mapping);
136
+ let bestDot = -Number.POSITIVE_INFINITY;
137
+ for (let y = 0; y < frames; y++) {
138
+ for (let x = 0; x < frames; x++) {
139
+ const candidate = gridToDirection([x / (frames - 1), y / (frames - 1)], mapping);
140
+ const similarity = dot3(direction, candidate);
141
+ if (similarity <= bestDot) continue;
142
+ bestDot = similarity;
143
+ bestFrame = [x, y];
144
+ bestDirection = candidate;
145
+ }
146
+ }
147
+ return {
148
+ frame: bestFrame,
149
+ direction: bestDirection,
150
+ dot: Math.max(-1, Math.min(1, bestDot)),
151
+ angleDegrees: Math.acos(Math.max(-1, Math.min(1, bestDot))) * 180 / Math.PI,
152
+ };
153
+ }
154
+
155
+ /**
156
+ * Projects a spherical direction beyond the latest sample. OrbitControls'
157
+ * residual release motion supplies the two samples; values above one look
158
+ * ahead in the same direction, so a fast release cannot select the capture
159
+ * behind its visible trajectory.
160
+ *
161
+ * @param {[number, number, number]} startDirection
162
+ * @param {[number, number, number]} endDirection
163
+ * @param {number} multiplier 1 returns the end sample, 1.5 projects half of
164
+ * the sampled angular motion beyond it.
165
+ * @returns {[number, number, number]}
166
+ */
167
+ export function extrapolateSphericalDirection(startDirection, endDirection, multiplier) {
168
+ if (!Number.isFinite(multiplier) || multiplier < 0) {
169
+ throw new RangeError('Spherical extrapolation multiplier must be finite and non-negative.');
170
+ }
171
+ const start = normalize3(startDirection);
172
+ const end = normalize3(endDirection);
173
+ const similarity = Math.max(-1, Math.min(1, dot3(start, end)));
174
+ const angle = Math.acos(similarity);
175
+ const axis = cross3(start, end);
176
+ const axisLength = Math.hypot(...axis);
177
+ if (angle < 1e-8 || axisLength < 1e-8) return end;
178
+ const unitAxis = /** @type {[number, number, number]} */ (
179
+ axis.map((component) => component / axisLength)
180
+ );
181
+ const projectedAngle = angle * multiplier;
182
+ const cosine = Math.cos(projectedAngle);
183
+ const sine = Math.sin(projectedAngle);
184
+ const axisCrossStart = cross3(unitAxis, start);
185
+ const axisDotStart = dot3(unitAxis, start);
186
+ return normalize3(/** @type {[number, number, number]} */ (start.map((component, index) => (
187
+ component * cosine
188
+ + axisCrossStart[index] * sine
189
+ + unitAxis[index] * axisDotStart * (1 - cosine)
190
+ ))));
191
+ }
192
+
193
+ /**
194
+ * Returns the three vertices and barycentric weights of the grid triangle
195
+ * containing a direction. The diagonal is chosen consistently on CPU and GPU.
196
+ *
197
+ * @param {[number, number, number]} direction
198
+ * @param {'hemisphere' | 'full'} mapping
199
+ * @param {number} frames
200
+ */
201
+ export function selectOctahedralFrames(direction, mapping, frames) {
202
+ const scale = frames - 1;
203
+ const grid = directionToGrid(direction, mapping);
204
+ const gx = grid[0] * scale;
205
+ const gy = grid[1] * scale;
206
+ const x = Math.min(scale - 1, Math.floor(gx));
207
+ const y = Math.min(scale - 1, Math.floor(gy));
208
+ const fx = gx - x;
209
+ const fy = gy - y;
210
+ const base = gridToDirection([x / scale, y / scale], mapping);
211
+ const right = gridToDirection([(x + 1) / scale, y / scale], mapping);
212
+ const up = gridToDirection([x / scale, (y + 1) / scale], mapping);
213
+ const diagonal = gridToDirection([(x + 1) / scale, (y + 1) / scale], mapping);
214
+ // Octahedral grid quads are strongly warped on the sphere, especially at
215
+ // low view counts. Use the shorter of their two spherical diagonals instead
216
+ // of always connecting base -> diagonal. Fold cells still necessarily use
217
+ // the anti-diagonal because it is the captured z=0 view edge.
218
+ const antiDiagonal = mapping === 'hemisphere' && x + y === scale - 1
219
+ || dot3(right, up) > dot3(base, diagonal);
220
+ if (antiDiagonal) {
221
+ if (fx + fy <= 1) {
222
+ return {
223
+ frames: /** @type {[[number, number], [number, number], [number, number]]} */ ([
224
+ [x, y], [x + 1, y], [x, y + 1],
225
+ ]),
226
+ weights: /** @type {[number, number, number]} */ ([1 - fx - fy, fx, fy]),
227
+ };
228
+ }
229
+ return {
230
+ frames: /** @type {[[number, number], [number, number], [number, number]]} */ ([
231
+ [x + 1, y + 1], [x, y + 1], [x + 1, y],
232
+ ]),
233
+ weights: /** @type {[number, number, number]} */ ([fx + fy - 1, 1 - fx, 1 - fy]),
234
+ };
235
+ }
236
+ const horizontal = fx > fy;
237
+ return {
238
+ frames: /** @type {[[number, number], [number, number], [number, number]]} */ ([
239
+ [x, y],
240
+ horizontal ? [x + 1, y] : [x, y + 1],
241
+ [x + 1, y + 1],
242
+ ]),
243
+ weights: /** @type {[number, number, number]} */ ([
244
+ Math.min(1 - fx, 1 - fy),
245
+ Math.abs(fx - fy),
246
+ Math.min(fx, fy),
247
+ ]),
248
+ };
249
+ }
250
+
251
+ /** @param {[number, number, number]} a @param {[number, number, number]} b */
252
+ function dot3(a, b) {
253
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
254
+ }
255
+
256
+ /** @param {[number, number, number]} a @param {[number, number, number]} b */
257
+ function cross3(a, b) {
258
+ return /** @type {[number, number, number]} */ ([
259
+ a[1] * b[2] - a[2] * b[1],
260
+ a[2] * b[0] - a[0] * b[2],
261
+ a[0] * b[1] - a[1] * b[0],
262
+ ]);
263
+ }
264
+
265
+ /** @param {[number, number, number]} value */
266
+ function normalize3(value) {
267
+ const length = Math.hypot(...value);
268
+ if (length < 1e-12) return /** @type {[number, number, number]} */ ([0, 1, 0]);
269
+ return /** @type {[number, number, number]} */ (value.map((component) => component / length));
270
+ }
271
+
272
+ /** @param {[number, number]} value */
273
+ function clamp2(value) {
274
+ return /** @type {[number, number]} */ (value.map((component) => Math.max(0, Math.min(1, component))));
275
+ }
276
+
277
+ /** @param {number} value */
278
+ function nonZeroSign(value) {
279
+ return value < 0 ? -1 : 1;
280
+ }