@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
|
@@ -43,6 +43,8 @@ const LOG_DRAW_TIMEOUT = 10000;
|
|
|
43
43
|
|
|
44
44
|
export type ComputationProps = Omit<ComputePipelineProps, 'shader'> & {
|
|
45
45
|
source?: string;
|
|
46
|
+
/** Input language compiled by the application-registered shader assembler transpiler. */
|
|
47
|
+
sourceLanguage?: string;
|
|
46
48
|
|
|
47
49
|
/** Shadertools shader modules added to shader code. */
|
|
48
50
|
modules?: ShaderModule[];
|
|
@@ -114,6 +116,7 @@ export class Computation {
|
|
|
114
116
|
userData: {},
|
|
115
117
|
|
|
116
118
|
source: '',
|
|
119
|
+
sourceLanguage: undefined!,
|
|
117
120
|
modules: [],
|
|
118
121
|
defines: {},
|
|
119
122
|
plugins: [],
|
|
@@ -212,17 +215,21 @@ export class Computation {
|
|
|
212
215
|
const {
|
|
213
216
|
source,
|
|
214
217
|
getUniforms,
|
|
218
|
+
entryPoints = {},
|
|
215
219
|
shaderLayout: assembledShaderLayout
|
|
216
220
|
} = shaderAssembler.assembleWGSLShader({
|
|
217
221
|
platformInfo,
|
|
218
222
|
...this.props,
|
|
219
223
|
modules,
|
|
220
224
|
defines,
|
|
225
|
+
shaderStage: 'compute',
|
|
226
|
+
computeEntryPoint: this.props.entryPoint,
|
|
221
227
|
scanVertexAttributes: false,
|
|
222
228
|
pluginInjections: resolvedPlugins.injections
|
|
223
229
|
});
|
|
224
230
|
|
|
225
231
|
this.source = source;
|
|
232
|
+
this.props.entryPoint = entryPoints.compute || this.props.entryPoint;
|
|
226
233
|
// @ts-ignore
|
|
227
234
|
this._getModuleUniforms = getUniforms;
|
|
228
235
|
const inferredShaderLayout =
|
|
@@ -0,0 +1,208 @@
|
|
|
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 type {TypedArray} from '@math.gl/core';
|
|
7
|
+
import type {Geometry as MathGeometry} from '@math.gl/geometry';
|
|
8
|
+
import {Geometry, type GeometryAttribute} from './geometry';
|
|
9
|
+
|
|
10
|
+
export type MakeEdgeGeometryOptions = {
|
|
11
|
+
/** Keep coplanar internal edges to inspect the original triangulation. Defaults to false. */
|
|
12
|
+
includeCoplanarEdges?: boolean;
|
|
13
|
+
/** Minimum angle between adjacent face normals, in degrees. Defaults to 30. */
|
|
14
|
+
angleThreshold?: number;
|
|
15
|
+
/** Position welding distance in source units. Zero (default) welds exact duplicates. */
|
|
16
|
+
weldTolerance?: number;
|
|
17
|
+
/** Packed three-component CPU position attribute. Defaults to POSITION. */
|
|
18
|
+
positionAttribute?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type Position = [number, number, number];
|
|
22
|
+
type Edge = {start: number; end: number; normal: Position; faceCount: number; crease: boolean};
|
|
23
|
+
|
|
24
|
+
/** Extracts boundary, crease, and non-manifold edges from a triangle-list CPU geometry. */
|
|
25
|
+
export function makeEdgeGeometry(
|
|
26
|
+
geometry: MathGeometry,
|
|
27
|
+
options: MakeEdgeGeometryOptions = {}
|
|
28
|
+
): Geometry {
|
|
29
|
+
const {angleThreshold = 30, weldTolerance = 0, positionAttribute = 'POSITION'} = options;
|
|
30
|
+
const positions = getTrianglePositions(geometry, positionAttribute);
|
|
31
|
+
// Thresholds use source units and degrees, respectively.
|
|
32
|
+
assert(Number.isFinite(weldTolerance) && weldTolerance >= 0);
|
|
33
|
+
assert(Number.isFinite(angleThreshold) && angleThreshold >= 0 && angleThreshold <= 180);
|
|
34
|
+
|
|
35
|
+
const sourceIndices = geometry.indices?.value;
|
|
36
|
+
const sourceCount = positions.value.length / 3;
|
|
37
|
+
const representatives = new Map<number, number>();
|
|
38
|
+
const buckets = new Map<string, number[]>();
|
|
39
|
+
const faces = new Set<string>();
|
|
40
|
+
const edges = new Map<string, Edge>();
|
|
41
|
+
const cosineThreshold = Math.cos((angleThreshold * Math.PI) / 180);
|
|
42
|
+
const toleranceSquared = weldTolerance * weldTolerance;
|
|
43
|
+
|
|
44
|
+
function readPosition(index: number): Position {
|
|
45
|
+
return [
|
|
46
|
+
positions!.value[index * 3],
|
|
47
|
+
positions!.value[index * 3 + 1],
|
|
48
|
+
positions!.value[index * 3 + 2]
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function weldPosition(index: number): number {
|
|
53
|
+
const cached = representatives.get(index);
|
|
54
|
+
if (cached !== undefined) return cached;
|
|
55
|
+
// Invalid source indices must not silently produce NaN geometry.
|
|
56
|
+
assert(Number.isInteger(index) && index >= 0 && index < sourceCount);
|
|
57
|
+
const position = readPosition(index);
|
|
58
|
+
assert(position.every(Number.isFinite));
|
|
59
|
+
const cell =
|
|
60
|
+
weldTolerance > 0 ? position.map(value => Math.floor(value / weldTolerance)) : position;
|
|
61
|
+
const radius = weldTolerance > 0 ? 1 : 0;
|
|
62
|
+
for (let east = -radius; east <= radius; east++) {
|
|
63
|
+
for (let north = -radius; north <= radius; north++) {
|
|
64
|
+
for (let up = -radius; up <= radius; up++) {
|
|
65
|
+
const candidates = buckets.get(`${cell[0] + east},${cell[1] + north},${cell[2] + up}`);
|
|
66
|
+
for (const candidate of candidates || []) {
|
|
67
|
+
const other = readPosition(candidate);
|
|
68
|
+
const distanceSquared =
|
|
69
|
+
(position[0] - other[0]) ** 2 +
|
|
70
|
+
(position[1] - other[1]) ** 2 +
|
|
71
|
+
(position[2] - other[2]) ** 2;
|
|
72
|
+
if (distanceSquared <= toleranceSquared) {
|
|
73
|
+
representatives.set(index, candidate);
|
|
74
|
+
return candidate;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const key = cell.join(',');
|
|
81
|
+
const bucket = buckets.get(key) || [];
|
|
82
|
+
bucket.push(index);
|
|
83
|
+
buckets.set(key, bucket);
|
|
84
|
+
representatives.set(index, index);
|
|
85
|
+
return index;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function addEdge(first: number, second: number, normal: Position): void {
|
|
89
|
+
const start = Math.min(first, second);
|
|
90
|
+
const end = Math.max(first, second);
|
|
91
|
+
const key = `${start},${end}`;
|
|
92
|
+
const edge = edges.get(key);
|
|
93
|
+
if (edge) {
|
|
94
|
+
edge.faceCount++;
|
|
95
|
+
const cosine =
|
|
96
|
+
normal[0] * edge.normal[0] + normal[1] * edge.normal[1] + normal[2] * edge.normal[2];
|
|
97
|
+
// Suppress numerical noise on coplanar triangles, including a zero-degree threshold.
|
|
98
|
+
edge.crease ||= cosine < 1 - 1e-7 && cosine <= cosineThreshold + 1e-7;
|
|
99
|
+
} else {
|
|
100
|
+
edges.set(key, {start, end, normal, faceCount: 1, crease: false});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
for (let offset = 0; offset < geometry.vertexCount; offset += 3) {
|
|
105
|
+
const first = weldPosition(sourceIndices?.[offset] ?? offset);
|
|
106
|
+
const second = weldPosition(sourceIndices?.[offset + 1] ?? offset + 1);
|
|
107
|
+
const third = weldPosition(sourceIndices?.[offset + 2] ?? offset + 2);
|
|
108
|
+
if (first === second || second === third || third === first) continue;
|
|
109
|
+
const faceKey = [first, second, third].sort((left, right) => left - right).join(',');
|
|
110
|
+
if (faces.has(faceKey)) continue;
|
|
111
|
+
faces.add(faceKey);
|
|
112
|
+
const start = readPosition(first);
|
|
113
|
+
const end = readPosition(second);
|
|
114
|
+
const corner = readPosition(third);
|
|
115
|
+
const firstDirection = end.map((value, axis) => value - start[axis]);
|
|
116
|
+
const secondDirection = corner.map((value, axis) => value - start[axis]);
|
|
117
|
+
const normal: Position = [
|
|
118
|
+
firstDirection[1] * secondDirection[2] - firstDirection[2] * secondDirection[1],
|
|
119
|
+
firstDirection[2] * secondDirection[0] - firstDirection[0] * secondDirection[2],
|
|
120
|
+
firstDirection[0] * secondDirection[1] - firstDirection[1] * secondDirection[0]
|
|
121
|
+
];
|
|
122
|
+
const magnitude = Math.hypot(...normal);
|
|
123
|
+
if (magnitude === 0) continue;
|
|
124
|
+
normal[0] /= magnitude;
|
|
125
|
+
normal[1] /= magnitude;
|
|
126
|
+
normal[2] /= magnitude;
|
|
127
|
+
addEdge(first, second, normal);
|
|
128
|
+
addEdge(second, third, normal);
|
|
129
|
+
addEdge(third, first, normal);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const selected = [...edges.values()]
|
|
133
|
+
.filter(edge => options.includeCoplanarEdges || edge.faceCount !== 2 || edge.crease)
|
|
134
|
+
.sort((left, right) => left.start - right.start || left.end - right.end);
|
|
135
|
+
return new Geometry({
|
|
136
|
+
id: `${geometry.id}-edges`,
|
|
137
|
+
topology: 'line-list',
|
|
138
|
+
attributes: {[positionAttribute]: positions},
|
|
139
|
+
indices: new Uint32Array(selected.flatMap(edge => [edge.start, edge.end]))
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Classifies adjacent meshes together to suppress coplanar tile seams and duplicate edges. */
|
|
144
|
+
export function makeEdgeGeometryFromGeometries(
|
|
145
|
+
geometries: readonly MathGeometry[],
|
|
146
|
+
options: MakeEdgeGeometryOptions = {}
|
|
147
|
+
): Geometry {
|
|
148
|
+
// A batch needs at least one source to define its position storage type.
|
|
149
|
+
assert(geometries.length > 0);
|
|
150
|
+
const positionAttribute = options.positionAttribute ?? 'POSITION';
|
|
151
|
+
const attributes = geometries.map(geometry => getTrianglePositions(geometry, positionAttribute));
|
|
152
|
+
const firstAttribute = attributes[0];
|
|
153
|
+
// Preserve source precision and normalization; callers convert incompatible inputs explicitly.
|
|
154
|
+
assert(
|
|
155
|
+
attributes.every(
|
|
156
|
+
attribute =>
|
|
157
|
+
attribute.value.constructor === firstAttribute.value.constructor &&
|
|
158
|
+
Boolean(attribute['normalized']) === Boolean(firstAttribute['normalized'])
|
|
159
|
+
)
|
|
160
|
+
);
|
|
161
|
+
const PositionArray = firstAttribute.value.constructor as {new (length: number): TypedArray};
|
|
162
|
+
const positions = new PositionArray(
|
|
163
|
+
attributes.reduce((count, attribute) => count + attribute.value.length, 0)
|
|
164
|
+
);
|
|
165
|
+
const indices = new Uint32Array(
|
|
166
|
+
geometries.reduce((count, geometry) => count + geometry.vertexCount, 0)
|
|
167
|
+
);
|
|
168
|
+
let positionOffset = 0;
|
|
169
|
+
let indexOffset = 0;
|
|
170
|
+
for (const [geometryIndex, geometry] of geometries.entries()) {
|
|
171
|
+
const attribute = attributes[geometryIndex];
|
|
172
|
+
positions.set(attribute.value, positionOffset);
|
|
173
|
+
for (let offset = 0; offset < geometry.vertexCount; offset++) {
|
|
174
|
+
const index = geometry.indices?.value[offset] ?? offset;
|
|
175
|
+
// Validate before offsetting: an invalid tile index could otherwise address its neighbor.
|
|
176
|
+
assert(Number.isInteger(index) && index >= 0 && index < attribute.value.length / 3);
|
|
177
|
+
indices[indexOffset++] = index + positionOffset / 3;
|
|
178
|
+
}
|
|
179
|
+
positionOffset += attribute.value.length;
|
|
180
|
+
}
|
|
181
|
+
return makeEdgeGeometry(
|
|
182
|
+
new Geometry({
|
|
183
|
+
id: `${geometries[0].id}-batch`,
|
|
184
|
+
topology: 'triangle-list',
|
|
185
|
+
attributes: {[positionAttribute]: {...firstAttribute, value: positions}},
|
|
186
|
+
indices
|
|
187
|
+
}),
|
|
188
|
+
options
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function getTrianglePositions(
|
|
193
|
+
geometry: MathGeometry,
|
|
194
|
+
positionAttribute: string
|
|
195
|
+
): GeometryAttribute {
|
|
196
|
+
const positions = geometry.attributes[positionAttribute];
|
|
197
|
+
// Edge extraction requires packed xyz positions and complete triangle-list primitives.
|
|
198
|
+
assert(geometry.topology === 'triangle-list' && positions?.size === 3);
|
|
199
|
+
assert(positions.value.length % 3 === 0 && geometry.vertexCount % 3 === 0);
|
|
200
|
+
assert(
|
|
201
|
+
!positions['byteStride'] || positions['byteStride'] === positions.value.BYTES_PER_ELEMENT * 3
|
|
202
|
+
);
|
|
203
|
+
assert(
|
|
204
|
+
geometry.vertexCount >= 0 &&
|
|
205
|
+
geometry.vertexCount <= (geometry.indices?.value.length ?? positions.value.length / 3)
|
|
206
|
+
);
|
|
207
|
+
return positions;
|
|
208
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
CubeGeometry as MathCubeGeometry,
|
|
7
|
+
SphereGeometry as MathSphereGeometry,
|
|
8
|
+
IcoSphereGeometry as MathIcoSphereGeometry,
|
|
9
|
+
PlaneGeometry as MathPlaneGeometry,
|
|
10
|
+
TruncatedConeGeometry as MathTruncatedConeGeometry,
|
|
11
|
+
unpackIndexedGeometry,
|
|
12
|
+
type Geometry as MathGeometry,
|
|
13
|
+
type GeometryAttributeInput,
|
|
14
|
+
type SphereGeometryProps,
|
|
15
|
+
type IcoSphereGeometryProps,
|
|
16
|
+
type TruncatedConeGeometryProps
|
|
17
|
+
} from '@math.gl/geometry';
|
|
18
|
+
import {Geometry} from './geometry';
|
|
19
|
+
|
|
20
|
+
export type {SphereGeometryProps, IcoSphereGeometryProps, TruncatedConeGeometryProps};
|
|
21
|
+
|
|
22
|
+
export type CubeGeometryProps = {
|
|
23
|
+
id?: string;
|
|
24
|
+
indices?: boolean;
|
|
25
|
+
attributes?: Record<string, GeometryAttributeInput>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Compatibility defaults and face metadata over the math.gl box tessellator. */
|
|
29
|
+
export class CubeGeometry extends Geometry {
|
|
30
|
+
constructor(props: CubeGeometryProps = {}) {
|
|
31
|
+
const source = new MathCubeGeometry({size: 2});
|
|
32
|
+
const faceIndices = new Uint32Array(24);
|
|
33
|
+
// The renderer's semantic face order is +Z, -Z, +Y, -Y, +X, -X.
|
|
34
|
+
const faceOrder = [4, 5, 2, 3, 0, 1];
|
|
35
|
+
for (let vertexIndex = 0; vertexIndex < faceIndices.length; vertexIndex++) {
|
|
36
|
+
faceIndices[vertexIndex] = faceOrder[Math.floor(vertexIndex / 4)];
|
|
37
|
+
}
|
|
38
|
+
source.attributes['faceIndex'] = {size: 1, value: faceIndices};
|
|
39
|
+
if (props.indices === false) {
|
|
40
|
+
source.attributes['COLOR_0'] = {
|
|
41
|
+
size: 3,
|
|
42
|
+
value: new Float32Array(
|
|
43
|
+
Array.from(source.attributes['POSITION'].value, value => (value + 1) / 2)
|
|
44
|
+
)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const data = props.indices === false ? unpackIndexedGeometry(source) : source;
|
|
48
|
+
super({
|
|
49
|
+
id: props.id,
|
|
50
|
+
topology: source.topology,
|
|
51
|
+
indices: props.indices === false ? undefined : source.indices,
|
|
52
|
+
attributes: mergeAttributes(data.attributes, props.attributes)
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class SphereGeometry extends Geometry {
|
|
58
|
+
constructor(props: SphereGeometryProps = {}) {
|
|
59
|
+
const source = new MathSphereGeometry({
|
|
60
|
+
...props,
|
|
61
|
+
attributes: undefined,
|
|
62
|
+
radius: props.radius ?? 1
|
|
63
|
+
});
|
|
64
|
+
// Retain the legacy longitude-to-UV mapping while math.gl tessellates the sphere.
|
|
65
|
+
for (const name of ['POSITION', 'NORMAL']) {
|
|
66
|
+
const values = source.attributes[name].value;
|
|
67
|
+
for (let index = 0; index < values.length; index += 3) {
|
|
68
|
+
const horizontal = values[index];
|
|
69
|
+
values[index] = values[index + 2];
|
|
70
|
+
values[index + 2] = horizontal;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const indices = source.indices?.value;
|
|
74
|
+
if (indices) {
|
|
75
|
+
for (let index = 0; index < indices.length; index += 3) {
|
|
76
|
+
const second = indices[index + 1];
|
|
77
|
+
indices[index + 1] = indices[index + 2];
|
|
78
|
+
indices[index + 2] = second;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
super({...source, attributes: {...source.attributes, ...props.attributes}});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export class IcoSphereGeometry extends Geometry {
|
|
86
|
+
constructor(props: IcoSphereGeometryProps = {}) {
|
|
87
|
+
const source = new MathIcoSphereGeometry({...props, radius: props.radius ?? 1});
|
|
88
|
+
super({...source, attributes: source.attributes});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export class TruncatedConeGeometry extends Geometry {
|
|
93
|
+
constructor(props: TruncatedConeGeometryProps = {}) {
|
|
94
|
+
const source = new MathTruncatedConeGeometry({...props, bottomRadius: props.bottomRadius ?? 1});
|
|
95
|
+
super({...source, attributes: source.attributes});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type CylinderGeometryProps = Omit<
|
|
100
|
+
TruncatedConeGeometryProps,
|
|
101
|
+
'topRadius' | 'bottomRadius'
|
|
102
|
+
> & {
|
|
103
|
+
radius?: number;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export class CylinderGeometry extends TruncatedConeGeometry {
|
|
107
|
+
constructor(props: CylinderGeometryProps = {}) {
|
|
108
|
+
const radius = props.radius ?? 1;
|
|
109
|
+
super({...props, topRadius: radius, bottomRadius: radius});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type ConeGeometryProps = Omit<
|
|
114
|
+
TruncatedConeGeometryProps,
|
|
115
|
+
'topRadius' | 'bottomRadius' | 'topCap' | 'bottomCap'
|
|
116
|
+
> & {radius?: number; cap?: boolean};
|
|
117
|
+
|
|
118
|
+
export class ConeGeometry extends TruncatedConeGeometry {
|
|
119
|
+
constructor(props: ConeGeometryProps = {}) {
|
|
120
|
+
const cap = props.cap ?? true;
|
|
121
|
+
super({...props, topRadius: 0, bottomRadius: props.radius ?? 1, topCap: cap, bottomCap: cap});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export type PlaneGeometryProps = {
|
|
126
|
+
id?: string;
|
|
127
|
+
radius?: number;
|
|
128
|
+
attributes?: Record<string, GeometryAttributeInput>;
|
|
129
|
+
type?: 'x,y' | 'x,z' | 'y,z';
|
|
130
|
+
offset?: number;
|
|
131
|
+
flipCull?: boolean;
|
|
132
|
+
unpack?: boolean;
|
|
133
|
+
xlen?: number;
|
|
134
|
+
ylen?: number;
|
|
135
|
+
zlen?: number;
|
|
136
|
+
nx?: number;
|
|
137
|
+
ny?: number;
|
|
138
|
+
nz?: number;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** Adapts legacy plane axes and facing to math.gl's XZ plane. */
|
|
142
|
+
export class PlaneGeometry extends Geometry {
|
|
143
|
+
constructor(props: PlaneGeometryProps = {}) {
|
|
144
|
+
const {type = 'x,y', offset = 0, flipCull = false} = props;
|
|
145
|
+
const planeAxes = {
|
|
146
|
+
'x,y': {sizeX: props.xlen, sizeZ: props.ylen, nx: props.nx, nz: props.ny},
|
|
147
|
+
'x,z': {sizeX: props.xlen, sizeZ: props.zlen, nx: props.nx, nz: props.nz},
|
|
148
|
+
'y,z': {sizeX: props.ylen, sizeZ: props.zlen, nx: props.ny, nz: props.nz}
|
|
149
|
+
}[type];
|
|
150
|
+
// Only the three legacy axis pairs are supported.
|
|
151
|
+
if (!planeAxes) throw new Error('Invalid plane axes');
|
|
152
|
+
const source = new MathPlaneGeometry({
|
|
153
|
+
sizeX: planeAxes.sizeX || 1,
|
|
154
|
+
sizeZ: planeAxes.sizeZ || 1,
|
|
155
|
+
nx: planeAxes.nx || 1,
|
|
156
|
+
nz: planeAxes.nz || 1
|
|
157
|
+
});
|
|
158
|
+
orientPlane(source, type, offset, flipCull);
|
|
159
|
+
const data = props.unpack ? unpackIndexedGeometry(source) : source;
|
|
160
|
+
super({
|
|
161
|
+
id: props.id,
|
|
162
|
+
topology: source.topology,
|
|
163
|
+
indices: props.unpack ? undefined : source.indices,
|
|
164
|
+
attributes: mergeAttributes(data.attributes, props.attributes)
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function orientPlane(
|
|
170
|
+
geometry: MathGeometry,
|
|
171
|
+
type: NonNullable<PlaneGeometryProps['type']>,
|
|
172
|
+
offset: number,
|
|
173
|
+
flipCull: boolean
|
|
174
|
+
): void {
|
|
175
|
+
const positions = geometry.attributes['POSITION'].value;
|
|
176
|
+
const normals = geometry.attributes['NORMAL'].value;
|
|
177
|
+
const texCoords = geometry.attributes['TEXCOORD_0'].value;
|
|
178
|
+
for (let vertexIndex = 0; vertexIndex < positions.length / 3; vertexIndex++) {
|
|
179
|
+
const positionIndex = vertexIndex * 3;
|
|
180
|
+
const horizontal = Number(positions[positionIndex]) * (flipCull ? -1 : 1);
|
|
181
|
+
const vertical = -Number(positions[positionIndex + 2]);
|
|
182
|
+
const normal = flipCull ? 1 : -1;
|
|
183
|
+
if (type === 'x,y') {
|
|
184
|
+
positions.set([horizontal, vertical, offset], positionIndex);
|
|
185
|
+
normals.set([0, 0, normal], positionIndex);
|
|
186
|
+
} else if (type === 'x,z') {
|
|
187
|
+
positions.set([horizontal, offset, vertical], positionIndex);
|
|
188
|
+
normals.set([0, normal, 0], positionIndex);
|
|
189
|
+
} else {
|
|
190
|
+
positions.set([offset, horizontal, vertical], positionIndex);
|
|
191
|
+
normals.set([normal, 0, 0], positionIndex);
|
|
192
|
+
}
|
|
193
|
+
if (flipCull) texCoords[vertexIndex * 2] = 1 - Number(texCoords[vertexIndex * 2]);
|
|
194
|
+
}
|
|
195
|
+
// The legacy XY and YZ planes face the negative perpendicular axis.
|
|
196
|
+
if (type !== 'x,z' && geometry.indices) {
|
|
197
|
+
const indices = geometry.indices.value;
|
|
198
|
+
for (let index = 0; index < indices.length; index += 3) {
|
|
199
|
+
const second = indices[index + 1];
|
|
200
|
+
indices[index + 1] = indices[index + 2];
|
|
201
|
+
indices[index + 2] = second;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function mergeAttributes(
|
|
207
|
+
source: Record<string, GeometryAttributeInput | undefined>,
|
|
208
|
+
overrides: Record<string, GeometryAttributeInput> = {}
|
|
209
|
+
): Record<string, GeometryAttributeInput> {
|
|
210
|
+
const attributes: Record<string, GeometryAttributeInput> = {};
|
|
211
|
+
for (const [name, attribute] of Object.entries(source)) {
|
|
212
|
+
if (attribute) attributes[name] = attribute;
|
|
213
|
+
}
|
|
214
|
+
return {...attributes, ...overrides};
|
|
215
|
+
}
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import type {VertexFormat} from '@luma.gl/core';
|
|
5
|
+
import type {BufferLayout, VertexFormat} from '@luma.gl/core';
|
|
6
6
|
import {vertexFormatDecoder} from '@luma.gl/core';
|
|
7
7
|
import type {TypedArray} from '@math.gl/core';
|
|
8
|
+
import type {Geometry as MathGeometry} from '@math.gl/geometry';
|
|
9
|
+
export {unpackIndexedGeometry} from '@math.gl/geometry';
|
|
8
10
|
import {Geometry, getGeometryShaderAttributeName, type GeometryAttribute} from './geometry';
|
|
9
11
|
|
|
10
12
|
type TypedArrayConstructor = {
|
|
@@ -13,11 +15,6 @@ type TypedArrayConstructor = {
|
|
|
13
15
|
readonly BYTES_PER_ELEMENT: number;
|
|
14
16
|
};
|
|
15
17
|
|
|
16
|
-
type GeometryLike = {
|
|
17
|
-
indices?: GeometryAttribute;
|
|
18
|
-
attributes: Record<string, GeometryAttribute | undefined>;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
18
|
/** Options for {@link makeInterleavedGeometry}. */
|
|
22
19
|
export type MakeInterleavedGeometryOptions = {
|
|
23
20
|
/** Name of the packed geometry buffer. Defaults to `geometry`. */
|
|
@@ -44,48 +41,6 @@ type InterleavedAttribute = {
|
|
|
44
41
|
byteLength: number;
|
|
45
42
|
};
|
|
46
43
|
|
|
47
|
-
/**
|
|
48
|
-
* Expands indexed geometry attributes into non-indexed attributes.
|
|
49
|
-
*
|
|
50
|
-
* The returned object keeps the original attribute keys and replaces each non-constant attribute
|
|
51
|
-
* with data expanded through the index buffer. The `indices` field is intentionally omitted from
|
|
52
|
-
* the returned geometry-like object.
|
|
53
|
-
*/
|
|
54
|
-
export function unpackIndexedGeometry<T extends GeometryLike>(geometry: T): GeometryLike {
|
|
55
|
-
const {indices, attributes} = geometry;
|
|
56
|
-
if (!indices) {
|
|
57
|
-
return geometry;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const vertexCount = indices.value.length;
|
|
61
|
-
const unpackedAttributes: Record<string, GeometryAttribute> = {};
|
|
62
|
-
|
|
63
|
-
for (const attributeName in attributes) {
|
|
64
|
-
const attribute = attributes[attributeName];
|
|
65
|
-
if (!attribute) {
|
|
66
|
-
continue; // eslint-disable-line
|
|
67
|
-
}
|
|
68
|
-
const {value, size} = attribute;
|
|
69
|
-
const constant = attribute['constant'];
|
|
70
|
-
if (constant || !size) {
|
|
71
|
-
continue; // eslint-disable-line
|
|
72
|
-
}
|
|
73
|
-
const ArrayType = value.constructor as TypedArrayConstructor;
|
|
74
|
-
const unpackedValue = new ArrayType(vertexCount * size);
|
|
75
|
-
for (let x = 0; x < vertexCount; ++x) {
|
|
76
|
-
const index = indices.value[x];
|
|
77
|
-
for (let i = 0; i < size; i++) {
|
|
78
|
-
unpackedValue[x * size + i] = value[index * size + i];
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
unpackedAttributes[attributeName] = {size, value: unpackedValue};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return {
|
|
85
|
-
attributes: Object.assign({}, attributes, unpackedAttributes)
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
44
|
/**
|
|
90
45
|
* Packs a CPU {@link Geometry} into one interleaved vertex buffer.
|
|
91
46
|
*
|
|
@@ -95,7 +50,7 @@ export function unpackIndexedGeometry<T extends GeometryLike>(geometry: T): Geom
|
|
|
95
50
|
* idempotent and returns the original instance.
|
|
96
51
|
*/
|
|
97
52
|
export function makeInterleavedGeometry(
|
|
98
|
-
geometry:
|
|
53
|
+
geometry: MathGeometry & {bufferLayout?: BufferLayout[]},
|
|
99
54
|
options: MakeInterleavedGeometryOptions = {}
|
|
100
55
|
): Geometry {
|
|
101
56
|
const bufferName = options.bufferName || 'geometry';
|
|
@@ -183,8 +138,11 @@ export function makeInterleavedGeometry(
|
|
|
183
138
|
});
|
|
184
139
|
}
|
|
185
140
|
|
|
186
|
-
function isInterleavedGeometry(
|
|
187
|
-
|
|
141
|
+
function isInterleavedGeometry(
|
|
142
|
+
geometry: MathGeometry & {bufferLayout?: BufferLayout[]},
|
|
143
|
+
bufferName: string
|
|
144
|
+
): geometry is Geometry {
|
|
145
|
+
if (geometry.bufferLayout?.length !== 1) {
|
|
188
146
|
return false;
|
|
189
147
|
}
|
|
190
148
|
|
|
@@ -197,13 +155,16 @@ function isInterleavedGeometry(geometry: Geometry, bufferName: string): boolean
|
|
|
197
155
|
}
|
|
198
156
|
|
|
199
157
|
function getInterleavedSourceAttributes(
|
|
200
|
-
geometry:
|
|
158
|
+
geometry: MathGeometry,
|
|
201
159
|
attributeNames?: string[]
|
|
202
160
|
): Array<[string, GeometryAttribute | undefined]> {
|
|
203
|
-
|
|
204
|
-
|
|
161
|
+
const attributes = new Map<string, [string, GeometryAttribute]>();
|
|
162
|
+
const sourceNames = attributeNames || Object.keys(geometry.attributes);
|
|
163
|
+
for (const name of sourceNames) {
|
|
164
|
+
const attribute = geometry.attributes[name];
|
|
165
|
+
if (attribute) attributes.set(getGeometryShaderAttributeName(name), [name, attribute]);
|
|
205
166
|
}
|
|
206
|
-
return
|
|
167
|
+
return Array.from(attributes.values());
|
|
207
168
|
}
|
|
208
169
|
|
|
209
170
|
function writeInterleavedAttribute(
|
|
@@ -236,19 +197,3 @@ function writeInterleavedAttribute(
|
|
|
236
197
|
function alignTo(byteOffset: number, alignment: number): number {
|
|
237
198
|
return Math.ceil(byteOffset / alignment) * alignment;
|
|
238
199
|
}
|
|
239
|
-
|
|
240
|
-
// export function calculateVertexNormals(positions: Float32Array): Uint8Array {
|
|
241
|
-
// let normals = new Uint8Array(positions.length / 3);
|
|
242
|
-
|
|
243
|
-
// for (let i = 0; i < positions.length; i++) {
|
|
244
|
-
// const vec1 = new Vector3(positions.subarray(i * 3, i + 0, i + 3));
|
|
245
|
-
// const vec2 = new Vector3(positions.subarray(i + 3, i + 6));
|
|
246
|
-
// const vec3 = new Vector3(positions.subarray(i + 6, i + 9));
|
|
247
|
-
|
|
248
|
-
// const normal = new Vector3(vec1).cross(vec2).normalize();
|
|
249
|
-
// normals.set(normal[0], i + 4);
|
|
250
|
-
// normals.set(normal[1], i + 4 + 1);
|
|
251
|
-
// normals.set(normal[2], i + 2);
|
|
252
|
-
// }
|
|
253
|
-
// const normal = new Vector3(vec1).cross(vec2).normalize();
|
|
254
|
-
// }
|