Skip to content

Commit fdb68e1

Browse files
committed
unify earth and non earth centered code paths
1 parent 90b3e09 commit fdb68e1

File tree

3 files changed

+26
-60
lines changed

3 files changed

+26
-60
lines changed

packages/engine/Source/Scene/Model/ModelDrawCommands.js

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import ModelVS from "../../Shaders/Model/ModelVS.js";
1313
import ModelFS from "../../Shaders/Model/ModelFS.js";
1414
import ModelUtility from "./ModelUtility.js";
1515
import DeveloperError from "../../Core/DeveloperError.js";
16-
import Cartesian3 from "../../Core/Cartesian3.js";
17-
18-
const scratchCartesian3 = new Cartesian3();
1916

2017
/**
2118
* Internal functions to build draw commands for models.
@@ -138,7 +135,7 @@ function buildDrawCommandForModel(
138135
? runtimePrimitive.boundingSphere2D
139136
: runtimePrimitive.boundingSphere;
140137
const boundingSphere = ModelDrawCommands.createCommandBoundingSphere(
141-
modelMatrix,
138+
model.modelMatrix,
142139
sceneGraph,
143140
runtimeNode,
144141
primitiveBoundingSphere,
@@ -219,22 +216,6 @@ ModelDrawCommands.createCommandModelMatrix = function (
219216

220217
if (sceneGraph.hasInstances) {
221218
if (use2D) {
222-
// earth centered model
223-
// const translation = Matrix4.getTranslation(modelMatrix, scratchCartesian3);
224-
// if (Cartesian3.equals(translation, Cartesian3.ZERO)) {
225-
// return sceneGraph._computedModelMatrix2D
226-
// }
227-
228-
//model centered on earth surface
229-
// const inverseAxisCorrection = Matrix4.inverse(
230-
// sceneGraph._axisCorrectionMatrix,
231-
// modelMatrixScratch,
232-
// );
233-
// return Matrix4.multiplyTransformation(
234-
// sceneGraph._computedModelMatrix2D,
235-
// inverseAxisCorrection,
236-
// result,
237-
// );
238219
return sceneGraph._modelMatrix2D;
239220
}
240221
return modelMatrix;
@@ -280,21 +261,6 @@ ModelDrawCommands.createCommandBoundingSphere = function (
280261
}
281262

282263
if (sceneGraph.hasInstances) {
283-
let useInstanceTransform2D = false;
284-
285-
const translation = Matrix4.getTranslation(
286-
commandModelMatrix,
287-
scratchCartesian3,
288-
);
289-
if (useModelMatrix2D && Cartesian3.equals(translation, Cartesian3.ZERO)) {
290-
useInstanceTransform2D = true;
291-
// commandModelMatrix = Matrix4.multiplyTransformation(
292-
// commandModelMatrix,
293-
// sceneGraph._axisCorrectionMatrix,
294-
// modelMatrixScratch
295-
// )
296-
}
297-
298264
const instanceBoundingSpheres = [];
299265

300266
for (const modelInstance of sceneGraph.modelInstances._instances) {
@@ -303,7 +269,7 @@ ModelDrawCommands.createCommandBoundingSphere = function (
303269
sceneGraph,
304270
runtimeNode,
305271
primitiveBoundingSphere,
306-
useInstanceTransform2D,
272+
useModelMatrix2D,
307273
mapProjection,
308274
);
309275
instanceBoundingSpheres.push(boundingSphere);
@@ -353,7 +319,7 @@ ModelDrawCommands.updateDrawCommand = function (
353319
? runtimePrimitive.boundingSphere2D
354320
: runtimePrimitive.boundingSphere;
355321
ModelDrawCommands.createCommandBoundingSphere(
356-
modelMatrix,
322+
model.modelMatrix,
357323
sceneGraph,
358324
runtimeNode,
359325
primitiveBoundingSphere,

packages/engine/Source/Scene/Model/ModelInstance.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const scratchTranslationRotationScale = new TranslationRotationScale();
1111
const scratchRotation = new Matrix3();
1212
const scratchTransform = new Matrix4();
1313
const scratchBoundingSphere = new BoundingSphere();
14-
const scratchCartesian3 = new Cartesian3();
1514
/**
1615
* A copy of a {@link Model} mesh, known as an instance, used for rendering multiple copies with GPU instancing. Instancing is useful for efficiently rendering a large number of the same model, such as trees in a forest or vehicles in a parking lot.
1716
* @see {@link ModelInstanceCollection} for a collection of instances.
@@ -194,19 +193,15 @@ class ModelInstance {
194193
* viewer.camera.flyToBoundingSphere(boundingSphere);
195194
*/
196195
getBoundingSphere(model, useBoundingSphere2D, mapProjection, result) {
197-
let modelMatrix = model.modelMatrix;
198196
const sceneGraph = model.sceneGraph;
199-
if (useBoundingSphere2D) {
200-
modelMatrix = sceneGraph._modelMatrix2D;
201-
}
202197
const instanceBoundingSpheres = [];
203198

204199
for (const runtimeNode of sceneGraph._runtimeNodes) {
205200
const runtimePrimitives = runtimeNode.runtimePrimitives;
206201
for (const runtimePrimitive of runtimePrimitives) {
207202
const primitiveBoundingSphere = runtimePrimitive.boundingSphere;
208203
const boundingSphere = this.getPrimitiveBoundingSphere(
209-
modelMatrix,
204+
model.modelMatrix,
210205
sceneGraph,
211206
runtimeNode,
212207
primitiveBoundingSphere,
@@ -318,17 +313,17 @@ class ModelInstance {
318313
);
319314

320315
let instanceTransform;
321-
const modelTranslation = Matrix4.getTranslation(
322-
modelMatrix,
323-
scratchCartesian3,
324-
);
325-
if (
326-
useInstanceTransform2D &
327-
Cartesian3.equals(modelTranslation, Cartesian3.ZERO)
328-
) {
329-
instanceTransform = Transforms.basisTo2D(
330-
mapProjection,
316+
let primitiveMatrix;
317+
318+
if (useInstanceTransform2D) {
319+
const combinedTransform = Matrix4.multiplyTransformation(
320+
modelMatrix,
331321
this.transform,
322+
new Matrix4(),
323+
);
324+
primitiveMatrix = Transforms.basisTo2D(
325+
mapProjection,
326+
combinedTransform,
332327
scratchTransform,
333328
);
334329
} else {
@@ -337,14 +332,13 @@ class ModelInstance {
337332
rootTransform,
338333
scratchTransform,
339334
);
335+
primitiveMatrix = Matrix4.multiplyTransformation(
336+
modelMatrix,
337+
instanceTransform,
338+
scratchTransform,
339+
);
340340
}
341341

342-
const primitiveMatrix = Matrix4.multiplyTransformation(
343-
modelMatrix,
344-
instanceTransform,
345-
scratchTransform,
346-
);
347-
348342
return BoundingSphere.transform(
349343
primitiveBoundingSphere,
350344
primitiveMatrix,

packages/engine/Source/Scene/Model/ModelSceneGraph.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ function ModelSceneGraph(options) {
155155

156156
// Model matrix in 2D without axis correction, etc.
157157
this._modelMatrix2D = Matrix4.clone(Matrix4.IDENTITY);
158+
this._inverseModelMatrix2D = Matrix4.clone(Matrix4.IDENTITY);
158159

159160
this._axisCorrectionMatrix = Matrix4.clone(Matrix4.IDENTITY);
160161

@@ -528,8 +529,13 @@ function computeModelMatrix2D(sceneGraph, frameState) {
528529
modelMatrix,
529530
sceneGraph._modelMatrix2D,
530531
);
532+
sceneGraph._inverseModelMatrix2D = Matrix4.inverseTransformation(
533+
sceneGraph._modelMatrix2D,
534+
sceneGraph._inverseModelMatrix2D,
535+
);
531536
} else {
532-
return Matrix4.IDENTITY;
537+
sceneGraph._modelMatrix2D = Matrix4.IDENTITY;
538+
sceneGraph._inverseModelMatrix2D = Matrix4.IDENTITY;
533539
}
534540
}
535541

0 commit comments

Comments
 (0)