|
@@ -1,4 +1,4 @@
|
|
|
-import { Vector3, Quaternion, Matrix } from 'babylonjs/Maths/math.vector';
|
|
|
+import { Vector3, Quaternion } from 'babylonjs/Maths/math.vector';
|
|
|
import { InstancedMesh } from 'babylonjs/Meshes/instancedMesh';
|
|
|
import { Mesh } from 'babylonjs/Meshes/mesh';
|
|
|
import { TransformNode } from "babylonjs/Meshes/transformNode";
|
|
@@ -10,18 +10,12 @@ import { INode } from "../glTFLoaderInterfaces";
|
|
|
import { AbstractMesh } from 'babylonjs/Meshes/abstractMesh';
|
|
|
|
|
|
const NAME = "EXT_mesh_gpu_instancing";
|
|
|
-const useThinInstances = true;
|
|
|
|
|
|
interface IEXTMeshGpuInstancing {
|
|
|
mesh?: number;
|
|
|
attributes: { [name: string]: number };
|
|
|
}
|
|
|
|
|
|
-const T = new Vector3(0, 0, 0);
|
|
|
-const R = new Quaternion(0, 0, 0, 1);
|
|
|
-const S = new Vector3(1, 1, 1);
|
|
|
-const M = new Matrix();
|
|
|
-
|
|
|
/**
|
|
|
* [Proposed Specification](https://github.com/KhronosGroup/glTF/pull/1691)
|
|
|
* [Playground Sample](https://playground.babylonjs.com/#QFIGLW#9)
|
|
@@ -61,10 +55,8 @@ export class EXT_mesh_gpu_instancing implements IGLTFLoaderExtension {
|
|
|
}
|
|
|
|
|
|
// Hide the source meshes.
|
|
|
- if (!useThinInstances) {
|
|
|
- for (const babylonMesh of node._primitiveBabylonMeshes) {
|
|
|
- babylonMesh.isVisible = false;
|
|
|
- }
|
|
|
+ for (const babylonMesh of node._primitiveBabylonMeshes) {
|
|
|
+ babylonMesh.isVisible = false;
|
|
|
}
|
|
|
|
|
|
const promises = new Array<Promise<Nullable<Float32Array>>>();
|
|
@@ -94,49 +86,25 @@ export class EXT_mesh_gpu_instancing implements IGLTFLoaderExtension {
|
|
|
return promise;
|
|
|
}
|
|
|
|
|
|
- if (!useThinInstances) {
|
|
|
- const digitLength = instanceCount.toString().length;
|
|
|
- for (let i = 0; i < instanceCount; ++i) {
|
|
|
- for (const babylonMesh of node._primitiveBabylonMeshes!) {
|
|
|
- const instanceName = `${babylonMesh.name || babylonMesh.id}_${StringTools.PadNumber(i, digitLength)}`;
|
|
|
- const babylonInstancedMesh = (babylonMesh as (InstancedMesh | Mesh)).createInstance(instanceName);
|
|
|
- babylonInstancedMesh.setParent(babylonMesh);
|
|
|
- }
|
|
|
+ const digitLength = instanceCount.toString().length;
|
|
|
+ for (let i = 0; i < instanceCount; ++i) {
|
|
|
+ for (const babylonMesh of node._primitiveBabylonMeshes!) {
|
|
|
+ const instanceName = `${babylonMesh.name || babylonMesh.id}_${StringTools.PadNumber(i, digitLength)}`;
|
|
|
+ const babylonInstancedMesh = (babylonMesh as (InstancedMesh | Mesh)).createInstance(instanceName);
|
|
|
+ babylonInstancedMesh.setParent(babylonMesh);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return promise.then((babylonTransformNode) => {
|
|
|
return Promise.all(promises).then(([translationBuffer, rotationBuffer, scaleBuffer]) => {
|
|
|
- if (!useThinInstances) {
|
|
|
- for (const babylonMesh of node._primitiveBabylonMeshes!) {
|
|
|
- const babylonInstancedMeshes = babylonMesh.getChildMeshes(true, (node) => (node as AbstractMesh).isAnInstance);
|
|
|
- for (let i = 0; i < instanceCount; ++i) {
|
|
|
- const babylonInstancedMesh = babylonInstancedMeshes[i];
|
|
|
- translationBuffer && Vector3.FromArrayToRef(translationBuffer, i * 3, babylonInstancedMesh.position);
|
|
|
- rotationBuffer && Quaternion.FromArrayToRef(rotationBuffer, i * 4, babylonInstancedMesh.rotationQuaternion!);
|
|
|
- scaleBuffer && Vector3.FromArrayToRef(scaleBuffer, i * 3, babylonInstancedMesh.scaling);
|
|
|
- babylonInstancedMesh.refreshBoundingInfo();
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- const matrices = new Float32Array(instanceCount * 16);
|
|
|
-
|
|
|
- T.copyFromFloats(0, 0, 0);
|
|
|
- R.copyFromFloats(0, 0, 0, 1);
|
|
|
- S.copyFromFloats(1, 1, 1);
|
|
|
-
|
|
|
+ for (const babylonMesh of node._primitiveBabylonMeshes!) {
|
|
|
+ const babylonInstancedMeshes = babylonMesh.getChildMeshes(true, (node) => (node as AbstractMesh).isAnInstance);
|
|
|
for (let i = 0; i < instanceCount; ++i) {
|
|
|
- translationBuffer && Vector3.FromArrayToRef(translationBuffer, i * 3, T);
|
|
|
- rotationBuffer && Quaternion.FromArrayToRef(rotationBuffer, i * 4, R);
|
|
|
- scaleBuffer && Vector3.FromArrayToRef(scaleBuffer, i * 3, S);
|
|
|
-
|
|
|
- Matrix.ComposeToRef(S, R, T, M);
|
|
|
-
|
|
|
- M.copyToArray(matrices, i * 16);
|
|
|
- }
|
|
|
-
|
|
|
- for (const babylonMesh of node._primitiveBabylonMeshes!) {
|
|
|
- (babylonMesh as Mesh).setThinInstanceBuffer("matrix", matrices);
|
|
|
+ const babylonInstancedMesh = babylonInstancedMeshes[i];
|
|
|
+ translationBuffer && Vector3.FromArrayToRef(translationBuffer, i * 3, babylonInstancedMesh.position);
|
|
|
+ rotationBuffer && Quaternion.FromArrayToRef(rotationBuffer, i * 4, babylonInstancedMesh.rotationQuaternion!);
|
|
|
+ scaleBuffer && Vector3.FromArrayToRef(scaleBuffer, i * 3, babylonInstancedMesh.scaling);
|
|
|
+ babylonInstancedMesh.refreshBoundingInfo();
|
|
|
}
|
|
|
}
|
|
|
|