Skip to content

Commit

Permalink
fix: Load boundingInfo when use KHR_draco_mesh_compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Starryi committed Nov 23, 2024
1 parent 73ab4d6 commit 8ee8466
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,25 @@ export class KHR_draco_mesh_compression implements IGLTFLoaderExtension {
loadAttribute("WEIGHTS_0", VertexBuffer.MatricesWeightsKind);
loadAttribute("COLOR_0", VertexBuffer.ColorKind);

const positionAccessor = ArrayItem.TryGet(this._loader.gltf.accessors, primitive.attributes["POSITION"]);

const bufferView = ArrayItem.Get(extensionContext, this._loader.gltf.bufferViews, extension.bufferView) as IBufferViewDraco;
if (!bufferView._dracoBabylonGeometry) {
bufferView._dracoBabylonGeometry = this._loader.loadBufferViewAsync(`/bufferViews/${bufferView.index}`, bufferView).then((data) => {
const dracoCompression = this.dracoCompression || DracoCompression.Default;
return dracoCompression._decodeMeshToGeometryForGltfAsync(babylonMesh.name, this._loader.babylonScene, data, attributes, normalized).catch((error) => {
throw new Error(`${context}: ${error.message}`);
});
return dracoCompression
._decodeMeshToGeometryForGltfAsync(babylonMesh.name, this._loader.babylonScene, data, attributes, normalized)
.catch((error) => {
throw new Error(`${context}: ${error.message}`);
})
.then((babylonGeometry) => {
const babylonBoundingInfo = positionAccessor && this._loader._loadBoundingInfo(positionAccessor);
if (babylonBoundingInfo) {
babylonGeometry._boundingInfo = babylonBoundingInfo;
babylonGeometry.useBoundingInfoFromGeometry = true;
}
return babylonGeometry;
});
});
}

Expand Down
56 changes: 32 additions & 24 deletions packages/dev/loaders/src/glTF/2.0/glTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,30 +1118,9 @@ export class GLTFLoader implements IGLTFLoader {
promises.push(
this._loadVertexAccessorAsync(`/accessors/${accessor.index}`, accessor, kind).then((babylonVertexBuffer) => {
if (babylonVertexBuffer.getKind() === VertexBuffer.PositionKind && !this.parent.alwaysComputeBoundingBox && !babylonMesh.skeleton) {
if (accessor.min && accessor.max) {
const min = TmpVectors.Vector3[0].copyFromFloats(...(accessor.min as [number, number, number]));
const max = TmpVectors.Vector3[1].copyFromFloats(...(accessor.max as [number, number, number]));
if (accessor.normalized && accessor.componentType !== AccessorComponentType.FLOAT) {
let divider = 1;
switch (accessor.componentType) {
case AccessorComponentType.BYTE:
divider = 127.0;
break;
case AccessorComponentType.UNSIGNED_BYTE:
divider = 255.0;
break;
case AccessorComponentType.SHORT:
divider = 32767.0;
break;
case AccessorComponentType.UNSIGNED_SHORT:
divider = 65535.0;
break;
}
const oneOverDivider = 1 / divider;
min.scaleInPlace(oneOverDivider);
max.scaleInPlace(oneOverDivider);
}
babylonGeometry._boundingInfo = new BoundingInfo(min, max);
const babylonBoundingInfo = this._loadBoundingInfo(accessor);
if (babylonBoundingInfo) {
babylonGeometry._boundingInfo = babylonBoundingInfo;
babylonGeometry.useBoundingInfoFromGeometry = true;
}
}
Expand Down Expand Up @@ -1883,6 +1862,35 @@ export class GLTFLoader implements IGLTFLoader {
return bufferView._data;
}

public _loadBoundingInfo(positionAccessor: IAccessor): Nullable<BoundingInfo> {
if (positionAccessor.min && positionAccessor.max) {
const min = TmpVectors.Vector3[0].copyFromFloats(...(positionAccessor.min as [number, number, number]));
const max = TmpVectors.Vector3[1].copyFromFloats(...(positionAccessor.max as [number, number, number]));
if (positionAccessor.normalized && positionAccessor.componentType !== AccessorComponentType.FLOAT) {
let divider = 1;
switch (positionAccessor.componentType) {
case AccessorComponentType.BYTE:
divider = 127.0;
break;
case AccessorComponentType.UNSIGNED_BYTE:
divider = 255.0;
break;
case AccessorComponentType.SHORT:
divider = 32767.0;
break;
case AccessorComponentType.UNSIGNED_SHORT:
divider = 65535.0;
break;
}
const oneOverDivider = 1 / divider;
min.scaleInPlace(oneOverDivider);
max.scaleInPlace(oneOverDivider);
}
return new BoundingInfo(min, max);
}
return null;
}

private _loadAccessorAsync(context: string, accessor: IAccessor, constructor: TypedArrayConstructor): Promise<ArrayBufferView> {
if (accessor._data) {
return accessor._data;
Expand Down

0 comments on commit 8ee8466

Please sign in to comment.