@needle-tools/three-mesh-effects 0.0.0-stage → 0.1.0-alpha

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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -3
  3. package/build/deformation-runtime.js +2143 -0
  4. package/build/deformation-runtime.js.map +7 -0
  5. package/build/impostor-runtime-needle-engine.js +42 -0
  6. package/build/impostor-runtime-needle-engine.js.map +7 -0
  7. package/build/impostor-runtime.js +1333 -0
  8. package/build/impostor-runtime.js.map +7 -0
  9. package/build/internal/deformation/cloth-surface.js +288 -0
  10. package/build/internal/deformation/cloth-surface.js.map +7 -0
  11. package/build/internal/deformation/fur-surface.js +130 -0
  12. package/build/internal/deformation/fur-surface.js.map +7 -0
  13. package/build/internal/deformation/ik-constraints.js +44 -0
  14. package/build/internal/deformation/ik-constraints.js.map +7 -0
  15. package/build/internal/deformation/joints-json.js +197 -0
  16. package/build/internal/deformation/joints-json.js.map +7 -0
  17. package/build/internal/deformation/limits.js +16 -0
  18. package/build/internal/deformation/limits.js.map +7 -0
  19. package/build/internal/deformation/motion-settings.js +12 -0
  20. package/build/internal/deformation/motion-settings.js.map +7 -0
  21. package/build/internal/deformation/pin-cloth.js +196 -0
  22. package/build/internal/deformation/pin-cloth.js.map +7 -0
  23. package/build/internal/deformation/pin-ik.js +650 -0
  24. package/build/internal/deformation/pin-ik.js.map +7 -0
  25. package/build/internal/deformation/pin-types.js +12 -0
  26. package/build/internal/deformation/pin-types.js.map +7 -0
  27. package/build/internal/deformation/pin-wiggle.js +249 -0
  28. package/build/internal/deformation/pin-wiggle.js.map +7 -0
  29. package/build/internal/deformation/pose-slots.js +84 -0
  30. package/build/internal/deformation/pose-slots.js.map +7 -0
  31. package/build/internal/deformation/pose-timeline.js +210 -0
  32. package/build/internal/deformation/pose-timeline.js.map +7 -0
  33. package/build/internal/deformation/rig-drivers.js +48 -0
  34. package/build/internal/deformation/rig-drivers.js.map +7 -0
  35. package/build/internal/deformation/rig-extension.js +334 -0
  36. package/build/internal/deformation/rig-extension.js.map +7 -0
  37. package/build/internal/deformation/rig-runtime.js +2141 -0
  38. package/build/internal/deformation/rig-runtime.js.map +7 -0
  39. package/build/internal/deformation/wind.js +34 -0
  40. package/build/internal/deformation/wind.js.map +7 -0
  41. package/build/internal/impostor/impostor-metadata.js +77 -0
  42. package/build/internal/impostor/impostor-metadata.js.map +7 -0
  43. package/build/internal/impostor/impostor-node-material.js +1331 -0
  44. package/build/internal/impostor/impostor-node-material.js.map +7 -0
  45. package/build/internal/impostor/impostor-runtime-support.js +162 -0
  46. package/build/internal/impostor/impostor-runtime-support.js.map +7 -0
  47. package/build/internal/impostor/octahedral.js +232 -0
  48. package/build/internal/impostor/octahedral.js.map +7 -0
  49. package/build/internal/pixel-impostor/pixel-impostor-display.js +728 -0
  50. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +7 -0
  51. package/build/internal/pixel-impostor/pixel-impostor-metadata.js +71 -0
  52. package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +7 -0
  53. package/build/internal/pixel-impostor/pixel-impostor-runtime.js +150 -0
  54. package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +7 -0
  55. package/build/internal/public/deformation-runtime.js +2143 -0
  56. package/build/internal/public/deformation-runtime.js.map +7 -0
  57. package/build/internal/public/impostor-runtime-needle-engine.js +42 -0
  58. package/build/internal/public/impostor-runtime-needle-engine.js.map +7 -0
  59. package/build/internal/public/impostor-runtime.js +1333 -0
  60. package/build/internal/public/impostor-runtime.js.map +7 -0
  61. package/build/internal/public/pixel-impostor-runtime.js +876 -0
  62. package/build/internal/public/pixel-impostor-runtime.js.map +7 -0
  63. package/build/internal/public/wrap-effects.js +5640 -0
  64. package/build/internal/public/wrap-effects.js.map +7 -0
  65. package/build/internal/rendering/skin-projection.js +4630 -0
  66. package/build/internal/rendering/skin-projection.js.map +7 -0
  67. package/build/internal/rendering/surface-query.js +4519 -0
  68. package/build/internal/rendering/surface-query.js.map +7 -0
  69. package/build/internal/rendering/wireframe.js +112 -0
  70. package/build/internal/rendering/wireframe.js.map +7 -0
  71. package/build/internal/rendering/wrap-preview-settings.js +126 -0
  72. package/build/internal/rendering/wrap-preview-settings.js.map +7 -0
  73. package/build/internal/rendering/wrap-preview.js +5610 -0
  74. package/build/internal/rendering/wrap-preview.js.map +7 -0
  75. package/build/internal/rendering/wrap-skin.js +4746 -0
  76. package/build/internal/rendering/wrap-skin.js.map +7 -0
  77. package/build/pixel-impostor-runtime.js +876 -0
  78. package/build/pixel-impostor-runtime.js.map +7 -0
  79. package/build/types/deformation/cloth-surface.d.ts +39 -0
  80. package/build/types/deformation/cloth-surface.js +1 -0
  81. package/build/types/deformation/fur-surface.d.ts +27 -0
  82. package/build/types/deformation/fur-surface.js +1 -0
  83. package/build/types/deformation/ik-constraints.d.ts +18 -0
  84. package/build/types/deformation/ik-constraints.js +1 -0
  85. package/build/types/deformation/joints-json.d.ts +17 -0
  86. package/build/types/deformation/joints-json.js +1 -0
  87. package/build/types/deformation/limits.d.ts +23 -0
  88. package/build/types/deformation/limits.js +1 -0
  89. package/build/types/deformation/motion-settings.d.ts +9 -0
  90. package/build/types/deformation/motion-settings.js +1 -0
  91. package/build/types/deformation/pin-cloth.d.ts +28 -0
  92. package/build/types/deformation/pin-cloth.js +1 -0
  93. package/build/types/deformation/pin-ik.d.ts +37 -0
  94. package/build/types/deformation/pin-ik.js +1 -0
  95. package/build/types/deformation/pin-types.d.ts +53 -0
  96. package/build/types/deformation/pin-types.js +1 -0
  97. package/build/types/deformation/pin-wiggle.d.ts +16 -0
  98. package/build/types/deformation/pin-wiggle.js +1 -0
  99. package/build/types/deformation/pose-slots.d.ts +41 -0
  100. package/build/types/deformation/pose-slots.js +1 -0
  101. package/build/types/deformation/pose-timeline.d.ts +65 -0
  102. package/build/types/deformation/pose-timeline.js +1 -0
  103. package/build/types/deformation/rig-drivers.d.ts +19 -0
  104. package/build/types/deformation/rig-drivers.js +1 -0
  105. package/build/types/deformation/rig-extension.d.ts +33 -0
  106. package/build/types/deformation/rig-extension.js +1 -0
  107. package/build/types/deformation/rig-runtime.d.ts +95 -0
  108. package/build/types/deformation/rig-runtime.js +1 -0
  109. package/build/types/deformation/wind.d.ts +16 -0
  110. package/build/types/deformation/wind.js +1 -0
  111. package/build/types/impostor/impostor-metadata.d.ts +61 -0
  112. package/build/types/impostor/impostor-metadata.js +1 -0
  113. package/build/types/impostor/impostor-node-material.d.ts +140 -0
  114. package/build/types/impostor/impostor-node-material.js +1 -0
  115. package/build/types/impostor/impostor-runtime-support.d.ts +52 -0
  116. package/build/types/impostor/impostor-runtime-support.js +1 -0
  117. package/build/types/impostor/octahedral.d.ts +84 -0
  118. package/build/types/impostor/octahedral.js +1 -0
  119. package/build/types/pixel-impostor/pixel-impostor-display.d.ts +48 -0
  120. package/build/types/pixel-impostor/pixel-impostor-display.js +1 -0
  121. package/build/types/pixel-impostor/pixel-impostor-metadata.d.ts +34 -0
  122. package/build/types/pixel-impostor/pixel-impostor-metadata.js +1 -0
  123. package/build/types/pixel-impostor/pixel-impostor-runtime.d.ts +18 -0
  124. package/build/types/pixel-impostor/pixel-impostor-runtime.js +1 -0
  125. package/build/types/public/deformation-runtime.d.ts +3 -0
  126. package/build/types/public/deformation-runtime.js +1 -0
  127. package/build/types/public/impostor-runtime-needle-engine.d.ts +31 -0
  128. package/build/types/public/impostor-runtime-needle-engine.js +1 -0
  129. package/build/types/public/impostor-runtime.d.ts +6 -0
  130. package/build/types/public/impostor-runtime.js +1 -0
  131. package/build/types/public/pixel-impostor-runtime.d.ts +3 -0
  132. package/build/types/public/pixel-impostor-runtime.js +1 -0
  133. package/build/types/public/wrap-effects.d.ts +4 -0
  134. package/build/types/public/wrap-effects.js +1 -0
  135. package/build/types/rendering/skin-projection.d.ts +44 -0
  136. package/build/types/rendering/skin-projection.js +1 -0
  137. package/build/types/rendering/surface-query.d.ts +66 -0
  138. package/build/types/rendering/surface-query.js +1 -0
  139. package/build/types/rendering/wireframe.d.ts +25 -0
  140. package/build/types/rendering/wireframe.js +1 -0
  141. package/build/types/rendering/wrap-preview-settings.d.ts +22 -0
  142. package/build/types/rendering/wrap-preview-settings.js +1 -0
  143. package/build/types/rendering/wrap-preview.d.ts +39 -0
  144. package/build/types/rendering/wrap-preview.js +1 -0
  145. package/build/types/rendering/wrap-skin.d.ts +14 -0
  146. package/build/types/rendering/wrap-skin.js +1 -0
  147. package/build/wrap-effects.js +5640 -0
  148. package/build/wrap-effects.js.map +7 -0
  149. package/package.json +94 -6
  150. package/src/deformation/cloth-surface.ts +223 -0
  151. package/src/deformation/fur-surface.ts +129 -0
  152. package/src/deformation/ik-constraints.ts +37 -0
  153. package/src/deformation/joints-json.ts +94 -0
  154. package/src/deformation/limits.ts +25 -0
  155. package/src/deformation/motion-settings.ts +9 -0
  156. package/src/deformation/pin-cloth.ts +149 -0
  157. package/src/deformation/pin-ik.ts +582 -0
  158. package/src/deformation/pin-types.ts +22 -0
  159. package/src/deformation/pin-wiggle.ts +119 -0
  160. package/src/deformation/pose-slots.ts +106 -0
  161. package/src/deformation/pose-timeline.ts +242 -0
  162. package/src/deformation/rig-drivers.ts +32 -0
  163. package/src/deformation/rig-extension.ts +87 -0
  164. package/src/deformation/rig-runtime.ts +416 -0
  165. package/src/deformation/wind.ts +36 -0
  166. package/src/impostor/impostor-metadata.js +137 -0
  167. package/src/impostor/impostor-node-material.js +1581 -0
  168. package/src/impostor/impostor-runtime-support.js +139 -0
  169. package/src/impostor/octahedral.js +280 -0
  170. package/src/pixel-impostor/pixel-impostor-display.js +794 -0
  171. package/src/pixel-impostor/pixel-impostor-metadata.js +74 -0
  172. package/src/pixel-impostor/pixel-impostor-runtime.js +78 -0
  173. package/src/public/deformation-runtime.ts +3 -0
  174. package/src/public/impostor-runtime-needle-engine.js +56 -0
  175. package/src/public/impostor-runtime.js +21 -0
  176. package/src/public/pixel-impostor-runtime.js +5 -0
  177. package/src/public/wrap-effects.js +21 -0
  178. package/src/rendering/skin-projection.js +113 -0
  179. package/src/rendering/surface-query.js +88 -0
  180. package/src/rendering/wireframe.js +115 -0
  181. package/src/rendering/wrap-preview-settings.js +123 -0
  182. package/src/rendering/wrap-preview.js +645 -0
  183. package/src/rendering/wrap-skin.js +109 -0
@@ -0,0 +1,139 @@
1
+ // @ts-check
2
+
3
+ import {
4
+ DataTexture,
5
+ FloatType,
6
+ NearestFilter,
7
+ RGBAFormat,
8
+ } from 'three';
9
+
10
+ import { parseImpostorDescriptor } from './impostor-metadata.js';
11
+ /** @import { ImpostorDescriptor } from './impostor-metadata.js' */
12
+
13
+ export { parseImpostorDescriptor } from './impostor-metadata.js';
14
+
15
+ export const REPROJECTION_CONFIDENCE_INNER_TEXELS = 0.25;
16
+ export const REPROJECTION_CONFIDENCE_OUTER_TEXELS = 16;
17
+
18
+ /**
19
+ * Restricts barycentric interpolation to a compact fraction of each angular
20
+ * transition. A value of one preserves ordinary linear barycentrics. Smaller
21
+ * values create an exact single-view region around each capture while the
22
+ * active transition remains continuous and normalized.
23
+ *
24
+ * Along a triangle edge, `blendZone` is exactly the fraction of that edge over
25
+ * which both endpoint captures contribute.
26
+ *
27
+ * @param {readonly [number, number, number]} inputWeights
28
+ * @param {number} blendZone
29
+ * @returns {[number, number, number]}
30
+ */
31
+ export function applyImpostorBlendZone(inputWeights, blendZone) {
32
+ if (!isBlendZone(blendZone)) {
33
+ throw new RangeError('Impostor blend zone must be from 0.35 to 1.');
34
+ }
35
+ if (inputWeights.some((weight) => !Number.isFinite(weight) || weight < 0)) {
36
+ throw new RangeError('Impostor barycentric weights must be finite and non-negative.');
37
+ }
38
+ const cutoff = (1 - blendZone) * 0.5;
39
+ const weights = /** @type {[number, number, number]} */ (
40
+ inputWeights.map((weight) => Math.max(0, weight - cutoff))
41
+ );
42
+ const sum = weights[0] + weights[1] + weights[2];
43
+ if (!(sum > 0)) return [1, 0, 0];
44
+ return /** @type {[number, number, number]} */ (
45
+ weights.map((weight) => weight / sum)
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Keeps the angular onset of a previously occluded contour approximately
51
+ * stable as the atlas grid gets denser. The returned value is the temperature
52
+ * used by the normalized log-sum-exp union in the runtime shader.
53
+ *
54
+ * @param {number} frames
55
+ */
56
+ export function impostorContourUnionSharpness(frames) {
57
+ if (!Number.isInteger(frames) || frames < 2) {
58
+ throw new RangeError('Impostor contour union requires at least two frames per atlas axis.');
59
+ }
60
+ return Math.max(1, 16 / frames);
61
+ }
62
+
63
+ /**
64
+ * CPU mirror of the runtime's continuous geometric confidence. A binary
65
+ * half-texel cutoff made individual pixels (and sometimes entire thin
66
+ * features) switch reconstruction paths at one camera angle. The fade keeps
67
+ * the same bounded rejection while making angular motion continuous.
68
+ *
69
+ * @param {boolean} startedOutOfBounds
70
+ * @param {number} rayError
71
+ * @param {number} footprint
72
+ * @param {boolean=} reprojectionActive
73
+ */
74
+ export function reprojectedSampleConfidence(
75
+ startedOutOfBounds,
76
+ rayError,
77
+ footprint,
78
+ reprojectionActive = true,
79
+ ) {
80
+ if (startedOutOfBounds) return 0;
81
+ if (!reprojectionActive) return 1;
82
+ if (!(footprint > 0)) return 0;
83
+ const residual = rayError / footprint;
84
+ const interval = REPROJECTION_CONFIDENCE_OUTER_TEXELS
85
+ - REPROJECTION_CONFIDENCE_INNER_TEXELS;
86
+ const t = Math.min(1, Math.max(
87
+ 0,
88
+ (residual - REPROJECTION_CONFIDENCE_INNER_TEXELS) / interval,
89
+ ));
90
+ const eased = t * t * (3 - 2 * t);
91
+ return 1 - eased;
92
+ }
93
+
94
+ /**
95
+ * Mirrors the runtime coverage contribution used after depth reprojection.
96
+ * Validity can remove a frame from a pixel, but it must never renormalize the
97
+ * remaining frame to full opacity. Preserving angular support prevents a
98
+ * surface visible in only one capture from appearing as a second opaque layer.
99
+ *
100
+ * @param {readonly [number, number, number]} visibilityWeights
101
+ * @param {readonly [number, number, number]} correctedValidity
102
+ * @returns {[number, number, number]}
103
+ */
104
+ export function resolveImpostorCoverageWeights(
105
+ visibilityWeights,
106
+ correctedValidity,
107
+ ) {
108
+ const values = [...visibilityWeights, ...correctedValidity];
109
+ if (values.some((value) => !Number.isFinite(value) || value < 0)) {
110
+ throw new RangeError('Coverage weights and validity must be finite and non-negative.');
111
+ }
112
+ return /** @type {[number, number, number]} */ (
113
+ visibilityWeights.map((weight, index) => weight * correctedValidity[index])
114
+ );
115
+ }
116
+
117
+ /** @param {ImpostorDescriptor} descriptor */
118
+ export function createImpostorViewBoundsTexture(descriptor) {
119
+ const texture = new DataTexture(
120
+ new Float32Array(descriptor.viewBounds),
121
+ descriptor.frames,
122
+ descriptor.frames,
123
+ RGBAFormat,
124
+ FloatType,
125
+ );
126
+ texture.name = 'impostor view bounds';
127
+ texture.minFilter = NearestFilter;
128
+ texture.magFilter = NearestFilter;
129
+ texture.generateMipmaps = false;
130
+ texture.needsUpdate = true;
131
+ return texture;
132
+ }
133
+
134
+ /** @param {unknown} value */
135
+ function isBlendZone(value) {
136
+ return Number.isFinite(value)
137
+ && /** @type {number} */ (value) >= 0.35
138
+ && /** @type {number} */ (value) <= 1;
139
+ }
@@ -0,0 +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
+ }