@luma.gl/engine 9.0.0-alpha.5 → 9.0.0-alpha.7
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/animation/timeline.d.ts +1 -1
- package/dist/animation/timeline.d.ts.map +1 -1
- package/dist/animation/timeline.js.map +1 -1
- package/dist/geometries/cube-geometry.d.ts.map +1 -1
- package/dist/geometries/cube-geometry.js +1 -0
- package/dist/geometries/cube-geometry.js.map +1 -1
- package/dist/geometries/ico-sphere-geometry.js.map +1 -1
- package/dist/geometry/geometry.d.ts +20 -29
- package/dist/geometry/geometry.d.ts.map +1 -1
- package/dist/geometry/geometry.js +20 -23
- package/dist/geometry/geometry.js.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/animation-loop.d.ts +12 -14
- package/dist/lib/animation-loop.d.ts.map +1 -1
- package/dist/lib/animation-loop.js +69 -43
- package/dist/lib/animation-loop.js.map +1 -1
- package/dist/lib/animation-props.d.ts +4 -4
- package/dist/lib/animation-props.d.ts.map +1 -1
- package/dist/lib/model.d.ts +6 -6
- package/dist/lib/model.d.ts.map +1 -1
- package/dist/lib/model.js +16 -10
- package/dist/lib/model.js.map +1 -1
- package/dist/lib/pipeline-factory.d.ts +15 -10
- package/dist/lib/pipeline-factory.d.ts.map +1 -1
- package/dist/lib/pipeline-factory.js +47 -19
- package/dist/lib/pipeline-factory.js.map +1 -1
- package/dist/lib/render-loop.d.ts +1 -1
- package/dist/lib/render-loop.d.ts.map +1 -1
- package/dist/lib/render-loop.js +2 -2
- package/dist/lib/render-loop.js.map +1 -1
- package/package.json +5 -5
- package/src/animation/timeline.ts +1 -1
- package/src/geometries/cube-geometry.ts +1 -0
- package/src/geometries/ico-sphere-geometry.ts +1 -1
- package/src/geometry/geometry.ts +54 -49
- package/src/index.ts +8 -6
- package/src/lib/animation-loop.ts +61 -52
- package/src/lib/animation-props.ts +4 -4
- package/src/lib/model.ts +20 -16
- package/src/lib/pipeline-factory.ts +59 -50
- package/src/lib/render-loop.ts +3 -3
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
2
|
import { luma } from '@luma.gl/api';
|
|
3
3
|
import { requestAnimationFrame, cancelAnimationFrame } from '@luma.gl/api';
|
|
4
|
+
import { Stats } from '@probe.gl/stats';
|
|
4
5
|
import { isBrowser } from '@probe.gl/env';
|
|
5
6
|
const isPage = isBrowser() && typeof document !== 'undefined';
|
|
6
7
|
let statIdCounter = 0;
|
|
7
8
|
const DEFAULT_ANIMATION_LOOP_PROPS = {
|
|
8
9
|
onCreateDevice: props => luma.createDevice(props),
|
|
9
|
-
onAddHTML:
|
|
10
|
+
onAddHTML: () => '',
|
|
10
11
|
onInitialize: () => ({}),
|
|
11
12
|
onRender: () => {},
|
|
12
13
|
onFinalize: () => {},
|
|
13
14
|
onError: error => console.error(error),
|
|
14
|
-
device:
|
|
15
|
+
device: null,
|
|
15
16
|
deviceProps: {},
|
|
16
17
|
debug: false,
|
|
17
18
|
stats: luma.stats.get("animation-loop-".concat(statIdCounter++)),
|
|
@@ -19,15 +20,15 @@ const DEFAULT_ANIMATION_LOOP_PROPS = {
|
|
|
19
20
|
autoResizeViewport: true,
|
|
20
21
|
autoResizeDrawingBuffer: true
|
|
21
22
|
};
|
|
22
|
-
export
|
|
23
|
+
export class AnimationLoop {
|
|
23
24
|
constructor(props = {}) {
|
|
24
|
-
_defineProperty(this, "device",
|
|
25
|
+
_defineProperty(this, "device", null);
|
|
25
26
|
|
|
26
|
-
_defineProperty(this, "canvas",
|
|
27
|
+
_defineProperty(this, "canvas", null);
|
|
27
28
|
|
|
28
29
|
_defineProperty(this, "props", void 0);
|
|
29
30
|
|
|
30
|
-
_defineProperty(this, "animationProps",
|
|
31
|
+
_defineProperty(this, "animationProps", null);
|
|
31
32
|
|
|
32
33
|
_defineProperty(this, "timeline", null);
|
|
33
34
|
|
|
@@ -62,9 +63,11 @@ export default class AnimationLoop {
|
|
|
62
63
|
let {
|
|
63
64
|
useDevicePixels = true
|
|
64
65
|
} = this.props;
|
|
65
|
-
this.device = props.device;
|
|
66
|
+
this.device = props.device || null;
|
|
66
67
|
this.gl = this.device && this.device.gl || props.gl;
|
|
67
|
-
this.stats = props.stats
|
|
68
|
+
this.stats = props.stats || new Stats({
|
|
69
|
+
id: 'animation-loop-stats'
|
|
70
|
+
});
|
|
68
71
|
this.cpuTime = this.stats.get('CPU Time');
|
|
69
72
|
this.gpuTime = this.stats.get('GPU Time');
|
|
70
73
|
this.frameRate = this.stats.get('Frame Rate');
|
|
@@ -96,15 +99,15 @@ export default class AnimationLoop {
|
|
|
96
99
|
|
|
97
100
|
setProps(props) {
|
|
98
101
|
if ('autoResizeViewport' in props) {
|
|
99
|
-
this.props.autoResizeViewport = props.autoResizeViewport;
|
|
102
|
+
this.props.autoResizeViewport = props.autoResizeViewport || false;
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
if ('autoResizeDrawingBuffer' in props) {
|
|
103
|
-
this.props.autoResizeDrawingBuffer = props.autoResizeDrawingBuffer;
|
|
106
|
+
this.props.autoResizeDrawingBuffer = props.autoResizeDrawingBuffer || false;
|
|
104
107
|
}
|
|
105
108
|
|
|
106
109
|
if ('useDevicePixels' in props) {
|
|
107
|
-
this.props.useDevicePixels = props.useDevicePixels;
|
|
110
|
+
this.props.useDevicePixels = props.useDevicePixels || false;
|
|
108
111
|
}
|
|
109
112
|
|
|
110
113
|
return this;
|
|
@@ -130,7 +133,7 @@ export default class AnimationLoop {
|
|
|
130
133
|
|
|
131
134
|
this._initialize();
|
|
132
135
|
|
|
133
|
-
await this.onInitialize(this.
|
|
136
|
+
await this.props.onInitialize(this._getAnimationProps());
|
|
134
137
|
}
|
|
135
138
|
|
|
136
139
|
if (!this._running) {
|
|
@@ -152,7 +155,9 @@ export default class AnimationLoop {
|
|
|
152
155
|
}
|
|
153
156
|
|
|
154
157
|
redraw() {
|
|
155
|
-
|
|
158
|
+
var _this$device;
|
|
159
|
+
|
|
160
|
+
if ((_this$device = this.device) !== null && _this$device !== void 0 && _this$device.isLost) {
|
|
156
161
|
return this;
|
|
157
162
|
}
|
|
158
163
|
|
|
@@ -160,9 +165,9 @@ export default class AnimationLoop {
|
|
|
160
165
|
|
|
161
166
|
this._setupFrame();
|
|
162
167
|
|
|
163
|
-
this.
|
|
168
|
+
this._updateAnimationProps();
|
|
164
169
|
|
|
165
|
-
this._renderFrame(this.
|
|
170
|
+
this._renderFrame(this._getAnimationProps());
|
|
166
171
|
|
|
167
172
|
this._clearNeedsRedraw();
|
|
168
173
|
|
|
@@ -180,7 +185,9 @@ export default class AnimationLoop {
|
|
|
180
185
|
|
|
181
186
|
stop() {
|
|
182
187
|
if (this._running) {
|
|
183
|
-
|
|
188
|
+
if (this.animationProps) {
|
|
189
|
+
this.props.onFinalize(this.animationProps);
|
|
190
|
+
}
|
|
184
191
|
|
|
185
192
|
this._cancelAnimationFrame();
|
|
186
193
|
|
|
@@ -216,31 +223,20 @@ export default class AnimationLoop {
|
|
|
216
223
|
async toDataURL() {
|
|
217
224
|
this.setNeedsRedraw('toDataURL');
|
|
218
225
|
await this.waitForRender();
|
|
219
|
-
return this.canvas.toDataURL();
|
|
220
|
-
}
|
|
221
226
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
onInitialize(animationProps) {
|
|
227
|
-
return this.props.onInitialize(animationProps);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
onRender(animationProps) {
|
|
231
|
-
return this.props.onRender(animationProps);
|
|
232
|
-
}
|
|
227
|
+
if (this.canvas instanceof HTMLCanvasElement) {
|
|
228
|
+
return this.canvas.toDataURL();
|
|
229
|
+
}
|
|
233
230
|
|
|
234
|
-
|
|
235
|
-
return this.props.onFinalize(animationProps);
|
|
231
|
+
throw new Error('OffscreenCanvas');
|
|
236
232
|
}
|
|
237
233
|
|
|
238
234
|
_initialize() {
|
|
239
235
|
this._startEventHandling();
|
|
240
236
|
|
|
241
|
-
this.
|
|
237
|
+
this._initializeAnimationProps();
|
|
242
238
|
|
|
243
|
-
this.
|
|
239
|
+
this._updateAnimationProps();
|
|
244
240
|
|
|
245
241
|
this._resizeCanvasDrawingBuffer();
|
|
246
242
|
|
|
@@ -294,11 +290,11 @@ export default class AnimationLoop {
|
|
|
294
290
|
return;
|
|
295
291
|
}
|
|
296
292
|
|
|
297
|
-
this.onRender(props);
|
|
293
|
+
this.props.onRender(props);
|
|
298
294
|
}
|
|
299
295
|
|
|
300
296
|
_clearNeedsRedraw() {
|
|
301
|
-
this.needsRedraw =
|
|
297
|
+
this.needsRedraw = false;
|
|
302
298
|
}
|
|
303
299
|
|
|
304
300
|
_setupFrame() {
|
|
@@ -307,14 +303,18 @@ export default class AnimationLoop {
|
|
|
307
303
|
this._resizeViewport();
|
|
308
304
|
}
|
|
309
305
|
|
|
310
|
-
|
|
306
|
+
_initializeAnimationProps() {
|
|
307
|
+
if (!this.device) {
|
|
308
|
+
throw new Error('loop');
|
|
309
|
+
}
|
|
310
|
+
|
|
311
311
|
this.animationProps = {
|
|
312
312
|
animationLoop: this,
|
|
313
313
|
device: this.device,
|
|
314
314
|
canvas: this.device.canvasContext.canvas,
|
|
315
315
|
timeline: this.timeline,
|
|
316
316
|
useDevicePixels: this.props.useDevicePixels,
|
|
317
|
-
needsRedraw:
|
|
317
|
+
needsRedraw: false,
|
|
318
318
|
width: 1,
|
|
319
319
|
height: 1,
|
|
320
320
|
aspect: 1,
|
|
@@ -327,7 +327,19 @@ export default class AnimationLoop {
|
|
|
327
327
|
};
|
|
328
328
|
}
|
|
329
329
|
|
|
330
|
-
|
|
330
|
+
_getAnimationProps() {
|
|
331
|
+
if (!this.animationProps) {
|
|
332
|
+
throw new Error('animationProps');
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return this.animationProps;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
_updateAnimationProps() {
|
|
339
|
+
if (!this.animationProps) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
|
|
331
343
|
const {
|
|
332
344
|
width,
|
|
333
345
|
height,
|
|
@@ -361,7 +373,7 @@ export default class AnimationLoop {
|
|
|
361
373
|
const deviceProps = { ...this.props,
|
|
362
374
|
...this.props.deviceProps
|
|
363
375
|
};
|
|
364
|
-
this.device = await this.onCreateDevice(deviceProps);
|
|
376
|
+
this.device = await this.props.onCreateDevice(deviceProps);
|
|
365
377
|
this.canvas = this.device.canvasContext.canvas;
|
|
366
378
|
|
|
367
379
|
this._createInfoDiv();
|
|
@@ -378,7 +390,11 @@ export default class AnimationLoop {
|
|
|
378
390
|
div.style.bottom = '10px';
|
|
379
391
|
div.style.width = '300px';
|
|
380
392
|
div.style.background = 'white';
|
|
381
|
-
|
|
393
|
+
|
|
394
|
+
if (this.canvas instanceof HTMLCanvasElement) {
|
|
395
|
+
wrapperDiv.appendChild(this.canvas);
|
|
396
|
+
}
|
|
397
|
+
|
|
382
398
|
wrapperDiv.appendChild(div);
|
|
383
399
|
const html = this.props.onAddHTML(div);
|
|
384
400
|
|
|
@@ -389,6 +405,14 @@ export default class AnimationLoop {
|
|
|
389
405
|
}
|
|
390
406
|
|
|
391
407
|
_getSizeAndAspect() {
|
|
408
|
+
if (!this.device) {
|
|
409
|
+
return {
|
|
410
|
+
width: 1,
|
|
411
|
+
height: 1,
|
|
412
|
+
aspect: 1
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
|
|
392
416
|
const [width, height] = this.device.canvasContext.getPixelSize();
|
|
393
417
|
let aspect = 1;
|
|
394
418
|
const canvas = this.device.canvasContext.canvas;
|
|
@@ -414,7 +438,9 @@ export default class AnimationLoop {
|
|
|
414
438
|
|
|
415
439
|
_resizeCanvasDrawingBuffer() {
|
|
416
440
|
if (this.props.autoResizeDrawingBuffer) {
|
|
417
|
-
|
|
441
|
+
var _this$device2;
|
|
442
|
+
|
|
443
|
+
(_this$device2 = this.device) === null || _this$device2 === void 0 ? void 0 : _this$device2.canvasContext.resize({
|
|
418
444
|
useDevicePixels: this.props.useDevicePixels
|
|
419
445
|
});
|
|
420
446
|
}
|
|
@@ -437,11 +463,11 @@ export default class AnimationLoop {
|
|
|
437
463
|
}
|
|
438
464
|
|
|
439
465
|
_onMousemove(e) {
|
|
440
|
-
this.
|
|
466
|
+
this._getAnimationProps()._mousePosition = [e.offsetX, e.offsetY];
|
|
441
467
|
}
|
|
442
468
|
|
|
443
469
|
_onMouseleave(e) {
|
|
444
|
-
this.
|
|
470
|
+
this._getAnimationProps()._mousePosition = null;
|
|
445
471
|
}
|
|
446
472
|
|
|
447
473
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/animation-loop.ts"],"names":["luma","requestAnimationFrame","cancelAnimationFrame","isBrowser","isPage","document","statIdCounter","DEFAULT_ANIMATION_LOOP_PROPS","onCreateDevice","props","createDevice","onAddHTML","undefined","onInitialize","onRender","onFinalize","onError","error","console","device","deviceProps","debug","stats","get","useDevicePixels","autoResizeViewport","autoResizeDrawingBuffer","AnimationLoop","constructor","gl","cpuTime","gpuTime","frameRate","setProps","start","bind","stop","_onMousemove","_onMouseleave","destroy","_setDisplay","delete","setNeedsRedraw","reason","needsRedraw","_running","appContext","_initialized","_createDevice","_initialize","animationProps","_cancelAnimationFrame","_requestAnimationFrame","err","Error","redraw","isLost","_beginTimers","_setupFrame","_updateCallbackData","_renderFrame","_clearNeedsRedraw","_resolveNextFrame","_nextFramePromise","_endTimers","attachTimeline","timeline","detachTimeline","waitForRender","Promise","resolve","toDataURL","canvas","_startEventHandling","_initializeCallbackData","_resizeCanvasDrawingBuffer","_resizeViewport","display","animationLoop","_animationFrameId","_animationFrame","canvasContext","width","height","aspect","time","startTime","Date","now","engineTime","tick","tock","_mousePosition","_getSizeAndAspect","update","Math","floor","getTime","_createInfoDiv","wrapperDiv","createElement","body","appendChild","style","position","div","left","bottom","background","html","innerHTML","getPixelSize","clientHeight","clientWidth","viewport","drawingBufferWidth","drawingBufferHeight","resize","timeEnd","timeStart","addEventListener","e","offsetX","offsetY"],"mappings":";AAAA,SAAQA,IAAR,QAAwC,cAAxC;AACA,SAAQC,qBAAR,EAA+BC,oBAA/B,QAA0D,cAA1D;AAIA,SAAQC,SAAR,QAAwB,eAAxB;AAEA,MAAMC,MAAM,GAAGD,SAAS,MAAM,OAAOE,QAAP,KAAoB,WAAlD;AAEA,IAAIC,aAAa,GAAG,CAApB;AAwBA,MAAMC,4BAA0D,GAAG;AACjEC,EAAAA,cAAc,EAAGC,KAAD,IAAyCT,IAAI,CAACU,YAAL,CAAkBD,KAAlB,CADQ;AAEjEE,EAAAA,SAAS,EAAEC,SAFsD;AAGjEC,EAAAA,YAAY,EAAE,OAAO,EAAP,CAHmD;AAIjEC,EAAAA,QAAQ,EAAE,MAAM,CAAE,CAJ+C;AAKjEC,EAAAA,UAAU,EAAE,MAAM,CAAE,CAL6C;AAMjEC,EAAAA,OAAO,EAAGC,KAAD,IAAWC,OAAO,CAACD,KAAR,CAAcA,KAAd,CAN6C;AAQjEE,EAAAA,MAAM,EAAEP,SARyD;AASjEQ,EAAAA,WAAW,EAAE,EAToD;AAUjEC,EAAAA,KAAK,EAAE,KAV0D;AAWjEC,EAAAA,KAAK,EAAEtB,IAAI,CAACsB,KAAL,CAAWC,GAAX,0BAAiCjB,aAAa,EAA9C,EAX0D;AAcjEkB,EAAAA,eAAe,EAAE,IAdgD;AAejEC,EAAAA,kBAAkB,EAAE,IAf6C;AAgBjEC,EAAAA,uBAAuB,EAAE;AAhBwC,CAAnE;AAoBA,eAAe,MAAMC,aAAN,CAAoB;AA4BjCC,EAAAA,WAAW,CAACnB,KAAyB,GAAG,EAA7B,EAAiC;AAAA;;AAAA;;AAAA;;AAAA;;AAAA,sCAtBvB,IAsBuB;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA,yCAdf,aAce;;AA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WAAKA,MAAL,CAAYgD,gBAAZ,CAA6B,WAA7B,EAA0C,KAAKnF,YAA/C;AACA,WAAKmC,MAAL,CAAYgD,gBAAZ,CAA6B,YAA7B,EAA2C,KAAKlF,aAAhD;AACD;AACF;;AAEDD,EAAAA,YAAY,CAACoF,CAAD,EAAI;AACd,SAAKvE,cAAL,CAAoB0C,cAApB,GAAqC,CAAC6B,CAAC,CAACC,OAAH,EAAYD,CAAC,CAACE,OAAd,CAArC;AACD;;AACDrF,EAAAA,aAAa,CAACmF,CAAD,EAAI;AACf,SAAKvE,cAAL,CAAoB0C,cAApB,GAAqC,IAArC;AACD;;AAhegC","sourcesContent":["import {luma, Device, DeviceProps} from '@luma.gl/api';\nimport {requestAnimationFrame, cancelAnimationFrame} from '@luma.gl/api';\nimport {Timeline} from '../animation/timeline';\nimport {AnimationProps} from '../lib/animation-props';\nimport {Stats, Stat} from '@probe.gl/stats';\nimport {isBrowser} from '@probe.gl/env';\n\nconst isPage = isBrowser() && typeof document !== 'undefined';\n\nlet statIdCounter = 0;\n\ntype ContextProps = DeviceProps;\n\n/** AnimationLoop properties */\nexport type AnimationLoopProps = {\n onCreateDevice?: (props: DeviceProps) => Promise<Device>;\n onAddHTML?: (div: HTMLDivElement) => string; // innerHTML\n onInitialize?: (animationProps: AnimationProps) => void;\n onRender?: (animationProps: AnimationProps) => void;\n onFinalize?: (animationProps: AnimationProps) => void;\n onError?: (reason: Error) => void;\n\n device?: Device;\n deviceProps?: DeviceProps;\n stats?: Stats;\n\n // view parameters\n debug?: boolean;\n autoResizeViewport?: boolean;\n autoResizeDrawingBuffer?: boolean;\n useDevicePixels?: number | boolean;\n};\n\nconst DEFAULT_ANIMATION_LOOP_PROPS: Required<AnimationLoopProps> = {\n onCreateDevice: (props: DeviceProps): Promise<Device> => luma.createDevice(props),\n onAddHTML: undefined,\n onInitialize: () => ({}),\n onRender: () => {},\n onFinalize: () => {},\n onError: (error) => console.error(error), // eslint-disable-line no-console\n\n device: undefined,\n deviceProps: {},\n debug: false,\n stats: luma.stats.get(`animation-loop-${statIdCounter++}`),\n\n // view parameters\n useDevicePixels: true,\n autoResizeViewport: true,\n autoResizeDrawingBuffer: true,\n};\n\n/** Convenient animation loop */\nexport default class AnimationLoop {\n device: Device;\n canvas: HTMLCanvasElement; // | OffscreenCanvas;\n\n props: Required<AnimationLoopProps>;\n animationProps: AnimationProps;\n timeline: Timeline = null;\n stats: Stats;\n cpuTime: Stat;\n gpuTime: Stat;\n frameRate: Stat;\n\n display: any;\n\n needsRedraw: string | null = 'initialized';\n\n _initialized: boolean = false;\n _running: boolean = false;\n _animationFrameId = null;\n _nextFramePromise: Promise<AnimationLoop> | null = null;\n _resolveNextFrame: ((AnimationLoop) => void) | null = null;\n _cpuStartTime: number = 0;\n\n // _gpuTimeQuery: Query | null = null;\n\n /*\n * @param {HTMLCanvasElement} canvas - if provided, width and height will be passed to context\n */\n constructor(props: AnimationLoopProps = {}) {\n this.props = {...DEFAULT_ANIMATION_LOOP_PROPS, ...props};\n props = this.props;\n\n let {useDevicePixels = true} = this.props;\n\n // state\n this.device = props.device;\n // @ts-expect-error\n this.gl = (this.device && this.device.gl) || props.gl;\n\n this.stats = props.stats;\n this.cpuTime = this.stats.get('CPU Time');\n this.gpuTime = this.stats.get('GPU Time');\n this.frameRate = this.stats.get('Frame Rate');\n\n this.setProps({\n autoResizeViewport: props.autoResizeViewport,\n autoResizeDrawingBuffer: props.autoResizeDrawingBuffer,\n useDevicePixels\n });\n\n // Bind methods\n this.start = this.start.bind(this);\n this.stop = this.stop.bind(this);\n\n this._onMousemove = this._onMousemove.bind(this);\n this._onMouseleave = this._onMouseleave.bind(this);\n }\n\n destroy(): void {\n this.stop();\n this._setDisplay(null);\n }\n\n /** @deprecated Use .destroy() */\n delete(): void {\n this.destroy();\n }\n\n setNeedsRedraw(reason: string): this {\n this.needsRedraw = this.needsRedraw || reason;\n return this;\n }\n\n // TODO - move to CanvasContext\n setProps(props: AnimationLoopProps): this {\n if ('autoResizeViewport' in props) {\n this.props.autoResizeViewport = props.autoResizeViewport;\n }\n if ('autoResizeDrawingBuffer' in props) {\n this.props.autoResizeDrawingBuffer = props.autoResizeDrawingBuffer;\n }\n if ('useDevicePixels' in props) {\n this.props.useDevicePixels = props.useDevicePixels;\n }\n return this;\n }\n\n /** Starts a render loop if not already running */\n async start() {\n if (this._running) {\n return this;\n }\n this._running = true;\n\n try {\n // check that we haven't been stopped\n if (!this._running) {\n return null;\n }\n\n let appContext;\n if (!this._initialized) {\n this._initialized = true;\n // Create the WebGL context\n await this._createDevice();\n this._initialize();\n\n // Note: onIntialize can return a promise (e.g. in case app needs to load resources)\n await this.onInitialize(this.animationProps);\n }\n\n // check that we haven't been stopped\n if (!this._running) {\n return null;\n }\n\n // Start the loop\n if (appContext !== false) {\n // cancel any pending renders to ensure only one loop can ever run\n this._cancelAnimationFrame();\n this._requestAnimationFrame();\n }\n\n return this;\n } catch (err: unknown) {\n const error = err instanceof Error ? err : new Error('Unknown error')\n this.props.onError(error);\n // this._running = false; // TODO\n throw error;\n }\n }\n\n /** Explicitly draw a frame */\n redraw(): this {\n if (this.device.isLost) {\n return this;\n }\n\n this._beginTimers();\n\n this._setupFrame();\n this._updateCallbackData();\n\n this._renderFrame(this.animationProps);\n\n // clear needsRedraw flag\n this._clearNeedsRedraw();\n\n if (this._resolveNextFrame) {\n this._resolveNextFrame(this);\n this._nextFramePromise = null;\n this._resolveNextFrame = null;\n }\n\n this._endTimers();\n\n return this;\n }\n\n // Stops a render loop if already running, finalizing\n stop() {\n // console.debug(`Stopping ${this.constructor.name}`);\n if (this._running) {\n // call callback\n this.onFinalize(this.animationProps);\n\n this._cancelAnimationFrame();\n this._nextFramePromise = null;\n this._resolveNextFrame = null;\n this._running = false;\n }\n return this;\n }\n\n attachTimeline(timeline: Timeline): Timeline {\n this.timeline = timeline;\n return this.timeline;\n }\n\n detachTimeline(): void {\n this.timeline = null;\n }\n\n waitForRender(): Promise<AnimationLoop> {\n this.setNeedsRedraw('waitForRender');\n\n if (!this._nextFramePromise) {\n this._nextFramePromise = new Promise((resolve) => {\n this._resolveNextFrame = resolve;\n });\n }\n return this._nextFramePromise;\n }\n\n async toDataURL() {\n this.setNeedsRedraw('toDataURL');\n await this.waitForRender();\n return this.canvas.toDataURL();\n }\n\n onCreateDevice(deviceProps: DeviceProps): Promise<Device> {\n return this.props.onCreateDevice(deviceProps);\n }\n\n onInitialize(animationProps: AnimationProps): {} | void {\n return this.props.onInitialize(animationProps);\n }\n\n onRender(animationProps: AnimationProps) {\n return this.props.onRender(animationProps);\n }\n\n onFinalize(animationProps: AnimationProps) {\n return this.props.onFinalize(animationProps);\n }\n\n // PRIVATE METHODS\n\n _initialize() {\n this._startEventHandling();\n\n // Initialize the callback data\n this._initializeCallbackData();\n this._updateCallbackData();\n\n // Default viewport setup, in case onInitialize wants to render\n this._resizeCanvasDrawingBuffer();\n this._resizeViewport();\n\n // this._gpuTimeQuery = Query.isSupported(this.gl, ['timers']) ? new Query(this.gl) : null;\n }\n\n _setDisplay(display) {\n if (this.display) {\n this.display.delete();\n this.display.animationLoop = null;\n }\n\n // store animation loop on the display\n if (display) {\n display.animationLoop = this;\n }\n\n this.display = display;\n }\n\n _requestAnimationFrame() {\n if (!this._running) {\n return;\n }\n\n // VR display has a separate animation frame to sync with headset\n // TODO WebVR API discontinued, replaced by WebXR: https://immersive-web.github.io/webxr/\n // See https://developer.mozilla.org/en-US/docs/Web/API/VRDisplay/requestAnimationFrame\n // if (this.display && this.display.requestAnimationFrame) {\n // this._animationFrameId = this.display.requestAnimationFrame(this._animationFrame.bind(this));\n // }\n this._animationFrameId = requestAnimationFrame(this._animationFrame.bind(this));\n }\n\n _cancelAnimationFrame() {\n if (this._animationFrameId !== null) {\n return;\n }\n\n // VR display has a separate animation frame to sync with headset\n // TODO WebVR API discontinued, replaced by WebXR: https://immersive-web.github.io/webxr/\n // See https://developer.mozilla.org/en-US/docs/Web/API/VRDisplay/requestAnimationFrame\n // if (this.display && this.display.cancelAnimationFrame) {\n // this.display.cancelAnimationFrame(this._animationFrameId);\n // }\n cancelAnimationFrame(this._animationFrameId);\n this._animationFrameId = null;\n }\n\n _animationFrame() {\n if (!this._running) {\n return;\n }\n this.redraw();\n this._requestAnimationFrame();\n }\n\n // Called on each frame, can be overridden to call onRender multiple times\n // to support e.g. stereoscopic rendering\n _renderFrame(props: AnimationProps) {\n // Allow e.g. VR display to render multiple frames.\n if (this.display) {\n this.display._renderFrame(props);\n return;\n }\n\n // call callback\n this.onRender(props);\n // end callback\n }\n\n _clearNeedsRedraw() {\n this.needsRedraw = null;\n }\n\n _setupFrame() {\n this._resizeCanvasDrawingBuffer();\n this._resizeViewport();\n }\n\n // Initialize the object that will be passed to app callbacks\n _initializeCallbackData() {\n this.animationProps = {\n animationLoop: this,\n device: this.device,\n canvas: this.device.canvasContext.canvas,\n timeline: this.timeline,\n\n // Initial values\n useDevicePixels: this.props.useDevicePixels,\n needsRedraw: null,\n\n // Placeholders\n width: 1,\n height: 1,\n aspect: 1,\n\n // Animation props\n time: 0,\n startTime: Date.now(),\n engineTime: 0,\n tick: 0,\n tock: 0,\n\n // Experimental\n _mousePosition: null // Event props\n };\n }\n\n // Update the context object that will be passed to app callbacks\n _updateCallbackData() {\n const {width, height, aspect} = this._getSizeAndAspect();\n if (width !== this.animationProps.width || height !== this.animationProps.height) {\n this.setNeedsRedraw('drawing buffer resized');\n }\n if (aspect !== this.animationProps.aspect) {\n this.setNeedsRedraw('drawing buffer aspect changed');\n }\n\n this.animationProps.width = width;\n this.animationProps.height = height;\n this.animationProps.aspect = aspect;\n\n this.animationProps.needsRedraw = this.needsRedraw;\n\n // Update time properties\n this.animationProps.engineTime = Date.now() - this.animationProps.startTime;\n\n if (this.timeline) {\n this.timeline.update(this.animationProps.engineTime);\n }\n\n this.animationProps.tick = Math.floor((this.animationProps.time / 1000) * 60);\n this.animationProps.tock++;\n\n // For back compatibility\n this.animationProps.time = this.timeline\n ? this.timeline.getTime()\n : this.animationProps.engineTime;\n }\n\n /** Either uses supplied or existing context, or calls provided callback to create one */\n async _createDevice() {\n const deviceProps = {...this.props, ...this.props.deviceProps};\n this.device = await this.onCreateDevice(deviceProps);\n // @ts-expect-error\n this.canvas = this.device.canvasContext.canvas;\n this._createInfoDiv();\n }\n\n _createInfoDiv() {\n if (this.canvas && this.props.onAddHTML) {\n const wrapperDiv = document.createElement('div');\n document.body.appendChild(wrapperDiv);\n wrapperDiv.style.position = 'relative';\n const div = document.createElement('div');\n div.style.position = 'absolute';\n div.style.left = '10px';\n div.style.bottom = '10px';\n div.style.width = '300px';\n div.style.background = 'white';\n wrapperDiv.appendChild(this.canvas);\n wrapperDiv.appendChild(div);\n const html = this.props.onAddHTML(div);\n if (html) {\n div.innerHTML = html;\n }\n }\n }\n\n _getSizeAndAspect() {\n // https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html\n const [width, height] = this.device.canvasContext.getPixelSize();\n\n // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html\n let aspect = 1;\n const canvas = this.device.canvasContext.canvas;\n\n // @ts-expect-error\n if (canvas && canvas.clientHeight) {\n // @ts-expect-error\n aspect = canvas.clientWidth / canvas.clientHeight;\n } else if (width > 0 && height > 0) {\n aspect = width / height;\n }\n\n return {width, height, aspect};\n }\n\n /** Default viewport setup */\n _resizeViewport() {\n // @ts-expect-error Expose on canvasContext\n if (this.props.autoResizeViewport && this.device.gl) {\n // @ts-expect-error Expose canvasContext\n this.device.gl.viewport(0, 0, this.device.gl.drawingBufferWidth, this.device.gl.drawingBufferHeight);\n }\n }\n\n /**\n * Resize the render buffer of the canvas to match canvas client size\n * Optionally multiplying with devicePixel ratio\n */\n _resizeCanvasDrawingBuffer() {\n if (this.props.autoResizeDrawingBuffer) {\n this.device.canvasContext.resize({useDevicePixels: this.props.useDevicePixels});\n }\n }\n\n _beginTimers() {\n this.frameRate.timeEnd();\n this.frameRate.timeStart();\n\n // Check if timer for last frame has completed.\n // GPU timer results are never available in the same\n // frame they are captured.\n // if (\n // this._gpuTimeQuery &&\n // this._gpuTimeQuery.isResultAvailable() &&\n // !this._gpuTimeQuery.isTimerDisjoint()\n // ) {\n // this.stats.get('GPU Time').addTime(this._gpuTimeQuery.getTimerMilliseconds());\n // }\n\n // if (this._gpuTimeQuery) {\n // // GPU time query start\n // this._gpuTimeQuery.beginTimeElapsedQuery();\n // }\n\n // this.cpuTime.timeStart();\n }\n\n _endTimers() {\n this.cpuTime.timeEnd();\n\n // if (this._gpuTimeQuery) {\n // // GPU time query end. Results will be available on next frame.\n // this._gpuTimeQuery.end();\n // }\n }\n\n // Event handling\n\n _startEventHandling() {\n if (this.canvas) {\n this.canvas.addEventListener('mousemove', this._onMousemove);\n this.canvas.addEventListener('mouseleave', this._onMouseleave);\n }\n }\n\n _onMousemove(e) {\n this.animationProps._mousePosition = [e.offsetX, e.offsetY];\n }\n _onMouseleave(e) {\n this.animationProps._mousePosition = null;\n }\n}\n"],"file":"animation-loop.js"}
|
|
1
|
+
{"version":3,"sources":["../../src/lib/animation-loop.ts"],"names":["luma","requestAnimationFrame","cancelAnimationFrame","Stats","isBrowser","isPage","document","statIdCounter","DEFAULT_ANIMATION_LOOP_PROPS","onCreateDevice","props","createDevice","onAddHTML","onInitialize","onRender","onFinalize","onError","error","console","device","deviceProps","debug","stats","get","useDevicePixels","autoResizeViewport","autoResizeDrawingBuffer","AnimationLoop","constructor","gl","id","cpuTime","gpuTime","frameRate","setProps","start","bind","stop","_onMousemove","_onMouseleave","destroy","_setDisplay","delete","setNeedsRedraw","reason","needsRedraw","_running","appContext","_initialized","_createDevice","_initialize","_getAnimationProps","_cancelAnimationFrame","_requestAnimationFrame","err","Error","redraw","isLost","_beginTimers","_setupFrame","_updateAnimationProps","_renderFrame","_clearNeedsRedraw","_resolveNextFrame","_nextFramePromise","_endTimers","animationProps","attachTimeline","timeline","detachTimeline","waitForRender","Promise","resolve","toDataURL","canvas","HTMLCanvasElement","_startEventHandling","_initializeAnimationProps","_resizeCanvasDrawingBuffer","_resizeViewport","display","animationLoop","_animationFrameId","_animationFrame","canvasContext","width","height","aspect","time","startTime","Date","now","engineTime","tick","tock","_mousePosition","_getSizeAndAspect","update","Math","floor","getTime","_createInfoDiv","wrapperDiv","createElement","body","appendChild","style","position","div","left","bottom","background","html","innerHTML","getPixelSize","clientHeight","clientWidth","viewport","drawingBufferWidth","drawingBufferHeight","resize","timeEnd","timeStart","addEventListener","e","offsetX","offsetY"],"mappings":";AAAA,SAAQA,IAAR,QAAwC,cAAxC;AACA,SAAQC,qBAAR,EAA+BC,oBAA/B,QAA0D,cAA1D;AAGA,SAAQC,KAAR,QAA0B,iBAA1B;AACA,SAAQC,SAAR,QAAwB,eAAxB;AAEA,MAAMC,MAAM,GAAGD,SAAS,MAAM,OAAOE,QAAP,KAAoB,WAAlD;AAEA,IAAIC,aAAa,GAAG,CAApB;AAwBA,MAAMC,4BAA0D,GAAG;AACjEC,EAAAA,cAAc,EAAGC,KAAD,IAAyCV,IAAI,CAACW,YAAL,CAAkBD,KAAlB,CADQ;AAEjEE,EAAAA,SAAS,EAAE,MAAM,EAFgD;AAGjEC,EAAAA,YAAY,EAAE,OAAO,EAAP,CAHmD;AAIjEC,EAAAA,QAAQ,EAAE,MAAM,CAAE,CAJ+C;AAKjEC,EAAAA,UAAU,EAAE,MAAM,CAAE,CAL6C;AAMjEC,EAAAA,OAAO,EAAGC,KAAD,IAAWC,OAAO,CAACD,KAAR,CAAcA,KAAd,CAN6C;AAQjEE,EAAAA,MAAM,EAAE,IARyD;AASjEC,EAAAA,WAAW,EAAE,EAToD;AAUjEC,EAAAA,KAAK,EAAE,KAV0D;AAWjEC,EAAAA,KAAK,EAAEtB,IAAI,CAACsB,KAAL,CAAWC,GAAX,0BAAiChB,aAAa,EAA9C,EAX0D;AAcjEiB,EAAAA,eAAe,EAAE,IAdgD;AAejEC,EAAAA,kBAAkB,EAAE,IAf6C;AAgBjEC,EAAAA,uBAAuB,EAAE;AAhBwC,CAAnE;AAoBA,OAAO,MA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AYU,EAAjD,EAAqD;AAEnD,WAAKV,MAAL,CAAYU,EAAZ,CAAewF,QAAf,CAAwB,CAAxB,EAA2B,CAA3B,EAA8B,KAAKlG,MAAL,CAAYU,EAAZ,CAAeyF,kBAA7C,EAAiE,KAAKnG,MAAL,CAAYU,EAAZ,CAAe0F,mBAAhF;AACD;AACF;;AAMDzC,EAAAA,0BAA0B,GAAG;AAC3B,QAAI,KAAKpE,KAAL,CAAWgB,uBAAf,EAAwC;AAAA;;AACtC,4BAAKP,MAAL,gEAAaiE,aAAb,CAA2BoC,MAA3B,CAAkC;AAAChG,QAAAA,eAAe,EAAE,KAAKd,KAAL,CAAWc;AAA7B,OAAlC;AACD;AACF;;AAEDkC,EAAAA,YAAY,GAAG;AACb,SAAKzB,SAAL,CAAewF,OAAf;AACA,SAAKxF,SAAL,CAAeyF,SAAf;AAmBD;;AAEDzD,EAAAA,UAAU,GAAG;AACX,SAAKlC,OAAL,CAAa0F,OAAb;AAMD;;AAID7C,EAAAA,mBAAmB,GAAG;AACpB,QAAI,KAAKF,MAAT,EAAiB;AACf,WAAKA,MAAL,CAAYiD,gBAAZ,CAA6B,WAA7B,EAA0C,KAAKrF,YAA/C;AACA,WAAKoC,MAAL,CAAYiD,gBAAZ,CAA6B,YAA7B,EAA2C,KAAKpF,aAAhD;AACD;AACF;;AAEDD,EAAAA,YAAY,CAACsF,CAAD,EAAI;AACd,SAAKzE,kBAAL,GAA0B4C,cAA1B,GAA2C,CAAC6B,CAAC,CAACC,OAAH,EAAYD,CAAC,CAACE,OAAd,CAA3C;AACD;;AAEDvF,EAAAA,aAAa,CAACqF,CAAD,EAAI;AACf,SAAKzE,kBAAL,GAA0B4C,cAA1B,GAA2C,IAA3C;AACD;;AAzewB","sourcesContent":["import {luma, Device, DeviceProps} from '@luma.gl/api';\nimport {requestAnimationFrame, cancelAnimationFrame} from '@luma.gl/api';\nimport {Timeline} from '../animation/timeline';\nimport {AnimationProps} from '../lib/animation-props';\nimport {Stats, Stat} from '@probe.gl/stats';\nimport {isBrowser} from '@probe.gl/env';\n\nconst isPage = isBrowser() && typeof document !== 'undefined';\n\nlet statIdCounter = 0;\n\ntype ContextProps = DeviceProps;\n\n/** AnimationLoop properties */\nexport type AnimationLoopProps = {\n onCreateDevice?: (props: DeviceProps) => Promise<Device>;\n onAddHTML?: (div: HTMLDivElement) => string; // innerHTML\n onInitialize?: (animationProps: AnimationProps) => void;\n onRender?: (animationProps: AnimationProps) => void;\n onFinalize?: (animationProps: AnimationProps) => void;\n onError?: (reason: Error) => void;\n\n device?: Device | null;\n deviceProps?: DeviceProps;\n stats?: Stats;\n\n // view parameters\n debug?: boolean;\n autoResizeViewport?: boolean;\n autoResizeDrawingBuffer?: boolean;\n useDevicePixels?: number | boolean;\n};\n\nconst DEFAULT_ANIMATION_LOOP_PROPS: Required<AnimationLoopProps> = {\n onCreateDevice: (props: DeviceProps): Promise<Device> => luma.createDevice(props),\n onAddHTML: () => '',\n onInitialize: () => ({}),\n onRender: () => {},\n onFinalize: () => {},\n onError: (error) => console.error(error), // eslint-disable-line no-console\n\n device: null,\n deviceProps: {},\n debug: false,\n stats: luma.stats.get(`animation-loop-${statIdCounter++}`),\n\n // view parameters\n useDevicePixels: true,\n autoResizeViewport: true,\n autoResizeDrawingBuffer: true,\n};\n\n/** Convenient animation loop */\nexport class AnimationLoop {\n device: Device | null = null;\n canvas: HTMLCanvasElement | OffscreenCanvas | null = null;\n\n props: Required<AnimationLoopProps>;\n animationProps: AnimationProps | null = null;\n timeline: Timeline | null = null;\n stats: Stats;\n cpuTime: Stat;\n gpuTime: Stat;\n frameRate: Stat;\n\n display: any;\n\n needsRedraw: string | false = 'initialized';\n\n _initialized: boolean = false;\n _running: boolean = false;\n _animationFrameId: any = null;\n _nextFramePromise: Promise<AnimationLoop> | null = null;\n _resolveNextFrame: ((AnimationLoop) => void) | null = null;\n _cpuStartTime: number = 0;\n\n // _gpuTimeQuery: Query | null = null;\n\n /*\n * @param {HTMLCanvasElement} canvas - if provided, width and height will be passed to context\n */\n constructor(props: AnimationLoopProps = {}) {\n this.props = {...DEFAULT_ANIMATION_LOOP_PROPS, ...props};\n props = this.props;\n\n let {useDevicePixels = true} = this.props;\n\n // state\n this.device = props.device || null;\n // @ts-expect-error\n this.gl = (this.device && this.device.gl) || props.gl;\n\n this.stats = props.stats || new Stats({id: 'animation-loop-stats'});\n this.cpuTime = this.stats.get('CPU Time');\n this.gpuTime = this.stats.get('GPU Time');\n this.frameRate = this.stats.get('Frame Rate');\n\n this.setProps({\n autoResizeViewport: props.autoResizeViewport,\n autoResizeDrawingBuffer: props.autoResizeDrawingBuffer,\n useDevicePixels\n });\n\n // Bind methods\n this.start = this.start.bind(this);\n this.stop = this.stop.bind(this);\n\n this._onMousemove = this._onMousemove.bind(this);\n this._onMouseleave = this._onMouseleave.bind(this);\n }\n\n destroy(): void {\n this.stop();\n this._setDisplay(null);\n }\n\n /** @deprecated Use .destroy() */\n delete(): void {\n this.destroy();\n }\n\n setNeedsRedraw(reason: string): this {\n this.needsRedraw = this.needsRedraw || reason;\n return this;\n }\n\n // TODO - move to CanvasContext\n setProps(props: AnimationLoopProps): this {\n if ('autoResizeViewport' in props) {\n this.props.autoResizeViewport = props.autoResizeViewport || false;\n }\n if ('autoResizeDrawingBuffer' in props) {\n this.props.autoResizeDrawingBuffer = props.autoResizeDrawingBuffer || false;\n }\n if ('useDevicePixels' in props) {\n this.props.useDevicePixels = props.useDevicePixels || false;\n }\n return this;\n }\n\n /** Starts a render loop if not already running */\n async start() {\n if (this._running) {\n return this;\n }\n this._running = true;\n\n try {\n // check that we haven't been stopped\n if (!this._running) {\n return null;\n }\n\n let appContext;\n if (!this._initialized) {\n this._initialized = true;\n // Create the WebGL context\n await this._createDevice();\n this._initialize();\n\n // Note: onIntialize can return a promise (e.g. in case app needs to load resources)\n await this.props.onInitialize(this._getAnimationProps());\n }\n\n // check that we haven't been stopped\n if (!this._running) {\n return null;\n }\n\n // Start the loop\n if (appContext !== false) {\n // cancel any pending renders to ensure only one loop can ever run\n this._cancelAnimationFrame();\n this._requestAnimationFrame();\n }\n\n return this;\n } catch (err: unknown) {\n const error = err instanceof Error ? err : new Error('Unknown error')\n this.props.onError(error);\n // this._running = false; // TODO\n throw error;\n }\n }\n\n /** Explicitly draw a frame */\n redraw(): this {\n if (this.device?.isLost) {\n return this;\n }\n\n this._beginTimers();\n\n this._setupFrame();\n this._updateAnimationProps();\n\n this._renderFrame(this._getAnimationProps());\n\n // clear needsRedraw flag\n this._clearNeedsRedraw();\n\n if (this._resolveNextFrame) {\n this._resolveNextFrame(this);\n this._nextFramePromise = null;\n this._resolveNextFrame = null;\n }\n\n this._endTimers();\n\n return this;\n }\n\n // Stops a render loop if already running, finalizing\n stop() {\n // console.debug(`Stopping ${this.constructor.name}`);\n if (this._running) {\n // call callback\n // If stop is called immediately, we can end up in a state where props haven't been initialized...\n if (this.animationProps) {\n this.props.onFinalize(this.animationProps);\n }\n\n this._cancelAnimationFrame();\n this._nextFramePromise = null;\n this._resolveNextFrame = null;\n this._running = false;\n }\n return this;\n }\n\n attachTimeline(timeline: Timeline): Timeline {\n this.timeline = timeline;\n return this.timeline;\n }\n\n detachTimeline(): void {\n this.timeline = null;\n }\n\n waitForRender(): Promise<AnimationLoop> {\n this.setNeedsRedraw('waitForRender');\n\n if (!this._nextFramePromise) {\n this._nextFramePromise = new Promise((resolve) => {\n this._resolveNextFrame = resolve;\n });\n }\n return this._nextFramePromise;\n }\n\n async toDataURL(): Promise<string> {\n this.setNeedsRedraw('toDataURL');\n await this.waitForRender();\n if (this.canvas instanceof HTMLCanvasElement) {\n return this.canvas.toDataURL();\n }\n throw new Error('OffscreenCanvas');\n }\n\n // PRIVATE METHODS\n\n _initialize() {\n this._startEventHandling();\n\n // Initialize the callback data\n this._initializeAnimationProps();\n this._updateAnimationProps();\n\n // Default viewport setup, in case onInitialize wants to render\n this._resizeCanvasDrawingBuffer();\n this._resizeViewport();\n\n // this._gpuTimeQuery = Query.isSupported(this.gl, ['timers']) ? new Query(this.gl) : null;\n }\n\n _setDisplay(display) {\n if (this.display) {\n this.display.delete();\n this.display.animationLoop = null;\n }\n\n // store animation loop on the display\n if (display) {\n display.animationLoop = this;\n }\n\n this.display = display;\n }\n\n _requestAnimationFrame() {\n if (!this._running) {\n return;\n }\n\n // VR display has a separate animation frame to sync with headset\n // TODO WebVR API discontinued, replaced by WebXR: https://immersive-web.github.io/webxr/\n // See https://developer.mozilla.org/en-US/docs/Web/API/VRDisplay/requestAnimationFrame\n // if (this.display && this.display.requestAnimationFrame) {\n // this._animationFrameId = this.display.requestAnimationFrame(this._animationFrame.bind(this));\n // }\n this._animationFrameId = requestAnimationFrame(this._animationFrame.bind(this));\n }\n\n _cancelAnimationFrame() {\n if (this._animationFrameId !== null) {\n return;\n }\n\n // VR display has a separate animation frame to sync with headset\n // TODO WebVR API discontinued, replaced by WebXR: https://immersive-web.github.io/webxr/\n // See https://developer.mozilla.org/en-US/docs/Web/API/VRDisplay/requestAnimationFrame\n // if (this.display && this.display.cancelAnimationFrame) {\n // this.display.cancelAnimationFrame(this._animationFrameId);\n // }\n cancelAnimationFrame(this._animationFrameId);\n this._animationFrameId = null;\n }\n\n _animationFrame() {\n if (!this._running) {\n return;\n }\n this.redraw();\n this._requestAnimationFrame();\n }\n\n // Called on each frame, can be overridden to call onRender multiple times\n // to support e.g. stereoscopic rendering\n _renderFrame(props: AnimationProps) {\n // Allow e.g. VR display to render multiple frames.\n if (this.display) {\n this.display._renderFrame(props);\n return;\n }\n\n // call callback\n this.props.onRender(props);\n // end callback\n }\n\n _clearNeedsRedraw() {\n this.needsRedraw = false;\n }\n\n _setupFrame() {\n this._resizeCanvasDrawingBuffer();\n this._resizeViewport();\n }\n\n // Initialize the object that will be passed to app callbacks\n _initializeAnimationProps() {\n if (!this.device) {\n throw new Error('loop');\n }\n this.animationProps = {\n animationLoop: this,\n device: this.device,\n canvas: this.device.canvasContext.canvas,\n timeline: this.timeline,\n\n // Initial values\n useDevicePixels: this.props.useDevicePixels,\n needsRedraw: false,\n\n // Placeholders\n width: 1,\n height: 1,\n aspect: 1,\n\n // Animation props\n time: 0,\n startTime: Date.now(),\n engineTime: 0,\n tick: 0,\n tock: 0,\n\n // Experimental\n _mousePosition: null // Event props\n };\n }\n\n _getAnimationProps(): AnimationProps {\n if (!this.animationProps) {\n throw new Error('animationProps');\n }\n return this.animationProps;\n }\n\n // Update the context object that will be passed to app callbacks\n _updateAnimationProps(): void {\n if (!this.animationProps) {\n return;\n }\n\n const {width, height, aspect} = this._getSizeAndAspect();\n if (width !== this.animationProps.width || height !== this.animationProps.height) {\n this.setNeedsRedraw('drawing buffer resized');\n }\n if (aspect !== this.animationProps.aspect) {\n this.setNeedsRedraw('drawing buffer aspect changed');\n }\n\n this.animationProps.width = width;\n this.animationProps.height = height;\n this.animationProps.aspect = aspect;\n\n this.animationProps.needsRedraw = this.needsRedraw;\n\n // Update time properties\n this.animationProps.engineTime = Date.now() - this.animationProps.startTime;\n\n if (this.timeline) {\n this.timeline.update(this.animationProps.engineTime);\n }\n\n this.animationProps.tick = Math.floor((this.animationProps.time / 1000) * 60);\n this.animationProps.tock++;\n\n // For back compatibility\n this.animationProps.time = this.timeline\n ? this.timeline.getTime()\n : this.animationProps.engineTime;\n }\n\n /** Either uses supplied or existing context, or calls provided callback to create one */\n async _createDevice() {\n const deviceProps = {...this.props, ...this.props.deviceProps};\n this.device = await this.props.onCreateDevice(deviceProps);\n this.canvas = this.device.canvasContext.canvas;\n this._createInfoDiv();\n }\n\n _createInfoDiv() {\n if (this.canvas && this.props.onAddHTML) {\n const wrapperDiv = document.createElement('div');\n document.body.appendChild(wrapperDiv);\n wrapperDiv.style.position = 'relative';\n const div = document.createElement('div');\n div.style.position = 'absolute';\n div.style.left = '10px';\n div.style.bottom = '10px';\n div.style.width = '300px';\n div.style.background = 'white';\n if (this.canvas instanceof HTMLCanvasElement) {\n wrapperDiv.appendChild(this.canvas);\n }\n wrapperDiv.appendChild(div);\n const html = this.props.onAddHTML(div);\n if (html) {\n div.innerHTML = html;\n }\n }\n }\n\n _getSizeAndAspect(): {width: number; height: number; aspect: number} {\n if (!this.device) {\n return {width: 1, height: 1, aspect: 1};\n }\n // https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html\n const [width, height] = this.device.canvasContext.getPixelSize();\n\n // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html\n let aspect = 1;\n const canvas = this.device.canvasContext.canvas;\n\n // @ts-expect-error\n if (canvas && canvas.clientHeight) {\n // @ts-expect-error\n aspect = canvas.clientWidth / canvas.clientHeight;\n } else if (width > 0 && height > 0) {\n aspect = width / height;\n }\n\n return {width, height, aspect};\n }\n\n /** Default viewport setup */\n _resizeViewport() {\n // @ts-expect-error Expose on canvasContext\n if (this.props.autoResizeViewport && this.device.gl) {\n // @ts-expect-error Expose canvasContext\n this.device.gl.viewport(0, 0, this.device.gl.drawingBufferWidth, this.device.gl.drawingBufferHeight);\n }\n }\n\n /**\n * Resize the render buffer of the canvas to match canvas client size\n * Optionally multiplying with devicePixel ratio\n */\n _resizeCanvasDrawingBuffer() {\n if (this.props.autoResizeDrawingBuffer) {\n this.device?.canvasContext.resize({useDevicePixels: this.props.useDevicePixels});\n }\n }\n\n _beginTimers() {\n this.frameRate.timeEnd();\n this.frameRate.timeStart();\n\n // Check if timer for last frame has completed.\n // GPU timer results are never available in the same\n // frame they are captured.\n // if (\n // this._gpuTimeQuery &&\n // this._gpuTimeQuery.isResultAvailable() &&\n // !this._gpuTimeQuery.isTimerDisjoint()\n // ) {\n // this.stats.get('GPU Time').addTime(this._gpuTimeQuery.getTimerMilliseconds());\n // }\n\n // if (this._gpuTimeQuery) {\n // // GPU time query start\n // this._gpuTimeQuery.beginTimeElapsedQuery();\n // }\n\n // this.cpuTime.timeStart();\n }\n\n _endTimers() {\n this.cpuTime.timeEnd();\n\n // if (this._gpuTimeQuery) {\n // // GPU time query end. Results will be available on next frame.\n // this._gpuTimeQuery.end();\n // }\n }\n\n // Event handling\n\n _startEventHandling() {\n if (this.canvas) {\n this.canvas.addEventListener('mousemove', this._onMousemove);\n this.canvas.addEventListener('mouseleave', this._onMouseleave);\n }\n }\n\n _onMousemove(e) {\n this._getAnimationProps()._mousePosition = [e.offsetX, e.offsetY];\n }\n\n _onMouseleave(e) {\n this._getAnimationProps()._mousePosition = null;\n }\n}\n"],"file":"animation-loop.js"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="offscreencanvas" />
|
|
2
2
|
import { Device } from '@luma.gl/api';
|
|
3
3
|
import { Timeline } from '../animation/timeline';
|
|
4
|
-
import type AnimationLoop from './animation-loop';
|
|
4
|
+
import type { AnimationLoop } from './animation-loop';
|
|
5
5
|
/** Properties passed to every render frame */
|
|
6
6
|
export declare type AnimationProps = {
|
|
7
7
|
device: Device;
|
|
@@ -17,8 +17,8 @@ export declare type AnimationProps = {
|
|
|
17
17
|
engineTime: number;
|
|
18
18
|
tick: number;
|
|
19
19
|
tock: number;
|
|
20
|
-
needsRedraw?: string;
|
|
21
|
-
timeline: Timeline;
|
|
22
|
-
_mousePosition?: [number, number];
|
|
20
|
+
needsRedraw?: string | false;
|
|
21
|
+
timeline: Timeline | null;
|
|
22
|
+
_mousePosition?: [number, number] | null;
|
|
23
23
|
};
|
|
24
24
|
//# sourceMappingURL=animation-props.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"animation-props.d.ts","sourceRoot":"","sources":["../../src/lib/animation-props.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,QAAQ,EAAC,MAAM,uBAAuB,CAAA;AAC9C,OAAO,KAAK,aAAa,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"animation-props.d.ts","sourceRoot":"","sources":["../../src/lib/animation-props.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,QAAQ,EAAC,MAAM,uBAAuB,CAAA;AAC9C,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,kBAAkB,CAAC;AAEpD,+CAA+C;AAC/C,oBAAY,cAAc,GAAG;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,EAAE,aAAa,CAAC;IAE7B,oCAAoC;IACpC,MAAM,EAAE,iBAAiB,GAAG,eAAe,CAAC;IAC5C,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IAGf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IAGb,WAAW,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAE7B,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;IAG1B,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC;CAC1C,CAAC"}
|
package/dist/lib/model.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Device, Buffer, RenderPipelineProps, RenderPass, Binding } from '@luma.gl/api';
|
|
1
|
+
import type { Device, Buffer, RenderPipelineProps, RenderPass, Binding, PrimitiveTopology } from '@luma.gl/api';
|
|
2
2
|
import { RenderPipeline } from '@luma.gl/api';
|
|
3
3
|
import type { ShaderModule } from '@luma.gl/shadertools';
|
|
4
4
|
import type Geometry from '../geometry/geometry';
|
|
@@ -6,14 +6,14 @@ export declare type ModelProps = Omit<RenderPipelineProps, 'vs' | 'fs'> & {
|
|
|
6
6
|
vs?: {
|
|
7
7
|
glsl?: string;
|
|
8
8
|
wgsl?: string;
|
|
9
|
-
} | string;
|
|
9
|
+
} | string | null;
|
|
10
10
|
fs?: {
|
|
11
11
|
glsl?: string;
|
|
12
12
|
wgsl?: string;
|
|
13
|
-
} | string;
|
|
13
|
+
} | string | null;
|
|
14
14
|
modules?: ShaderModule[];
|
|
15
15
|
moduleSettings?: Record<string, Record<string, any>>;
|
|
16
|
-
geometry?: Geometry;
|
|
16
|
+
geometry?: Geometry | null;
|
|
17
17
|
};
|
|
18
18
|
/** v9 API */
|
|
19
19
|
export default class Model {
|
|
@@ -21,8 +21,8 @@ export default class Model {
|
|
|
21
21
|
readonly pipeline: RenderPipeline;
|
|
22
22
|
readonly id: string;
|
|
23
23
|
readonly vs: string;
|
|
24
|
-
readonly fs: string |
|
|
25
|
-
readonly topology:
|
|
24
|
+
readonly fs: string | null;
|
|
25
|
+
readonly topology: PrimitiveTopology;
|
|
26
26
|
readonly vertexCount: any;
|
|
27
27
|
props: Required<ModelProps>;
|
|
28
28
|
private _getModuleUniforms;
|
package/dist/lib/model.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/lib/model.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/lib/model.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,MAAM,EAAE,MAAM,EAAE,mBAAmB,EAAE,UAAU,EAAE,OAAO,EAAE,iBAAiB,EAAC,MAAM,cAAc,CAAC;AAC9G,OAAO,EAAC,cAAc,EAAe,MAAM,cAAc,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,KAAK,QAAQ,MAAM,sBAAsB,CAAC;AAIjD,oBAAY,UAAU,GAAG,IAAI,CAAC,mBAAmB,EAAE,IAAI,GAAG,IAAI,CAAC,GAAG;IAEhE,EAAE,CAAC,EAAE;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAC,GAAG,MAAM,GAAG,IAAI,CAAC;IACpD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OAAO,CAAC,GAAG,IAAI;IAMpD,WAAW,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;IAMhD,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;CAWvC"}
|
package/dist/lib/model.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
2
|
import { RenderPipeline } from '@luma.gl/api';
|
|
3
3
|
import { getAttributeBuffersFromGeometry, getIndexBufferFromGeometry } from './model-utils';
|
|
4
|
-
import PipelineFactory from './pipeline-factory';
|
|
4
|
+
import { PipelineFactory } from './pipeline-factory';
|
|
5
5
|
const DEFAULT_MODEL_PROPS = { ...RenderPipeline._DEFAULT_PROPS,
|
|
6
|
-
vs:
|
|
7
|
-
fs:
|
|
6
|
+
vs: null,
|
|
7
|
+
fs: null,
|
|
8
8
|
id: 'unnamed',
|
|
9
9
|
handle: undefined,
|
|
10
10
|
userData: {},
|
|
11
11
|
modules: [],
|
|
12
12
|
moduleSettings: {},
|
|
13
|
-
geometry:
|
|
13
|
+
geometry: null
|
|
14
14
|
};
|
|
15
15
|
export default class Model {
|
|
16
16
|
constructor(device, props) {
|
|
@@ -22,7 +22,7 @@ export default class Model {
|
|
|
22
22
|
|
|
23
23
|
_defineProperty(this, "vs", void 0);
|
|
24
24
|
|
|
25
|
-
_defineProperty(this, "fs",
|
|
25
|
+
_defineProperty(this, "fs", null);
|
|
26
26
|
|
|
27
27
|
_defineProperty(this, "topology", void 0);
|
|
28
28
|
|
|
@@ -36,8 +36,13 @@ export default class Model {
|
|
|
36
36
|
...props
|
|
37
37
|
};
|
|
38
38
|
props = this.props;
|
|
39
|
-
this.id = props.id;
|
|
39
|
+
this.id = this.props.id;
|
|
40
40
|
this.device = device;
|
|
41
|
+
|
|
42
|
+
if (!props.vs) {
|
|
43
|
+
throw new Error('no vertex shader');
|
|
44
|
+
}
|
|
45
|
+
|
|
41
46
|
this.vs = getShaderSource(this.device, props.vs);
|
|
42
47
|
|
|
43
48
|
if (props.fs) {
|
|
@@ -49,20 +54,21 @@ export default class Model {
|
|
|
49
54
|
|
|
50
55
|
if (this.props.geometry) {
|
|
51
56
|
this.vertexCount = this.props.geometry.vertexCount;
|
|
52
|
-
this.topology = this.props.geometry.topology;
|
|
57
|
+
this.topology = this.props.geometry.topology || 'triangle-list';
|
|
53
58
|
}
|
|
54
59
|
|
|
60
|
+
const pipelineFactory = PipelineFactory.getDefaultPipelineFactory(this.device);
|
|
55
61
|
const {
|
|
56
|
-
|
|
62
|
+
pipeline,
|
|
57
63
|
getUniforms
|
|
58
|
-
} =
|
|
64
|
+
} = pipelineFactory.createRenderPipeline({ ...this.props,
|
|
59
65
|
vs: this.vs,
|
|
60
66
|
fs: this.fs,
|
|
61
67
|
topology: this.topology,
|
|
62
68
|
parameters: props.parameters,
|
|
63
69
|
layout: props.layout
|
|
64
70
|
});
|
|
65
|
-
this.pipeline =
|
|
71
|
+
this.pipeline = pipeline;
|
|
66
72
|
this._getModuleUniforms = getUniforms;
|
|
67
73
|
|
|
68
74
|
if (this.props.geometry) {
|
package/dist/lib/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/model.ts"],"names":["RenderPipeline","getAttributeBuffersFromGeometry","getIndexBufferFromGeometry","PipelineFactory","DEFAULT_MODEL_PROPS","_DEFAULT_PROPS","vs","undefined","fs","id","handle","userData","modules","moduleSettings","geometry","Model","constructor","device","props","getShaderSource","vertexCount","topology","renderPipeline","getUniforms","getDefaultPipelineFactory","createRenderPipeline","parameters","layout","pipeline","_getModuleUniforms","_setGeometry","setUniforms","setProps","destroy","draw","renderPass","instanceCount","indices","setIndexBuffer","attributes","setAttributes","bindings","setBindings","uniforms","updateModuleSettings","Object","assign","geometryBuffers","indexBuffer","shader","info","type","wgsl","Error","glsl"],"mappings":";AACA,SAAQA,cAAR,QAA2C,cAA3C;AAGA,S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iC,eAAe,GAAG9C,+BAA+B,CAAC,KAAKgB,MAAN,EAAcH,QAAd,CAAvD;AACA,SAAK0B,aAAL,CAAmBO,eAAnB;AAEA,UAAMC,WAAW,GAAG9C,0BAA0B,CAAC,KAAKe,MAAN,EAAcH,QAAd,CAA9C;;AACA,QAAIkC,WAAJ,EAAiB;AACf,WAAKV,cAAL,CAAoBU,WAApB;AACD;AACF;;AA9HwB;;AAkI3B,SAAS7B,eAAT,CAAyBF,MAAzB,EAAyCgC,MAAzC,EAAkG;AAEhG,MAAI,OAAOA,MAAP,KAAkB,QAAtB,EAAgC;AAC9B,WAAOA,MAAP;AACD;;AAED,UAAQhC,MAAM,CAACiC,IAAP,CAAYC,IAApB;AACE,SAAK,QAAL;AACE,UAAIF,MAAJ,aAAIA,MAAJ,eAAIA,MAAM,CAAEG,IAAZ,EAAkB;AAChB,eAAOH,MAAM,CAACG,IAAd;AACD;;AACD,YAAM,IAAIC,KAAJ,CAAU,sCAAV,CAAN;;AAEF;AACE,UAAIJ,MAAJ,aAAIA,MAAJ,eAAIA,MAAM,CAAEK,IAAZ,EAAkB;AAChB,eAAOL,MAAM,CAACK,IAAd;AACD;;AACD,YAAM,IAAID,KAAJ,CAAU,qCAAV,CAAN;AAXJ;AAaD","sourcesContent":["import type {Device, Buffer, RenderPipelineProps, RenderPass, Binding} from '@luma.gl/api';\nimport {RenderPipeline, Shader, cast} from '@luma.gl/api';\nimport type { ShaderModule } from '@luma.gl/shadertools';\nimport type Geometry from '../geometry/geometry';\nimport {getAttributeBuffersFromGeometry, getIndexBufferFromGeometry} from './model-utils';\nimport PipelineFactory from './pipeline-factory';\n\nexport type ModelProps = Omit<RenderPipelineProps, 'vs' | 'fs'> & {\n // Model also accepts a string\n vs?: {glsl?: string; wgsl?: string} | string;\n fs?: {glsl?: string; wgsl?: string} | string;\n modules?: ShaderModule[];\n moduleSettings?: Record<string, Record<string, any>>;\n geometry?: Geometry;\n};\n\nconst DEFAULT_MODEL_PROPS: Required<ModelProps> = {\n ...RenderPipeline._DEFAULT_PROPS,\n vs: undefined,\n fs: undefined,\n id: 'unnamed',\n handle: undefined,\n userData: {},\n modules: [],\n moduleSettings: {},\n geometry: undefined\n};\n\n/** v9 API */\nexport default class Model {\n readonly device: Device;\n readonly pipeline: RenderPipeline;\n readonly id: string;\n readonly vs: string;\n readonly fs: string | undefined;\n readonly topology: string;\n readonly vertexCount;\n props: Required<ModelProps>;\n\n private _getModuleUniforms: (props?: Record<string, Record<string, any>>) => Record<string, any>;\n\n constructor(device: Device, props: ModelProps) {\n this.props = {...DEFAULT_MODEL_PROPS, ...props};\n props = this.props;\n this.id = props.id;\n this.device = device;\n\n // Create the pipeline\n this.vs = getShaderSource(this.device, props.vs);\n if (props.fs) {\n this.fs = getShaderSource(this.device, props.fs);\n }\n\n this.vertexCount = this.props.vertexCount;\n this.topology = this.props.topology;\n\n if (this.props.geometry) {\n this.vertexCount = this.props.geometry.vertexCount;\n this.topology = this.props.geometry.topology;\n }\n\n const {renderPipeline, getUniforms} = PipelineFactory.getDefaultPipelineFactory(this.device).createRenderPipeline({\n ...this.props,\n vs: this.vs,\n fs: this.fs,\n topology: this.topology,\n parameters: props.parameters,\n // Geometry in the vertex shader!\n // @ts-expect-error\n layout: props.layout\n });\n\n this.pipeline = renderPipeline;\n this._getModuleUniforms = getUniforms;\n\n if (this.props.geometry) {\n this._setGeometry(this.props.geometry);\n }\n this.setUniforms(this._getModuleUniforms()) // Get all default module uniforms\n this.setProps(this.props);\n }\n\n destroy(): void {\n this.pipeline.destroy();\n }\n\n draw(renderPass?: RenderPass): this {\n this.pipeline.draw({\n renderPass,\n vertexCount: this.vertexCount,\n instanceCount: this.props.instanceCount\n });\n return this;\n }\n\n setProps(props: ModelProps): this {\n if (props.indices) {\n this.setIndexBuffer(props.indices);\n }\n if (props.attributes) {\n this.setAttributes(props.attributes);\n }\n if (props.bindings) {\n this.setBindings(props.bindings);\n }\n if (props.uniforms) {\n this.setUniforms(props.uniforms);\n }\n if (props.moduleSettings) {\n this.updateModuleSettings(props.moduleSettings);\n }\n return this;\n }\n\n updateModuleSettings(props: Record<string, any>): this {\n const uniforms = this._getModuleUniforms(props);\n this.setUniforms(uniforms);\n return this;\n }\n\n setIndexBuffer(indices: Buffer): this {\n this.pipeline.setIndexBuffer(indices);\n // this._indices = indices;\n return this;\n }\n\n setAttributes(attributes: Record<string, Buffer>): this {\n this.pipeline.setAttributes(attributes);\n Object.assign(this.props.attributes, attributes);\n return this;\n }\n\n /** Set the bindings */\n setBindings(bindings: Record<string, Binding>): this {\n this.pipeline.setBindings(bindings);\n Object.assign(this.props.bindings, bindings);\n return this;\n }\n\n setUniforms(uniforms: Record<string, any>): this {\n this.pipeline.setUniforms(uniforms);\n Object.assign(this.props.uniforms, uniforms);\n return this;\n }\n\n _setGeometry(geometry: Geometry): void {\n // this._deleteGeometryBuffers();\n\n const geometryBuffers = getAttributeBuffersFromGeometry(this.device, geometry);\n this.setAttributes(geometryBuffers);\n\n const indexBuffer = getIndexBufferFromGeometry(this.device, geometry);\n if (indexBuffer) {\n this.setIndexBuffer(indexBuffer);\n }\n }\n}\n\n/** Create a shader from the different overloads */\nfunction getShaderSource(device: Device, shader: string | {glsl?: string; wgsl?: string}): string {\n // TODO - detect WGSL/GLSL and throw an error if not supported\n if (typeof shader === 'string') {\n return shader;\n }\n\n switch (device.info.type) {\n case 'webgpu':\n if (shader?.wgsl) {\n return shader.wgsl;\n }\n throw new Error('WebGPU does not support GLSL shaders');\n\n default:\n if (shader?.glsl) {\n return shader.glsl;\n }\n throw new Error('WebGL does not support WGSL shaders');\n }\n}\n"],"file":"model.js"}
|
|
1
|
+
{"version":3,"sources":["../../src/lib/model.ts"],"names":["RenderPipeline","getAttributeBuffersFromGeometry","getIndexBufferFromGeometry","PipelineFactory","DEFAULT_MODEL_PROPS","_DEFAULT_PROPS","vs","fs","id","handle","undefined","userData","modules","moduleSettings","geometry","Model","constructor","device","props","Error","getShaderSource","vertexCount","topology","pipelineFactory","getDefaultPipelineFactory","pipeline","getUniforms","createRenderPipeline","parameters","layout","_getModuleUniforms","_setGeometry","setUniforms","setProps","destroy","draw","renderPass","instanceCount","indices","setIndexBuffer","attributes","setAttributes","bindings","setBindings","uniforms","updateModuleSettings","Object","assign","geometryBuffers","indexBuffer","shader","info","type","wgsl","glsl"],"mappings":";AAGA,SAAQA,cAAR,QAA2C,cAA3C;AAGA,SAAQC,+BAAR,EAAyCC,0BAAzC,QAA0E,eAA1E;AACA,SAAQC,eAAR,QAA8B,oBAA9B;AAWA,MAAMC,mBAAyC,GAAG,EAChD,GAAGJ,cAAc,CAACK,cAD8B;AAEhDC,EAAAA,EAAE,EAAE,IAF4C;AAGhDC,EAAAA,EAAE,EAAE,IAH4C;AAIhDC,EAAAA,EAAE,EAAE,SAJ4C;AAKhDC,EAAAA,MAAM,EAAEC,SALwC;AAMhDC,EAAAA,QAAQ,EAAE,EANsC;AAOhDC,EAAAA,OAAO,EAAE,EAPuC;AAQhDC,EAAAA,cAAc,EAAE,EARgC;AAShDC,EAAAA,QAAQ,EAAE;AATsC,CAAlD;AAaA,eAAe,MAAMC,KAAN,CAAY;AAYzBC,EAAAA,WAAW,CAACC,MAAD,EAAiBC,KAAjB,EAAoC;AAAA;;AAAA;;AAAA;;AAAA;;AAAA,gCAPlB,IAOkB;;AAAA;;AAAA;;AAAA;;AAAA;;AAC7C,SAAKA,KAAL,GAAa,EAAC,GAAGd,mBAAJ;AAAyB,SAAGc;AAA5B,KAAb;AACAA,IAAAA,KAAK,GAAG,KAAKA,KAAb;AACA,SAAKV,EAAL,GAAU,KAAKU,KAAL,CAAWV,EAArB;AACA,SAAKS,MAAL,GAAcA,MAAd;;AAGA,QAAI,CAACC,KAAK,CAACZ,EAAX,EAAe;AACb,YAAM,IAAIa,KAAJ,CAAU,kBAAV,CAAN;AACD;;AACD,SAAKb,EAAL,GAAUc,eAAe,CAAC,KAAKH,MAAN,EAAcC,KAAK,CAACZ,EAApB,CAAzB;;AACA,QAAIY,KAAK,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Device, Buffer, RenderPipelineProps, RenderPass, Binding, PrimitiveTopology} from '@luma.gl/api';\nimport {RenderPipeline, Shader, cast} from '@luma.gl/api';\nimport type { ShaderModule } from '@luma.gl/shadertools';\nimport type Geometry from '../geometry/geometry';\nimport {getAttributeBuffersFromGeometry, getIndexBufferFromGeometry} from './model-utils';\nimport {PipelineFactory} from './pipeline-factory';\n\nexport type ModelProps = Omit<RenderPipelineProps, 'vs' | 'fs'> & {\n // Model also accepts a string\n vs?: {glsl?: string; wgsl?: string} | string | null;\n fs?: {glsl?: string; wgsl?: string} | string | null;\n modules?: ShaderModule[];\n moduleSettings?: Record<string, Record<string, any>>;\n geometry?: Geometry | null;\n};\n\nconst DEFAULT_MODEL_PROPS: Required<ModelProps> = {\n ...RenderPipeline._DEFAULT_PROPS,\n vs: null,\n fs: null,\n id: 'unnamed',\n handle: undefined,\n userData: {},\n modules: [],\n moduleSettings: {},\n geometry: null\n};\n\n/** v9 API */\nexport default class Model {\n readonly device: Device;\n readonly pipeline: RenderPipeline;\n readonly id: string;\n readonly vs: string;\n readonly fs: string | null = null;\n readonly topology: PrimitiveTopology;\n readonly vertexCount;\n props: Required<ModelProps>;\n\n private _getModuleUniforms: (props?: Record<string, Record<string, any>>) => Record<string, any>;\n\n constructor(device: Device, props: ModelProps) {\n this.props = {...DEFAULT_MODEL_PROPS, ...props};\n props = this.props;\n this.id = this.props.id;\n this.device = device;\n\n // Create the pipeline\n if (!props.vs) {\n throw new Error('no vertex shader');\n }\n this.vs = getShaderSource(this.device, props.vs);\n if (props.fs) {\n this.fs = getShaderSource(this.device, props.fs);\n }\n\n this.vertexCount = this.props.vertexCount;\n this.topology = this.props.topology;\n\n if (this.props.geometry) {\n this.vertexCount = this.props.geometry.vertexCount;\n this.topology = this.props.geometry.topology || 'triangle-list';\n }\n\n const pipelineFactory = PipelineFactory.getDefaultPipelineFactory(this.device);\n const {pipeline, getUniforms} = pipelineFactory.createRenderPipeline({\n ...this.props,\n vs: this.vs,\n fs: this.fs,\n topology: this.topology,\n parameters: props.parameters,\n layout: props.layout\n });\n\n this.pipeline = pipeline;\n this._getModuleUniforms = getUniforms;\n\n if (this.props.geometry) {\n this._setGeometry(this.props.geometry);\n }\n this.setUniforms(this._getModuleUniforms()) // Get all default module uniforms\n this.setProps(this.props);\n }\n\n destroy(): void {\n this.pipeline.destroy();\n }\n\n draw(renderPass?: RenderPass): this {\n this.pipeline.draw({\n renderPass,\n vertexCount: this.vertexCount,\n instanceCount: this.props.instanceCount\n });\n return this;\n }\n\n setProps(props: ModelProps): this {\n if (props.indices) {\n this.setIndexBuffer(props.indices);\n }\n if (props.attributes) {\n this.setAttributes(props.attributes);\n }\n if (props.bindings) {\n this.setBindings(props.bindings);\n }\n if (props.uniforms) {\n this.setUniforms(props.uniforms);\n }\n if (props.moduleSettings) {\n this.updateModuleSettings(props.moduleSettings);\n }\n return this;\n }\n\n updateModuleSettings(props: Record<string, any>): this {\n const uniforms = this._getModuleUniforms(props);\n this.setUniforms(uniforms);\n return this;\n }\n\n setIndexBuffer(indices: Buffer): this {\n this.pipeline.setIndexBuffer(indices);\n // this._indices = indices;\n return this;\n }\n\n setAttributes(attributes: Record<string, Buffer>): this {\n this.pipeline.setAttributes(attributes);\n Object.assign(this.props.attributes, attributes);\n return this;\n }\n\n /** Set the bindings */\n setBindings(bindings: Record<string, Binding>): this {\n this.pipeline.setBindings(bindings);\n Object.assign(this.props.bindings, bindings);\n return this;\n }\n\n setUniforms(uniforms: Record<string, any>): this {\n this.pipeline.setUniforms(uniforms);\n Object.assign(this.props.uniforms, uniforms);\n return this;\n }\n\n _setGeometry(geometry: Geometry): void {\n // this._deleteGeometryBuffers();\n\n const geometryBuffers = getAttributeBuffersFromGeometry(this.device, geometry);\n this.setAttributes(geometryBuffers);\n\n const indexBuffer = getIndexBufferFromGeometry(this.device, geometry);\n if (indexBuffer) {\n this.setIndexBuffer(indexBuffer);\n }\n }\n}\n\n/** Create a shader from the different overloads */\nfunction getShaderSource(device: Device, shader: string | {glsl?: string; wgsl?: string}): string {\n // TODO - detect WGSL/GLSL and throw an error if not supported\n if (typeof shader === 'string') {\n return shader;\n }\n\n switch (device.info.type) {\n case 'webgpu':\n if (shader?.wgsl) {\n return shader.wgsl;\n }\n throw new Error('WebGPU does not support GLSL shaders');\n\n default:\n if (shader?.glsl) {\n return shader.glsl;\n }\n throw new Error('WebGL does not support WGSL shaders');\n }\n}\n"],"file":"model.js"}
|