@@ -9,9 +9,11 @@ import BufferUsage from "../../Renderer/BufferUsage.js";
99import ShaderDestination from "../../Renderer/ShaderDestination.js" ;
1010import InstancingStageCommon from "../../Shaders/Model/InstancingStageCommon.js" ;
1111import RuntimeModelInstancingPipelineStageVS from "../../Shaders/Model/RuntimeModelInstancingPipelineStageVS.js" ;
12+ import SceneMode from "../SceneMode.js" ;
1213
1314const nodeTransformScratch = new Matrix4 ( ) ;
1415const relativeScaledTransformScratch = new Matrix4 ( ) ;
16+ const scratchCartesian3 = new Cartesian3 ( ) ;
1517
1618/**
1719 * The runtime model instancing pipeline stage is responsible for handling GPU mesh instancing
@@ -84,6 +86,20 @@ RuntimeModelInstancingPipelineStage._getTransformsTypedArray = function (
8486 const count = modelInstances . length ;
8587 const transformsTypedArray = new Float32Array ( count * elements ) ;
8688
89+ const useBoundingSphere2D =
90+ ! frameState . scene3DOnly && model . sceneGraph . _projectTo2D ;
91+ const useModelMatrix2D =
92+ frameState . mode !== SceneMode . SCENE3D && ! useBoundingSphere2D ;
93+
94+ const modelTranslation = Matrix4 . getTranslation (
95+ model . modelMatrix ,
96+ scratchCartesian3 ,
97+ ) ;
98+ let earthCenteredInstances = false ;
99+ if ( Cartesian3 . equals ( modelTranslation , Cartesian3 . ZERO ) ) {
100+ earthCenteredInstances = true ;
101+ }
102+
87103 for ( let i = 0 ; i < count ; i ++ ) {
88104 const modelInstance = modelInstances [ i ] ;
89105 if ( ! defined ( modelInstance ) ) {
@@ -110,7 +126,10 @@ RuntimeModelInstancingPipelineStage._getTransformsTypedArray = function (
110126 transformsTypedArray [ offset + 10 ] = transform [ 10 ] ;
111127 transformsTypedArray [ offset + 11 ] = transform [ 14 ] ;
112128
113- const translation = modelInstance . center ?? Cartesian3 . ZERO ;
129+ let translation = modelInstance . center ?? Cartesian3 . ZERO ;
130+ if ( useModelMatrix2D && earthCenteredInstances ) {
131+ translation = modelInstance . getCenter2D ( frameState . mapProjection ) ;
132+ }
114133
115134 EncodedCartesian3 . writeElements (
116135 translation ,
0 commit comments