@luma.gl/engine 10.0.0-alpha.2 → 10.0.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.
@@ -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 {
@@ -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(buffers: Record<string, ModelBuffer>, options?: {disableWarnings?: boolean}): void {
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
- this.vertexArray.setBuffer(bufferSlot, resolvedBuffer);
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
  }