@carbonenginejs/runtime-resource 0.8.0 → 0.9.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 (259) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +29 -29
  3. package/README.md +67 -548
  4. package/dist/CjsMotherLode.js +1271 -1271
  5. package/dist/CjsResMan.js +3005 -3005
  6. package/dist/CjsResManQueue.js +214 -214
  7. package/dist/CjsResource.js +566 -566
  8. package/dist/CjsResourceSource.js +59 -59
  9. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -153
  10. package/dist/format/CjsBlueReader.js +269 -269
  11. package/dist/format/CjsFormat.js +192 -192
  12. package/dist/format/CjsReader.js +18 -18
  13. package/dist/format/CjsResourceProbe.js +277 -277
  14. package/dist/format/payloadContract.js +173 -173
  15. package/dist/formats/black/CjsBlackFormat.js +278 -278
  16. package/dist/formats/black/core/CjsBlackBinaryReader.js +153 -153
  17. package/dist/formats/black/core/CjsBlackPropertyReaders.js +382 -382
  18. package/dist/formats/black/core/CjsBlackReader.js +639 -639
  19. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +433 -433
  20. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js +4 -4
  21. package/dist/formats/black/core/blackConstants.js +7 -7
  22. package/dist/formats/black/core/blackDefinitions.js +10 -10
  23. package/dist/formats/black/core/blackEnums.js +6 -6
  24. package/dist/formats/black/core/blackSchema.js +3 -3
  25. package/dist/formats/black/core/blackVersion.js +22 -22
  26. package/dist/formats/black/core/helpers.js +182 -182
  27. package/dist/formats/black/core/schema.js +4 -4
  28. package/dist/formats/black/index.js +2 -2
  29. package/dist/formats/bnk/CjsBnkFormat.js +125 -125
  30. package/dist/formats/bnk/core/graph.js +140 -140
  31. package/dist/formats/bnk/core/helpers.js +409 -409
  32. package/dist/formats/bnk/core/musicNodes.js +489 -489
  33. package/dist/formats/bnk/core/soundbanksInfo.js +246 -246
  34. package/dist/formats/bnk/index.js +2 -2
  35. package/dist/formats/cmf/CjsCmfFormat.js +497 -497
  36. package/dist/formats/cmf/core/binary.js +118 -118
  37. package/dist/formats/cmf/core/buffers.js +233 -233
  38. package/dist/formats/cmf/core/constants.js +47 -47
  39. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  40. package/dist/formats/cmf/core/helpers.js +287 -287
  41. package/dist/formats/cmf/core/pack.js +276 -276
  42. package/dist/formats/cmf/core/schema.js +364 -364
  43. package/dist/formats/cmf/core/shared.js +268 -268
  44. package/dist/formats/cmf/core/writer.js +517 -517
  45. package/dist/formats/cmf/index.js +2 -2
  46. package/dist/formats/dds/CjsDdsFormat.js +193 -193
  47. package/dist/formats/dds/core/bc6h.js +288 -288
  48. package/dist/formats/dds/core/bc7.js +251 -251
  49. package/dist/formats/dds/core/helpers.js +815 -815
  50. package/dist/formats/dds/index.js +2 -2
  51. package/dist/formats/fbx/CjsFbxFormat.js +266 -266
  52. package/dist/formats/fbx/core/helpers.js +3901 -3901
  53. package/dist/formats/fbx/index.js +2 -2
  54. package/dist/formats/flac/CjsFlacFormat.js +87 -87
  55. package/dist/formats/flac/core/helpers.js +295 -295
  56. package/dist/formats/flac/index.js +2 -2
  57. package/dist/formats/gif/CjsGifFormat.js +87 -87
  58. package/dist/formats/gif/core/helpers.js +360 -360
  59. package/dist/formats/gif/index.js +2 -2
  60. package/dist/formats/gltf/CjsGltfFormat.js +290 -290
  61. package/dist/formats/gltf/core/helpers.js +292 -292
  62. package/dist/formats/gltf/core/json.js +79 -79
  63. package/dist/formats/gltf/core/parser.js +666 -666
  64. package/dist/formats/gltf/core/targets.js +163 -163
  65. package/dist/formats/gltf/index.js +2 -2
  66. package/dist/formats/gr2/CjsGr2Format.js +46 -0
  67. package/dist/formats/gr2/CjsGr2Format.js.map +1 -0
  68. package/dist/formats/gr2/core/CjsFormatGr2.js +273 -0
  69. package/dist/formats/gr2/core/CjsFormatGr2.js.map +1 -0
  70. package/dist/formats/gr2/core/bitknit2.js +280 -0
  71. package/dist/formats/gr2/core/bitknit2.js.map +1 -0
  72. package/dist/formats/gr2/core/curves.js +1047 -0
  73. package/dist/formats/gr2/core/curves.js.map +1 -0
  74. package/dist/formats/gr2/core/gsf.js +72 -0
  75. package/dist/formats/gr2/core/gsf.js.map +1 -0
  76. package/dist/formats/gr2/core/helpers.js +332 -0
  77. package/dist/formats/gr2/core/helpers.js.map +1 -0
  78. package/dist/formats/gr2/core/json.js +622 -0
  79. package/dist/formats/gr2/core/json.js.map +1 -0
  80. package/dist/formats/gr2/core/oodle1.js +388 -0
  81. package/dist/formats/gr2/core/oodle1.js.map +1 -0
  82. package/dist/formats/gr2/core/reader.js +617 -0
  83. package/dist/formats/gr2/core/reader.js.map +1 -0
  84. package/dist/formats/gr2/core/tangents.js +48 -0
  85. package/dist/formats/gr2/core/tangents.js.map +1 -0
  86. package/dist/formats/gr2/core/targets.js +351 -0
  87. package/dist/formats/gr2/core/targets.js.map +1 -0
  88. package/dist/formats/gr2/index.js +3 -0
  89. package/dist/formats/gr2/index.js.map +1 -0
  90. package/dist/formats/index.js +30 -23
  91. package/dist/formats/index.js.map +1 -1
  92. package/dist/formats/jpeg/CjsJpegFormat.js +206 -206
  93. package/dist/formats/jpeg/core/helpers.js +350 -350
  94. package/dist/formats/jpeg/core/jpeg.js +377 -377
  95. package/dist/formats/jpeg/index.js +2 -2
  96. package/dist/formats/mp3/CjsMp3Format.js +192 -192
  97. package/dist/formats/mp3/core/helpers.js +338 -338
  98. package/dist/formats/mp3/index.js +2 -2
  99. package/dist/formats/mp4/CjsMp4Format.js +191 -191
  100. package/dist/formats/mp4/core/helpers.js +449 -449
  101. package/dist/formats/mp4/index.js +2 -2
  102. package/dist/formats/obj/CjsObjFormat.js +253 -253
  103. package/dist/formats/obj/core/helpers.js +573 -573
  104. package/dist/formats/obj/core/json.js +64 -64
  105. package/dist/formats/obj/core/parser.js +321 -321
  106. package/dist/formats/obj/index.js +2 -2
  107. package/dist/formats/ogg/CjsOggFormat.js +88 -88
  108. package/dist/formats/ogg/core/helpers.js +387 -387
  109. package/dist/formats/ogg/core/imdct.js +178 -178
  110. package/dist/formats/ogg/core/vorbis.js +999 -999
  111. package/dist/formats/ogg/index.js +2 -2
  112. package/dist/formats/png/CjsPngFormat.js +195 -195
  113. package/dist/formats/png/core/helpers.js +583 -583
  114. package/dist/formats/png/index.js +2 -2
  115. package/dist/formats/red/CjsRedFormat.js +261 -261
  116. package/dist/formats/red/core/CjsRedReader.js +194 -194
  117. package/dist/formats/red/core/blackDefinitions.js +3 -3
  118. package/dist/formats/red/core/helpers.js +139 -139
  119. package/dist/formats/red/core/redGraph.js +68 -68
  120. package/dist/formats/red/core/schema.js +4 -4
  121. package/dist/formats/red/index.js +2 -2
  122. package/dist/formats/stl/CjsStlFormat.js +365 -365
  123. package/dist/formats/stl/core/helpers.js +261 -261
  124. package/dist/formats/stl/core/json.js +51 -51
  125. package/dist/formats/stl/core/stl.js +629 -629
  126. package/dist/formats/stl/core/targets.js +163 -163
  127. package/dist/formats/stl/index.js +2 -2
  128. package/dist/formats/tga/CjsTgaFormat.js +192 -192
  129. package/dist/formats/tga/core/helpers.js +446 -446
  130. package/dist/formats/tga/index.js +2 -2
  131. package/dist/formats/wav/CjsWavFormat.js +192 -192
  132. package/dist/formats/wav/core/helpers.js +328 -328
  133. package/dist/formats/wav/index.js +2 -2
  134. package/dist/formats/webm/CjsWebmFormat.js +191 -191
  135. package/dist/formats/webm/core/helpers.js +537 -537
  136. package/dist/formats/webm/index.js +2 -2
  137. package/dist/formats/webp/CjsWebpFormat.js +86 -86
  138. package/dist/formats/webp/core/helpers.js +214 -214
  139. package/dist/formats/webp/index.js +2 -2
  140. package/dist/formats/wem/CjsWemFormat.js +241 -241
  141. package/dist/formats/wem/core/bitStream.js +259 -259
  142. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  143. package/dist/formats/wem/core/helpers.js +417 -417
  144. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  145. package/dist/formats/wem/core/ptadpcm.js +77 -77
  146. package/dist/formats/wem/core/resolve.js +121 -121
  147. package/dist/formats/wem/core/wemToOgg.js +485 -485
  148. package/dist/formats/wem/index.js +2 -2
  149. package/dist/formats/yaml/CjsYamlFormat.js +83 -83
  150. package/dist/formats/yaml/core/CjsYamlReader.js +305 -305
  151. package/dist/formats/yaml/core/helpers.js +160 -160
  152. package/dist/formats/yaml/index.js +2 -2
  153. package/dist/index.js +49 -48
  154. package/dist/index.js.map +1 -1
  155. package/dist/resourcePath.js +18 -18
  156. package/dist/resourceStates.js +9 -9
  157. package/dist/resources/AudioGeometryResData.js +47 -47
  158. package/dist/resources/GStateBindingCallbackData.js +31 -31
  159. package/dist/resources/MeshDecalData.js +37 -37
  160. package/dist/resources/MeshDecalLodData.js +34 -34
  161. package/dist/resources/Tr2EffectRes.js +71 -71
  162. package/dist/resources/Tr2GrannyIntersectionResult.js +60 -60
  163. package/dist/resources/Tr2GrannyStateRes.js +44 -44
  164. package/dist/resources/Tr2ImageRes.js +114 -114
  165. package/dist/resources/Tr2LightProfileRes.js +40 -40
  166. package/dist/resources/Tr2MaterialArea.js +34 -34
  167. package/dist/resources/Tr2MaterialMesh.js +31 -31
  168. package/dist/resources/Tr2MaterialRes.js +34 -34
  169. package/dist/resources/Tr2ShaderPermutation.js +43 -43
  170. package/dist/resources/Tr2TextureLodManager.js +80 -80
  171. package/dist/resources/Tr2TextureLodUpdateRequest.js +37 -37
  172. package/dist/resources/Tr2TexturePackChannel.js +37 -37
  173. package/dist/resources/Tr2TexturePipeline.js +52 -52
  174. package/dist/resources/Tr2TexturePipelineParams.js +34 -34
  175. package/dist/resources/Tr2TexturePipelineStepCompress.js +40 -40
  176. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js +22 -0
  177. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js.map +1 -0
  178. package/dist/resources/Tr2TexturePipelineStepLimitSize.js +34 -34
  179. package/dist/resources/Tr2TexturePipelineStepLoad.js +31 -31
  180. package/dist/resources/Tr2TexturePipelineStepPack.js +43 -43
  181. package/dist/resources/TriGeometryRes.js +239 -239
  182. package/dist/resources/TriGeometryResAreaData.js +59 -59
  183. package/dist/resources/TriGeometryResJointData.js +38 -38
  184. package/dist/resources/TriGeometryResLodData.js +88 -88
  185. package/dist/resources/TriGeometryResMeshData.js +63 -63
  186. package/dist/resources/TriGeometryResSkeletonData.js +34 -34
  187. package/dist/resources/TriGrannyRes.js +43 -43
  188. package/dist/resources/TriJointBinding.js +38 -38
  189. package/dist/resources/TriMorphTargetGeometryConstants.js +46 -46
  190. package/dist/resources/TriRtGeometryConstants.js +88 -88
  191. package/dist/resources/TriTextureRes.js +304 -304
  192. package/dist/resources/enums.js +18 -18
  193. package/dist/resources/resourceBoundary.js +47 -47
  194. package/dist/resources/texturePipelineBehavior.js +308 -308
  195. package/dist/texture/CjsTextureArrayRes.js +406 -406
  196. package/dist/texture/CjsTextureParameterProxy.js +133 -133
  197. package/docs/README.md +69 -0
  198. package/docs/architecture.md +86 -0
  199. package/docs/concepts/resource-lifecycle.md +217 -0
  200. package/docs/formats/README.md +104 -0
  201. package/{FORMAT-PROVENANCE.md → docs/formats/provenance.md} +173 -155
  202. package/docs/formats/stl.md +37 -0
  203. package/docs/formats/wwise.md +44 -0
  204. package/docs/reference/events.md +92 -0
  205. package/docs/reference/motherlode-cache.md +244 -0
  206. package/docs/reference/queues.md +102 -0
  207. package/docs/reference/reload.md +107 -0
  208. package/docs/reference/texture-arrays.md +113 -0
  209. package/docs/reference/texture-pipeline.md +53 -0
  210. package/docs/roadmap.md +104 -0
  211. package/format-notices/black/LICENSE +21 -21
  212. package/format-notices/black/NOTICE +47 -47
  213. package/format-notices/bnk/LICENSE +21 -21
  214. package/format-notices/bnk/NOTICE +20 -20
  215. package/format-notices/cmf/LICENSE +21 -21
  216. package/format-notices/cmf/NOTICE +36 -36
  217. package/format-notices/dds/LICENSE +21 -21
  218. package/format-notices/dds/NOTICE +14 -14
  219. package/format-notices/fbx/LICENSE +21 -21
  220. package/format-notices/fbx/NOTICE +14 -14
  221. package/format-notices/flac/LICENSE +21 -21
  222. package/format-notices/flac/NOTICE +14 -14
  223. package/format-notices/gif/LICENSE +21 -21
  224. package/format-notices/gif/NOTICE +14 -14
  225. package/format-notices/gltf/LICENSE +21 -21
  226. package/format-notices/gltf/NOTICE +27 -27
  227. package/format-notices/gr2/LICENSE +21 -0
  228. package/format-notices/gr2/NOTICE +60 -0
  229. package/format-notices/gr2/THIRD-PARTY-NOTICES.md +93 -0
  230. package/format-notices/jpeg/LICENSE +21 -21
  231. package/format-notices/jpeg/NOTICE +14 -14
  232. package/format-notices/mp3/LICENSE +21 -21
  233. package/format-notices/mp3/NOTICE +14 -14
  234. package/format-notices/mp4/LICENSE +21 -21
  235. package/format-notices/mp4/NOTICE +14 -14
  236. package/format-notices/obj/LICENSE +21 -21
  237. package/format-notices/obj/NOTICE +26 -26
  238. package/format-notices/ogg/LICENSE +21 -21
  239. package/format-notices/ogg/NOTICE +28 -28
  240. package/format-notices/png/LICENSE +21 -21
  241. package/format-notices/png/NOTICE +14 -14
  242. package/format-notices/red/LICENSE +21 -21
  243. package/format-notices/red/NOTICE +31 -31
  244. package/format-notices/stl/LICENSE +21 -21
  245. package/format-notices/stl/NOTICE +21 -21
  246. package/format-notices/tga/LICENSE +21 -21
  247. package/format-notices/tga/NOTICE +14 -14
  248. package/format-notices/wav/LICENSE +21 -21
  249. package/format-notices/wav/NOTICE +14 -14
  250. package/format-notices/webm/LICENSE +21 -21
  251. package/format-notices/webm/NOTICE +14 -14
  252. package/format-notices/webp/LICENSE +21 -21
  253. package/format-notices/webp/NOTICE +14 -14
  254. package/format-notices/wem/LICENSE +57 -57
  255. package/format-notices/wem/NOTICE +33 -33
  256. package/format-notices/yaml/LICENSE +21 -21
  257. package/format-notices/yaml/NOTICE +44 -44
  258. package/package.json +52 -51
  259. package/resource-lifecycle.md +0 -679
@@ -0,0 +1,1047 @@
1
+ /**
2
+ * Granny animation-curve decompression helpers
3
+ * `decodeCurve(curveJson, dimension)` takes a GR2 JSON curve object in the
4
+ * shape emitted by PrintCurve2 and returns explicit knots and controls.
5
+ */
6
+
7
+ /** Format id for raw float keyframes with implicit knots. */
8
+ const FORMAT_DA_KEYFRAMES_32F = 0;
9
+
10
+ /** Format id for uncompressed float knots and float controls. */
11
+ const FORMAT_DA_K32F_C32F = 1;
12
+
13
+ /** Format id for an identity transform curve. */
14
+ const FORMAT_DA_IDENTITY = 2;
15
+
16
+ /** Format id for a constant float control vector. */
17
+ const FORMAT_DA_CONSTANT_32F = 3;
18
+
19
+ /** Format id for a constant vec3 control. */
20
+ const FORMAT_D3_CONSTANT_32F = 4;
21
+
22
+ /** Format id for a constant quaternion control. */
23
+ const FORMAT_D4_CONSTANT_32F = 5;
24
+
25
+ /** Format id for uint16 DaK packed knots and controls. */
26
+ const FORMAT_DA_K16U_C16U = 6;
27
+
28
+ /** Format id for uint8 DaK packed knots and controls. */
29
+ const FORMAT_DA_K8U_C8U = 7;
30
+
31
+ /** Format id for uint16 normalized quaternion curves with 15-bit controls. */
32
+ const FORMAT_D4N_K16U_C15U = 8;
33
+
34
+ /** Format id for uint8 normalized quaternion curves with 7-bit controls. */
35
+ const FORMAT_D4N_K8U_C7U = 9;
36
+
37
+ /** Format id for uint16 vec3 position curves. */
38
+ const FORMAT_D3_K16U_C16U = 10;
39
+
40
+ /** Format id for uint8 vec3 position curves. */
41
+ const FORMAT_D3_K8U_C8U = 11;
42
+
43
+ /** Format id for uint16 uniform scale/shear curves. */
44
+ const FORMAT_D9I1_K16U_C16U = 12;
45
+
46
+ /** Format id for uint16 per-axis scale/shear curves. */
47
+ const FORMAT_D9I3_K16U_C16U = 13;
48
+
49
+ /** Format id for uint8 uniform scale/shear curves. */
50
+ const FORMAT_D9I1_K8U_C8U = 14;
51
+
52
+ /** Format id for uint8 per-axis scale/shear curves. */
53
+ const FORMAT_D9I3_K8U_C8U = 15;
54
+
55
+ /** Format id for float D3I1 line-parameterized vec3 curves. */
56
+ const FORMAT_D3I1_K32F_C32F = 16;
57
+
58
+ /** Format id for uint16 D3I1 line-parameterized vec3 curves. */
59
+ const FORMAT_D3I1_K16U_C16U = 17;
60
+
61
+ /** Format id for uint8 D3I1 line-parameterized vec3 curves. */
62
+ const FORMAT_D3I1_K8U_C8U = 18;
63
+
64
+ /**
65
+ * Granny curve format ids keyed by descriptive uppercase names.
66
+ */
67
+ const CURVE_FORMATS = Object.freeze({
68
+ DA_KEYFRAMES_32F: FORMAT_DA_KEYFRAMES_32F,
69
+ DA_K32F_C32F: FORMAT_DA_K32F_C32F,
70
+ DA_IDENTITY: FORMAT_DA_IDENTITY,
71
+ DA_CONSTANT_32F: FORMAT_DA_CONSTANT_32F,
72
+ D3_CONSTANT_32F: FORMAT_D3_CONSTANT_32F,
73
+ D4_CONSTANT_32F: FORMAT_D4_CONSTANT_32F,
74
+ DA_K16U_C16U: FORMAT_DA_K16U_C16U,
75
+ DA_K8U_C8U: FORMAT_DA_K8U_C8U,
76
+ D4N_K16U_C15U: FORMAT_D4N_K16U_C15U,
77
+ D4N_K8U_C7U: FORMAT_D4N_K8U_C7U,
78
+ D3_K16U_C16U: FORMAT_D3_K16U_C16U,
79
+ D3_K8U_C8U: FORMAT_D3_K8U_C8U,
80
+ D9I1_K16U_C16U: FORMAT_D9I1_K16U_C16U,
81
+ D9I3_K16U_C16U: FORMAT_D9I3_K16U_C16U,
82
+ D9I1_K8U_C8U: FORMAT_D9I1_K8U_C8U,
83
+ D9I3_K8U_C8U: FORMAT_D9I3_K8U_C8U,
84
+ D3I1_K32F_C32F: FORMAT_D3I1_K32F_C32F,
85
+ D3I1_K16U_C16U: FORMAT_D3I1_K16U_C16U,
86
+ D3I1_K8U_C8U: FORMAT_D3I1_K8U_C8U
87
+ });
88
+
89
+ /**
90
+ * Float32 rounding helper used to mirror the reference curve decoders.
91
+ *
92
+ * The reference implementation stores every decoded value into Float32Array
93
+ * buffers, so this helper applies the same rounding at each write.
94
+ */
95
+ const fr = Math.fround;
96
+
97
+ /**
98
+ * Granny stores 1/knotScale as the top 16 bits of the float only
99
+ * ("OneOverKnotScaleTrunc"). Reconstruct the float32.
100
+ * @param {number} oneOverKnotScaleTrunc uint16
101
+ * @returns {number}
102
+ */
103
+ function knotScaleFromTrunc(oneOverKnotScaleTrunc) {
104
+ const u = new Uint32Array([oneOverKnotScaleTrunc << 16]);
105
+ return new Float32Array(u.buffer)[0];
106
+ }
107
+
108
+ /**
109
+ * Expand the leading `count` entries of a knotsControls array into knots.
110
+ * @param {ArrayLike<number>} knotsControls
111
+ * @param {number} count
112
+ * @param {number} scale divisor (1/knotScale)
113
+ * @returns {number[]}
114
+ */
115
+ function knotsFromControls(knotsControls, count, scale) {
116
+ const out = new Array(count);
117
+ for (let i = 0; i < count; i++) {
118
+ out[i] = fr(knotsControls[i] / scale);
119
+ }
120
+ return out;
121
+ }
122
+
123
+ /**
124
+ * Knots via the truncated knot scale.
125
+ *
126
+ * @param {ArrayLike<number>} knotsControls Packed knot/control array.
127
+ * @param {number} count Number of knots at the front of `knotsControls`.
128
+ * @param {number} oneOverKnotScaleTrunc Truncated reciprocal knot scale.
129
+ * @returns {number[]} Decoded knot times.
130
+ */
131
+ function knotsFromControlsTrunc(knotsControls, count, oneOverKnotScaleTrunc) {
132
+ return knotsFromControls(knotsControls, count, knotScaleFromTrunc(oneOverKnotScaleTrunc));
133
+ }
134
+
135
+ /**
136
+ * Identity control vector for a track dimension.
137
+ * @param {number} dimension 3 (position), 4 (quaternion) or 9 (mat3)
138
+ * @returns {number[]}
139
+ */
140
+ function identityControls(dimension) {
141
+ switch (dimension) {
142
+ case 3:
143
+ return [0, 0, 0];
144
+ case 4:
145
+ return [0, 0, 0, 1];
146
+ case 9:
147
+ return [1, 0, 0, 0, 1, 0, 0, 0, 1];
148
+ default:
149
+ throw new Error(`gr2reader: invalid curve dimension ${dimension}`);
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Ensure a divisibility relation holds; throws with a descriptive error.
155
+ *
156
+ * @param {number} numerator Value expected to divide evenly.
157
+ * @param {number} divisor Divisor to check against.
158
+ * @param {string} what Human-readable operation name for the error message.
159
+ * @returns {number} Exact quotient.
160
+ * @throws {Error} If the quotient is not an integer.
161
+ */
162
+ function exactDiv(numerator, divisor, what) {
163
+ const v = numerator / divisor;
164
+ if (!Number.isInteger(v)) {
165
+ throw new Error(`gr2reader: curve ${what}: ${numerator} is not divisible by ${divisor}`);
166
+ }
167
+ return v;
168
+ }
169
+
170
+ /**
171
+ * Decode raw keyframe controls with implicit knots `0..n-1`.
172
+ *
173
+ * @param {object} c Curve object with `controls` and optional `dimension`.
174
+ * @param {number} dimension Fallback control-vector width.
175
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
176
+ */
177
+ function decodeDaKeyframes32f(c, dimension) {
178
+ const dim = c.dimension || dimension,
179
+ controls = c.controls || [],
180
+ count = exactDiv(controls.length, dim, "DaKeyframes32f controls/dimension"),
181
+ knots = new Array(count);
182
+ for (let i = 0; i < count; i++) {
183
+ knots[i] = i;
184
+ }
185
+ return {
186
+ knots,
187
+ controls: controls.map(fr),
188
+ dimension: dim
189
+ };
190
+ }
191
+
192
+ /**
193
+ * Decode uncompressed float knots and float controls.
194
+ *
195
+ * @param {object} c Curve object with `knots` and `controls`.
196
+ * @param {number} dimension Fallback control-vector width when no knots exist.
197
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
198
+ */
199
+ function decodeDaK32fC32f(c, dimension) {
200
+ const knots = (c.knots || []).map(fr),
201
+ controls = (c.controls || []).map(fr),
202
+ dim = knots.length ? exactDiv(controls.length, knots.length, "DaK32fC32f controls/knots") : dimension;
203
+ return {
204
+ knots,
205
+ controls,
206
+ dimension: dim
207
+ };
208
+ }
209
+
210
+ /**
211
+ * Decode an identity transform curve for the requested track dimension.
212
+ *
213
+ * @param {object} c Curve object with optional `dimension`.
214
+ * @param {number} dimension Fallback control-vector width.
215
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
216
+ */
217
+ function decodeDaIdentity(c, dimension) {
218
+ const dim = c.dimension || dimension;
219
+ return {
220
+ knots: [0],
221
+ controls: identityControls(dim),
222
+ dimension: dim
223
+ };
224
+ }
225
+
226
+ /**
227
+ * Decode a constant float control vector.
228
+ *
229
+ * @param {object} c Curve object with `controls`.
230
+ * @param {number} dimension Fallback control-vector width.
231
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
232
+ */
233
+ function decodeDaConstant32f(c, dimension) {
234
+ const controls = (c.controls || []).map(fr);
235
+ return {
236
+ knots: [0],
237
+ controls,
238
+ dimension: controls.length || dimension
239
+ };
240
+ }
241
+
242
+ /**
243
+ * Decode a constant vec3 control.
244
+ *
245
+ * @param {object} c Curve object with optional `controls`.
246
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
247
+ */
248
+ function decodeD3Constant32f(c) {
249
+ const controls = (c.controls || [0, 0, 0]).slice(0, 3).map(fr);
250
+ return {
251
+ knots: [0],
252
+ controls,
253
+ dimension: 3
254
+ };
255
+ }
256
+
257
+ /**
258
+ * Decode a constant quaternion control.
259
+ *
260
+ * @param {object} c Curve object with optional `controls`.
261
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
262
+ */
263
+ function decodeD4Constant32f(c) {
264
+ const controls = (c.controls || [0, 0, 0, 1]).slice(0, 4).map(fr);
265
+ return {
266
+ knots: [0],
267
+ controls,
268
+ dimension: 4
269
+ };
270
+ }
271
+
272
+ /**
273
+ * Decode a DaK packed knot/control curve using uint16 payload values.
274
+ *
275
+ * @param {object} c Curve object with `knotsControls`, `controlScaleOffsets`,
276
+ * and `oneOverKnotScaleTrunc`.
277
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
278
+ */
279
+ function decodeDaK(c) {
280
+ const so = c.controlScaleOffsets || [],
281
+ kc = c.knotsControls || [],
282
+ dim = exactDiv(so.length, 2, "DaK controlScaleOffsets/2"),
283
+ count = exactDiv(kc.length, dim + 1, "DaK knotsControls/(dim+1)"),
284
+ knots = knotsFromControlsTrunc(kc, count, c.oneOverKnotScaleTrunc),
285
+ controls = new Array(count * dim);
286
+ for (let i = 0; i < count; i++) {
287
+ for (let x = 0; x < dim; x++) {
288
+ controls[i * dim + x] = fr(kc[count + i * dim + x] * so[x] + so[dim + x]);
289
+ }
290
+ }
291
+ return {
292
+ knots,
293
+ controls,
294
+ dimension: dim
295
+ };
296
+ }
297
+
298
+ /** Scale lookup table for D4n normalized quaternion controls. */
299
+ const D4N_SCALE_TABLE = new Float32Array([1.4142135, 0.70710677, 0.35355338, 0.35355338, 0.35355338, 0.17677669, 0.17677669, 0.17677669, -1.4142135, -0.70710677, -0.35355338, -0.35355338, -0.35355338, -0.17677669, -0.17677669, -0.17677669]);
300
+
301
+ /** Offset lookup table for D4n normalized quaternion controls. */
302
+ const D4N_OFFSET_TABLE = new Float32Array([-0.70710677, -0.35355338, -0.53033006, -0.17677669, 0.17677669, -0.17677669, -0.088388346, 0.0, 0.70710677, 0.35355338, 0.53033006, 0.17677669, -0.17677669, 0.17677669, 0.088388346, -0]);
303
+
304
+ /**
305
+ * Decode one 16-bit control triple into a quaternion (matches ccpwgl
306
+ * Gr2CurveDataD4nK16uC15u.GetQuatFromControl).
307
+ *
308
+ * @param {number[]} out Mutable quaternion output buffer.
309
+ * @param {number} a First packed control value.
310
+ * @param {number} b Second packed control value.
311
+ * @param {number} c Third packed control value.
312
+ * @param {ArrayLike<number>} scales Four scale values selected from the table.
313
+ * @param {ArrayLike<number>} offsets Four offset values selected from the table.
314
+ * @returns {number[]} The same `out` quaternion buffer.
315
+ */
316
+ function quatFromControl16(out, a, b, c, scales, offsets) {
317
+ const swizzle1 = (b & 0x8000) >> 14 | c >> 15,
318
+ swizzle2 = swizzle1 + 1 & 3,
319
+ swizzle3 = swizzle2 + 1 & 3,
320
+ swizzle4 = swizzle3 + 1 & 3;
321
+ const dataA = (a & 0x7fff) * scales[swizzle2] + offsets[swizzle2],
322
+ dataB = (b & 0x7fff) * scales[swizzle3] + offsets[swizzle3],
323
+ dataC = (c & 0x7fff) * scales[swizzle4] + offsets[swizzle4];
324
+ let dataD = Math.sqrt(Math.max(0, 1 - (dataA * dataA + dataB * dataB + dataC * dataC)));
325
+ if ((a & 0x8000) !== 0) dataD = -dataD;
326
+ out[swizzle2] = fr(dataA);
327
+ out[swizzle3] = fr(dataB);
328
+ out[swizzle4] = fr(dataC);
329
+ out[swizzle1] = fr(dataD);
330
+ return out;
331
+ }
332
+
333
+ /** Scale-table multiplier for D4n 16-bit knot / 15-bit control curves. */
334
+ const D4N_SCALE_TABLE_MULTIPLIER_16 = 0.000030518509;
335
+
336
+ /**
337
+ * Decode a D4n normalized quaternion curve using a supplied control decoder.
338
+ *
339
+ * @param {object} c Curve object with `knotsControls`, `scaleOffsetTableEntries`,
340
+ * and `oneOverKnotScale`.
341
+ * @param {Function} quatFromControl Function that expands one packed control
342
+ * triple into a quaternion.
343
+ * @param {number} scaleTableMultiplier Multiplier for selected scale table entries.
344
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
345
+ */
346
+ function decodeD4n(c, quatFromControl, scaleTableMultiplier) {
347
+ const kc = c.knotsControls || [],
348
+ count = exactDiv(kc.length, 4, "D4n knotsControls/4"),
349
+ knots = knotsFromControls(kc, count, c.oneOverKnotScale);
350
+ const selector = c.scaleOffsetTableEntries >>> 0,
351
+ scales = new Float32Array([D4N_SCALE_TABLE[selector >> 0 & 0x0f] * scaleTableMultiplier, D4N_SCALE_TABLE[selector >> 4 & 0x0f] * scaleTableMultiplier, D4N_SCALE_TABLE[selector >> 8 & 0x0f] * scaleTableMultiplier, D4N_SCALE_TABLE[selector >> 12 & 0x0f] * scaleTableMultiplier]),
352
+ offsets = new Float32Array([D4N_OFFSET_TABLE[selector >> 0 & 0x0f], D4N_OFFSET_TABLE[selector >> 4 & 0x0f], D4N_OFFSET_TABLE[selector >> 8 & 0x0f], D4N_OFFSET_TABLE[selector >> 12 & 0x0f]]);
353
+ const controls = new Array(count * 4),
354
+ quat = [0, 0, 0, 1];
355
+ for (let i = 0; i < count; i++) {
356
+ quatFromControl(quat, kc[count + i * 3], kc[count + i * 3 + 1], kc[count + i * 3 + 2], scales, offsets);
357
+ controls[i * 4] = quat[0];
358
+ controls[i * 4 + 1] = quat[1];
359
+ controls[i * 4 + 2] = quat[2];
360
+ controls[i * 4 + 3] = quat[3];
361
+ }
362
+ return {
363
+ knots,
364
+ controls,
365
+ dimension: 4
366
+ };
367
+ }
368
+
369
+ /**
370
+ * Decode a D4n curve using 16-bit knots and 15-bit quaternion controls.
371
+ *
372
+ * @param {object} c Curve object with packed quaternion data.
373
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
374
+ */
375
+ function decodeD4nK16uC15u(c) {
376
+ return decodeD4n(c, quatFromControl16, D4N_SCALE_TABLE_MULTIPLIER_16);
377
+ }
378
+
379
+ /** Scale-table multiplier for D4n 8-bit knot / 7-bit control curves. */
380
+ const D4N_SCALE_TABLE_MULTIPLIER_8 = 0.0078740157;
381
+
382
+ /**
383
+ * Decode one 8-bit control triple into a quaternion.
384
+ *
385
+ * @param {number[]} out Mutable quaternion output buffer.
386
+ * @param {number} a First packed control value.
387
+ * @param {number} b Second packed control value.
388
+ * @param {number} c Third packed control value.
389
+ * @param {ArrayLike<number>} scales Four scale values selected from the table.
390
+ * @param {ArrayLike<number>} offsets Four offset values selected from the table.
391
+ * @returns {number[]} The same `out` quaternion buffer.
392
+ */
393
+ function quatFromControl8(out, a, b, c, scales, offsets) {
394
+ const swizzle1 = (b & 0x80) >> 6 | (c & 0x80) >> 7,
395
+ swizzle2 = swizzle1 + 1 & 3,
396
+ swizzle3 = swizzle2 + 1 & 3,
397
+ swizzle4 = swizzle3 + 1 & 3;
398
+ const dataA = (a & 0x7f) * scales[swizzle2] + offsets[swizzle2],
399
+ dataB = (b & 0x7f) * scales[swizzle3] + offsets[swizzle3],
400
+ dataC = (c & 0x7f) * scales[swizzle4] + offsets[swizzle4];
401
+ let dataD = Math.sqrt(Math.max(0, 1 - (dataA * dataA + dataB * dataB + dataC * dataC)));
402
+ if ((a & 0x80) !== 0) dataD = -dataD;
403
+ out[swizzle2] = fr(dataA);
404
+ out[swizzle3] = fr(dataB);
405
+ out[swizzle4] = fr(dataC);
406
+ out[swizzle1] = fr(dataD);
407
+ return out;
408
+ }
409
+
410
+ /**
411
+ * Decode a D4n curve using 8-bit knots and 7-bit quaternion controls.
412
+ *
413
+ * @param {object} c Curve object with packed quaternion data.
414
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
415
+ */
416
+ function decodeD4nK8uC7u(c) {
417
+ return decodeD4n(c, quatFromControl8, D4N_SCALE_TABLE_MULTIPLIER_8);
418
+ }
419
+
420
+ /**
421
+ * Decode a D3K packed vec3 curve using uint16 payload values.
422
+ *
423
+ * @param {object} c Curve object with `knotsControls`, `controlScales`,
424
+ * `controlOffsets`, and `oneOverKnotScaleTrunc`.
425
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
426
+ */
427
+ function decodeD3K(c) {
428
+ const kc = c.knotsControls || [],
429
+ count = exactDiv(kc.length, 4, "D3K knotsControls/4"),
430
+ knots = knotsFromControlsTrunc(kc, count, c.oneOverKnotScaleTrunc),
431
+ scales = c.controlScales,
432
+ offsets = c.controlOffsets,
433
+ controls = new Array(count * 3);
434
+ for (let i = 0; i < count; i++) {
435
+ for (let x = 0; x < 3; x++) {
436
+ controls[i * 3 + x] = fr(kc[count + i * 3 + x] * scales[x] + offsets[x]);
437
+ }
438
+ }
439
+ return {
440
+ knots,
441
+ controls,
442
+ dimension: 3
443
+ };
444
+ }
445
+
446
+ /**
447
+ * Decode a D9I1 uniform scale/shear curve.
448
+ *
449
+ * @param {object} c Curve object with `knotsControls`, scalar scale/offset
450
+ * fields, and `oneOverKnotScaleTrunc`.
451
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
452
+ */
453
+ function decodeD9I1(c) {
454
+ const kc = c.knotsControls || [],
455
+ count = exactDiv(kc.length, 2, "D9I1 knotsControls/2"),
456
+ knots = knotsFromControlsTrunc(kc, count, c.oneOverKnotScaleTrunc);
457
+ const scale = Array.isArray(c.controlScales) ? c.controlScales[0] : c.controlScale,
458
+ offset = Array.isArray(c.controlOffsets) ? c.controlOffsets[0] : c.controlOffset,
459
+ controls = new Array(count * 9).fill(0);
460
+ for (let i = 0; i < count; i++) {
461
+ const s = fr(kc[count + i] * scale + offset);
462
+ controls[i * 9] = s;
463
+ controls[i * 9 + 4] = s;
464
+ controls[i * 9 + 8] = s;
465
+ }
466
+ return {
467
+ knots,
468
+ controls,
469
+ dimension: 9
470
+ };
471
+ }
472
+
473
+ /**
474
+ * Decode a D9I3 per-axis scale/shear curve.
475
+ *
476
+ * @param {object} c Curve object with `knotsControls`, `controlScales`,
477
+ * `controlOffsets`, and `oneOverKnotScaleTrunc`.
478
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
479
+ */
480
+ function decodeD9I3(c) {
481
+ const kc = c.knotsControls || [],
482
+ count = exactDiv(kc.length, 4, "D9I3 knotsControls/4"),
483
+ knots = knotsFromControlsTrunc(kc, count, c.oneOverKnotScaleTrunc),
484
+ scales = c.controlScales,
485
+ offsets = c.controlOffsets,
486
+ controls = new Array(count * 9).fill(0);
487
+ for (let i = 0; i < count; i++) {
488
+ controls[i * 9] = fr(kc[count + i * 3] * scales[0] + offsets[0]);
489
+ controls[i * 9 + 4] = fr(kc[count + i * 3 + 1] * scales[1] + offsets[1]);
490
+ controls[i * 9 + 8] = fr(kc[count + i * 3 + 2] * scales[2] + offsets[2]);
491
+ }
492
+ return {
493
+ knots,
494
+ controls,
495
+ dimension: 9
496
+ };
497
+ }
498
+
499
+ /**
500
+ * Expand one scalar per knot into vec3 controls for the D3I1 family.
501
+ *
502
+ * @param {ArrayLike<number>} kc Packed knots followed by scalar controls.
503
+ * @param {number} count Number of knots and scalar controls.
504
+ * @param {ArrayLike<number>} scales vec3 component scales.
505
+ * @param {ArrayLike<number>} offsets vec3 component offsets.
506
+ * @returns {number[]} Flat vec3 controls.
507
+ */
508
+ function d3I1Controls(kc, count, scales, offsets) {
509
+ const controls = new Array(count * 3);
510
+ for (let i = 0; i < count; i++) {
511
+ const v = kc[count + i];
512
+ controls[i * 3] = fr(v * scales[0] + offsets[0]);
513
+ controls[i * 3 + 1] = fr(v * scales[1] + offsets[1]);
514
+ controls[i * 3 + 2] = fr(v * scales[2] + offsets[2]);
515
+ }
516
+ return controls;
517
+ }
518
+
519
+ /**
520
+ * Decode a D3I1 curve with float knots and scalar float controls.
521
+ *
522
+ * @param {object} c Curve object with `knotsControls`, `controlScales`, and
523
+ * `controlOffsets`.
524
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
525
+ */
526
+ function decodeD3I1K32fC32f(c) {
527
+ const kc = c.knotsControls || [],
528
+ count = exactDiv(kc.length, 2, "D3I1K32f knotsControls/2"),
529
+ knots = new Array(count);
530
+ for (let i = 0; i < count; i++) {
531
+ knots[i] = fr(kc[i]);
532
+ }
533
+ return {
534
+ knots,
535
+ controls: d3I1Controls(kc, count, c.controlScales, c.controlOffsets),
536
+ dimension: 3
537
+ };
538
+ }
539
+
540
+ /**
541
+ * Decode a D3I1 curve with uint knots and scalar controls.
542
+ *
543
+ * @param {object} c Curve object with `knotsControls`, `controlScales`,
544
+ * `controlOffsets`, and `oneOverKnotScaleTrunc`.
545
+ * @returns {{knots: number[], controls: number[], dimension: number}} Decoded curve.
546
+ */
547
+ function decodeD3I1u(c) {
548
+ const kc = c.knotsControls || [],
549
+ count = exactDiv(kc.length, 2, "D3I1 knotsControls/2"),
550
+ knots = knotsFromControlsTrunc(kc, count, c.oneOverKnotScaleTrunc);
551
+ return {
552
+ knots,
553
+ controls: d3I1Controls(kc, count, c.controlScales, c.controlOffsets),
554
+ dimension: 3
555
+ };
556
+ }
557
+
558
+ /**
559
+ * Decode format 6, DaK16uC16u.
560
+ *
561
+ * @type {typeof decodeDaK}
562
+ */
563
+ const decodeDaK16uC16u = decodeDaK;
564
+
565
+ /**
566
+ * Decode format 7, DaK8uC8u.
567
+ *
568
+ * @type {typeof decodeDaK}
569
+ */
570
+ const decodeDaK8uC8u = decodeDaK;
571
+
572
+ /**
573
+ * Decode format 10, D3K16uC16u.
574
+ *
575
+ * @type {typeof decodeD3K}
576
+ */
577
+ const decodeD3K16uC16u = decodeD3K;
578
+
579
+ /**
580
+ * Decode format 11, D3K8uC8u.
581
+ *
582
+ * @type {typeof decodeD3K}
583
+ */
584
+ const decodeD3K8uC8u = decodeD3K;
585
+
586
+ /**
587
+ * Decode format 12, D9I1K16uC16u.
588
+ *
589
+ * @type {typeof decodeD9I1}
590
+ */
591
+ const decodeD9I1K16uC16u = decodeD9I1;
592
+
593
+ /**
594
+ * Decode format 14, D9I1K8uC8u.
595
+ *
596
+ * @type {typeof decodeD9I1}
597
+ */
598
+ const decodeD9I1K8uC8u = decodeD9I1;
599
+
600
+ /**
601
+ * Decode format 13, D9I3K16uC16u.
602
+ *
603
+ * @type {typeof decodeD9I3}
604
+ */
605
+ const decodeD9I3K16uC16u = decodeD9I3;
606
+
607
+ /**
608
+ * Decode format 15, D9I3K8uC8u.
609
+ *
610
+ * @type {typeof decodeD9I3}
611
+ */
612
+ const decodeD9I3K8uC8u = decodeD9I3;
613
+
614
+ /**
615
+ * Decode format 17, D3I1K16uC16u.
616
+ *
617
+ * @type {typeof decodeD3I1u}
618
+ */
619
+ const decodeD3I1K16uC16u = decodeD3I1u;
620
+
621
+ /**
622
+ * Decode format 18, D3I1K8uC8u.
623
+ *
624
+ * @type {typeof decodeD3I1u}
625
+ */
626
+ const decodeD3I1K8uC8u = decodeD3I1u;
627
+
628
+ /**
629
+ * Curve object as emitted inside GR2 JSON transform tracks.
630
+ *
631
+ * @typedef {object} CurveJson
632
+ * @property {number} format Granny animation-curve format id.
633
+ * @property {number} degree Curve degree from the Granny curve header.
634
+ * @property {number[]} [knots] Explicit knot times when already decompressed.
635
+ * @property {number[]} [controls] Explicit control values when already decoded.
636
+ * @property {number} [dimension] Control vector width.
637
+ * @property {string} [error] Emitter error marker for unsupported raw curves.
638
+ */
639
+
640
+ /**
641
+ * Explicit curve data returned by {@link decodeCurve}.
642
+ *
643
+ * controls is a flat array in knot-major order:
644
+ * controls[knotIndex * dimension + componentIndex].
645
+ *
646
+ * @typedef {object} DecodedCurve
647
+ * @property {number[]} knots Knot times, non-decreasing.
648
+ * @property {number[]} controls Flat control values.
649
+ * @property {number} degree Curve degree.
650
+ * @property {number} dimension Control vector width.
651
+ */
652
+
653
+ /**
654
+ * Track dimension accepted by Granny transform curves.
655
+ *
656
+ * @typedef {1|3|4|9} TransformCurveDimension
657
+ */
658
+
659
+ /**
660
+ * Decoder table ordered by Granny curve format id.
661
+ *
662
+ * Each entry exposes the numeric format and the function used by
663
+ * {@link decodeCurve}.
664
+ */
665
+ const CURVE_DECODERS = Object.freeze([Object.freeze({
666
+ format: FORMAT_DA_KEYFRAMES_32F,
667
+ decode: decodeDaKeyframes32f
668
+ }), Object.freeze({
669
+ format: FORMAT_DA_K32F_C32F,
670
+ decode: decodeDaK32fC32f
671
+ }), Object.freeze({
672
+ format: FORMAT_DA_IDENTITY,
673
+ decode: decodeDaIdentity
674
+ }), Object.freeze({
675
+ format: FORMAT_DA_CONSTANT_32F,
676
+ decode: decodeDaConstant32f
677
+ }), Object.freeze({
678
+ format: FORMAT_D3_CONSTANT_32F,
679
+ decode: decodeD3Constant32f
680
+ }), Object.freeze({
681
+ format: FORMAT_D4_CONSTANT_32F,
682
+ decode: decodeD4Constant32f
683
+ }), Object.freeze({
684
+ format: FORMAT_DA_K16U_C16U,
685
+ decode: decodeDaK16uC16u
686
+ }), Object.freeze({
687
+ format: FORMAT_DA_K8U_C8U,
688
+ decode: decodeDaK8uC8u
689
+ }), Object.freeze({
690
+ format: FORMAT_D4N_K16U_C15U,
691
+ decode: decodeD4nK16uC15u
692
+ }), Object.freeze({
693
+ format: FORMAT_D4N_K8U_C7U,
694
+ decode: decodeD4nK8uC7u
695
+ }), Object.freeze({
696
+ format: FORMAT_D3_K16U_C16U,
697
+ decode: decodeD3K16uC16u
698
+ }), Object.freeze({
699
+ format: FORMAT_D3_K8U_C8U,
700
+ decode: decodeD3K8uC8u
701
+ }), Object.freeze({
702
+ format: FORMAT_D9I1_K16U_C16U,
703
+ decode: decodeD9I1K16uC16u
704
+ }), Object.freeze({
705
+ format: FORMAT_D9I3_K16U_C16U,
706
+ decode: decodeD9I3K16uC16u
707
+ }), Object.freeze({
708
+ format: FORMAT_D9I1_K8U_C8U,
709
+ decode: decodeD9I1K8uC8u
710
+ }), Object.freeze({
711
+ format: FORMAT_D9I3_K8U_C8U,
712
+ decode: decodeD9I3K8uC8u
713
+ }), Object.freeze({
714
+ format: FORMAT_D3I1_K32F_C32F,
715
+ decode: decodeD3I1K32fC32f
716
+ }), Object.freeze({
717
+ format: FORMAT_D3I1_K16U_C16U,
718
+ decode: decodeD3I1K16uC16u
719
+ }), Object.freeze({
720
+ format: FORMAT_D3I1_K8U_C8U,
721
+ decode: decodeD3I1K8uC8u
722
+ })]);
723
+ for (let i = 0; i < CURVE_DECODERS.length; i++) {
724
+ if (CURVE_DECODERS[i].format !== i) {
725
+ throw new Error(`gr2reader: curve decoder table corrupt at format ${i}`);
726
+ }
727
+ }
728
+
729
+ /**
730
+ * Decode a GR2 JSON curve object into explicit knots/controls.
731
+ *
732
+ * @param {CurveJson} curveJson Curve as emitted in GR2 JSON.
733
+ * @param {TransformCurveDimension} dimension Track dimension: position = 3, orientation = 4, scaleShear = 9.
734
+ * @returns {DecodedCurve} Explicit knots and flat control values.
735
+ * @throws {Error} If the curve is missing a numeric format, the format is not
736
+ * supported, or the decoded dimension conflicts with the requested dimension.
737
+ */
738
+ function decodeCurve(curveJson, dimension) {
739
+ if (!curveJson || typeof curveJson.format !== "number") {
740
+ throw new Error("gr2reader: decodeCurve requires a curve object with a numeric format");
741
+ }
742
+ const dec = CURVE_DECODERS[curveJson.format];
743
+ if (!dec) {
744
+ throw new Error(`gr2reader: unsupported granny curve format ${curveJson.format}`);
745
+ }
746
+ const {
747
+ knots,
748
+ controls,
749
+ dimension: dim
750
+ } = dec.decode(curveJson, dimension);
751
+ if (dimension && dim && dim !== dimension) {
752
+ throw new Error(`gr2reader: curve format ${curveJson.format} decoded dimension ${dim} does not match track dimension ${dimension}`);
753
+ }
754
+ return {
755
+ knots,
756
+ controls,
757
+ degree: curveJson.degree | 0,
758
+ dimension: dim || dimension
759
+ };
760
+ }
761
+
762
+ /**
763
+ * Copy one decoded control point into an output buffer.
764
+ *
765
+ * @param {ArrayLike<number> & { [index: number]: number }} out Mutable output buffer.
766
+ * @param {DecodedCurve} curve Decoded curve.
767
+ * @param {number} index Control point index.
768
+ * @returns {ArrayLike<number>} The same output buffer.
769
+ */
770
+ function copyControl(out, curve, index) {
771
+ const dim = curve.dimension,
772
+ offset = index * dim;
773
+ for (let i = 0; i < dim; i++) {
774
+ out[i] = curve.controls[offset + i];
775
+ }
776
+ return out;
777
+ }
778
+
779
+ /**
780
+ * Locate the first knot strictly greater than time, matching the existing
781
+ * runtime curve evaluator's step/segment selection.
782
+ *
783
+ * @param {number[]} knots Decoded knot times.
784
+ * @param {number} time Sample time.
785
+ * @returns {number} Knot index.
786
+ */
787
+ function findKnotIndex(knots, time) {
788
+ let low = 0;
789
+ let high = knots.length - 1;
790
+ while (low < high) {
791
+ const mid = low + high >> 1;
792
+ if (knots[mid] > time) high = mid;else low = mid + 1;
793
+ }
794
+ return low;
795
+ }
796
+
797
+ /**
798
+ * Sample Carbon's raw keyframed Granny curves (`DaKeyframes32f`).
799
+ *
800
+ * @param {ArrayLike<number> & { [index: number]: number }} out Mutable output buffer.
801
+ * @param {DecodedCurve} curve Decoded curve.
802
+ * @param {number} time Sample time.
803
+ * @param {number} duration Animation duration.
804
+ * @returns {ArrayLike<number>} The same output buffer.
805
+ */
806
+ function sampleKeyframedCurve(out, curve, time, duration) {
807
+ const count = curve.controls.length / curve.dimension;
808
+ const frame = duration > 0 ? Math.trunc(count * time / duration) : 0;
809
+ return copyControl(out, curve, Math.max(0, Math.min(count - 1, frame)));
810
+ }
811
+
812
+ /**
813
+ * Sample a decoded degree-one curve.
814
+ *
815
+ * @param {ArrayLike<number> & { [index: number]: number }} out Mutable output buffer.
816
+ * @param {DecodedCurve} curve Decoded curve.
817
+ * @param {number} time Sample time.
818
+ * @param {boolean} cycle Whether the owning track cycles.
819
+ * @param {number} duration Animation duration.
820
+ * @param {number} knot Selected knot index.
821
+ * @returns {ArrayLike<number>} The same output buffer.
822
+ */
823
+ function sampleLinearCurve(out, curve, time, cycle, duration, knot) {
824
+ const knots = curve.knots,
825
+ count = knots.length,
826
+ dim = curve.dimension,
827
+ knot0 = cycle ? (knot + count - 1) % count : knot === 0 ? 0 : knot - 1;
828
+ let start = knots[knot0],
829
+ end = knots[knot],
830
+ localTime = time;
831
+ if (cycle && end < start) {
832
+ end += duration;
833
+ }
834
+ if (cycle && localTime < start) {
835
+ localTime += duration;
836
+ }
837
+ const t = end !== start ? (localTime - start) / (end - start) : 0;
838
+ const p0 = knot0 * dim;
839
+ const p1 = knot * dim;
840
+ for (let i = 0; i < dim; i++) {
841
+ out[i] = curve.controls[p0 + i] * (1 - t) + curve.controls[p1 + i] * t;
842
+ }
843
+ return out;
844
+ }
845
+
846
+ /**
847
+ * Sample the quadratic decoded curves emitted by modern Granny animation data.
848
+ *
849
+ * @param {ArrayLike<number> & { [index: number]: number }} out Mutable output buffer.
850
+ * @param {DecodedCurve} curve Decoded curve.
851
+ * @param {number} time Sample time.
852
+ * @param {boolean} cycle Whether the owning track cycles.
853
+ * @param {number} duration Animation duration.
854
+ * @param {number} knot Selected knot index.
855
+ * @returns {ArrayLike<number>} The same output buffer.
856
+ */
857
+ function sampleQuadraticCurve(out, curve, time, cycle, duration, knot) {
858
+ const knots = curve.knots,
859
+ count = knots.length,
860
+ dim = curve.dimension,
861
+ k2 = cycle ? (knot + count - 2) % count : knot === 0 ? 0 : Math.max(0, knot - 2),
862
+ k1 = cycle ? (knot + count - 1) % count : knot === 0 ? 0 : knot - 1;
863
+ let ti2 = knots[k2],
864
+ ti1 = knots[k1],
865
+ ti = knots[knot],
866
+ tiNext = knots[(knot + 1) % count],
867
+ localTime = time;
868
+ if (ti2 > ti) {
869
+ ti += duration;
870
+ tiNext += duration;
871
+ localTime += duration;
872
+ }
873
+ if (ti1 > ti) {
874
+ ti += duration;
875
+ tiNext += duration;
876
+ localTime += duration;
877
+ }
878
+ if (tiNext < ti) {
879
+ tiNext += duration;
880
+ }
881
+ const d0 = ti - ti1,
882
+ d1a = ti - ti2,
883
+ d1b = tiNext - ti1,
884
+ l0 = d0 !== 0 ? (localTime - ti1) / d0 : 0,
885
+ l1a = d1a !== 0 ? (localTime - ti2) / d1a : 0,
886
+ l1b = d1b !== 0 ? (localTime - ti1) / d1b : 0;
887
+ let c2 = l1a + l0 - l0 * l1a;
888
+ const ci = l0 * l1b,
889
+ c1 = c2 - ci;
890
+ c2 = 1 - c2;
891
+ const p0 = k2 * dim,
892
+ p1 = k1 * dim,
893
+ p2 = knot * dim;
894
+ for (let i = 0; i < dim; i++) {
895
+ out[i] = c2 * curve.controls[p0 + i] + c1 * curve.controls[p1 + i] + ci * curve.controls[p2 + i];
896
+ }
897
+ return out;
898
+ }
899
+
900
+ /**
901
+ * Sample a decoded Granny curve into a caller-provided output buffer.
902
+ *
903
+ * This works only on decoded `{ knots, controls, degree, dimension }` data.
904
+ * Packed Granny curve parsing remains the responsibility of `decodeCurve`.
905
+ *
906
+ * @param {ArrayLike<number> & { [index: number]: number }} out Mutable output buffer.
907
+ * @param {DecodedCurve} curve Decoded curve.
908
+ * @param {number} time Sample time.
909
+ * @param {boolean} [cycle=false] Whether the owning track cycles.
910
+ * @param {number} [duration=0] Animation duration.
911
+ * @param {{ keyframed?: boolean }} [options] Sampling options.
912
+ * @returns {ArrayLike<number>} The same output buffer.
913
+ */
914
+ function sampleDecodedCurve(out, curve, time, cycle = false, duration = 0, options = {}) {
915
+ if (!curve || !curve.knots || !curve.controls || !curve.dimension) {
916
+ return out;
917
+ }
918
+ const knots = curve.knots,
919
+ count = knots.length,
920
+ dim = curve.dimension,
921
+ controlCount = curve.controls.length / dim;
922
+ if (!count || !controlCount) {
923
+ return out;
924
+ }
925
+ if (options.keyframed) {
926
+ return sampleKeyframedCurve(out, curve, time, duration);
927
+ }
928
+ const knot = findKnotIndex(knots, time);
929
+ if (curve.degree <= 0 || count === 1 || controlCount === 1) {
930
+ return copyControl(out, curve, Math.min(knot, controlCount - 1));
931
+ }
932
+ if (curve.degree === 1) {
933
+ return sampleLinearCurve(out, curve, time, cycle, duration || knots[count - 1], knot);
934
+ }
935
+ return sampleQuadraticCurve(out, curve, time, cycle, duration || knots[count - 1], knot);
936
+ }
937
+
938
+ /**
939
+ * Decode the three curves of every transform track of a GR2 JSON object in
940
+ * place. Adds knots, controls and dimension to each curve object; all raw
941
+ * compressed fields are left untouched.
942
+ *
943
+ * @param {object} json GR2 JSON root object to mutate.
944
+ * @returns {object} The same object, for chaining.
945
+ */
946
+ function decompressAnimationCurves(json) {
947
+ for (const anim of json.animations || []) {
948
+ for (const tg of anim.trackGroups || []) {
949
+ for (const tt of tg.transformTracks || []) {
950
+ decorate(tt.orientation, 4);
951
+ decorate(tt.position, 3);
952
+ decorate(tt.scaleShear, 9);
953
+ }
954
+ }
955
+ }
956
+ return json;
957
+ }
958
+
959
+ /**
960
+ * Decode and attach explicit curve data to a transform-track curve object.
961
+ *
962
+ * @param {CurveJson|undefined|null} curve Curve object to decorate in place.
963
+ * @param {TransformCurveDimension} dimension Expected track dimension.
964
+ * @returns {void}
965
+ */
966
+ function decorate(curve, dimension) {
967
+ if (!curve || typeof curve.format !== "number" || curve.error) return;
968
+ const d = decodeCurve(curve, dimension);
969
+ curve.knots = d.knots;
970
+ curve.controls = d.controls;
971
+ curve.dimension = d.dimension;
972
+ }
973
+
974
+ /**
975
+ * Frozen convenience namespace for animation-curve decoding helpers.
976
+ *
977
+ * The same constants and functions are also exported directly from curves.js.
978
+ */
979
+ const curves = Object.freeze({
980
+ FORMAT_DA_KEYFRAMES_32F,
981
+ FORMAT_DA_K32F_C32F,
982
+ FORMAT_DA_IDENTITY,
983
+ FORMAT_DA_CONSTANT_32F,
984
+ FORMAT_D3_CONSTANT_32F,
985
+ FORMAT_D4_CONSTANT_32F,
986
+ FORMAT_DA_K16U_C16U,
987
+ FORMAT_DA_K8U_C8U,
988
+ FORMAT_D4N_K16U_C15U,
989
+ FORMAT_D4N_K8U_C7U,
990
+ FORMAT_D3_K16U_C16U,
991
+ FORMAT_D3_K8U_C8U,
992
+ FORMAT_D9I1_K16U_C16U,
993
+ FORMAT_D9I3_K16U_C16U,
994
+ FORMAT_D9I1_K8U_C8U,
995
+ FORMAT_D9I3_K8U_C8U,
996
+ FORMAT_D3I1_K32F_C32F,
997
+ FORMAT_D3I1_K16U_C16U,
998
+ FORMAT_D3I1_K8U_C8U,
999
+ FORMATS: CURVE_FORMATS,
1000
+ DECODERS: CURVE_DECODERS,
1001
+ D4N_SCALE_TABLE,
1002
+ D4N_OFFSET_TABLE,
1003
+ D4N_SCALE_TABLE_MULTIPLIER_16,
1004
+ D4N_SCALE_TABLE_MULTIPLIER_8,
1005
+ decode: decodeCurve,
1006
+ decodeCurve,
1007
+ sample: sampleDecodedCurve,
1008
+ sampleDecodedCurve,
1009
+ decompress: decompressAnimationCurves,
1010
+ decompressAnimationCurves,
1011
+ knotScaleFromTrunc,
1012
+ knotsFromControls,
1013
+ knotsFromControlsTrunc,
1014
+ identityControls,
1015
+ exactDiv,
1016
+ decodeDaKeyframes32f,
1017
+ decodeDaK32fC32f,
1018
+ decodeDaIdentity,
1019
+ decodeDaConstant32f,
1020
+ decodeD3Constant32f,
1021
+ decodeD4Constant32f,
1022
+ decodeDaK,
1023
+ decodeDaK16uC16u,
1024
+ decodeDaK8uC8u,
1025
+ quatFromControl16,
1026
+ quatFromControl8,
1027
+ decodeD4n,
1028
+ decodeD4nK16uC15u,
1029
+ decodeD4nK8uC7u,
1030
+ decodeD3K,
1031
+ decodeD3K16uC16u,
1032
+ decodeD3K8uC8u,
1033
+ decodeD9I1,
1034
+ decodeD9I1K16uC16u,
1035
+ decodeD9I1K8uC8u,
1036
+ decodeD9I3,
1037
+ decodeD9I3K16uC16u,
1038
+ decodeD9I3K8uC8u,
1039
+ d3I1Controls,
1040
+ decodeD3I1K32fC32f,
1041
+ decodeD3I1u,
1042
+ decodeD3I1K16uC16u,
1043
+ decodeD3I1K8uC8u
1044
+ });
1045
+
1046
+ export { CURVE_DECODERS, CURVE_FORMATS, D4N_OFFSET_TABLE, D4N_SCALE_TABLE, D4N_SCALE_TABLE_MULTIPLIER_16, D4N_SCALE_TABLE_MULTIPLIER_8, FORMAT_D3I1_K16U_C16U, FORMAT_D3I1_K32F_C32F, FORMAT_D3I1_K8U_C8U, FORMAT_D3_CONSTANT_32F, FORMAT_D3_K16U_C16U, FORMAT_D3_K8U_C8U, FORMAT_D4N_K16U_C15U, FORMAT_D4N_K8U_C7U, FORMAT_D4_CONSTANT_32F, FORMAT_D9I1_K16U_C16U, FORMAT_D9I1_K8U_C8U, FORMAT_D9I3_K16U_C16U, FORMAT_D9I3_K8U_C8U, FORMAT_DA_CONSTANT_32F, FORMAT_DA_IDENTITY, FORMAT_DA_K16U_C16U, FORMAT_DA_K32F_C32F, FORMAT_DA_K8U_C8U, FORMAT_DA_KEYFRAMES_32F, curves, d3I1Controls, decodeCurve, decodeD3Constant32f, decodeD3I1K16uC16u, decodeD3I1K32fC32f, decodeD3I1K8uC8u, decodeD3I1u, decodeD3K, decodeD3K16uC16u, decodeD3K8uC8u, decodeD4Constant32f, decodeD4n, decodeD4nK16uC15u, decodeD4nK8uC7u, decodeD9I1, decodeD9I1K16uC16u, decodeD9I1K8uC8u, decodeD9I3, decodeD9I3K16uC16u, decodeD9I3K8uC8u, decodeDaConstant32f, decodeDaIdentity, decodeDaK, decodeDaK16uC16u, decodeDaK32fC32f, decodeDaK8uC8u, decodeDaKeyframes32f, decompressAnimationCurves, exactDiv, fr, identityControls, knotScaleFromTrunc, knotsFromControls, knotsFromControlsTrunc, quatFromControl16, quatFromControl8, sampleDecodedCurve };
1047
+ //# sourceMappingURL=curves.js.map