Skip to content
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

fix: Load boundingInfo when use KHR_draco_mesh_compression #15882

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/dev/core/src/Meshes/Compression/dracoCompression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { BoundingInfo } from "../../Culling/boundingInfo";
import { Tools } from "../../Misc/tools";
import { AutoReleaseWorkerPool } from "../../Misc/workerPool";
import type { Nullable } from "../../types";
Expand Down Expand Up @@ -428,10 +429,15 @@ export class DracoCompression implements IDisposable {
scene: Scene,
data: ArrayBuffer | ArrayBufferView,
attributes: { [kind: string]: number },
gltfNormalizedOverride: { [kind: string]: boolean }
gltfNormalizedOverride: { [kind: string]: boolean },
boundingInfo: Nullable<BoundingInfo>
): Promise<Geometry> {
const meshData = await this.decodeMeshToMeshDataAsync(data, attributes, gltfNormalizedOverride);
const geometry = new Geometry(name, scene);
if (boundingInfo) {
geometry._boundingInfo = boundingInfo;
geometry.useBoundingInfoFromGeometry = true;
}
if (meshData.indices) {
geometry.setIndices(meshData.indices);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MeshPrimitiveMode } from "babylonjs-gltf2interface";
import type { IKHRDracoMeshCompression } from "babylonjs-gltf2interface";
import type { IMeshPrimitive, IBufferView } from "../glTFLoaderInterfaces";
import type { IGLTFLoaderExtension } from "../glTFLoaderExtension";
import { GLTFLoader, ArrayItem } from "../glTFLoader";
import { GLTFLoader, ArrayItem, LoadBoundingInfoFromPositionAccessor } from "../glTFLoader";
import { registerGLTFExtension, unregisterGLTFExtension } from "../glTFLoaderExtensionRegistry";

const NAME = "KHR_draco_mesh_compression";
Expand Down Expand Up @@ -120,9 +120,14 @@ export class KHR_draco_mesh_compression implements IGLTFLoaderExtension {
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}`);
});
const positionAccessor = ArrayItem.TryGet(this._loader.gltf.accessors, primitive.attributes["POSITION"]);
const babylonBoundingInfo =
!this._loader.parent.alwaysComputeBoundingBox && !babylonMesh.skeleton && positionAccessor ? LoadBoundingInfoFromPositionAccessor(positionAccessor) : null;
return dracoCompression
._decodeMeshToGeometryForGltfAsync(babylonMesh.name, this._loader.babylonScene, data, attributes, normalized, babylonBoundingInfo)
.catch((error) => {
throw new Error(`${context}: ${error.message}`);
});
});
}

Expand Down
59 changes: 35 additions & 24 deletions packages/dev/loaders/src/glTF/2.0/glTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ export interface IAnimationTargetInfo {
properties: Array<AnimationPropertyInfo>;
}

/** @internal */
export function LoadBoundingInfoFromPositionAccessor(accessor: IAccessor): Nullable<BoundingInfo> {
if (accessor.min && accessor.max) {
const minArray = accessor.min as [number, number, number];
const maxArray = accessor.max as [number, number, number];
const minVector = TmpVectors.Vector3[0].copyFromFloats(minArray[0], minArray[1], minArray[2]);
const maxVector = TmpVectors.Vector3[1].copyFromFloats(maxArray[0], maxArray[1], maxArray[2]);
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;
minVector.scaleInPlace(oneOverDivider);
maxVector.scaleInPlace(oneOverDivider);
}
return new BoundingInfo(minVector, maxVector);
}
return null;
}

/**
* The glTF 2.0 loader
*/
Expand Down Expand Up @@ -1118,30 +1150,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 = LoadBoundingInfoFromPositionAccessor(accessor);
if (babylonBoundingInfo) {
babylonGeometry._boundingInfo = babylonBoundingInfo;
babylonGeometry.useBoundingInfoFromGeometry = true;
}
}
Expand Down