@luma.gl/engine 9.4.0-alpha.1 → 9.4.0-alpha.2

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.
@@ -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
- bounds?: [[number, number, number], [number, number, number]];
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
- bounds: [[number, number, number], [number, number, number]] | null = null;
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.bounds = props.bounds || null;
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
+ }