@luma.gl/engine 9.4.0-alpha.1 → 9.4.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dist.dev.js +138 -64
- package/dist/dist.min.js +30 -30
- package/dist/index.cjs +130 -70
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/model/model.d.ts +1 -1
- package/dist/model/model.d.ts.map +1 -1
- package/dist/passes/shader-pass-renderer.d.ts +13 -1
- package/dist/passes/shader-pass-renderer.d.ts.map +1 -1
- package/dist/passes/shader-pass-renderer.js +33 -13
- package/dist/passes/shader-pass-renderer.js.map +1 -1
- package/dist/scenegraph/group-node.d.ts +17 -1
- package/dist/scenegraph/group-node.d.ts.map +1 -1
- package/dist/scenegraph/group-node.js +29 -23
- package/dist/scenegraph/group-node.js.map +1 -1
- package/dist/scenegraph/model-node.d.ts +8 -2
- package/dist/scenegraph/model-node.d.ts.map +1 -1
- package/dist/scenegraph/model-node.js +15 -1
- package/dist/scenegraph/model-node.js.map +1 -1
- package/dist/scenegraph/scenegraph-bounds.d.ts +7 -0
- package/dist/scenegraph/scenegraph-bounds.d.ts.map +1 -0
- package/dist/scenegraph/scenegraph-bounds.js +25 -0
- package/dist/scenegraph/scenegraph-bounds.js.map +1 -0
- package/package.json +4 -4
- package/src/index.ts +6 -0
- package/src/model/model.ts +1 -1
- package/src/passes/shader-pass-renderer.ts +45 -13
- package/src/scenegraph/group-node.ts +63 -26
- package/src/scenegraph/model-node.ts +32 -3
- package/src/scenegraph/scenegraph-bounds.ts +48 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
import { Matrix4, Vector3 } from '@math.gl/core';
|
|
5
|
+
export function makeEmptyScenegraphBounds() {
|
|
6
|
+
return [
|
|
7
|
+
[Infinity, Infinity, Infinity],
|
|
8
|
+
[-Infinity, -Infinity, -Infinity]
|
|
9
|
+
];
|
|
10
|
+
}
|
|
11
|
+
export function expandScenegraphBounds(targetBounds, sourceBounds, transform) {
|
|
12
|
+
const transformMatrix = new Matrix4(transform);
|
|
13
|
+
for (let cornerIndex = 0; cornerIndex < 8; cornerIndex++) {
|
|
14
|
+
const corner = new Vector3(sourceBounds[cornerIndex & 0b001 ? 1 : 0][0], sourceBounds[cornerIndex & 0b010 ? 1 : 0][1], sourceBounds[cornerIndex & 0b100 ? 1 : 0][2]);
|
|
15
|
+
transformMatrix.transformAsPoint(corner, corner);
|
|
16
|
+
for (let componentIndex = 0; componentIndex < 3; componentIndex++) {
|
|
17
|
+
targetBounds[0][componentIndex] = Math.min(targetBounds[0][componentIndex], corner[componentIndex]);
|
|
18
|
+
targetBounds[1][componentIndex] = Math.max(targetBounds[1][componentIndex], corner[componentIndex]);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function areScenegraphBoundsDefined(bounds) {
|
|
23
|
+
return Number.isFinite(bounds[0][0]);
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=scenegraph-bounds.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scenegraph-bounds.js","sourceRoot":"","sources":["../../src/scenegraph/scenegraph-bounds.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,oCAAoC;AAEpC,OAAO,EAAC,OAAO,EAAE,OAAO,EAAC,MAAM,eAAe,CAAC;AAM/C,MAAM,UAAU,yBAAyB;IACvC,OAAO;QACL,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC;QAC9B,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC;KAClC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,sBAAsB,CACpC,YAA8B,EAC9B,YAAkC,EAClC,SAAuB;IAEvB,MAAM,eAAe,GAAG,IAAI,OAAO,CAAC,SAAS,CAAC,CAAC;IAE/C,KAAK,IAAI,WAAW,GAAG,CAAC,EAAE,WAAW,GAAG,CAAC,EAAE,WAAW,EAAE,EAAE,CAAC;QACzD,MAAM,MAAM,GAAG,IAAI,OAAO,CACxB,YAAY,CAAC,WAAW,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAC5C,YAAY,CAAC,WAAW,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAC5C,YAAY,CAAC,WAAW,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC7C,CAAC;QACF,eAAe,CAAC,gBAAgB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAEjD,KAAK,IAAI,cAAc,GAAG,CAAC,EAAE,cAAc,GAAG,CAAC,EAAE,cAAc,EAAE,EAAE,CAAC;YAClE,YAAY,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,GAAG,CACxC,YAAY,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,EAC/B,MAAM,CAAC,cAAc,CAAC,CACvB,CAAC;YACF,YAAY,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,GAAG,CACxC,YAAY,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,EAC/B,MAAM,CAAC,cAAc,CAAC,CACvB,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,MAAwB;IACjE,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACvC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luma.gl/engine",
|
|
3
|
-
"version": "9.4.0-alpha.
|
|
3
|
+
"version": "9.4.0-alpha.3",
|
|
4
4
|
"description": "3D Engine Components for luma.gl",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"prepublishOnly": "npm run build-minified-bundle && npm run build-dev-bundle"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@luma.gl/core": "~9.
|
|
44
|
-
"@luma.gl/shadertools": "~9.
|
|
43
|
+
"@luma.gl/core": "~9.4.0-alpha.1",
|
|
44
|
+
"@luma.gl/shadertools": "~9.4.0-alpha.1"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@math.gl/core": "^4.1.0",
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"@probe.gl/log": "^4.1.1",
|
|
50
50
|
"@probe.gl/stats": "^4.1.1"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "6860ad64f8eb845e4f58bfee653e58ed7371e776"
|
|
53
53
|
}
|
package/src/index.ts
CHANGED
|
@@ -53,6 +53,12 @@ export {DirectionalLightModel} from './models/directional-light-model';
|
|
|
53
53
|
|
|
54
54
|
// Scenegraph Core nodes
|
|
55
55
|
export {ScenegraphNode} from './scenegraph/scenegraph-node';
|
|
56
|
+
export type {ScenegraphBounds} from './scenegraph/scenegraph-bounds';
|
|
57
|
+
export type {
|
|
58
|
+
DepthSortedTraversalContext,
|
|
59
|
+
DepthSortedTraversalOptions,
|
|
60
|
+
GroupNodeProps
|
|
61
|
+
} from './scenegraph/group-node';
|
|
56
62
|
export {GroupNode} from './scenegraph/group-node';
|
|
57
63
|
export type {ModelNodeProps} from './scenegraph/model-node';
|
|
58
64
|
export {ModelNode} from './scenegraph/model-node';
|
package/src/model/model.ts
CHANGED
|
@@ -101,7 +101,7 @@ export type ModelProps = Omit<RenderPipelineProps, 'vs' | 'fs' | 'bindings'> & {
|
|
|
101
101
|
fs?: string | null;
|
|
102
102
|
|
|
103
103
|
/** Shadertools shader modules added to shader code. */
|
|
104
|
-
modules?: ShaderModule[];
|
|
104
|
+
modules?: ShaderModule<any, any, any>[];
|
|
105
105
|
/** Shadertools boolean or numeric preprocessor defines that configure shader code. */
|
|
106
106
|
defines?: Record<string, boolean | number>;
|
|
107
107
|
/** Reusable shader assembly plugins resolved for the active GLSL or WGSL backend. */
|
|
@@ -146,7 +146,19 @@ export class ShaderPassRenderer {
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
renderToScreen(options: ShaderPassRendererRenderOptions): boolean {
|
|
149
|
-
|
|
149
|
+
return this.encodeToScreen(this.device.commandEncoder, options);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Records the shader-pass chain and presentation into a caller-owned command encoder.
|
|
154
|
+
* The encoder must belong to this renderer's device and have no active pass. This method does
|
|
155
|
+
* not finish or submit the encoder.
|
|
156
|
+
*/
|
|
157
|
+
encodeToScreen(
|
|
158
|
+
commandEncoder: CommandEncoder,
|
|
159
|
+
options: ShaderPassRendererRenderOptions
|
|
160
|
+
): boolean {
|
|
161
|
+
const outputTexture = this.encodeToTexture(commandEncoder, options);
|
|
150
162
|
if (!outputTexture) {
|
|
151
163
|
return false;
|
|
152
164
|
}
|
|
@@ -157,8 +169,8 @@ export class ShaderPassRenderer {
|
|
|
157
169
|
// Prepare fullscreen presentation bindings before opening the render pass so WebGPU
|
|
158
170
|
// uploads stay on the parent command encoder rather than the active render pass encoder.
|
|
159
171
|
this.textureModel.setProps({backgroundTexture: outputTexture});
|
|
160
|
-
this.textureModel.predraw(
|
|
161
|
-
const renderPass =
|
|
172
|
+
this.textureModel.predraw(commandEncoder);
|
|
173
|
+
const renderPass = commandEncoder.beginRenderPass({
|
|
162
174
|
id: 'shader-pass-renderer-to-screen',
|
|
163
175
|
framebuffer,
|
|
164
176
|
clearDepth: false
|
|
@@ -172,6 +184,21 @@ export class ShaderPassRenderer {
|
|
|
172
184
|
* @returns null if the the sourceTexture has not yet been loaded
|
|
173
185
|
*/
|
|
174
186
|
renderToTexture(options: ShaderPassRendererRenderOptions): Texture | null {
|
|
187
|
+
return this.encodeToTexture(this.device.commandEncoder, options);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Records the shader-pass chain into a caller-owned command encoder.
|
|
192
|
+
* The encoder must belong to this renderer's device and have no active pass. This method does
|
|
193
|
+
* not finish or submit the encoder.
|
|
194
|
+
*/
|
|
195
|
+
encodeToTexture(
|
|
196
|
+
commandEncoder: CommandEncoder,
|
|
197
|
+
options: ShaderPassRendererRenderOptions
|
|
198
|
+
): Texture | null {
|
|
199
|
+
if (commandEncoder.device !== this.device) {
|
|
200
|
+
throw new Error('ShaderPassRenderer command encoder must belong to the renderer device');
|
|
201
|
+
}
|
|
175
202
|
const {sourceTexture} = options;
|
|
176
203
|
if (sourceTexture instanceof DynamicTexture && !sourceTexture.isReady) {
|
|
177
204
|
return null;
|
|
@@ -192,10 +219,10 @@ export class ShaderPassRenderer {
|
|
|
192
219
|
this.textureModel.setProps({backgroundTexture: originalTexture});
|
|
193
220
|
// The seed draw updates fullscreen bind groups, so it must be prepared before the
|
|
194
221
|
// render pass begins to avoid command-encoder locking errors on WebGPU.
|
|
195
|
-
this.textureModel.predraw(
|
|
222
|
+
this.textureModel.predraw(commandEncoder);
|
|
196
223
|
|
|
197
224
|
const sourceFramebuffer = getSeedFramebuffer(this.swapFramebuffers, originalTexture);
|
|
198
|
-
const seedRenderPass =
|
|
225
|
+
const seedRenderPass = commandEncoder.beginRenderPass({
|
|
199
226
|
id: 'shader-pass-renderer-seed-source',
|
|
200
227
|
framebuffer: sourceFramebuffer,
|
|
201
228
|
clearColor: [0, 0, 0, 1],
|
|
@@ -209,7 +236,7 @@ export class ShaderPassRenderer {
|
|
|
209
236
|
let frameSucceeded = false;
|
|
210
237
|
try {
|
|
211
238
|
for (const passRenderer of this.passRenderers) {
|
|
212
|
-
passRenderer.initializeHistoryTargets(originalTexture, this.textureModel);
|
|
239
|
+
passRenderer.initializeHistoryTargets(originalTexture, this.textureModel, commandEncoder);
|
|
213
240
|
for (const execution of passRenderer.subPassExecutions) {
|
|
214
241
|
const outputName = execution.output || 'previous';
|
|
215
242
|
const outputFramebuffer: Framebuffer =
|
|
@@ -235,7 +262,7 @@ export class ShaderPassRenderer {
|
|
|
235
262
|
);
|
|
236
263
|
|
|
237
264
|
execution.subPassRenderer.prepare({
|
|
238
|
-
commandEncoder
|
|
265
|
+
commandEncoder,
|
|
239
266
|
bindings: resolvedBindings,
|
|
240
267
|
textureScale: calculateTextureScale(
|
|
241
268
|
(resolvedBindings['sourceTexture'] as Texture) || previousTexture,
|
|
@@ -243,7 +270,7 @@ export class ShaderPassRenderer {
|
|
|
243
270
|
),
|
|
244
271
|
uniforms: resolvedUniforms
|
|
245
272
|
});
|
|
246
|
-
const renderPass =
|
|
273
|
+
const renderPass = commandEncoder.beginRenderPass({
|
|
247
274
|
id: 'shader-pass-renderer-run-pass',
|
|
248
275
|
framebuffer: outputFramebuffer,
|
|
249
276
|
clearColor: [0, 0, 0, 1],
|
|
@@ -324,7 +351,11 @@ class PassRenderer {
|
|
|
324
351
|
}
|
|
325
352
|
}
|
|
326
353
|
|
|
327
|
-
initializeHistoryTargets(
|
|
354
|
+
initializeHistoryTargets(
|
|
355
|
+
originalTexture: Texture,
|
|
356
|
+
textureModel: BackgroundTextureModel,
|
|
357
|
+
commandEncoder: CommandEncoder
|
|
358
|
+
): void {
|
|
328
359
|
for (const renderTarget of Object.values(this.renderTargets)) {
|
|
329
360
|
if (renderTarget.spec.lifetime !== 'history' || renderTarget.historyInitialized) {
|
|
330
361
|
continue;
|
|
@@ -335,8 +366,8 @@ class PassRenderer {
|
|
|
335
366
|
};
|
|
336
367
|
if (initialize === 'original') {
|
|
337
368
|
textureModel.setProps({backgroundTexture: originalTexture});
|
|
338
|
-
textureModel.predraw(
|
|
339
|
-
const renderPass =
|
|
369
|
+
textureModel.predraw(commandEncoder);
|
|
370
|
+
const renderPass = commandEncoder.beginRenderPass({
|
|
340
371
|
id: `${renderTarget.name}-initialize-history`,
|
|
341
372
|
framebuffer: historyFramebuffer,
|
|
342
373
|
clearColor: [0, 0, 0, 0],
|
|
@@ -345,7 +376,7 @@ class PassRenderer {
|
|
|
345
376
|
textureModel.draw(renderPass);
|
|
346
377
|
renderPass.end();
|
|
347
378
|
} else {
|
|
348
|
-
const renderPass =
|
|
379
|
+
const renderPass = commandEncoder.beginRenderPass({
|
|
349
380
|
id: `${renderTarget.name}-clear-history`,
|
|
350
381
|
framebuffer: historyFramebuffer,
|
|
351
382
|
clearColor: initialize.clearColor,
|
|
@@ -765,7 +796,8 @@ function createTargetResources(
|
|
|
765
796
|
width: targetSize[0],
|
|
766
797
|
height: targetSize[1],
|
|
767
798
|
format: spec.format || device.preferredColorFormat,
|
|
768
|
-
usage: Texture.SAMPLE | Texture.RENDER | Texture.COPY_SRC | Texture.COPY_DST
|
|
799
|
+
usage: Texture.SAMPLE | Texture.RENDER | Texture.COPY_SRC | Texture.COPY_DST,
|
|
800
|
+
...(spec.sampler ? {sampler: spec.sampler} : {})
|
|
769
801
|
});
|
|
770
802
|
const framebuffer = device.createFramebuffer({
|
|
771
803
|
id: `${name}-framebuffer`,
|
|
@@ -3,13 +3,34 @@
|
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import {Matrix4, Vector3} from '@math.gl/core';
|
|
6
|
+
import type {NumericArray} from '@math.gl/core';
|
|
6
7
|
import {log} from '@luma.gl/core';
|
|
7
8
|
import {ScenegraphNode, ScenegraphNodeProps} from './scenegraph-node';
|
|
9
|
+
import {
|
|
10
|
+
areScenegraphBoundsDefined,
|
|
11
|
+
expandScenegraphBounds,
|
|
12
|
+
makeEmptyScenegraphBounds
|
|
13
|
+
} from './scenegraph-bounds';
|
|
14
|
+
import type {ScenegraphBounds} from './scenegraph-bounds';
|
|
8
15
|
|
|
9
16
|
export type GroupNodeProps = ScenegraphNodeProps & {
|
|
10
17
|
children?: ScenegraphNode[];
|
|
11
18
|
};
|
|
12
19
|
|
|
20
|
+
/** Camera-relative ordering options for a flattened scenegraph traversal. */
|
|
21
|
+
export type DepthSortedTraversalOptions = {
|
|
22
|
+
viewMatrix: NumericArray;
|
|
23
|
+
worldMatrix?: NumericArray;
|
|
24
|
+
order?: 'back-to-front' | 'front-to-back';
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** Model transform, local aggregate bounds, and positive camera depth for one visited node. */
|
|
28
|
+
export type DepthSortedTraversalContext = {
|
|
29
|
+
worldMatrix: Matrix4;
|
|
30
|
+
bounds: [number[], number[]] | null;
|
|
31
|
+
depth: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
13
34
|
export class GroupNode extends ScenegraphNode {
|
|
14
35
|
children: ScenegraphNode[];
|
|
15
36
|
|
|
@@ -27,39 +48,18 @@ export class GroupNode extends ScenegraphNode {
|
|
|
27
48
|
this.children = children;
|
|
28
49
|
}
|
|
29
50
|
|
|
30
|
-
override getBounds():
|
|
31
|
-
const result
|
|
32
|
-
[Infinity, Infinity, Infinity],
|
|
33
|
-
[-Infinity, -Infinity, -Infinity]
|
|
34
|
-
];
|
|
51
|
+
override getBounds(): ScenegraphBounds | null {
|
|
52
|
+
const result = makeEmptyScenegraphBounds();
|
|
35
53
|
|
|
36
54
|
this.traverse((node, {worldMatrix}) => {
|
|
37
55
|
const bounds = node.getBounds();
|
|
38
56
|
if (!bounds) {
|
|
39
57
|
return;
|
|
40
58
|
}
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
worldMatrix.transformAsPoint(center, center);
|
|
44
|
-
const halfSize = new Vector3(max).subtract(min).divide([2, 2, 2]);
|
|
45
|
-
worldMatrix.transformAsVector(halfSize, halfSize);
|
|
46
|
-
|
|
47
|
-
for (let v = 0; v < 8; v++) {
|
|
48
|
-
// Test all 8 corners of the box
|
|
49
|
-
const position = new Vector3(v & 0b001 ? -1 : 1, v & 0b010 ? -1 : 1, v & 0b100 ? -1 : 1)
|
|
50
|
-
.multiply(halfSize)
|
|
51
|
-
.add(center);
|
|
52
|
-
|
|
53
|
-
for (let i = 0; i < 3; i++) {
|
|
54
|
-
result[0][i] = Math.min(result[0][i], position[i]);
|
|
55
|
-
result[1][i] = Math.max(result[1][i], position[i]);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
59
|
+
const nodeWorldMatrix = new Matrix4(worldMatrix).multiplyRight(node.matrix);
|
|
60
|
+
expandScenegraphBounds(result, bounds, nodeWorldMatrix);
|
|
58
61
|
});
|
|
59
|
-
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
return result;
|
|
62
|
+
return areScenegraphBoundsDefined(result) ? result : null;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
override destroy(): void {
|
|
@@ -109,6 +109,43 @@ export class GroupNode extends ScenegraphNode {
|
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
/** Visits leaf nodes ordered by the camera-space centers of their aggregate bounds. */
|
|
113
|
+
traverseDepthSorted(
|
|
114
|
+
visitor: (node: ScenegraphNode, context: DepthSortedTraversalContext) => void,
|
|
115
|
+
{viewMatrix, worldMatrix = new Matrix4(), order = 'back-to-front'}: DepthSortedTraversalOptions
|
|
116
|
+
): void {
|
|
117
|
+
const cameraMatrix = new Matrix4(viewMatrix);
|
|
118
|
+
const nodes: {node: ScenegraphNode; context: DepthSortedTraversalContext; index: number}[] = [];
|
|
119
|
+
|
|
120
|
+
this.traverse(
|
|
121
|
+
(node, context) => {
|
|
122
|
+
const bounds = node.getBounds();
|
|
123
|
+
const center = bounds
|
|
124
|
+
? new Vector3(bounds[0]).add(bounds[1]).divide([2, 2, 2])
|
|
125
|
+
: new Vector3();
|
|
126
|
+
const nodeWorldMatrix = new Matrix4(context.worldMatrix).multiplyRight(node.matrix);
|
|
127
|
+
nodeWorldMatrix.transformAsPoint(center, center);
|
|
128
|
+
cameraMatrix.transformAsPoint(center, center);
|
|
129
|
+
nodes.push({
|
|
130
|
+
node,
|
|
131
|
+
context: {worldMatrix: nodeWorldMatrix, bounds, depth: -center[2]},
|
|
132
|
+
index: nodes.length
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
{worldMatrix: new Matrix4(worldMatrix)}
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const direction = order === 'back-to-front' ? -1 : 1;
|
|
139
|
+
nodes.sort(
|
|
140
|
+
(first, second) =>
|
|
141
|
+
direction * (first.context.depth - second.context.depth) || first.index - second.index
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
for (const {node, context} of nodes) {
|
|
145
|
+
visitor(node, context);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
112
149
|
preorderTraversal(
|
|
113
150
|
visitor: (node: ScenegraphNode, context: {worldMatrix: Matrix4}) => void,
|
|
114
151
|
{worldMatrix = new Matrix4()} = {}
|
|
@@ -3,18 +3,29 @@
|
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import {RenderPass} from '@luma.gl/core';
|
|
6
|
+
import type {NumericArray} from '@math.gl/core';
|
|
6
7
|
import {ScenegraphNode, ScenegraphNodeProps} from './scenegraph-node';
|
|
8
|
+
import {
|
|
9
|
+
areScenegraphBoundsDefined,
|
|
10
|
+
expandScenegraphBounds,
|
|
11
|
+
makeEmptyScenegraphBounds
|
|
12
|
+
} from './scenegraph-bounds';
|
|
13
|
+
import type {ScenegraphBounds} from './scenegraph-bounds';
|
|
7
14
|
import {Model} from '../model/model';
|
|
8
15
|
|
|
9
16
|
export type ModelNodeProps = ScenegraphNodeProps & {
|
|
10
17
|
model: Model;
|
|
11
18
|
managedResources?: any[];
|
|
12
|
-
|
|
19
|
+
/** Bounds of one model instance before instance and node transforms. */
|
|
20
|
+
bounds?: ScenegraphBounds;
|
|
21
|
+
/** Optional per-instance transforms used to calculate aggregate local bounds. */
|
|
22
|
+
instanceMatrices?: readonly NumericArray[];
|
|
13
23
|
};
|
|
14
24
|
|
|
15
25
|
export class ModelNode extends ScenegraphNode {
|
|
16
26
|
readonly model: Model;
|
|
17
|
-
|
|
27
|
+
readonly instanceMatrices: readonly NumericArray[] | null;
|
|
28
|
+
bounds: ScenegraphBounds | null = null;
|
|
18
29
|
managedResources: any[];
|
|
19
30
|
|
|
20
31
|
// TODO - is this used? override callbacks to make sure we call them with this
|
|
@@ -28,7 +39,12 @@ export class ModelNode extends ScenegraphNode {
|
|
|
28
39
|
// Create new Model or used supplied Model
|
|
29
40
|
this.model = props.model;
|
|
30
41
|
this.managedResources = props.managedResources || [];
|
|
31
|
-
this.
|
|
42
|
+
this.instanceMatrices = props.instanceMatrices || null;
|
|
43
|
+
this.bounds = props.bounds
|
|
44
|
+
? this.instanceMatrices
|
|
45
|
+
? getInstancedScenegraphBounds(props.bounds, this.instanceMatrices)
|
|
46
|
+
: props.bounds
|
|
47
|
+
: null;
|
|
32
48
|
this.setProps(props);
|
|
33
49
|
}
|
|
34
50
|
|
|
@@ -52,3 +68,16 @@ export class ModelNode extends ScenegraphNode {
|
|
|
52
68
|
return this.model.draw(renderPass);
|
|
53
69
|
}
|
|
54
70
|
}
|
|
71
|
+
|
|
72
|
+
function getInstancedScenegraphBounds(
|
|
73
|
+
bounds: ScenegraphBounds,
|
|
74
|
+
instanceMatrices: readonly NumericArray[]
|
|
75
|
+
): ScenegraphBounds | null {
|
|
76
|
+
const instancedBounds = makeEmptyScenegraphBounds();
|
|
77
|
+
|
|
78
|
+
for (const instanceMatrix of instanceMatrices) {
|
|
79
|
+
expandScenegraphBounds(instancedBounds, bounds, instanceMatrix);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return areScenegraphBoundsDefined(instancedBounds) ? instancedBounds : null;
|
|
83
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {Matrix4, Vector3} from '@math.gl/core';
|
|
6
|
+
import type {NumericArray} from '@math.gl/core';
|
|
7
|
+
|
|
8
|
+
/** Axis-aligned bounds in a scenegraph node's local coordinate system. */
|
|
9
|
+
export type ScenegraphBounds = [[number, number, number], [number, number, number]];
|
|
10
|
+
|
|
11
|
+
export function makeEmptyScenegraphBounds(): ScenegraphBounds {
|
|
12
|
+
return [
|
|
13
|
+
[Infinity, Infinity, Infinity],
|
|
14
|
+
[-Infinity, -Infinity, -Infinity]
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function expandScenegraphBounds(
|
|
19
|
+
targetBounds: ScenegraphBounds,
|
|
20
|
+
sourceBounds: [number[], number[]],
|
|
21
|
+
transform: NumericArray
|
|
22
|
+
): void {
|
|
23
|
+
const transformMatrix = new Matrix4(transform);
|
|
24
|
+
|
|
25
|
+
for (let cornerIndex = 0; cornerIndex < 8; cornerIndex++) {
|
|
26
|
+
const corner = new Vector3(
|
|
27
|
+
sourceBounds[cornerIndex & 0b001 ? 1 : 0][0],
|
|
28
|
+
sourceBounds[cornerIndex & 0b010 ? 1 : 0][1],
|
|
29
|
+
sourceBounds[cornerIndex & 0b100 ? 1 : 0][2]
|
|
30
|
+
);
|
|
31
|
+
transformMatrix.transformAsPoint(corner, corner);
|
|
32
|
+
|
|
33
|
+
for (let componentIndex = 0; componentIndex < 3; componentIndex++) {
|
|
34
|
+
targetBounds[0][componentIndex] = Math.min(
|
|
35
|
+
targetBounds[0][componentIndex],
|
|
36
|
+
corner[componentIndex]
|
|
37
|
+
);
|
|
38
|
+
targetBounds[1][componentIndex] = Math.max(
|
|
39
|
+
targetBounds[1][componentIndex],
|
|
40
|
+
corner[componentIndex]
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function areScenegraphBoundsDefined(bounds: ScenegraphBounds): boolean {
|
|
47
|
+
return Number.isFinite(bounds[0][0]);
|
|
48
|
+
}
|