Skip to content

Commit

Permalink
order draco vertex data correctly (#5346)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimbuck authored and Martin Valigursky committed May 23, 2023
1 parent d796969 commit 5e006b9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/framework/parsers/draco-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class JobQueue {
if (callback) {
callback(data.error, {
indices: data.indices,
vertices: data.vertices
vertices: data.vertices,
attributes: data.attributes
});
}
this.jobCallbacks.delete(data.jobId);
Expand Down
6 changes: 5 additions & 1 deletion src/framework/parsers/draco-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ function DracoWorker(jsUrl, wasmUrl) {
return (attributeOrder[a.attribute_type()] ?? attributeOrder.length) - (attributeOrder[b.attribute_type()] ?? attributeOrder.length);
});

// store attribute order by unique_id
result.attributes = attributes.map(a => a.unique_id());

// calculate total vertex size and attribute offsets
let totalVertexSize = 0;
const offsets = attributes.map((a) => {
Expand Down Expand Up @@ -226,7 +229,8 @@ function DracoWorker(jsUrl, wasmUrl) {
jobId: data.jobId,
error: result.error,
indices: result.indices,
vertices: result.vertices
vertices: result.vertices,
attributes: result.attributes
}, [result.indices, result.vertices].filter(t => t != null));
};

Expand Down
20 changes: 13 additions & 7 deletions src/framework/parsers/glb-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,13 +704,6 @@ const createDracoMesh = (device, primitive, accessors, bufferViews, meshVariants
});
}

// sort vertex elements by engine-ideal order
vertexDesc.sort((lhs, rhs) => {
return attributeOrder[lhs.semantic] - attributeOrder[rhs.semantic];
});

const vertexFormat = new VertexFormat(device, vertexDesc);

promises.push(new Promise((resolve, reject) => {
// decode draco data
const dracoExt = primitive.extensions.KHR_draco_mesh_compression;
Expand All @@ -719,6 +712,19 @@ const createDracoMesh = (device, primitive, accessors, bufferViews, meshVariants
console.log(err);
reject(err);
} else {
// worker reports order of attributes as array of attribute unique_id
const order = { };
for (const [name, index] of Object.entries(dracoExt.attributes)) {
order[gltfToEngineSemanticMap[name]] = decompressedData.attributes.indexOf(index);
}

// order vertexDesc
vertexDesc.sort((a, b) => {
return order[a.semantic] - order[b.semantic];
});

const vertexFormat = new VertexFormat(device, vertexDesc);

// create vertex buffer
const numVertices = decompressedData.vertices.byteLength / vertexFormat.size;
const indexFormat = numVertices <= 65535 ? INDEXFORMAT_UINT16 : INDEXFORMAT_UINT32;
Expand Down

0 comments on commit 5e006b9

Please sign in to comment.