Skip to content

Commit d697bde

Browse files
committed
earth centered 2D wip
1 parent 883932d commit d697bde

File tree

3 files changed

+101
-16
lines changed

3 files changed

+101
-16
lines changed

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ 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";
1617

17-
const modelMatrixScratch = new Matrix4();
18+
const scratchCartesian3 = new Cartesian3();
1819

1920
/**
2021
* Internal functions to build draw commands for models.
@@ -141,6 +142,8 @@ function buildDrawCommandForModel(
141142
sceneGraph,
142143
runtimeNode,
143144
primitiveBoundingSphere,
145+
frameState.mapProjection,
146+
useModelMatrix2D,
144147
);
145148

146149
// Initialize render state with default values
@@ -216,15 +219,23 @@ ModelDrawCommands.createCommandModelMatrix = function (
216219

217220
if (sceneGraph.hasInstances) {
218221
if (use2D) {
219-
const inverseAxisCorrection = Matrix4.inverse(
220-
sceneGraph._axisCorrectionMatrix,
221-
modelMatrixScratch,
222-
);
223-
return Matrix4.multiplyTransformation(
224-
sceneGraph._computedModelMatrix2D,
225-
inverseAxisCorrection,
226-
result,
227-
);
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+
// );
238+
return sceneGraph._modelMatrix2D;
228239
}
229240
return modelMatrix;
230241
}
@@ -260,13 +271,30 @@ ModelDrawCommands.createCommandBoundingSphere = function (
260271
sceneGraph,
261272
runtimeNode,
262273
primitiveBoundingSphere,
274+
mapProjection,
275+
useModelMatrix2D = false,
263276
result,
264277
) {
265278
if (!defined(result)) {
266279
result = new BoundingSphere();
267280
}
268281

269282
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+
270298
const instanceBoundingSpheres = [];
271299

272300
for (const modelInstance of sceneGraph.modelInstances._instances) {
@@ -275,6 +303,8 @@ ModelDrawCommands.createCommandBoundingSphere = function (
275303
sceneGraph,
276304
runtimeNode,
277305
primitiveBoundingSphere,
306+
useInstanceTransform2D,
307+
mapProjection,
278308
);
279309
instanceBoundingSpheres.push(boundingSphere);
280310
}
@@ -327,6 +357,8 @@ ModelDrawCommands.updateDrawCommand = function (
327357
sceneGraph,
328358
runtimeNode,
329359
primitiveBoundingSphere,
360+
frameState.mapProjection,
361+
useModelMatrix2D,
330362
drawCommand.boundingVolume,
331363
);
332364
};

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import Matrix3 from "../../Core/Matrix3.js";
55
import Matrix4 from "../../Core/Matrix4.js";
66
import TranslationRotationScale from "../../Core/TranslationRotationScale.js";
77
import Quaternion from "../../Core/Quaternion.js";
8+
import Transforms from "../../Core/Transforms.js";
89

910
const scratchTranslationRotationScale = new TranslationRotationScale();
1011
const scratchRotation = new Matrix3();
11-
const scratchBoundingSphereTransform = new Matrix4();
12+
const scratchTransform = new Matrix4();
1213
const scratchBoundingSphere = new BoundingSphere();
1314
/**
1415
* 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.
@@ -205,8 +206,9 @@ class ModelInstance {
205206
sceneGraph,
206207
runtimeNode,
207208
primitiveBoundingSphere,
209+
false,
210+
undefined,
208211
);
209-
210212
instanceBoundingSpheres.push(boundingSphere);
211213
}
212214
}
@@ -273,6 +275,8 @@ class ModelInstance {
273275
sceneGraph,
274276
runtimeNode,
275277
primitiveBoundingSphere,
278+
useInstanceTransform2D,
279+
mapProjection,
276280
result,
277281
) {
278282
result = result ?? new BoundingSphere();
@@ -283,11 +287,34 @@ class ModelInstance {
283287
runtimeNode.computedTransform,
284288
new Matrix4(),
285289
);
286-
const primitiveMatrix = this.computeModelMatrix(
290+
291+
let instanceTransform;
292+
if (useInstanceTransform2D) {
293+
instanceTransform = Transforms.basisTo2D(
294+
mapProjection,
295+
this.transform,
296+
scratchTransform,
297+
);
298+
299+
instanceTransform = Matrix4.multiplyTransformation(
300+
instanceTransform,
301+
sceneGraph._axisCorrectionMatrix,
302+
scratchTransform,
303+
);
304+
} else {
305+
instanceTransform = Matrix4.multiplyTransformation(
306+
this.transform,
307+
rootTransform,
308+
scratchTransform,
309+
);
310+
}
311+
312+
const primitiveMatrix = Matrix4.multiplyTransformation(
287313
modelMatrix,
288-
rootTransform,
289-
scratchBoundingSphereTransform,
314+
instanceTransform,
315+
scratchTransform,
290316
);
317+
291318
return BoundingSphere.transform(
292319
primitiveBoundingSphere,
293320
primitiveMatrix,

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ function ModelSceneGraph(options) {
153153
this._computedModelMatrix = Matrix4.clone(Matrix4.IDENTITY);
154154
this._computedModelMatrix2D = Matrix4.clone(Matrix4.IDENTITY);
155155

156+
// Model matrix in 2D without axis correction, etc.
157+
this._modelMatrix2D = Matrix4.clone(Matrix4.IDENTITY);
158+
156159
this._axisCorrectionMatrix = Matrix4.clone(Matrix4.IDENTITY);
157160

158161
// Store articulations from the AGI_articulations extension
@@ -476,7 +479,7 @@ function computeModelMatrix(
476479

477480
const scratchComputedTranslation = new Cartesian3();
478481

479-
function computeModelMatrix2D(sceneGraph, frameState) {
482+
function updateComputedModelMatrix2D(sceneGraph, frameState) {
480483
const computedModelMatrix = sceneGraph._computedModelMatrix;
481484
const translation = Matrix4.getTranslation(
482485
computedModelMatrix,
@@ -513,6 +516,28 @@ function computeModelMatrix2D(sceneGraph, frameState) {
513516
);
514517
}
515518

519+
function computeModelMatrix2D(sceneGraph, frameState) {
520+
const modelMatrix = sceneGraph._model.modelMatrix;
521+
// sceneGraph._modelMatrix2D = Transforms.basisTo2D(
522+
// frameState.mapProjection,
523+
// modelMatrix,
524+
// sceneGraph._modelMatrix2D,
525+
// );
526+
const translation = Matrix4.getTranslation(
527+
modelMatrix,
528+
scratchComputedTranslation,
529+
);
530+
if (!Cartesian3.equals(translation, Cartesian3.ZERO)) {
531+
sceneGraph._modelMatrix2D = Transforms.basisTo2D(
532+
frameState.mapProjection,
533+
modelMatrix,
534+
sceneGraph._modelMatrix2D,
535+
);
536+
} else {
537+
return Matrix4.IDENTITY;
538+
}
539+
}
540+
516541
/**
517542
* Recursively traverse through the nodes in the scene graph to create
518543
* their runtime versions, using a post-order depth-first traversal.
@@ -955,6 +980,7 @@ ModelSceneGraph.prototype.updateModelMatrix = function (
955980
);
956981

957982
if (frameState.mode !== SceneMode.SCENE3D) {
983+
updateComputedModelMatrix2D(this, frameState);
958984
computeModelMatrix2D(this, frameState);
959985
}
960986

0 commit comments

Comments
 (0)