-
Notifications
You must be signed in to change notification settings - Fork 82
Add collision mesh generation for voxel output #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ | |
| "lib/" | ||
| ], | ||
| "dependencies": { | ||
| "meshoptimizer": "^1.0.1", | ||
| "webgpu": "^0.3.8" | ||
| }, | ||
| "devDependencies": { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /** | ||
| * Build a minimal GLB (glTF 2.0 binary) file containing a single triangle mesh. | ||
| * | ||
| * The output contains only positions and triangle indices — no normals, | ||
| * UVs, or materials — suitable for collision meshes. | ||
| * | ||
| * @param positions - Vertex positions (3 floats per vertex) | ||
| * @param indices - Triangle indices (3 per triangle, unsigned 32-bit) | ||
| * @returns GLB file as a Uint8Array | ||
| */ | ||
| function buildCollisionGlb(positions: Float32Array, indices: Uint32Array): Uint8Array { | ||
| const vertexCount = positions.length / 3; | ||
| const indexCount = indices.length; | ||
|
|
||
| let minX = Infinity, minY = Infinity, minZ = Infinity; | ||
| let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity; | ||
| for (let i = 0; i < positions.length; i += 3) { | ||
| const x = positions[i], y = positions[i + 1], z = positions[i + 2]; | ||
| if (x < minX) minX = x; | ||
| if (y < minY) minY = y; | ||
| if (z < minZ) minZ = z; | ||
| if (x > maxX) maxX = x; | ||
| if (y > maxY) maxY = y; | ||
| if (z > maxZ) maxZ = z; | ||
| } | ||
|
slimbuck marked this conversation as resolved.
|
||
|
|
||
| const positionsByteLength = positions.byteLength; | ||
| const indicesByteLength = indices.byteLength; | ||
| const totalBinSize = positionsByteLength + indicesByteLength; | ||
|
|
||
| const gltf = { | ||
| asset: { version: '2.0', generator: 'splat-transform' }, | ||
| scene: 0, | ||
| scenes: [{ nodes: [0] }], | ||
| nodes: [{ mesh: 0 }], | ||
| meshes: [{ | ||
| primitives: [{ | ||
| attributes: { POSITION: 0 }, | ||
| indices: 1 | ||
| }] | ||
| }], | ||
| accessors: [ | ||
| { | ||
| bufferView: 0, | ||
| componentType: 5126, // FLOAT | ||
| count: vertexCount, | ||
| type: 'VEC3', | ||
| min: [minX, minY, minZ], | ||
| max: [maxX, maxY, maxZ] | ||
| }, | ||
| { | ||
| bufferView: 1, | ||
| componentType: 5125, // UNSIGNED_INT | ||
| count: indexCount, | ||
| type: 'SCALAR' | ||
| } | ||
| ], | ||
| bufferViews: [ | ||
| { | ||
| buffer: 0, | ||
| byteOffset: 0, | ||
| byteLength: positionsByteLength, | ||
| target: 34962 // ARRAY_BUFFER | ||
| }, | ||
| { | ||
| buffer: 0, | ||
| byteOffset: positionsByteLength, | ||
| byteLength: indicesByteLength, | ||
| target: 34963 // ELEMENT_ARRAY_BUFFER | ||
| } | ||
| ], | ||
| buffers: [{ byteLength: totalBinSize }] | ||
| }; | ||
|
|
||
| const jsonString = JSON.stringify(gltf); | ||
| const jsonEncoder = new TextEncoder(); | ||
| const jsonBytes = jsonEncoder.encode(jsonString); | ||
|
|
||
| // JSON chunk must be padded to 4-byte alignment with spaces (0x20) | ||
| const jsonPadding = (4 - (jsonBytes.length % 4)) % 4; | ||
| const jsonChunkLength = jsonBytes.length + jsonPadding; | ||
|
|
||
| // BIN chunk must be padded to 4-byte alignment with zeros | ||
| const binPadding = (4 - (totalBinSize % 4)) % 4; | ||
| const binChunkLength = totalBinSize + binPadding; | ||
|
|
||
|
slimbuck marked this conversation as resolved.
|
||
| // GLB layout: header (12) + JSON chunk header (8) + JSON data + BIN chunk header (8) + BIN data | ||
| const totalLength = 12 + 8 + jsonChunkLength + 8 + binChunkLength; | ||
| const buffer = new ArrayBuffer(totalLength); | ||
| const view = new DataView(buffer); | ||
| const byteArray = new Uint8Array(buffer); | ||
| let offset = 0; | ||
|
|
||
| // GLB header | ||
| view.setUint32(offset, 0x46546C67, true); offset += 4; // magic: "glTF" | ||
| view.setUint32(offset, 2, true); offset += 4; // version: 2 | ||
| view.setUint32(offset, totalLength, true); offset += 4; // total length | ||
|
|
||
| // JSON chunk header | ||
| view.setUint32(offset, jsonChunkLength, true); offset += 4; | ||
| view.setUint32(offset, 0x4E4F534A, true); offset += 4; // type: "JSON" | ||
|
|
||
| // JSON chunk data | ||
| byteArray.set(jsonBytes, offset); offset += jsonBytes.length; | ||
| for (let i = 0; i < jsonPadding; i++) { | ||
| byteArray[offset++] = 0x20; | ||
| } | ||
|
|
||
| // BIN chunk header | ||
| view.setUint32(offset, binChunkLength, true); offset += 4; | ||
| view.setUint32(offset, 0x004E4942, true); offset += 4; // type: "BIN\0" | ||
|
|
||
| // BIN chunk data: positions then indices | ||
| byteArray.set(new Uint8Array(positions.buffer, positions.byteOffset, positionsByteLength), offset); | ||
| offset += positionsByteLength; | ||
| byteArray.set(new Uint8Array(indices.buffer, indices.byteOffset, indicesByteLength), offset); | ||
|
|
||
| return byteArray; | ||
| } | ||
|
|
||
| export { buildCollisionGlb }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.