@luma.gl/engine 10.0.0-alpha.14 → 10.0.0-alpha.16

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.
Files changed (29) hide show
  1. package/dist/arrow/index.cjs.map +2 -2
  2. package/dist/arrow/renderers/splats/conversion/make-gpu-splat-data-from-gltf.cjs +12 -13
  3. package/dist/arrow/renderers/splats/conversion/make-gpu-splat-data-from-gltf.d.ts.map +1 -1
  4. package/dist/arrow/renderers/splats/conversion/make-gpu-splat-data-from-gltf.js +22 -17
  5. package/dist/arrow/renderers/splats/conversion/make-gpu-splat-data-from-gltf.js.map +1 -1
  6. package/dist/dist.dev.js +0 -37
  7. package/dist/dist.min.js +58 -58
  8. package/dist/lighting/index.cjs +12 -2
  9. package/dist/lighting/index.cjs.map +3 -3
  10. package/dist/lighting/index.d.ts +4 -0
  11. package/dist/lighting/index.d.ts.map +1 -1
  12. package/dist/lighting/index.js +2 -0
  13. package/dist/lighting/index.js.map +1 -1
  14. package/dist/lighting/lightmap-atlas.cjs +246 -0
  15. package/dist/lighting/lightmap-atlas.d.ts +53 -0
  16. package/dist/lighting/lightmap-atlas.d.ts.map +1 -0
  17. package/dist/lighting/lightmap-atlas.js +279 -0
  18. package/dist/lighting/lightmap-atlas.js.map +1 -0
  19. package/dist/lighting/reflection-probe.cjs +321 -0
  20. package/dist/lighting/reflection-probe.d.ts +77 -0
  21. package/dist/lighting/reflection-probe.d.ts.map +1 -0
  22. package/dist/lighting/reflection-probe.js +298 -0
  23. package/dist/lighting/reflection-probe.js.map +1 -0
  24. package/package.json +8 -8
  25. package/src/arrow/renderers/splats/conversion/make-gpu-splat-data-from-gltf.ts +23 -19
  26. package/src/lighting/index.ts +18 -0
  27. package/src/lighting/lightmap-atlas.ts +348 -0
  28. package/src/lighting/reflection-probe.ts +360 -0
  29. package/src/splats/README.md +1 -1
@@ -0,0 +1,360 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
3
+
4
+ import {
5
+ assert,
6
+ Buffer,
7
+ type Device,
8
+ type Framebuffer,
9
+ Texture,
10
+ type TextureView
11
+ } from '@luma.gl/core';
12
+ import {Matrix4, type NumberArray3} from '@math.gl/core';
13
+ import {PBR_ENVIRONMENT_FRAGMENT_WGSL, pbrEnvironmentFilter} from '@luma.gl/shaders';
14
+ import {ClipSpace} from '../models/clip-space';
15
+ import {ShaderInputs} from '../shader-inputs';
16
+
17
+ const FACES: {direction: NumberArray3; up: NumberArray3}[] = [
18
+ {direction: [1, 0, 0], up: [0, -1, 0]},
19
+ {direction: [-1, 0, 0], up: [0, -1, 0]},
20
+ {direction: [0, 1, 0], up: [0, 0, 1]},
21
+ {direction: [0, -1, 0], up: [0, 0, -1]},
22
+ {direction: [0, 0, 1], up: [0, -1, 0]},
23
+ {direction: [0, 0, -1], up: [0, -1, 0]}
24
+ ];
25
+
26
+ export type ReflectionProbeView = {
27
+ face: number;
28
+ framebuffer: Framebuffer;
29
+ /** Face rotation. The renderer subtracts the GPU origin before applying this matrix. */
30
+ viewMatrix: Matrix4;
31
+ projectionMatrix: Matrix4;
32
+ /** One vec4f containing the capture origin; borrowed until the next update cycle. */
33
+ origin: Buffer;
34
+ /** Snapshot of all source poses, shared across the six capture faces. */
35
+ matrices: Buffer;
36
+ };
37
+
38
+ export type ReflectionProbeUpdate = {
39
+ /** Producer buffer with COPY_SRC usage and a column-major float32 matrix at matrixIndex. */
40
+ matrices: Buffer;
41
+ matrixIndex: number;
42
+ /** Last completed frame's measured CPU-to-GPU-completion work, in milliseconds. */
43
+ frameMilliseconds?: number;
44
+ /** When false, finish any current refresh and retain it until the scene changes. */
45
+ refresh?: boolean;
46
+ /** World-space directions from the probe toward the views that sample it. */
47
+ viewDirections?: readonly Readonly<NumberArray3>[];
48
+ draw: (view: ReflectionProbeView) => void;
49
+ };
50
+
51
+ export type ReflectionProbeProps = {
52
+ size?: number;
53
+ sampleCount?: number;
54
+ frameBudgetMilliseconds?: number;
55
+ maximumFacesPerFrame?: number;
56
+ /** Capture and publish in the same frame, with the budget controlling filtering quality. */
57
+ refreshMode?: 'incremental' | 'same-frame';
58
+ /** Number of specular mips, including the sharp capture. Defaults to four, up to the texture limit. */
59
+ mipLevels?: number;
60
+ };
61
+
62
+ /** WebGPU capture and specular filtering with a complete, double-buffered publication. */
63
+ export class ReflectionProbe {
64
+ private readonly device: Device;
65
+ private readonly origin: Buffer;
66
+ private matrixSnapshot?: Buffer;
67
+ private readonly capture: Texture;
68
+ private readonly depth: Texture;
69
+ private readonly targets: [Texture, Texture];
70
+ private readonly views: TextureView[] = [];
71
+ private readonly framebuffers: Framebuffer[] = [];
72
+ private readonly captureTargets: Framebuffer[] = [];
73
+ private readonly filterTargets: Framebuffer[][][] = [];
74
+ private readonly filter: ClipSpace;
75
+ private readonly size: number;
76
+ private readonly sampleCount: number;
77
+ private activeSampleCount: number;
78
+ private readonly budget: number;
79
+ private readonly maximumFaces: number;
80
+ private readonly sameFrame: boolean;
81
+ private readonly projectionMatrix = new Matrix4([
82
+ 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 1
83
+ ]).multiplyRight(new Matrix4().perspective({fovy: Math.PI / 2, aspect: 1, near: 0.05, far: 100}));
84
+ private readonly pendingFaces = new Set<number>();
85
+ private captureCount = 0;
86
+ private filteredCount = 0;
87
+ private front: number | undefined;
88
+ private back = 0;
89
+ private credit = 0;
90
+ private pausedFrames = 0;
91
+ private destroyed = false;
92
+
93
+ constructor(device: Device, props: ReflectionProbeProps = {}) {
94
+ assert(device.type === 'webgpu');
95
+ this.device = device;
96
+ this.size = props.size ?? 32;
97
+ this.sampleCount = props.sampleCount ?? 16;
98
+ this.activeSampleCount = this.sampleCount;
99
+ this.budget = props.frameBudgetMilliseconds ?? 1000 / 60;
100
+ this.maximumFaces = props.maximumFacesPerFrame ?? 2;
101
+ this.sameFrame = props.refreshMode === 'same-frame';
102
+ assert(
103
+ Number.isInteger(this.size) &&
104
+ this.size > 0 &&
105
+ this.size <= device.limits.maxTextureDimension2D
106
+ );
107
+ assert(Number.isInteger(this.sampleCount) && this.sampleCount > 0 && this.sampleCount <= 1024);
108
+ assert(Number.isFinite(this.budget) && this.budget > 0);
109
+ assert(Number.isInteger(this.maximumFaces) && this.maximumFaces > 0 && this.maximumFaces <= 6);
110
+ const capabilities = device.getTextureFormatCapabilities('rgba16float');
111
+ assert(capabilities.render && capabilities.filter);
112
+ const requestedMipLevels = props.mipLevels ?? 4;
113
+ assert(Number.isSafeInteger(requestedMipLevels) && requestedMipLevels > 0);
114
+ const mipLevels = Math.min(requestedMipLevels, device.getMipLevelCount(this.size, this.size));
115
+ this.origin = device.createBuffer({byteLength: 16, usage: Buffer.STORAGE | Buffer.COPY_DST});
116
+ this.capture = this.createCube('reflection-capture', 1);
117
+ this.targets = [
118
+ this.createCube('reflection-filter-a', mipLevels),
119
+ this.createCube('reflection-filter-b', mipLevels)
120
+ ];
121
+ this.depth = device.createTexture({
122
+ width: this.size,
123
+ height: this.size,
124
+ format: 'depth24plus',
125
+ usage: Texture.RENDER
126
+ });
127
+ for (let face = 0; face < FACES.length; face++)
128
+ this.captureTargets.push(this.createTarget(this.capture, face, 0, this.depth));
129
+ for (const texture of this.targets) {
130
+ this.filterTargets.push(
131
+ FACES.map((_, face) =>
132
+ Array.from({length: mipLevels - 1}, (_, mip) => this.createTarget(texture, face, mip + 1))
133
+ )
134
+ );
135
+ }
136
+ this.filter = new ClipSpace(device, {
137
+ id: 'reflection-probe-filter',
138
+ source: PBR_ENVIRONMENT_FRAGMENT_WGSL,
139
+ defines: {PBR_ENVIRONMENT_CUBE_SOURCE: true},
140
+ modules: [pbrEnvironmentFilter],
141
+ shaderInputs: new ShaderInputs({pbrEnvironmentFilter}),
142
+ colorAttachmentFormats: ['rgba16float'],
143
+ parameters: {depthWriteEnabled: false}
144
+ });
145
+ }
146
+
147
+ get texture(): Texture | undefined {
148
+ return this.front === undefined ? undefined : this.targets[this.front];
149
+ }
150
+
151
+ /**
152
+ * Record capture and filtering before the main draw. A budget estimate controls work or quality.
153
+ * Returns the number of face work units recorded. Submit the frame before calling again.
154
+ */
155
+ update(options: ReflectionProbeUpdate): number {
156
+ assert(!this.destroyed);
157
+ assert(options.matrices.device === this.device && options.matrices.usage & Buffer.COPY_SRC);
158
+ assert(
159
+ Number.isSafeInteger(options.matrixIndex) &&
160
+ options.matrixIndex >= 0 &&
161
+ (options.matrixIndex + 1) * 64 <= options.matrices.byteLength
162
+ );
163
+ if (options.refresh === false && this.front !== undefined && this.captureCount === 0) return 0;
164
+ const snapshotChanged = this.matrixSnapshot?.byteLength !== options.matrices.byteLength;
165
+ if (snapshotChanged) {
166
+ this.matrixSnapshot?.destroy();
167
+ this.matrixSnapshot = this.device.createBuffer({
168
+ byteLength: options.matrices.byteLength,
169
+ usage: Buffer.STORAGE | Buffer.COPY_DST | Buffer.COPY_SRC
170
+ });
171
+ this.captureCount = 0;
172
+ this.filteredCount = 0;
173
+ this.pendingFaces.clear();
174
+ }
175
+ const measured = options.frameMilliseconds;
176
+ const headroom =
177
+ measured !== undefined && Number.isFinite(measured)
178
+ ? Math.max(0, Math.min(this.budget, this.budget - measured))
179
+ : this.budget / 4;
180
+ let available: number;
181
+ if (this.sameFrame) {
182
+ // Keep the probe in step with the scene. Recover quality only with spare
183
+ // headroom so the sample count stays stable near the frame budget.
184
+ if (measured !== undefined && Number.isFinite(measured)) {
185
+ if (measured > this.budget) {
186
+ this.activeSampleCount = Math.max(
187
+ Math.min(4, this.sampleCount),
188
+ Math.floor(this.activeSampleCount / 2)
189
+ );
190
+ } else if (headroom >= this.budget / 4) {
191
+ this.activeSampleCount = Math.min(this.sampleCount, this.activeSampleCount + 1);
192
+ }
193
+ }
194
+ available = FACES.length * 2;
195
+ } else {
196
+ this.credit = Math.min(this.maximumFaces, this.credit + headroom);
197
+ available = Math.floor(this.credit);
198
+ if (available === 0 && ++this.pausedFrames >= 3) available = 1;
199
+ if (available === 0) return 0;
200
+ this.credit = Math.max(0, this.credit - available);
201
+ }
202
+ this.pausedFrames = 0;
203
+
204
+ if (this.captureCount === 0 && this.filteredCount === 0) {
205
+ this.device.commandEncoder.copyBufferToBuffer({
206
+ sourceBuffer: options.matrices,
207
+ destinationBuffer: this.matrixSnapshot!,
208
+ size: options.matrices.byteLength
209
+ });
210
+ this.device.commandEncoder.copyBufferToBuffer({
211
+ sourceBuffer: this.matrixSnapshot!,
212
+ sourceOffset: options.matrixIndex * 64 + 48,
213
+ destinationBuffer: this.origin,
214
+ size: 16
215
+ });
216
+ for (let face = 0; face < FACES.length; face++) this.pendingFaces.add(face);
217
+ }
218
+ let used = 0;
219
+ while (used < available && this.captureCount < FACES.length) {
220
+ const face = this.nextFace(options.viewDirections ?? []);
221
+ const {direction, up} = FACES[face];
222
+ options.draw({
223
+ face,
224
+ framebuffer: this.captureTargets[face],
225
+ viewMatrix: new Matrix4().lookAt({eye: [0, 0, 0], center: direction, up}),
226
+ projectionMatrix: this.projectionMatrix,
227
+ matrices: this.matrixSnapshot!,
228
+ origin: this.origin
229
+ });
230
+ this.pendingFaces.delete(face);
231
+ this.captureCount++;
232
+ used++;
233
+ }
234
+ while (used < available && this.filteredCount < FACES.length) {
235
+ this.filterFace(this.filteredCount);
236
+ this.filteredCount++;
237
+ used++;
238
+ }
239
+ if (this.filteredCount === FACES.length) {
240
+ this.front = this.back;
241
+ this.back = 1 - this.back;
242
+ this.captureCount = 0;
243
+ this.filteredCount = 0;
244
+ }
245
+ return used;
246
+ }
247
+
248
+ destroy(): void {
249
+ if (this.destroyed) return;
250
+ this.destroyed = true;
251
+ this.filter.destroy();
252
+ for (const framebuffer of this.framebuffers) framebuffer.destroy();
253
+ for (const view of this.views) view.destroy();
254
+ for (const target of this.targets) target.destroy();
255
+ this.capture.destroy();
256
+ this.depth.destroy();
257
+ this.origin.destroy();
258
+ this.matrixSnapshot?.destroy();
259
+ }
260
+
261
+ private nextFace(directions: readonly Readonly<NumberArray3>[]): number {
262
+ let bestFace = -1;
263
+ let bestScore = -Infinity;
264
+ for (const face of this.pendingFaces) {
265
+ const normal = FACES[face].direction;
266
+ const score = directions.length
267
+ ? Math.max(
268
+ ...directions.map(direction => {
269
+ const length = Math.hypot(...direction);
270
+ return length > 0
271
+ ? (normal[0] * direction[0] + normal[1] * direction[1] + normal[2] * direction[2]) /
272
+ length
273
+ : 0;
274
+ })
275
+ )
276
+ : 0;
277
+ if (score > bestScore) {
278
+ bestScore = score;
279
+ bestFace = face;
280
+ }
281
+ }
282
+ return bestFace;
283
+ }
284
+
285
+ private createCube(id: string, mipLevels: number): Texture {
286
+ return this.device.createTexture({
287
+ id,
288
+ dimension: 'cube',
289
+ width: this.size,
290
+ height: this.size,
291
+ depth: 6,
292
+ format: 'rgba16float',
293
+ mipLevels,
294
+ usage: Texture.RENDER | Texture.SAMPLE | Texture.COPY_SRC | Texture.COPY_DST,
295
+ sampler: {minFilter: 'linear', magFilter: 'linear', mipmapFilter: 'linear'}
296
+ });
297
+ }
298
+
299
+ private createTarget(
300
+ texture: Texture,
301
+ face: number,
302
+ mip: number,
303
+ depth: Texture | null = null
304
+ ): Framebuffer {
305
+ const view = texture.createView({
306
+ dimension: '2d',
307
+ baseArrayLayer: face,
308
+ arrayLayerCount: 1,
309
+ baseMipLevel: mip,
310
+ mipLevelCount: 1
311
+ });
312
+ this.views.push(view);
313
+ const size = Math.max(1, this.size >> mip);
314
+ const framebuffer = this.device.createFramebuffer({
315
+ width: size,
316
+ height: size,
317
+ colorAttachments: [view],
318
+ depthStencilAttachment: depth
319
+ });
320
+ this.framebuffers.push(framebuffer);
321
+ return framebuffer;
322
+ }
323
+
324
+ private filterFace(face: number): void {
325
+ const targets = this.filterTargets[this.back][face];
326
+ this.device.commandEncoder.copyTextureToTexture({
327
+ sourceTexture: this.capture,
328
+ destinationTexture: this.targets[this.back],
329
+ origin: [0, 0, face],
330
+ destinationOrigin: [0, 0, face],
331
+ width: this.size,
332
+ height: this.size,
333
+ depthOrArrayLayers: 1
334
+ });
335
+ for (const [index, framebuffer] of targets.entries()) {
336
+ const mip = index + 1;
337
+ this.filter.shaderInputs.setProps({
338
+ pbrEnvironmentFilter: {
339
+ face,
340
+ roughness: mip / targets.length,
341
+ mode: 0,
342
+ sampleCount: this.activeSampleCount,
343
+ sourceEncoding: 0,
344
+ pbrEnvironmentSource: this.capture
345
+ }
346
+ });
347
+ this.filter.predraw(this.device.commandEncoder);
348
+ const pass = this.device.beginRenderPass({
349
+ framebuffer,
350
+ clearColor: [0, 0, 0, 1],
351
+ clearDepth: false
352
+ });
353
+ try {
354
+ this.filter.draw(pass);
355
+ } finally {
356
+ pass.end();
357
+ }
358
+ }
359
+ }
360
+ }
@@ -57,7 +57,7 @@ import type {GLTFWithBuffers} from '@luma.gl/engine/gltf';
57
57
  import {getGLTFSplatSceneInstances, makeGPUSplatDataFromGLTFScene} from '@luma.gl/engine/arrow';
58
58
 
59
59
  const loaded = await load('scene.glb', [GLTFLoader, SPZLoader], {
60
- splats: {shape: 'arrow-table', targetCoordinateSystem: 'LUF'}
60
+ splats: {shape: 'arrow', targetCoordinateSystem: 'LUF'}
61
61
  });
62
62
  const gltf = postProcessGLTF(loaded as GLTFWithBuffers);
63
63
  const scenegraphs = createScenegraphsFromGLTF(device, gltf, {