From d381c1dd309fe9331d5b47e1bcf10b5f7a43c267 Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Thu, 1 Aug 2024 18:44:27 +0200 Subject: [PATCH 1/3] Draft for fixing disappearing I3DM --- .../Scene/Model/PrimitiveRenderResources.js | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/packages/engine/Source/Scene/Model/PrimitiveRenderResources.js b/packages/engine/Source/Scene/Model/PrimitiveRenderResources.js index ccf947bd3fd0..5a176d905cd0 100644 --- a/packages/engine/Source/Scene/Model/PrimitiveRenderResources.js +++ b/packages/engine/Source/Scene/Model/PrimitiveRenderResources.js @@ -5,6 +5,7 @@ import clone from "../../Core/clone.js"; import defined from "../../Core/defined.js"; import ModelUtility from "./ModelUtility.js"; import ModelLightingOptions from "./ModelLightingOptions.js"; +import Matrix4 from "../../Core/Matrix4.js"; /** * Each node may have many mesh primitives. Most model pipeline stages operate @@ -232,15 +233,67 @@ function PrimitiveRenderResources(nodeRenderResources, runtimePrimitive) { */ this.primitiveType = primitive.primitiveType; + // For computing the `positionMin` and `positionMax` properties, + // the instancing translation (if present) has to be taken into + // account. + // The `positionMin` and `positionMax` are supposed to NOT take + // the node transform into account. + // So when the instancing translations are supposed to be applied + // in world space (as indicated by instances.transformInWorldSpace), + // then the instancing translations have to be multiplied with the + // inverse of the node transform, to eventually yield the true + // minimum/maximum after applying the node transform. + + let instancingTranslationMin = this.runtimeNode.instancingTranslationMin; + let instancingTranslationMax = this.runtimeNode.instancingTranslationMax; + + //*/ + const instances = this.runtimeNode.node.instances; + if (defined(instances)) { + // TODO Is this check necessary? + if ( + !defined(instancingTranslationMin) || + !defined(instancingTranslationMax) + ) { + console.log("Something wrong, they should be defined... I guess..."); + } + if (instances.transformInWorldSpace) { + const nodeTransform = this.runtimeNode.computedTransform; + // TODO Use scratches here + const inverseNodeTransform = Matrix4.inverse( + nodeTransform, + new Matrix4() + ); + instancingTranslationMin = Matrix4.multiplyByPoint( + inverseNodeTransform, + instancingTranslationMin, + new Cartesian3() + ); + instancingTranslationMax = Matrix4.multiplyByPoint( + inverseNodeTransform, + instancingTranslationMax, + new Cartesian3() + ); + } + } + //*/ const positionMinMax = ModelUtility.getPositionMinMax( primitive, - this.runtimeNode.instancingTranslationMin, - this.runtimeNode.instancingTranslationMax + instancingTranslationMin, + instancingTranslationMax ); /** * The minimum position value for this primitive. * + * This does not take into account the transform of the node that this + * primitive is attached to. + * + * But it does take into account any possible instancing transforms, + * multiplied with the inverse of the node transform. So transforming + * this position value with the node transform will yield the actual + * minimum position, including the instancing transforms. + * * @type {Cartesian3} * @readonly * @@ -251,6 +304,14 @@ function PrimitiveRenderResources(nodeRenderResources, runtimePrimitive) { /** * The maximum position value for this primitive. * + * This does not take into account the transform of the node that this + * primitive is attached to. + * + * But it does take into account any possible instancing transforms, + * multiplied with the inverse of the node transform. So transforming + * this position value with the node transform will yield the actual + * maximum position, including the instancing transforms. + * * @type {Cartesian3} * @readonly * From 21ef2d765f6dd8a6399f31f8baa9a0592be29065 Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Thu, 1 Aug 2024 18:44:52 +0200 Subject: [PATCH 2/3] Debug output for review and tests --- .../Source/Scene/Model/buildDrawCommand.js | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/engine/Source/Scene/Model/buildDrawCommand.js b/packages/engine/Source/Scene/Model/buildDrawCommand.js index f23b712f06d4..f5a940f3ffb4 100644 --- a/packages/engine/Source/Scene/Model/buildDrawCommand.js +++ b/packages/engine/Source/Scene/Model/buildDrawCommand.js @@ -78,6 +78,27 @@ function buildDrawCommand(primitiveRenderResources, frameState) { ); } + console.log("buildDrawCommand"); + console.log(" positionMin", primitiveRenderResources.positionMin); + console.log(" positionMax", primitiveRenderResources.positionMax); + console.log( + " instancingTranslationMin ", + primitiveRenderResources.runtimeNode.instancingTranslationMin + ); + console.log( + " instancingTranslationMax ", + primitiveRenderResources.runtimeNode.instancingTranslationMax + ); + console.log( + " node scale ", + primitiveRenderResources.runtimeNode.computedTransform[0] + ); + console.log( + " instances transformInWorldSpace ", + primitiveRenderResources.runtimeNode.node?.instances?.transformInWorldSpace + ); + console.log(" radius", primitiveRenderResources.boundingSphere.radius); + // Initialize render state with default values let renderState = clone( RenderState.fromCache(primitiveRenderResources.renderStateOptions), @@ -117,7 +138,7 @@ function buildDrawCommand(primitiveRenderResources, frameState) { pickId: pickId, instanceCount: primitiveRenderResources.instanceCount, primitiveType: primitiveRenderResources.primitiveType, - debugShowBoundingVolume: model.debugShowBoundingVolume, + debugShowBoundingVolume: true, //model.debugShowBoundingVolume, castShadows: castShadows, receiveShadows: receiveShadows, }); From 8aa67fd2ca376259710aec6df65b1ae5d2beabc3 Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Thu, 1 Aug 2024 18:51:18 +0200 Subject: [PATCH 3/3] Do not modify the render resources --- packages/engine/Source/Scene/Model/buildDrawCommand.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/engine/Source/Scene/Model/buildDrawCommand.js b/packages/engine/Source/Scene/Model/buildDrawCommand.js index f5a940f3ffb4..836998d1d7fd 100644 --- a/packages/engine/Source/Scene/Model/buildDrawCommand.js +++ b/packages/engine/Source/Scene/Model/buildDrawCommand.js @@ -73,8 +73,7 @@ function buildDrawCommand(primitiveRenderResources, frameState) { boundingSphere = BoundingSphere.transform( primitiveRenderResources.boundingSphere, - modelMatrix, - primitiveRenderResources.boundingSphere + modelMatrix ); }