@luma.gl/engine 10.0.0-alpha.2 → 10.0.0-alpha.4
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/compute/computation.d.ts +2 -0
- package/dist/compute/computation.d.ts.map +1 -1
- package/dist/compute/computation.js +5 -1
- package/dist/compute/computation.js.map +1 -1
- package/dist/dist.dev.js +1033 -1320
- package/dist/dist.min.js +23 -23
- package/dist/geometry/edge-geometry.d.ts +17 -0
- package/dist/geometry/edge-geometry.d.ts.map +1 -0
- package/dist/geometry/edge-geometry.js +160 -0
- package/dist/geometry/edge-geometry.js.map +1 -0
- package/dist/geometry/geometry-primitives.d.ts +54 -0
- package/dist/geometry/geometry-primitives.d.ts.map +1 -0
- package/dist/geometry/geometry-primitives.js +153 -0
- package/dist/geometry/geometry-primitives.js.map +1 -0
- package/dist/geometry/geometry-utils.d.ts +7 -15
- package/dist/geometry/geometry-utils.d.ts.map +1 -1
- package/dist/geometry/geometry-utils.js +9 -55
- package/dist/geometry/geometry-utils.js.map +1 -1
- package/dist/geometry/geometry.d.ts +9 -69
- package/dist/geometry/geometry.d.ts.map +1 -1
- package/dist/geometry/geometry.js +27 -93
- package/dist/geometry/geometry.js.map +1 -1
- package/dist/geometry/gpu-geometry.d.ts +4 -2
- package/dist/geometry/gpu-geometry.d.ts.map +1 -1
- package/dist/geometry/gpu-geometry.js +3 -3
- package/dist/geometry/gpu-geometry.js.map +1 -1
- package/dist/geometry/stroke-geometry.d.ts +15 -0
- package/dist/geometry/stroke-geometry.d.ts.map +1 -0
- package/dist/geometry/stroke-geometry.js +168 -0
- package/dist/geometry/stroke-geometry.js.map +1 -0
- package/dist/index.cjs +554 -1309
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +17 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -7
- package/dist/index.js.map +1 -1
- package/dist/model/model.d.ts +6 -1
- package/dist/model/model.d.ts.map +1 -1
- package/dist/model/model.js +11 -4
- package/dist/model/model.js.map +1 -1
- package/dist/models/clip-space.d.ts +4 -1
- package/dist/models/clip-space.d.ts.map +1 -1
- package/dist/models/clip-space.js +16 -13
- package/dist/models/clip-space.js.map +1 -1
- package/dist/models/directional-light-model.js +1 -1
- package/dist/models/directional-light-model.js.map +1 -1
- package/dist/models/point-light-model.js +1 -1
- package/dist/models/point-light-model.js.map +1 -1
- package/dist/models/spot-light-model.js +1 -1
- package/dist/models/spot-light-model.js.map +1 -1
- package/package.json +7 -6
- package/src/compute/computation.ts +7 -0
- package/src/geometry/edge-geometry.ts +208 -0
- package/src/geometry/geometry-primitives.ts +215 -0
- package/src/geometry/geometry-utils.ts +16 -71
- package/src/geometry/geometry.ts +36 -163
- package/src/geometry/gpu-geometry.ts +9 -4
- package/src/geometry/stroke-geometry.ts +236 -0
- package/src/index.ts +21 -14
- package/src/model/model.ts +19 -6
- package/src/models/clip-space.ts +21 -14
- package/src/models/directional-light-model.ts +1 -1
- package/src/models/point-light-model.ts +1 -1
- package/src/models/spot-light-model.ts +1 -1
- package/dist/geometries/cone-geometry.d.ts +0 -12
- package/dist/geometries/cone-geometry.d.ts.map +0 -1
- package/dist/geometries/cone-geometry.js +0 -19
- package/dist/geometries/cone-geometry.js.map +0 -1
- package/dist/geometries/cube-geometry.d.ts +0 -14
- package/dist/geometries/cube-geometry.d.ts.map +0 -1
- package/dist/geometries/cube-geometry.js +0 -222
- package/dist/geometries/cube-geometry.js.map +0 -1
- package/dist/geometries/cylinder-geometry.d.ts +0 -11
- package/dist/geometries/cylinder-geometry.d.ts.map +0 -1
- package/dist/geometries/cylinder-geometry.js +0 -17
- package/dist/geometries/cylinder-geometry.js.map +0 -1
- package/dist/geometries/ico-sphere-geometry.d.ts +0 -11
- package/dist/geometries/ico-sphere-geometry.d.ts.map +0 -1
- package/dist/geometries/ico-sphere-geometry.js +0 -158
- package/dist/geometries/ico-sphere-geometry.js.map +0 -1
- package/dist/geometries/plane-geometry.d.ts +0 -20
- package/dist/geometries/plane-geometry.d.ts.map +0 -1
- package/dist/geometries/plane-geometry.js +0 -106
- package/dist/geometries/plane-geometry.js.map +0 -1
- package/dist/geometries/sphere-geometry.d.ts +0 -12
- package/dist/geometries/sphere-geometry.d.ts.map +0 -1
- package/dist/geometries/sphere-geometry.js +0 -88
- package/dist/geometries/sphere-geometry.js.map +0 -1
- package/dist/geometries/truncated-cone-geometry.d.ts +0 -23
- package/dist/geometries/truncated-cone-geometry.d.ts.map +0 -1
- package/dist/geometries/truncated-cone-geometry.js +0 -112
- package/dist/geometries/truncated-cone-geometry.js.map +0 -1
- package/src/geometries/cone-geometry.ts +0 -31
- package/src/geometries/cube-geometry.ts +0 -258
- package/src/geometries/cylinder-geometry.ts +0 -28
- package/src/geometries/ico-sphere-geometry.ts +0 -193
- package/src/geometries/plane-geometry.ts +0 -142
- package/src/geometries/sphere-geometry.ts +0 -113
- package/src/geometries/truncated-cone-geometry.ts +0 -145
package/src/geometry/geometry.ts
CHANGED
|
@@ -2,181 +2,54 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import type
|
|
6
|
-
import type {BufferLayout
|
|
5
|
+
import {Geometry as MathGeometry, type GeometryProps as MathGeometryProps} from '@math.gl/geometry';
|
|
6
|
+
import type {BufferLayout} from '@luma.gl/core';
|
|
7
7
|
import {vertexFormatDecoder} from '@luma.gl/core';
|
|
8
|
-
import {
|
|
8
|
+
import type {GeometryAttribute} from '@math.gl/geometry';
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
export type {
|
|
11
|
+
GeometryAttribute,
|
|
12
|
+
GeometryAttributeInput,
|
|
13
|
+
GeometryAttributes
|
|
14
|
+
} from '@math.gl/geometry';
|
|
12
15
|
|
|
13
|
-
/**
|
|
14
|
-
export type GeometryProps = {
|
|
15
|
-
/** Application-provided identifier. */
|
|
16
|
-
id?: string;
|
|
17
|
-
/** Determines how vertices are assembled into primitives. */
|
|
18
|
-
topology: 'point-list' | 'line-list' | 'line-strip' | 'triangle-list' | 'triangle-strip';
|
|
19
|
-
/** Draw vertex count. Auto-calculated from attributes or indices when omitted. */
|
|
20
|
-
vertexCount?: number;
|
|
21
|
-
/** CPU attributes, keyed by caller-provided names such as glTF mesh attribute semantics. */
|
|
22
|
-
attributes: Record<string, GeometryAttributeInput>;
|
|
23
|
-
/**
|
|
24
|
-
* Maps geometry buffers to shader attributes.
|
|
25
|
-
*
|
|
26
|
-
* If omitted, the constructor creates one shader-facing buffer layout entry for each attribute.
|
|
27
|
-
* Explicit buffer layouts are preserved unchanged.
|
|
28
|
-
*/
|
|
29
|
-
bufferLayout?: BufferLayout[];
|
|
30
|
-
/** Optional index data. Indices are always stored separately from vertex attributes. */
|
|
31
|
-
indices?: GeometryAttribute | TypedArray;
|
|
32
|
-
};
|
|
16
|
+
/** Legacy engine geometry properties, including caller-owned shader layout metadata. */
|
|
17
|
+
export type GeometryProps = MathGeometryProps & {bufferLayout?: BufferLayout[]};
|
|
33
18
|
|
|
34
|
-
/**
|
|
35
|
-
export
|
|
36
|
-
/** Optional index attribute, included when the geometry is indexed. */
|
|
37
|
-
indices?: GeometryAttribute & {size: 1; value: Uint32Array | Uint16Array};
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/** Typed-array backed CPU geometry attribute. */
|
|
41
|
-
export type GeometryAttribute = {
|
|
42
|
-
/** Number of typed-array elements per vertex. */
|
|
43
|
-
size?: number;
|
|
44
|
-
/** Attribute data. */
|
|
45
|
-
value: TypedArray;
|
|
46
|
-
/** Additional attribute metadata consumed by geometry utilities. */
|
|
47
|
-
[key: string]: any;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* CPU-side geometry container.
|
|
52
|
-
*
|
|
53
|
-
* `Geometry` stores typed-array vertex data, optional index data, and an always-populated
|
|
54
|
-
* `bufferLayout` that describes how its attributes map to shader inputs. CPU attribute names are
|
|
55
|
-
* preserved as supplied; synthesized buffer layouts map supported glTF-style names such as
|
|
56
|
-
* `POSITION` and `TEXCOORD_0` to default shader names such as `positions` and `texCoords`.
|
|
57
|
-
*/
|
|
58
|
-
export class Geometry {
|
|
59
|
-
/** Application-provided or generated identifier. */
|
|
60
|
-
readonly id: string;
|
|
61
|
-
|
|
62
|
-
/** Determines how vertices are assembled into primitives. */
|
|
63
|
-
readonly topology?: PrimitiveTopology;
|
|
64
|
-
|
|
65
|
-
/** Resolved draw vertex count. */
|
|
66
|
-
readonly vertexCount: number;
|
|
67
|
-
|
|
68
|
-
/** Optional index attribute. */
|
|
69
|
-
readonly indices?: GeometryAttribute;
|
|
70
|
-
|
|
71
|
-
/** CPU attributes, keyed by caller-provided source attribute name. */
|
|
72
|
-
readonly attributes: Record<string, GeometryAttribute | undefined>;
|
|
73
|
-
|
|
74
|
-
/** Buffer layout matching the geometry attributes. Always populated. */
|
|
19
|
+
/** Compatibility adapter for math.gl CPU geometry with engine shader layout metadata. */
|
|
20
|
+
export class Geometry extends MathGeometry {
|
|
75
21
|
readonly bufferLayout: BufferLayout[];
|
|
76
22
|
|
|
77
|
-
/** Application-owned metadata. */
|
|
78
|
-
userData: Record<string, unknown> = {};
|
|
79
|
-
|
|
80
|
-
/** Creates a CPU geometry and wraps raw typed arrays into attribute records. */
|
|
81
23
|
constructor(props: GeometryProps) {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
24
|
+
const attributes: MathGeometryProps['attributes'] = {};
|
|
25
|
+
const sourceNames = new Map<string, string>();
|
|
26
|
+
for (const [name, attribute] of Object.entries(props.attributes)) {
|
|
27
|
+
const shaderName = getGeometryShaderAttributeName(name);
|
|
28
|
+
const previousName = sourceNames.get(shaderName);
|
|
29
|
+
if (previousName) delete attributes[previousName];
|
|
30
|
+
sourceNames.set(shaderName, name);
|
|
31
|
+
attributes[name] = attribute;
|
|
89
32
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if ((attributeName === 'POSITION' || attributeName === 'positions') && !attribute.size) {
|
|
106
|
-
attribute.size = 3;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Move indices to separate field
|
|
110
|
-
if (attributeName === 'indices') {
|
|
111
|
-
if (this.indices) {
|
|
112
|
-
throw new Error('Multiple indices detected');
|
|
113
|
-
}
|
|
114
|
-
this.indices = attribute;
|
|
115
|
-
} else {
|
|
116
|
-
const shaderAttributeName = getGeometryShaderAttributeName(attributeName);
|
|
117
|
-
const storedAttributeName = Object.keys(this.attributes).find(
|
|
118
|
-
name => getGeometryShaderAttributeName(name) === shaderAttributeName
|
|
119
|
-
);
|
|
120
|
-
if (storedAttributeName) {
|
|
121
|
-
delete this.attributes[storedAttributeName];
|
|
122
|
-
}
|
|
123
|
-
this.attributes[attributeName] = attribute;
|
|
33
|
+
const sourceIndices = props.indices;
|
|
34
|
+
const indices =
|
|
35
|
+
sourceIndices instanceof Uint8Array
|
|
36
|
+
? new Uint16Array(sourceIndices)
|
|
37
|
+
: sourceIndices &&
|
|
38
|
+
!ArrayBuffer.isView(sourceIndices) &&
|
|
39
|
+
sourceIndices.value instanceof Uint8Array
|
|
40
|
+
? {...sourceIndices, value: new Uint16Array(sourceIndices.value)}
|
|
41
|
+
: sourceIndices;
|
|
42
|
+
super({...props, attributes, indices});
|
|
43
|
+
// Preserve legacy descriptor identity while math.gl owns normalization and validation.
|
|
44
|
+
for (const [name, input] of Object.entries(attributes)) {
|
|
45
|
+
if (!ArrayBuffer.isView(input) && Object.isExtensible(input) && name !== 'indices') {
|
|
46
|
+
Object.assign(input, this.attributes[name]);
|
|
47
|
+
this.attributes[name] = input;
|
|
124
48
|
}
|
|
125
49
|
}
|
|
126
|
-
|
|
127
|
-
if (this.indices && this.indices['isIndexed'] !== undefined) {
|
|
128
|
-
this.indices = Object.assign({}, this.indices);
|
|
129
|
-
delete this.indices['isIndexed'];
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
this.vertexCount = vertexCount || this._calculateVertexCount(this.attributes, this.indices);
|
|
133
50
|
this.bufferLayout =
|
|
134
51
|
props.bufferLayout || getBufferLayoutFromGeometryAttributes(this.attributes);
|
|
135
52
|
}
|
|
136
|
-
|
|
137
|
-
/** Returns the resolved draw vertex count. */
|
|
138
|
-
getVertexCount(): number {
|
|
139
|
-
return this.vertexCount;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/** Returns all attributes, including `indices` when index data is present. */
|
|
143
|
-
getAttributes(): GeometryAttributes {
|
|
144
|
-
return (
|
|
145
|
-
this.indices ? {indices: this.indices, ...this.attributes} : this.attributes
|
|
146
|
-
) as GeometryAttributes;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// PRIVATE
|
|
150
|
-
|
|
151
|
-
_print(attributeName: string): string {
|
|
152
|
-
return `Geometry ${this.id} attribute ${attributeName}`;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
_setAttributes(attributes: Record<string, GeometryAttribute>, indices: any): this {
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
_calculateVertexCount(
|
|
160
|
-
attributes: Record<string, GeometryAttribute | undefined>,
|
|
161
|
-
indices?: GeometryAttribute
|
|
162
|
-
): number {
|
|
163
|
-
if (indices) {
|
|
164
|
-
return indices.value.length;
|
|
165
|
-
}
|
|
166
|
-
let vertexCount = Infinity;
|
|
167
|
-
for (const attribute of Object.values(attributes)) {
|
|
168
|
-
if (!attribute) {
|
|
169
|
-
continue; // eslint-disable-line no-continue
|
|
170
|
-
}
|
|
171
|
-
const {value, size, constant} = attribute;
|
|
172
|
-
if (!constant && value && size !== undefined && size >= 1) {
|
|
173
|
-
vertexCount = Math.min(vertexCount, value.length / size);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// assert(Number.isFinite(vertexCount));
|
|
178
|
-
return vertexCount;
|
|
179
|
-
}
|
|
180
53
|
}
|
|
181
54
|
|
|
182
55
|
/**
|
|
@@ -202,7 +75,7 @@ export function getGeometryShaderAttributeName(attributeName: string): string {
|
|
|
202
75
|
}
|
|
203
76
|
}
|
|
204
77
|
|
|
205
|
-
function getBufferLayoutFromGeometryAttributes(
|
|
78
|
+
export function getBufferLayoutFromGeometryAttributes(
|
|
206
79
|
attributes: Record<string, GeometryAttribute | undefined>
|
|
207
80
|
): BufferLayout[] {
|
|
208
81
|
const bufferLayout: BufferLayout[] = [];
|
|
@@ -4,7 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
import type {PrimitiveTopology, BufferLayout} from '@luma.gl/core';
|
|
6
6
|
import {Device, Buffer} from '@luma.gl/core';
|
|
7
|
-
import
|
|
7
|
+
import type {Geometry} from '@math.gl/geometry';
|
|
8
|
+
import {
|
|
9
|
+
getBufferLayoutFromGeometryAttributes,
|
|
10
|
+
getGeometryShaderAttributeName
|
|
11
|
+
} from '../geometry/geometry';
|
|
8
12
|
import {makeInterleavedGeometry} from './geometry-utils';
|
|
9
13
|
import {uid} from '../utils/uid';
|
|
10
14
|
|
|
@@ -143,12 +147,12 @@ export function getIndexBufferFromGeometry(device: Device, geometry: Geometry):
|
|
|
143
147
|
*/
|
|
144
148
|
export function getAttributeBuffersFromGeometry(
|
|
145
149
|
device: Device,
|
|
146
|
-
geometry: Geometry
|
|
150
|
+
geometry: Geometry & {bufferLayout?: BufferLayout[]}
|
|
147
151
|
): {attributes: Record<string, Buffer>; bufferLayout: BufferLayout[]; vertexCount: number} {
|
|
148
152
|
const attributes: Record<string, Buffer> = {};
|
|
149
153
|
for (const [attributeName, attribute] of Object.entries(geometry.attributes)) {
|
|
150
154
|
const name =
|
|
151
|
-
geometry.bufferLayout
|
|
155
|
+
geometry.bufferLayout?.find(bufferLayout => bufferLayout.name === attributeName)?.name ||
|
|
152
156
|
getGeometryShaderAttributeName(attributeName);
|
|
153
157
|
if (attribute) {
|
|
154
158
|
attributes[name] = device.createBuffer({
|
|
@@ -160,7 +164,8 @@ export function getAttributeBuffersFromGeometry(
|
|
|
160
164
|
|
|
161
165
|
return {
|
|
162
166
|
attributes,
|
|
163
|
-
bufferLayout:
|
|
167
|
+
bufferLayout:
|
|
168
|
+
geometry.bufferLayout || getBufferLayoutFromGeometryAttributes(geometry.attributes),
|
|
164
169
|
vertexCount: geometry.vertexCount
|
|
165
170
|
};
|
|
166
171
|
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {assert} from '@luma.gl/core';
|
|
6
|
+
import {Geometry} from './geometry';
|
|
7
|
+
|
|
8
|
+
export type StrokePosition = readonly [number, number, number?];
|
|
9
|
+
export type StrokeGeometryOptions = {
|
|
10
|
+
width?: number;
|
|
11
|
+
cap?: 'butt' | 'square' | 'round';
|
|
12
|
+
join?: 'miter' | 'bevel' | 'round';
|
|
13
|
+
miterLimit?: number;
|
|
14
|
+
/** Number of triangles used for a semicircle. */
|
|
15
|
+
roundSegments?: number;
|
|
16
|
+
closed?: boolean;
|
|
17
|
+
id?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type Position = [number, number, number];
|
|
21
|
+
type Direction = [number, number];
|
|
22
|
+
type StrokeVertex = {position: Position; distance: number; across: number};
|
|
23
|
+
type Segment = {direction: Direction; length: number};
|
|
24
|
+
type Corners = {
|
|
25
|
+
incomingLeft: StrokeVertex;
|
|
26
|
+
incomingRight: StrokeVertex;
|
|
27
|
+
outgoingLeft: StrokeVertex;
|
|
28
|
+
outgoingRight: StrokeVertex;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Triangulates a local XY stroke; optional source elevations are preserved at each path vertex. */
|
|
32
|
+
export function makeStrokeGeometry(
|
|
33
|
+
path: readonly StrokePosition[],
|
|
34
|
+
options: StrokeGeometryOptions = {}
|
|
35
|
+
): Geometry {
|
|
36
|
+
const {width = 1, cap = 'butt', join = 'miter', miterLimit = 4, roundSegments = 16} = options;
|
|
37
|
+
// Stroke dimensions must be finite; resolution and miter limits bound generated geometry.
|
|
38
|
+
assert(Number.isFinite(width) && width >= 0 && Number.isFinite(miterLimit) && miterLimit >= 1);
|
|
39
|
+
assert(Number.isInteger(roundSegments) && roundSegments >= 2 && roundSegments <= 256);
|
|
40
|
+
const points: Position[] = [];
|
|
41
|
+
for (const position of path) {
|
|
42
|
+
assert(
|
|
43
|
+
Number.isFinite(position[0]) &&
|
|
44
|
+
Number.isFinite(position[1]) &&
|
|
45
|
+
Number.isFinite(position[2] ?? 0)
|
|
46
|
+
);
|
|
47
|
+
const previous = points.at(-1);
|
|
48
|
+
if (!previous || previous[0] !== position[0] || previous[1] !== position[1])
|
|
49
|
+
points.push([position[0], position[1], position[2] ?? 0]);
|
|
50
|
+
}
|
|
51
|
+
if (
|
|
52
|
+
options.closed &&
|
|
53
|
+
points.length > 1 &&
|
|
54
|
+
points[0][0] === points.at(-1)![0] &&
|
|
55
|
+
points[0][1] === points.at(-1)![1]
|
|
56
|
+
)
|
|
57
|
+
points.pop();
|
|
58
|
+
const closed = Boolean(options.closed && points.length > 2);
|
|
59
|
+
const positions: number[] = [];
|
|
60
|
+
const coordinates: number[] = [];
|
|
61
|
+
const halfWidth = width / 2;
|
|
62
|
+
if (points.length > 1 && width > 0) {
|
|
63
|
+
const segments: Segment[] = [];
|
|
64
|
+
const distances = [0];
|
|
65
|
+
for (let index = 0; index < points.length - (closed ? 0 : 1); index++) {
|
|
66
|
+
const start = points[index];
|
|
67
|
+
const end = points[(index + 1) % points.length];
|
|
68
|
+
const horizontal = end[0] - start[0];
|
|
69
|
+
const vertical = end[1] - start[1];
|
|
70
|
+
const length = Math.hypot(horizontal, vertical);
|
|
71
|
+
segments.push({direction: [horizontal / length, vertical / length], length});
|
|
72
|
+
distances.push(distances.at(-1)! + length);
|
|
73
|
+
}
|
|
74
|
+
const corners = points.map((point, index): Corners => {
|
|
75
|
+
const previous = segments[(index - 1 + segments.length) % segments.length];
|
|
76
|
+
const next = segments[index % segments.length];
|
|
77
|
+
const distance = distances[index];
|
|
78
|
+
if (!closed && (index === 0 || index === points.length - 1)) {
|
|
79
|
+
const direction = index === 0 ? next.direction : previous.direction;
|
|
80
|
+
const extension = cap === 'square' ? (index === 0 ? -halfWidth : halfWidth) : 0;
|
|
81
|
+
const left = createVertex(point, direction, halfWidth, extension, distance + extension);
|
|
82
|
+
const right = createVertex(point, direction, -halfWidth, extension, distance + extension);
|
|
83
|
+
if (cap === 'round') appendCap(point, direction, distance, index === 0);
|
|
84
|
+
return {incomingLeft: left, outgoingLeft: left, incomingRight: right, outgoingRight: right};
|
|
85
|
+
}
|
|
86
|
+
const incoming = previous.direction;
|
|
87
|
+
const outgoing = next.direction;
|
|
88
|
+
const turn = incoming[0] * outgoing[1] - incoming[1] * outgoing[0];
|
|
89
|
+
const cosine = incoming[0] * outgoing[0] + incoming[1] * outgoing[1];
|
|
90
|
+
const result = {
|
|
91
|
+
incomingLeft: createVertex(point, incoming, halfWidth, 0, distance),
|
|
92
|
+
incomingRight: createVertex(point, incoming, -halfWidth, 0, distance),
|
|
93
|
+
outgoingLeft: createVertex(point, outgoing, halfWidth, 0, distance),
|
|
94
|
+
outgoingRight: createVertex(point, outgoing, -halfWidth, 0, distance)
|
|
95
|
+
};
|
|
96
|
+
if (1 + cosine < 1e-8) {
|
|
97
|
+
// Exact reversals are split strokes; retraced geometry may overlap.
|
|
98
|
+
if (join === 'round') {
|
|
99
|
+
appendCap(point, incoming, distance, false);
|
|
100
|
+
appendCap(point, outgoing, distance, true);
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
const miter: Direction = [
|
|
105
|
+
(-(incoming[1] + outgoing[1]) * halfWidth) / (1 + cosine),
|
|
106
|
+
((incoming[0] + outgoing[0]) * halfWidth) / (1 + cosine)
|
|
107
|
+
];
|
|
108
|
+
const miterLength = Math.hypot(...miter);
|
|
109
|
+
if (Math.abs(turn) < 1e-8 || (join === 'miter' && miterLength <= miterLimit * halfWidth)) {
|
|
110
|
+
const left = offsetVertex(point, miter, distance, halfWidth);
|
|
111
|
+
const right = offsetVertex(point, [-miter[0], -miter[1]], distance, -halfWidth);
|
|
112
|
+
return {incomingLeft: left, outgoingLeft: left, incomingRight: right, outgoingRight: right};
|
|
113
|
+
}
|
|
114
|
+
const innerSide = Math.sign(turn);
|
|
115
|
+
// Short hairpins cannot accommodate an unbounded inner intersection.
|
|
116
|
+
const innerScale = Math.min(1, Math.min(previous.length, next.length) / (2 * miterLength));
|
|
117
|
+
const inner = offsetVertex(
|
|
118
|
+
point,
|
|
119
|
+
[miter[0] * innerSide * innerScale, miter[1] * innerSide * innerScale],
|
|
120
|
+
distance,
|
|
121
|
+
halfWidth * innerSide
|
|
122
|
+
);
|
|
123
|
+
if (innerSide > 0) {
|
|
124
|
+
result.incomingLeft = inner;
|
|
125
|
+
result.outgoingLeft = inner;
|
|
126
|
+
} else {
|
|
127
|
+
result.incomingRight = inner;
|
|
128
|
+
result.outgoingRight = inner;
|
|
129
|
+
}
|
|
130
|
+
const outerStart = innerSide > 0 ? result.incomingRight : result.incomingLeft;
|
|
131
|
+
const outerEnd = innerSide > 0 ? result.outgoingRight : result.outgoingLeft;
|
|
132
|
+
if (join === 'round') {
|
|
133
|
+
const sweep = Math.atan2(turn, cosine);
|
|
134
|
+
const startAngle = Math.atan2(
|
|
135
|
+
outerStart.position[1] - point[1],
|
|
136
|
+
outerStart.position[0] - point[0]
|
|
137
|
+
);
|
|
138
|
+
const steps = Math.max(1, Math.ceil((Math.abs(sweep) / Math.PI) * roundSegments));
|
|
139
|
+
let previousOuter = outerStart;
|
|
140
|
+
for (let step = 1; step <= steps; step++) {
|
|
141
|
+
const angle = startAngle + (sweep * step) / steps;
|
|
142
|
+
const currentOuter =
|
|
143
|
+
step === steps
|
|
144
|
+
? outerEnd
|
|
145
|
+
: offsetVertex(
|
|
146
|
+
point,
|
|
147
|
+
[Math.cos(angle) * halfWidth, Math.sin(angle) * halfWidth],
|
|
148
|
+
distance,
|
|
149
|
+
-halfWidth * innerSide
|
|
150
|
+
);
|
|
151
|
+
appendTriangle(inner, previousOuter, currentOuter);
|
|
152
|
+
previousOuter = currentOuter;
|
|
153
|
+
}
|
|
154
|
+
} else appendTriangle(inner, outerStart, outerEnd);
|
|
155
|
+
return result;
|
|
156
|
+
});
|
|
157
|
+
segments.forEach((_segment, index) => {
|
|
158
|
+
const start = corners[index];
|
|
159
|
+
const end = corners[(index + 1) % corners.length];
|
|
160
|
+
// Duplicate the closing seam with total distance to keep interpolation continuous.
|
|
161
|
+
const closing = closed && index === segments.length - 1;
|
|
162
|
+
const endLeft = closing
|
|
163
|
+
? {...end.incomingLeft, distance: distances.at(-1)!}
|
|
164
|
+
: end.incomingLeft;
|
|
165
|
+
const endRight = closing
|
|
166
|
+
? {...end.incomingRight, distance: distances.at(-1)!}
|
|
167
|
+
: end.incomingRight;
|
|
168
|
+
appendTriangle(start.outgoingLeft, start.outgoingRight, endLeft);
|
|
169
|
+
appendTriangle(start.outgoingRight, endRight, endLeft);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
return new Geometry({
|
|
173
|
+
id: options.id,
|
|
174
|
+
topology: 'triangle-list',
|
|
175
|
+
attributes: {
|
|
176
|
+
POSITION: {size: 3, value: new Float32Array(positions)},
|
|
177
|
+
TEXCOORD_0: {size: 2, value: new Float32Array(coordinates)}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
function appendTriangle(first: StrokeVertex, second: StrokeVertex, third: StrokeVertex): void {
|
|
182
|
+
const area =
|
|
183
|
+
(second.position[0] - first.position[0]) * (third.position[1] - first.position[1]) -
|
|
184
|
+
(second.position[1] - first.position[1]) * (third.position[0] - first.position[0]);
|
|
185
|
+
if (area === 0) return;
|
|
186
|
+
for (const vertex of area > 0 ? [first, second, third] : [first, third, second]) {
|
|
187
|
+
positions.push(...vertex.position);
|
|
188
|
+
coordinates.push(vertex.distance, vertex.across);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function offsetVertex(
|
|
192
|
+
point: Position,
|
|
193
|
+
offset: Direction,
|
|
194
|
+
distance: number,
|
|
195
|
+
across: number
|
|
196
|
+
): StrokeVertex {
|
|
197
|
+
return {position: [point[0] + offset[0], point[1] + offset[1], point[2]], distance, across};
|
|
198
|
+
}
|
|
199
|
+
function createVertex(
|
|
200
|
+
point: Position,
|
|
201
|
+
direction: Direction,
|
|
202
|
+
across: number,
|
|
203
|
+
along: number,
|
|
204
|
+
distance: number
|
|
205
|
+
): StrokeVertex {
|
|
206
|
+
return offsetVertex(
|
|
207
|
+
point,
|
|
208
|
+
[-direction[1] * across + direction[0] * along, direction[0] * across + direction[1] * along],
|
|
209
|
+
distance,
|
|
210
|
+
across
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
function appendCap(
|
|
214
|
+
point: Position,
|
|
215
|
+
direction: Direction,
|
|
216
|
+
distance: number,
|
|
217
|
+
start: boolean
|
|
218
|
+
): void {
|
|
219
|
+
const center: StrokeVertex = {position: point, distance, across: 0};
|
|
220
|
+
const startAngle =
|
|
221
|
+
Math.atan2(direction[1], direction[0]) + (start ? -Math.PI / 2 : Math.PI / 2);
|
|
222
|
+
let previous: StrokeVertex | undefined;
|
|
223
|
+
for (let step = 0; step <= roundSegments; step++) {
|
|
224
|
+
const angle = startAngle - (Math.PI * step) / roundSegments;
|
|
225
|
+
const offset: Direction = [Math.cos(angle) * halfWidth, Math.sin(angle) * halfWidth];
|
|
226
|
+
const current = offsetVertex(
|
|
227
|
+
point,
|
|
228
|
+
offset,
|
|
229
|
+
distance + offset[0] * direction[0] + offset[1] * direction[1],
|
|
230
|
+
-offset[0] * direction[1] + offset[1] * direction[0]
|
|
231
|
+
);
|
|
232
|
+
if (previous) appendTriangle(center, previous, current);
|
|
233
|
+
previous = current;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -91,6 +91,13 @@ export {ModelNode} from './scenegraph/model-node';
|
|
|
91
91
|
// Geometries
|
|
92
92
|
export type {GeometryProps, GeometryAttribute, GeometryAttributeInput} from './geometry/geometry';
|
|
93
93
|
export {Geometry} from './geometry/geometry';
|
|
94
|
+
export {makeEdgeGeometry, makeEdgeGeometryFromGeometries} from './geometry/edge-geometry';
|
|
95
|
+
export type {MakeEdgeGeometryOptions} from './geometry/edge-geometry';
|
|
96
|
+
export {
|
|
97
|
+
makeStrokeGeometry,
|
|
98
|
+
type StrokeGeometryOptions,
|
|
99
|
+
type StrokePosition
|
|
100
|
+
} from './geometry/stroke-geometry';
|
|
94
101
|
export type {MakeInterleavedGeometryOptions} from './geometry/geometry-utils';
|
|
95
102
|
export {makeInterleavedGeometry} from './geometry/geometry-utils';
|
|
96
103
|
export type {
|
|
@@ -103,20 +110,20 @@ export type {GPUGeometryProps} from './geometry/gpu-geometry';
|
|
|
103
110
|
export {GPUGeometry} from './geometry/gpu-geometry';
|
|
104
111
|
|
|
105
112
|
// Primitives
|
|
106
|
-
export type {ConeGeometryProps} from './
|
|
107
|
-
export {ConeGeometry} from './
|
|
108
|
-
export type {CubeGeometryProps} from './
|
|
109
|
-
export {CubeGeometry} from './
|
|
110
|
-
export type {CylinderGeometryProps} from './
|
|
111
|
-
export {CylinderGeometry} from './
|
|
112
|
-
export type {IcoSphereGeometryProps} from './
|
|
113
|
-
export {IcoSphereGeometry} from './
|
|
114
|
-
export type {PlaneGeometryProps} from './
|
|
115
|
-
export {PlaneGeometry} from './
|
|
116
|
-
export type {SphereGeometryProps} from './
|
|
117
|
-
export {SphereGeometry} from './
|
|
118
|
-
export type {TruncatedConeGeometryProps} from './
|
|
119
|
-
export {TruncatedConeGeometry} from './
|
|
113
|
+
export type {ConeGeometryProps} from './geometry/geometry-primitives';
|
|
114
|
+
export {ConeGeometry} from './geometry/geometry-primitives';
|
|
115
|
+
export type {CubeGeometryProps} from './geometry/geometry-primitives';
|
|
116
|
+
export {CubeGeometry} from './geometry/geometry-primitives';
|
|
117
|
+
export type {CylinderGeometryProps} from './geometry/geometry-primitives';
|
|
118
|
+
export {CylinderGeometry} from './geometry/geometry-primitives';
|
|
119
|
+
export type {IcoSphereGeometryProps} from './geometry/geometry-primitives';
|
|
120
|
+
export {IcoSphereGeometry} from './geometry/geometry-primitives';
|
|
121
|
+
export type {PlaneGeometryProps} from './geometry/geometry-primitives';
|
|
122
|
+
export {PlaneGeometry} from './geometry/geometry-primitives';
|
|
123
|
+
export type {SphereGeometryProps} from './geometry/geometry-primitives';
|
|
124
|
+
export {SphereGeometry} from './geometry/geometry-primitives';
|
|
125
|
+
export type {TruncatedConeGeometryProps} from './geometry/geometry-primitives';
|
|
126
|
+
export {TruncatedConeGeometry} from './geometry/geometry-primitives';
|
|
120
127
|
|
|
121
128
|
export {ShaderInputs} from './shader-inputs';
|
|
122
129
|
export type {ShaderInputBinding, ShaderInputsProps} from './shader-inputs';
|
package/src/model/model.ts
CHANGED
|
@@ -53,7 +53,7 @@ import {
|
|
|
53
53
|
ShaderAssembler
|
|
54
54
|
} from '@luma.gl/shadertools';
|
|
55
55
|
|
|
56
|
-
import type {Geometry} from '
|
|
56
|
+
import type {Geometry} from '@math.gl/geometry';
|
|
57
57
|
import {GPUGeometry, makeGPUGeometry} from '../geometry/gpu-geometry';
|
|
58
58
|
import {getDebugTableForShaderLayout} from '../debug/debug-shader-layout';
|
|
59
59
|
import {debugFramebuffer} from '../debug/debug-framebuffer';
|
|
@@ -101,6 +101,8 @@ type AnyShaderLayout = Pick<ShaderLayout | ComputeShaderLayout, 'bindings'>;
|
|
|
101
101
|
|
|
102
102
|
export type ModelProps = Omit<RenderPipelineProps, 'vs' | 'fs' | 'bindings'> & {
|
|
103
103
|
source?: string;
|
|
104
|
+
/** Input language compiled by the application-registered shader assembler transpiler. */
|
|
105
|
+
sourceLanguage?: string;
|
|
104
106
|
vs?: string | null;
|
|
105
107
|
fs?: string | null;
|
|
106
108
|
|
|
@@ -208,6 +210,7 @@ export class Model {
|
|
|
208
210
|
static defaultProps: Required<ModelProps> = {
|
|
209
211
|
...RenderPipeline.defaultProps,
|
|
210
212
|
source: undefined!,
|
|
213
|
+
sourceLanguage: undefined!,
|
|
211
214
|
vs: null,
|
|
212
215
|
fs: null,
|
|
213
216
|
id: 'unnamed',
|
|
@@ -322,7 +325,7 @@ export class Model {
|
|
|
322
325
|
private _dynamicIndexBufferSource: {source: DynamicBuffer; generation: number} | null = null;
|
|
323
326
|
private _dynamicAttributeBufferSources: Record<
|
|
324
327
|
number,
|
|
325
|
-
{source: DynamicBuffer; generation: number}
|
|
328
|
+
{source: DynamicBuffer; generation: number; byteOffset: number}
|
|
326
329
|
> = {};
|
|
327
330
|
private _colorAttachmentFormats: (TextureFormatColor | null)[] | undefined;
|
|
328
331
|
private _depthStencilAttachmentFormat: TextureFormatDepthStencil | undefined;
|
|
@@ -411,6 +414,7 @@ export class Model {
|
|
|
411
414
|
source,
|
|
412
415
|
getUniforms,
|
|
413
416
|
bindingTable,
|
|
417
|
+
entryPoints = {},
|
|
414
418
|
shaderLayout: assembledShaderLayout
|
|
415
419
|
} = shaderAssembler.assembleWGSLShader({
|
|
416
420
|
platformInfo,
|
|
@@ -422,6 +426,8 @@ export class Model {
|
|
|
422
426
|
pluginVaryings: resolvedPlugins.varyings
|
|
423
427
|
});
|
|
424
428
|
this.source = source;
|
|
429
|
+
this.props.vertexEntryPoint = entryPoints.vertex || this.props.vertexEntryPoint;
|
|
430
|
+
this.props.fragmentEntryPoint = entryPoints.fragment || this.props.fragmentEntryPoint;
|
|
425
431
|
// @ts-expect-error
|
|
426
432
|
this._getModuleUniforms = getUniforms;
|
|
427
433
|
this._bindingTable = bindingTable;
|
|
@@ -881,9 +887,14 @@ export class Model {
|
|
|
881
887
|
|
|
882
888
|
/**
|
|
883
889
|
* Sets attributes (buffers)
|
|
890
|
+
* @param options.byteOffsets Optional byte offsets, keyed by buffer name, where each buffer's
|
|
891
|
+
* vertex data starts. Buffers without an entry are bound at offset 0.
|
|
884
892
|
* @note Overrides any attributes previously set with the same name
|
|
885
893
|
*/
|
|
886
|
-
setAttributes(
|
|
894
|
+
setAttributes(
|
|
895
|
+
buffers: Record<string, ModelBuffer>,
|
|
896
|
+
options?: {disableWarnings?: boolean; byteOffsets?: Record<string, number>}
|
|
897
|
+
): void {
|
|
887
898
|
this._drawBlockedReason = false;
|
|
888
899
|
const disableWarnings = options?.disableWarnings ?? this.props.disableWarnings;
|
|
889
900
|
if (buffers['indices']) {
|
|
@@ -931,11 +942,13 @@ export class Model {
|
|
|
931
942
|
continue; // eslint-disable-line no-continue
|
|
932
943
|
}
|
|
933
944
|
|
|
934
|
-
|
|
945
|
+
const byteOffset = options?.byteOffsets?.[bufferName] ?? 0;
|
|
946
|
+
this.vertexArray.setBuffer(bufferSlot, resolvedBuffer, byteOffset);
|
|
935
947
|
if (buffer instanceof DynamicBuffer) {
|
|
936
948
|
this._dynamicAttributeBufferSources[bufferSlot] = {
|
|
937
949
|
source: buffer,
|
|
938
|
-
generation: buffer.generation
|
|
950
|
+
generation: buffer.generation,
|
|
951
|
+
byteOffset
|
|
939
952
|
};
|
|
940
953
|
} else {
|
|
941
954
|
delete this._dynamicAttributeBufferSources[bufferSlot];
|
|
@@ -1341,7 +1354,7 @@ export class Model {
|
|
|
1341
1354
|
|
|
1342
1355
|
for (const [locationKey, entry] of Object.entries(this._dynamicAttributeBufferSources)) {
|
|
1343
1356
|
if (entry.generation !== entry.source.generation) {
|
|
1344
|
-
this.vertexArray.setBuffer(Number(locationKey), entry.source.buffer);
|
|
1357
|
+
this.vertexArray.setBuffer(Number(locationKey), entry.source.buffer, entry.byteOffset);
|
|
1345
1358
|
entry.generation = entry.source.generation;
|
|
1346
1359
|
this.setNeedsRedraw('dynamic attribute buffer');
|
|
1347
1360
|
}
|