@driftengine/drft 3.61.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 (67) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +9 -0
  3. package/README.md +9 -0
  4. package/dist/animationData.d.ts +53 -0
  5. package/dist/animationData.js +13 -0
  6. package/dist/coarseFirst.d.ts +27 -0
  7. package/dist/coarseFirst.js +118 -0
  8. package/dist/drftColliders.d.ts +53 -0
  9. package/dist/drftColliders.js +137 -0
  10. package/dist/drftFormat.d.ts +454 -0
  11. package/dist/drftFormat.js +350 -0
  12. package/dist/drftRead.d.ts +121 -0
  13. package/dist/drftRead.js +487 -0
  14. package/dist/drftSkin.d.ts +40 -0
  15. package/dist/drftSkin.js +270 -0
  16. package/dist/drftStream.d.ts +170 -0
  17. package/dist/drftStream.js +315 -0
  18. package/dist/drftSubs.d.ts +18 -0
  19. package/dist/drftSubs.js +70 -0
  20. package/dist/drftWrite.d.ts +103 -0
  21. package/dist/drftWrite.js +478 -0
  22. package/dist/fixtures/v1-0.d.ts +9 -0
  23. package/dist/fixtures/v1-0.js +9 -0
  24. package/dist/fixtures/v1-1.d.ts +16 -0
  25. package/dist/fixtures/v1-1.js +16 -0
  26. package/dist/fixtures/v1-11.d.ts +14 -0
  27. package/dist/fixtures/v1-11.js +14 -0
  28. package/dist/fixtures/v1-2.d.ts +19 -0
  29. package/dist/fixtures/v1-2.js +19 -0
  30. package/dist/fixtures/v1-3.d.ts +18 -0
  31. package/dist/fixtures/v1-3.js +18 -0
  32. package/dist/fixtures/v1-4.d.ts +13 -0
  33. package/dist/fixtures/v1-4.js +13 -0
  34. package/dist/fixtures/v1-5.d.ts +14 -0
  35. package/dist/fixtures/v1-5.js +14 -0
  36. package/dist/fixtures/v1-6.d.ts +9 -0
  37. package/dist/fixtures/v1-6.js +9 -0
  38. package/dist/fixtures/v1-7.d.ts +9 -0
  39. package/dist/fixtures/v1-7.js +9 -0
  40. package/dist/fixtures/v1-9.d.ts +9 -0
  41. package/dist/fixtures/v1-9.js +9 -0
  42. package/dist/index.d.ts +26 -0
  43. package/dist/index.js +29 -0
  44. package/dist/meshData.d.ts +210 -0
  45. package/dist/meshData.js +105 -0
  46. package/package.json +57 -0
  47. package/src/animationData.ts +58 -0
  48. package/src/coarseFirst.ts +113 -0
  49. package/src/drftColliders.ts +153 -0
  50. package/src/drftFormat.ts +537 -0
  51. package/src/drftRead.ts +652 -0
  52. package/src/drftSkin.ts +326 -0
  53. package/src/drftStream.ts +436 -0
  54. package/src/drftSubs.ts +86 -0
  55. package/src/drftWrite.ts +613 -0
  56. package/src/fixtures/v1-0.ts +10 -0
  57. package/src/fixtures/v1-1.ts +17 -0
  58. package/src/fixtures/v1-11.ts +15 -0
  59. package/src/fixtures/v1-2.ts +20 -0
  60. package/src/fixtures/v1-3.ts +19 -0
  61. package/src/fixtures/v1-4.ts +14 -0
  62. package/src/fixtures/v1-5.ts +15 -0
  63. package/src/fixtures/v1-6.ts +10 -0
  64. package/src/fixtures/v1-7.ts +10 -0
  65. package/src/fixtures/v1-9.ts +10 -0
  66. package/src/index.ts +56 -0
  67. package/src/meshData.ts +301 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Drift Technologies
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,9 @@
1
+ DriftEngine
2
+ Copyright 2026 Drift Technologies
3
+
4
+ This product includes software developed at Drift Technologies
5
+ (https://github.com/drftrun/driftengine).
6
+
7
+ Licensed under the Apache License, Version 2.0. Section 4(d) of that licence
8
+ requires this NOTICE to be reproduced in any derivative work you distribute,
9
+ in the source, the documentation, or a display generated by the work.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # @driftengine/drft
2
+
3
+ The `.drft` container: read and write a baked scene. Declares no runtime dependency.
4
+
5
+ **Cost: 5.0 KB gzipped.** Measured by `scripts/size-gate.test.mjs`, which fails if it drifts more than
6
+ 3% — the number is a fact about the build rather than a claim in a document.
7
+
8
+ Part of [DriftEngine](../../README.md). See [`docs/ARCHITECTURE.md`](../../docs/ARCHITECTURE.md)
9
+ for how the packages divide, and why some features are compiled into core on demand instead.
@@ -0,0 +1,53 @@
1
+ /**
2
+ * What a rig and its clips are, as data.
3
+ *
4
+ * **Here for the reason `MeshData` is here**, and stated in that file's header: these are the
5
+ * boundary objects between the container and everything that uses one. `SKIN` and `ANIM` are
6
+ * chunks, so what they carry belongs to the format package — which also means the two packages
7
+ * that meet over a rig, `@driftengine/assets` producing one and `@driftengine/animation` consuming
8
+ * one, do so without either depending on the other. Both already depend on this.
9
+ *
10
+ * Nothing here has behaviour. `Skeleton`, `Pose` and `sampleClip` live in the animation package,
11
+ * which re-exports these types so a consumer imports from one place.
12
+ */
13
+ /** One joint: where it hangs and what it is called. */
14
+ export interface Joint {
15
+ /**
16
+ * Index of this joint's parent, or -1 for a root. **Always less than this joint's own index.**
17
+ *
18
+ * Parents-first is a requirement rather than a convention: a palette is resolved in index order
19
+ * and reads each joint's parent as it goes, so a nearly-sorted rig is wrong in one limb — which
20
+ * reads as a bad animation rather than as a data error. The importer sorts, because it is also
21
+ * the layer that can remap every index naming a joint; `Skeleton` refuses an unsorted one.
22
+ */
23
+ readonly parent: number;
24
+ /** The name the source rig gave it, which is what retargeting matches on. */
25
+ readonly name: string;
26
+ }
27
+ /** Which of a joint's three transforms a track drives. */
28
+ export type TrackPath = 'translation' | 'rotation' | 'scale';
29
+ /** One joint's keys for one of its three transforms. */
30
+ export interface JointTrack {
31
+ readonly joint: number;
32
+ readonly path: TrackPath;
33
+ /** Ascending, in seconds. One entry per key. */
34
+ readonly times: Float32Array;
35
+ /** Three floats a key for translation and scale, four for rotation. */
36
+ readonly values: Float32Array;
37
+ }
38
+ export interface AnimationClip {
39
+ readonly name: string;
40
+ readonly durationSec: number;
41
+ readonly tracks: readonly JointTrack[];
42
+ }
43
+ /**
44
+ * A skin: the joints, their inverse bind matrices, and nothing about how it is played.
45
+ *
46
+ * `inverseBind` is sixteen floats a joint, column-major, in the same order as `joints`. An entry
47
+ * takes a vertex from model space into that joint's bind-pose space, which is what makes a palette
48
+ * identity at the bind pose whatever the bind pose is.
49
+ */
50
+ export interface DrftSkin {
51
+ readonly joints: readonly Joint[];
52
+ readonly inverseBind: Float32Array;
53
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * What a rig and its clips are, as data.
3
+ *
4
+ * **Here for the reason `MeshData` is here**, and stated in that file's header: these are the
5
+ * boundary objects between the container and everything that uses one. `SKIN` and `ANIM` are
6
+ * chunks, so what they carry belongs to the format package — which also means the two packages
7
+ * that meet over a rig, `@driftengine/assets` producing one and `@driftengine/animation` consuming
8
+ * one, do so without either depending on the other. Both already depend on this.
9
+ *
10
+ * Nothing here has behaviour. `Skeleton`, `Pose` and `sampleClip` live in the animation package,
11
+ * which re-exports these types so a consumer imports from one place.
12
+ */
13
+ export {};
@@ -0,0 +1,27 @@
1
+ /** The order splats are written to the container in, so any prefix of the file is the whole place. */
2
+ /**
3
+ * The order to write a capture's splats in, so that **every prefix of the file is a complete
4
+ * sparse capture** rather than a finished corner of one.
5
+ *
6
+ * **This is the reason the container is worth having at all.** Everything above it is a splat
7
+ * viewer; a load that opens on a recognisable place and densifies is what a viewer streaming from
8
+ * a plain file cannot do by accident. It is the same trick `coarseLevel.ts` plays for meshes,
9
+ * where a decimated whole body reads as a car and one finished wheel does not.
10
+ *
11
+ * Two steps, and both are needed:
12
+ *
13
+ * 1. **Sort by Morton code**, so index order becomes spatial order. Without this the sequence
14
+ * below decimates whatever order the trainer happened to leave — which is usually fine and is
15
+ * a slab of one axis when it is not, and the difference is invisible until somebody opens a
16
+ * capture that was written out in scan order. `coarseFirst.test.ts` builds exactly that case.
17
+ * 2. **Walk that order bit-reversed**, which is the van der Corput sequence and is what makes
18
+ * each *block* a sparse capture rather than only the first. Reversing the bits of a counter
19
+ * visits 0, half way, quarter, three quarters, and so on, so consecutive slots are maximally
20
+ * far apart in the spatial order and every block is an even scattering over the whole thing.
21
+ *
22
+ * What it costs is a sort at bake time — `O(n log n)` once, on a machine with a keyboard — and
23
+ * nothing at all at load. **What would make it wrong** is a capture meant to be read in its
24
+ * authored order, which this format has no notion of: a `.drft` splat chunk is a set, and a
25
+ * consumer that needed the original indices would need them stored.
26
+ */
27
+ export declare function coarseFirstOrder(positions: Float32Array, count: number): Uint32Array;
@@ -0,0 +1,118 @@
1
+ /** The order splats are written to the container in, so any prefix of the file is the whole place. */
2
+ /**
3
+ * Bits per axis in the spatial key. Ten, so three of them fit a 30-bit integer sort key.
4
+ *
5
+ * A thousand and twenty-four cells an axis over the capture's own extent, which for a room-sized
6
+ * capture is under a centimetre. What it gives up is that two splats inside one cell are ordered
7
+ * arbitrarily against each other, which is invisible at any block size a stream uses. What would
8
+ * make it wrong is a capture whose extent is dominated by one distant outlier, squeezing the rest
9
+ * into a handful of cells — the same failure any quantisation has.
10
+ */
11
+ const AXIS_BITS = 10;
12
+ const AXIS_CELLS = (1 << AXIS_BITS) - 1;
13
+ /** Spread ten bits out to every third bit, so three of them interleave into one integer. */
14
+ function spread(value) {
15
+ let bits = value & AXIS_CELLS;
16
+ bits = (bits | (bits << 16)) & 0x030000ff;
17
+ bits = (bits | (bits << 8)) & 0x0300f00f;
18
+ bits = (bits | (bits << 4)) & 0x030c30c3;
19
+ bits = (bits | (bits << 2)) & 0x09249249;
20
+ return bits;
21
+ }
22
+ /**
23
+ * The order to write a capture's splats in, so that **every prefix of the file is a complete
24
+ * sparse capture** rather than a finished corner of one.
25
+ *
26
+ * **This is the reason the container is worth having at all.** Everything above it is a splat
27
+ * viewer; a load that opens on a recognisable place and densifies is what a viewer streaming from
28
+ * a plain file cannot do by accident. It is the same trick `coarseLevel.ts` plays for meshes,
29
+ * where a decimated whole body reads as a car and one finished wheel does not.
30
+ *
31
+ * Two steps, and both are needed:
32
+ *
33
+ * 1. **Sort by Morton code**, so index order becomes spatial order. Without this the sequence
34
+ * below decimates whatever order the trainer happened to leave — which is usually fine and is
35
+ * a slab of one axis when it is not, and the difference is invisible until somebody opens a
36
+ * capture that was written out in scan order. `coarseFirst.test.ts` builds exactly that case.
37
+ * 2. **Walk that order bit-reversed**, which is the van der Corput sequence and is what makes
38
+ * each *block* a sparse capture rather than only the first. Reversing the bits of a counter
39
+ * visits 0, half way, quarter, three quarters, and so on, so consecutive slots are maximally
40
+ * far apart in the spatial order and every block is an even scattering over the whole thing.
41
+ *
42
+ * What it costs is a sort at bake time — `O(n log n)` once, on a machine with a keyboard — and
43
+ * nothing at all at load. **What would make it wrong** is a capture meant to be read in its
44
+ * authored order, which this format has no notion of: a `.drft` splat chunk is a set, and a
45
+ * consumer that needed the original indices would need them stored.
46
+ */
47
+ export function coarseFirstOrder(positions, count) {
48
+ const order = new Uint32Array(Math.max(0, count));
49
+ if (count <= 0)
50
+ return order;
51
+ if (count === 1)
52
+ return order;
53
+ let minX = Infinity;
54
+ let minY = Infinity;
55
+ let minZ = Infinity;
56
+ let maxX = -Infinity;
57
+ let maxY = -Infinity;
58
+ let maxZ = -Infinity;
59
+ for (let index = 0; index < count; index++) {
60
+ const at = index * 3;
61
+ const x = positions[at] ?? 0;
62
+ const y = positions[at + 1] ?? 0;
63
+ const z = positions[at + 2] ?? 0;
64
+ if (x < minX)
65
+ minX = x;
66
+ if (y < minY)
67
+ minY = y;
68
+ if (z < minZ)
69
+ minZ = z;
70
+ if (x > maxX)
71
+ maxX = x;
72
+ if (y > maxY)
73
+ maxY = y;
74
+ if (z > maxZ)
75
+ maxZ = z;
76
+ }
77
+ /* A capture with no extent on an axis quantises every splat to cell zero there, which is the
78
+ right answer: with no range there is no spatial order along it to preserve. */
79
+ const scaleX = maxX > minX ? AXIS_CELLS / (maxX - minX) : 0;
80
+ const scaleY = maxY > minY ? AXIS_CELLS / (maxY - minY) : 0;
81
+ const scaleZ = maxZ > minZ ? AXIS_CELLS / (maxZ - minZ) : 0;
82
+ const spatial = new Uint32Array(count);
83
+ const keys = new Uint32Array(count);
84
+ for (let index = 0; index < count; index++) {
85
+ const at = index * 3;
86
+ const x = Math.round(((positions[at] ?? 0) - minX) * scaleX);
87
+ const y = Math.round(((positions[at + 1] ?? 0) - minY) * scaleY);
88
+ const z = Math.round(((positions[at + 2] ?? 0) - minZ) * scaleZ);
89
+ keys[index] = (spread(x) | (spread(y) << 1) | (spread(z) << 2)) >>> 0;
90
+ spatial[index] = index;
91
+ }
92
+ /*
93
+ * A comparison sort, deliberately. This runs once per bake on a workstation, against a counting
94
+ * sort's 4 MB of buckets for a 30-bit key; the frame-time sort in `@driftengine/splats` is the
95
+ * one where that trade goes the other way, and it says so.
96
+ */
97
+ const sorted = Array.from(spatial).sort((a, b) => (keys[a] ?? 0) - (keys[b] ?? 0));
98
+ /*
99
+ * Bit-reversal over the spatial order. `width` is the number of bits the count needs, so the
100
+ * counter covers a power of two at least as large; reversed values landing past the end are
101
+ * skipped, which keeps the sequence's spread and costs at most one extra pass.
102
+ */
103
+ let width = 0;
104
+ while (1 << width < count)
105
+ width++;
106
+ let slot = 0;
107
+ for (let counter = 0; counter < 1 << width; counter++) {
108
+ let reversed = 0;
109
+ for (let bit = 0; bit < width; bit++) {
110
+ if ((counter & (1 << bit)) !== 0)
111
+ reversed |= 1 << (width - 1 - bit);
112
+ }
113
+ if (reversed >= count)
114
+ continue;
115
+ order[slot++] = sorted[reversed] ?? 0;
116
+ }
117
+ return order;
118
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * `COLL`: the convex hulls an asset collides as.
3
+ *
4
+ * **A FourCC that was claimed and undefined for the whole of v1.** `docs/FORMAT.md` §4.3 listed the
5
+ * row and `KNOWN_CHUNKS` held the code while no writer emitted one and no reader consumed one.
6
+ * `readModel.ts` said why in as many words when it declined to put a vehicle's collision hull inside
7
+ * the model's own file: *"a format decision about whether a hull is triangle soup or a set of convex
8
+ * hulls, and not one an import should make in passing."* This is that decision, made — **a set of
9
+ * convex hulls**.
10
+ *
11
+ * **Points, not shapes, because this package depends on nothing.** `boundaries.test.mjs` asserts
12
+ * that import graph and `drft-only` measures what it is worth. A serialised `ConvexShape` would mean
13
+ * the format package knowing what a face plane and a separating axis are, and it would freeze a
14
+ * representation the physics package is still free to change. A hull here is the points whose hull
15
+ * it is, which is exactly what `hullShape` takes:
16
+ *
17
+ * ```ts
18
+ * const shapes = asset.colliders.map((points) => hullShape(points));
19
+ * ```
20
+ *
21
+ * **One chunk for the file**, pairing with nothing. An asset's collision is a fact about the whole
22
+ * asset the way `SUBS` is a fact about all its materials, not a thing that belongs to mesh three.
23
+ *
24
+ * ```
25
+ * u32 hullCount
26
+ * u32 pointTotal
27
+ * u32 starts[hullCount + 1] offsets in points; starts[0] = 0, starts[n] = pointTotal
28
+ * f32 points[pointTotal * 3] xyz, in the asset's own space
29
+ * ```
30
+ *
31
+ * The starts table is written whole, with its closing entry, so a reader takes every hull's extent
32
+ * from one subtraction and never has to special-case the last one. Everything is four-byte aligned,
33
+ * so the points come back as one `Float32Array` view over the fetched buffer and each hull is a
34
+ * `subarray` of it — the same property `MESH` exists for.
35
+ */
36
+ /** How many points one hull may carry, which is what `hullShape` accepts. */
37
+ export declare const MAX_COLLIDER_POINTS = 64;
38
+ /**
39
+ * A cap on hulls per asset, so a malformed length cannot ask a reader for gigabytes.
40
+ *
41
+ * Matches `MAX_BODY_PARTS` in `@driftengine/physics` by intent rather than by import — this package
42
+ * depends on nothing, and the two are checked against each other by a test in the engine instead.
43
+ */
44
+ export declare const MAX_COLLIDER_HULLS = 32;
45
+ export declare function buildColliders(hulls: readonly Float32Array[]): Uint8Array;
46
+ /**
47
+ * The hulls a `COLL` chunk carries, as views over the fetched buffer.
48
+ *
49
+ * Views rather than copies, exactly like the vertex arrays, so a load allocates nothing beyond the
50
+ * array holding them. They keep the file alive for as long as they are held, which is the same trade
51
+ * `DrftTexture.bytes` makes and for the same reason.
52
+ */
53
+ export declare function readColliders(buffer: ArrayBuffer, offset: number, byteLength: number): readonly Float32Array[];
@@ -0,0 +1,137 @@
1
+ import { DrftError, align } from './drftFormat.js';
2
+ /**
3
+ * `COLL`: the convex hulls an asset collides as.
4
+ *
5
+ * **A FourCC that was claimed and undefined for the whole of v1.** `docs/FORMAT.md` §4.3 listed the
6
+ * row and `KNOWN_CHUNKS` held the code while no writer emitted one and no reader consumed one.
7
+ * `readModel.ts` said why in as many words when it declined to put a vehicle's collision hull inside
8
+ * the model's own file: *"a format decision about whether a hull is triangle soup or a set of convex
9
+ * hulls, and not one an import should make in passing."* This is that decision, made — **a set of
10
+ * convex hulls**.
11
+ *
12
+ * **Points, not shapes, because this package depends on nothing.** `boundaries.test.mjs` asserts
13
+ * that import graph and `drft-only` measures what it is worth. A serialised `ConvexShape` would mean
14
+ * the format package knowing what a face plane and a separating axis are, and it would freeze a
15
+ * representation the physics package is still free to change. A hull here is the points whose hull
16
+ * it is, which is exactly what `hullShape` takes:
17
+ *
18
+ * ```ts
19
+ * const shapes = asset.colliders.map((points) => hullShape(points));
20
+ * ```
21
+ *
22
+ * **One chunk for the file**, pairing with nothing. An asset's collision is a fact about the whole
23
+ * asset the way `SUBS` is a fact about all its materials, not a thing that belongs to mesh three.
24
+ *
25
+ * ```
26
+ * u32 hullCount
27
+ * u32 pointTotal
28
+ * u32 starts[hullCount + 1] offsets in points; starts[0] = 0, starts[n] = pointTotal
29
+ * f32 points[pointTotal * 3] xyz, in the asset's own space
30
+ * ```
31
+ *
32
+ * The starts table is written whole, with its closing entry, so a reader takes every hull's extent
33
+ * from one subtraction and never has to special-case the last one. Everything is four-byte aligned,
34
+ * so the points come back as one `Float32Array` view over the fetched buffer and each hull is a
35
+ * `subarray` of it — the same property `MESH` exists for.
36
+ */
37
+ /** How many points one hull may carry, which is what `hullShape` accepts. */
38
+ export const MAX_COLLIDER_POINTS = 64;
39
+ /**
40
+ * A cap on hulls per asset, so a malformed length cannot ask a reader for gigabytes.
41
+ *
42
+ * Matches `MAX_BODY_PARTS` in `@driftengine/physics` by intent rather than by import — this package
43
+ * depends on nothing, and the two are checked against each other by a test in the engine instead.
44
+ */
45
+ export const MAX_COLLIDER_HULLS = 32;
46
+ export function buildColliders(hulls) {
47
+ if (hulls.length > MAX_COLLIDER_HULLS) {
48
+ throw new DrftError(`${hulls.length} collision hulls exceeds ${MAX_COLLIDER_HULLS}`);
49
+ }
50
+ let pointTotal = 0;
51
+ for (const hull of hulls) {
52
+ if (hull.length % 3 !== 0) {
53
+ throw new DrftError(`a collision hull has ${hull.length} floats, which is not whole points`);
54
+ }
55
+ const points = hull.length / 3;
56
+ if (points === 0)
57
+ throw new DrftError('a collision hull has no points');
58
+ if (points > MAX_COLLIDER_POINTS) {
59
+ throw new DrftError(`a collision hull has ${points} points, over the ${MAX_COLLIDER_POINTS} cap`);
60
+ }
61
+ pointTotal += points;
62
+ }
63
+ const startsBytes = (hulls.length + 1) * 4;
64
+ const size = 8 + startsBytes + pointTotal * 3 * 4;
65
+ const bytes = new Uint8Array(align(size));
66
+ const view = new DataView(bytes.buffer);
67
+ view.setUint32(0, hulls.length, true);
68
+ view.setUint32(4, pointTotal, true);
69
+ let start = 0;
70
+ for (let i = 0; i < hulls.length; i++) {
71
+ view.setUint32(8 + i * 4, start, true);
72
+ start += hulls[i].length / 3;
73
+ }
74
+ view.setUint32(8 + hulls.length * 4, pointTotal, true);
75
+ const points = new Float32Array(bytes.buffer, 8 + startsBytes, pointTotal * 3);
76
+ let at = 0;
77
+ for (const hull of hulls) {
78
+ points.set(hull, at);
79
+ at += hull.length;
80
+ }
81
+ return bytes;
82
+ }
83
+ /**
84
+ * The hulls a `COLL` chunk carries, as views over the fetched buffer.
85
+ *
86
+ * Views rather than copies, exactly like the vertex arrays, so a load allocates nothing beyond the
87
+ * array holding them. They keep the file alive for as long as they are held, which is the same trade
88
+ * `DrftTexture.bytes` makes and for the same reason.
89
+ */
90
+ export function readColliders(buffer, offset, byteLength) {
91
+ if (byteLength < 8)
92
+ throw new DrftError('COLL is too short to hold its counts');
93
+ const view = new DataView(buffer, offset, byteLength);
94
+ const hullCount = view.getUint32(0, true);
95
+ const pointTotal = view.getUint32(4, true);
96
+ if (hullCount > MAX_COLLIDER_HULLS) {
97
+ throw new DrftError(`COLL claims ${hullCount} hulls, over the ${MAX_COLLIDER_HULLS} cap`);
98
+ }
99
+ const startsBytes = (hullCount + 1) * 4;
100
+ const needed = 8 + startsBytes + pointTotal * 3 * 4;
101
+ if (byteLength < needed) {
102
+ throw new DrftError(`COLL claims ${hullCount} hulls and ${pointTotal} points, which runs past the chunk`);
103
+ }
104
+ /*
105
+ * The starts are read and checked before a single point is handed out. A table that runs backwards
106
+ * or past the total would otherwise produce a `subarray` of a length nobody wrote, which is a hull
107
+ * of arbitrary geometry rather than an error.
108
+ */
109
+ const starts = new Uint32Array(hullCount + 1);
110
+ for (let i = 0; i <= hullCount; i++)
111
+ starts[i] = view.getUint32(8 + i * 4, true);
112
+ if (starts[0] !== 0)
113
+ throw new DrftError('COLL does not start its first hull at zero');
114
+ if (starts[hullCount] !== pointTotal) {
115
+ throw new DrftError('COLL closes its table at a point count it did not declare');
116
+ }
117
+ for (let i = 0; i < hullCount; i++) {
118
+ const from = starts[i];
119
+ const to = starts[i + 1];
120
+ if (to < from)
121
+ throw new DrftError(`COLL hull ${i} runs backwards`);
122
+ const points = to - from;
123
+ if (points === 0)
124
+ throw new DrftError(`COLL hull ${i} has no points`);
125
+ if (points > MAX_COLLIDER_POINTS) {
126
+ throw new DrftError(`COLL hull ${i} has ${points} points, over the ${MAX_COLLIDER_POINTS} cap`);
127
+ }
128
+ }
129
+ const all = new Float32Array(buffer, offset + 8 + startsBytes, pointTotal * 3);
130
+ const hulls = [];
131
+ for (let i = 0; i < hullCount; i++) {
132
+ const from = starts[i] * 3;
133
+ const to = starts[i + 1] * 3;
134
+ hulls.push(all.subarray(from, to));
135
+ }
136
+ return hulls;
137
+ }