Skip to content

Commit

Permalink
update glTF parser to handle empties
Browse files Browse the repository at this point in the history
  • Loading branch information
sdumetz committed Nov 22, 2024
1 parent 28a3828 commit d72ffc3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 14 additions & 0 deletions source/server/utils/glTF.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,19 @@ describe("parse_gltf()", function(){
bounds: {min: [-0.5,0,-0.5], max: [0.5,0.20000000298023224,0.5]},
});
})

it("handles empties", async function(){
let gltf = {
"asset":{"generator":"Khronos glTF Blender I/O v4.2.69","version":"2.0"},
"scene":0,
"scenes":[{"name":"Scene","nodes":[0]}],
"nodes":[{"name":"Empty"}]
};
let res = parse_glTF(gltf);
expect(res).to.deep.equal({
meshes:[],
bounds: {min: [0,0,0], max: [0,0,0]},
});
})
// @todo add more edge-cases from https://github.com/KhronosGroup/glTF-Sample-Models
})
12 changes: 6 additions & 6 deletions source/server/utils/glTF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ interface GlbDescription extends SceneDescription{
}

export interface JSONglTF extends Record<string,any>{
meshes: Mesh[];
accessors :Accessor[];
meshes?: Mesh[];
accessors ?:Accessor[];
}

function asBounds(a :Accessor) :Bounds{
Expand All @@ -70,12 +70,12 @@ function mergeBounds(a:Bounds, b:Bounds) :Bounds{
* Float values are rounded to single precision.
* @see https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/Specification.adoc#3625-accessors-bounds
*/
export function parse_glTF(gltf :JSONglTF) :SceneDescription {
export function parse_glTF({meshes = [], accessors = []}:JSONglTF) :SceneDescription {
let scene :SceneDescription = {
meshes:[],
bounds: {min:[0,0,0], max:[0,0,0]},
};
for(let mesh of gltf.meshes){
for(let mesh of meshes){
let out :MeshDescription = {
position: [0, 0, 0],
bounds: {min:[0,0,0], max:[0,0,0]},
Expand All @@ -85,9 +85,9 @@ export function parse_glTF(gltf :JSONglTF) :SceneDescription {
for(let primitive of mesh.primitives){
let positions = [primitive.attributes.POSITION, ...(primitive.targets ?? []).map(t=>t.POSITION)]
for (let positionIndex of positions){
let position :Bounds|Accessor = gltf.accessors[positionIndex];
let position :Bounds|Accessor = accessors[positionIndex];
out.bounds = mergeBounds(out.bounds, asBounds(position));
out.numFaces+= gltf.accessors[primitive.indices].count /3; //every 3 indices form a triangle
out.numFaces+= accessors[primitive.indices].count /3; //every 3 indices form a triangle
}
}
scene.meshes.push(out);
Expand Down

0 comments on commit d72ffc3

Please sign in to comment.