@luma.gl/engine 9.0.0-alpha.10 → 9.0.0-alpha.11

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.
@@ -7,14 +7,13 @@ import { Stats, Stat } from '@probe.gl/stats';
7
7
  export declare type AnimationLoopProps = {
8
8
  onCreateDevice?: (props: DeviceProps) => Promise<Device>;
9
9
  onAddHTML?: (div: HTMLDivElement) => string;
10
- onInitialize?: (animationProps: AnimationProps) => void;
11
- onRender?: (animationProps: AnimationProps) => void;
10
+ onInitialize?: (animationProps: AnimationProps) => Promise<unknown>;
11
+ onRender?: (animationProps: AnimationProps) => unknown;
12
12
  onFinalize?: (animationProps: AnimationProps) => void;
13
13
  onError?: (reason: Error) => void;
14
14
  device?: Device | null;
15
15
  deviceProps?: DeviceProps;
16
16
  stats?: Stats;
17
- debug?: boolean;
18
17
  autoResizeViewport?: boolean;
19
18
  autoResizeDrawingBuffer?: boolean;
20
19
  useDevicePixels?: number | boolean;
@@ -58,7 +57,7 @@ export declare class AnimationLoop {
58
57
  _requestAnimationFrame(): void;
59
58
  _cancelAnimationFrame(): void;
60
59
  _animationFrame(): void;
61
- _renderFrame(props: AnimationProps): void;
60
+ _renderFrame(animationProps: AnimationProps): void;
62
61
  _clearNeedsRedraw(): void;
63
62
  _setupFrame(): void;
64
63
  _initializeAnimationProps(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"animation-loop.d.ts","sourceRoot":"","sources":["../../src/lib/animation-loop.ts"],"names":[],"mappings":";AAAA,OAAO,EAAO,MAAM,EAAE,WAAW,EAAC,MAAM,cAAc,CAAC;AAEvD,OAAO,EAAC,QAAQ,EAAC,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAC,KAAK,EAAE,IAAI,EAAC,MAAM,iBAAiB,CAAC;AAS5C,+BAA+B;AAC/B,oBAAY,kBAAkB,GAAG;IAC/B,cAAc,C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ACnF,aAAa;IAOnB,cAAc;IAsBd,iBAAiB,IAAI;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAC;IAsBpE,6BAA6B;IAC7B,eAAe;IAQf;;;OAGG;IACH,0BAA0B;IAM1B,YAAY;IAuBZ,UAAU;IAWV,mBAAmB;IAOnB,YAAY,CAAC,KAAK,EAAE,KAAK;IAMzB,aAAa,CAAC,KAAK,EAAE,KAAK;CAG3B"}
@@ -8,13 +8,14 @@ let statIdCounter = 0;
8
8
  const DEFAULT_ANIMATION_LOOP_PROPS = {
9
9
  onCreateDevice: props => luma.createDevice(props),
10
10
  onAddHTML: () => '',
11
- onInitialize: () => ({}),
11
+ onInitialize: async () => {
12
+ return null;
13
+ },
12
14
  onRender: () => {},
13
15
  onFinalize: () => {},
14
16
  onError: error => console.error(error),
15
17
  device: null,
16
18
  deviceProps: {},
17
- debug: false,
18
19
  stats: luma.stats.get("animation-loop-".concat(statIdCounter++)),
19
20
  useDevicePixels: true,
20
21
  autoResizeViewport: true,
@@ -283,14 +284,14 @@ export class AnimationLoop {
283
284
  this._requestAnimationFrame();
284
285
  }
285
286
 
286
- _renderFrame(props) {
287
+ _renderFrame(animationProps) {
287
288
  if (this.display) {
288
- this.display._renderFrame(props);
289
+ this.display._renderFrame(animationProps);
289
290
 
290
291
  return;
291
292
  }
292
293
 
293
- this.props.onRender(props);
294
+ this.props.onRender(this._getAnimationProps());
294
295
  }
295
296
 
296
297
  _clearNeedsRedraw() {
@@ -1 +1 @@
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","event","MouseEvent","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,EAAG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C,MAAL,CAAYiD,gBAAZ,CAA6B,YAA7B,EAA2C,KAAKpF,aAAhD;AACD;AACF;;AAEDD,EAAAA,YAAY,CAACsF,KAAD,EAAe;AACzB,QAAIA,KAAK,YAAYC,UAArB,EAAiC;AAC/B,WAAK1E,kBAAL,GAA0B4C,cAA1B,GAA2C,CAAC6B,KAAK,CAACE,OAAP,EAAgBF,KAAK,CAACG,OAAtB,CAA3C;AACD;AACF;;AAEDxF,EAAAA,aAAa,CAACqF,KAAD,EAAe;AAC1B,SAAKzE,kBAAL,GAA0B4C,cAA1B,GAA2C,IAA3C;AACD;;AA3ewB","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() || [1, 1];\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(event: Event) {\n if (event instanceof MouseEvent) {\n this._getAnimationProps()._mousePosition = [event.offsetX, event.offsetY];\n }\n }\n\n _onMouseleave(event: Event) {\n this._getAnimationProps()._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","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","event","MouseEvent","offsetX","offsetY"],"mappings":";AAEA,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;AAuBA,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,YAAY;AAAE,WAAO,IAAP;AAAc,GAHuB;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,E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cAA1B,GAA2C,CAAC6B,KAAK,CAACE,OAAP,EAAgBF,KAAK,CAACG,OAAtB,CAA3C;AACD;AACF;;AAEDxF,EAAAA,aAAa,CAACqF,KAAD,EAAe;AAC1B,SAAKzE,kBAAL,GAA0B4C,cAA1B,GAA2C,IAA3C;AACD;;AA3ewB","sourcesContent":["// luma.gl, MIT license\n\nimport {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) => Promise<unknown>;\n onRender?: (animationProps: AnimationProps) => unknown;\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 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: async () => { return null; },\n onRender: () => {},\n onFinalize: () => {},\n onError: (error) => console.error(error), // eslint-disable-line no-console\n\n device: null,\n deviceProps: {},\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(animationProps: AnimationProps) {\n // Allow e.g. VR display to render multiple frames.\n if (this.display) {\n this.display._renderFrame(animationProps);\n return;\n }\n\n // call callback\n this.props.onRender(this._getAnimationProps());\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() || [1, 1];\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(event: Event) {\n if (event instanceof MouseEvent) {\n this._getAnimationProps()._mousePosition = [event.offsetX, event.offsetY];\n }\n }\n\n _onMouseleave(event: Event) {\n this._getAnimationProps()._mousePosition = null;\n }\n}\n"],"file":"animation-loop.js"}
@@ -7,7 +7,12 @@ import { AnimationLoop } from './animation-loop';
7
7
  * v9 API
8
8
  */
9
9
  export declare abstract class RenderLoop {
10
+ animationLoop?: AnimationLoop;
10
11
  constructor(animationProps?: AnimationProps);
12
+ destroy(): void;
13
+ onInitialize(animationProps: AnimationProps): Promise<unknown>;
14
+ abstract onRender(animationProps: AnimationProps): unknown;
15
+ abstract onFinalize(animationProps: AnimationProps): void;
11
16
  /** Instantiates and runs the render loop */
12
17
  static run(RenderLoopConstructor: typeof RenderLoop, deviceProps?: DeviceProps): AnimationLoop;
13
18
  }
@@ -1 +1 @@
1
- {"version":3,"file":"render-loop.d.ts","sourceRoot":"","sources":["../../src/lib/render-loop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAC,aAAa,EAAC,MAAM,kBAAkB,CAAC;AAE/C;;;;GAIG;AACH,8BAAsB,UAAU;gBAClB,cAAc,CAAC,EAAE,cAAc;IAE3C,4CAA4C;IAC5C,MAAM,CAAC,GAAG,CAAC,qBAAqB,EAAE,OAAO,UAAU,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,aAAa;CAS/F"}
1
+ {"version":3,"file":"render-loop.d.ts","sourceRoot":"","sources":["../../src/lib/render-loop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAC,aAAa,EAAC,MAAM,kBAAkB,CAAC;AAE/C;;;;GAIG;AACH,8BAAsB,UAAU;IAE9B,aAAa,CAAC,EAAE,aAAa,CAAC;gBAElB,cAAc,CAAC,EAAE,cAAc;IAE3C,OAAO,IAAI,IAAI;IAIT,YAAY,CAAC,cAAc,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC;IACpE,QAAQ,CAAC,QAAQ,CAAC,cAAc,EAAE,cAAc,GAAG,OAAO;IAC1D,QAAQ,CAAC,UAAU,CAAC,cAAc,EAAE,cAAc,GAAG,IAAI;IAEzD,4CAA4C;IAC5C,MAAM,CAAC,GAAG,CAAC,qBAAqB,EAAE,OAAO,UAAU,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,aAAa;CAmC/F"}
@@ -1,48 +1,52 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
2
  import { AnimationLoop } from './animation-loop';
3
3
  export class RenderLoop {
4
- constructor(animationProps) {}
5
-
6
- static run(RenderLoopConstructor, deviceProps) {
7
- const animationLoop = new SyncInitAnimationLoop(RenderLoopConstructor, deviceProps);
8
- return animationLoop;
4
+ constructor(animationProps) {
5
+ _defineProperty(this, "animationLoop", void 0);
9
6
  }
10
7
 
11
- }
8
+ destroy() {
9
+ var _this$animationLoop;
12
10
 
13
- class SyncInitAnimationLoop extends AnimationLoop {
14
- getInfo() {
15
- return this.RenderLoopConstructor.info;
11
+ (_this$animationLoop = this.animationLoop) === null || _this$animationLoop === void 0 ? void 0 : _this$animationLoop.destroy();
16
12
  }
17
13
 
18
- constructor(RenderLoopConstructor, deviceProps) {
19
- super({
20
- deviceProps
21
- });
14
+ async onInitialize(animationProps) {
15
+ return null;
16
+ }
17
+
18
+ static run(RenderLoopConstructor, deviceProps) {
19
+ let renderLoop = null;
20
+ const animationLoop = new AnimationLoop({
21
+ deviceProps,
22
22
 
23
- _defineProperty(this, "RenderLoopConstructor", void 0);
23
+ async onInitialize(animationProps) {
24
+ var _renderLoop;
24
25
 
25
- _defineProperty(this, "renderLoop", null);
26
+ renderLoop = new RenderLoopConstructor(animationProps);
27
+ renderLoop.animationLoop = animationLoop;
28
+ return await ((_renderLoop = renderLoop) === null || _renderLoop === void 0 ? void 0 : _renderLoop.onInitialize(animationProps));
29
+ },
26
30
 
27
- this.RenderLoopConstructor = RenderLoopConstructor;
28
- }
31
+ onRender(animationProps) {
32
+ var _renderLoop2;
29
33
 
30
- onInitialize(animationProps) {
31
- this.renderLoop = new this.RenderLoopConstructor(animationProps);
32
- }
34
+ (_renderLoop2 = renderLoop) === null || _renderLoop2 === void 0 ? void 0 : _renderLoop2.onRender(animationProps);
35
+ },
33
36
 
34
- onRender(animationProps) {
35
- var _this$renderLoop, _this$renderLoop$onRe, _this$renderLoop2, _this$renderLoop2$fra;
37
+ onFinalize(animationProps) {
38
+ var _renderLoop3;
36
39
 
37
- (_this$renderLoop = this.renderLoop) === null || _this$renderLoop === void 0 ? void 0 : (_this$renderLoop$onRe = _this$renderLoop.onRender) === null || _this$renderLoop$onRe === void 0 ? void 0 : _this$renderLoop$onRe.call(_this$renderLoop, animationProps);
38
- (_this$renderLoop2 = this.renderLoop) === null || _this$renderLoop2 === void 0 ? void 0 : (_this$renderLoop2$fra = _this$renderLoop2.frame) === null || _this$renderLoop2$fra === void 0 ? void 0 : _this$renderLoop2$fra.call(_this$renderLoop2, animationProps);
39
- }
40
+ (_renderLoop3 = renderLoop) === null || _renderLoop3 === void 0 ? void 0 : _renderLoop3.onFinalize(animationProps);
41
+ }
40
42
 
41
- onFinalize(animationProps) {
42
- var _this$renderLoop3, _this$renderLoop3$onF, _this$renderLoop4, _this$renderLoop4$des;
43
+ });
44
+
45
+ animationLoop.getInfo = () => {
46
+ return this.RenderLoopConstructor.info;
47
+ };
43
48
 
44
- (_this$renderLoop3 = this.renderLoop) === null || _this$renderLoop3 === void 0 ? void 0 : (_this$renderLoop3$onF = _this$renderLoop3.onFinalize) === null || _this$renderLoop3$onF === void 0 ? void 0 : _this$renderLoop3$onF.call(_this$renderLoop3, animationProps);
45
- (_this$renderLoop4 = this.renderLoop) === null || _this$renderLoop4 === void 0 ? void 0 : (_this$renderLoop4$des = _this$renderLoop4.destroy) === null || _this$renderLoop4$des === void 0 ? void 0 : _this$renderLoop4$des.call(_this$renderLoop4, animationProps);
49
+ return animationLoop;
46
50
  }
47
51
 
48
52
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/lib/render-loop.ts"],"names":["AnimationLoop","RenderLoop","constructor","animationProps","run","RenderLoopConstructor","deviceProps","animationLoop","SyncInitAnimationLoop","getInfo","info","onInitialize","renderLoop","onRender","frame","onFinalize","destroy"],"mappings":";AAEA,SAAQA,aAAR,QAA4B,kBAA5B;AAOA,OAAO,MAAeC,UAAf,CAA0B;AAC/BC,EAAAA,WAAW,CAACC,cAAD,EAAkC,CAAE;;AAGrC,SAAHC,GAAG,CAACC,qBAAD,EAA2CC,WAA3C,EAAqF;AAE7F,UAAMC,aAAa,GAAG,IAAIC,qBAAJ,CAA0BH,qBAA1B,EAAiDC,WAAjD,CAAtB;AAKA,WAAOC,aAAP;AACD;;AAZ8B;;AAgBjC,MAAMC,qBAAN,SAAoCR,aAApC,CAAkD;AAIhDS,EAAAA,OAAO,GAAG;AAER,WAAO,KAAKJ,qBAAL,CAA2BK,IAAlC;AACD;;AAEDR,EAAAA,WAAW,CAACG,qBAAD,EAA2CC,WAA3C,EAAsE;AAC/E,UAAM;AAACA,MAAAA;AAAD,KAAN;;AAD+E;;AAAA,wCAPjD,IAOiD;;AAE/E,SAAKD,qBAAL,GAA6BA,qBAA7B;AACD;;AAEDM,EAAAA,YAAY,CAACR,cAAD,EAAiC;AAE3C,SAAKS,UAAL,GAAkB,IAAI,KAAKP,qBAAT,CAA+BF,cAA/B,CAAlB;AACD;;AAEDU,EAAAA,QAAQ,CAACV,cAAD,EAAiC;AAAA;;AAEvC,6BAAKS,UAAL,+FAAiBC,QAAjB,uGAA4BV,cAA5B;AAEA,8BAAKS,UAAL,iGAAiBE,KAAjB,wGAAyBX,cAAzB;AACD;;AAEDY,EAAAA,UAAU,CAACZ,cAAD,EAAiC;AAAA;;AAEzC,8BAAKS,UAAL,iGAAiBG,UAAjB,wGAA8BZ,cAA9B;AAEA,8BAAKS,UAAL,iGAAiBI,OAAjB,wGAA2Bb,cAA3B;AACD;;AA/B+C","sourcesContent":["import type {DeviceProps} from '@luma.gl/api';\nimport type {AnimationProps} from '../lib/animation-props';\nimport {AnimationLoop} from './animation-loop';\n\n/**\n * Minimal animation loop that initializes models in constructor\n * Simplifying type management\n * v9 API\n */\nexport abstract class RenderLoop {\n constructor(animationProps?: AnimationProps) {}\n\n /** Instantiates and runs the render loop */\n static run(RenderLoopConstructor: typeof RenderLoop, deviceProps?: DeviceProps): AnimationLoop {\n // Create an animation loop;\n const animationLoop = new SyncInitAnimationLoop(RenderLoopConstructor, deviceProps);\n\n // Start the loop automatically\n // animationLoop.start();\n\n return animationLoop;\n }\n}\n\n/** Instantiates the RenderLoop once the device is created */\nclass SyncInitAnimationLoop extends AnimationLoop {\n RenderLoopConstructor: typeof RenderLoop;\n renderLoop: RenderLoop | null = null;\n\n getInfo() {\n // @ts-ignore\n return this.RenderLoopConstructor.info;\n }\n\n constructor(RenderLoopConstructor: typeof RenderLoop, deviceProps?: DeviceProps) {\n super({deviceProps});\n this.RenderLoopConstructor = RenderLoopConstructor;\n }\n\n onInitialize(animationProps: AnimationProps) {\n // @ts-expect-error abstract to prevent instantiation\n this.renderLoop = new this.RenderLoopConstructor(animationProps);\n }\n\n onRender(animationProps: AnimationProps) {\n // @ts-expect-error API still TBD\n this.renderLoop?.onRender?.(animationProps);\n // @ts-expect-error API still TBD\n this.renderLoop?.frame?.(animationProps);\n }\n\n onFinalize(animationProps: AnimationProps) {\n // @ts-expect-error API still TBD\n this.renderLoop?.onFinalize?.(animationProps);\n // @ts-expect-error API still TBD\n this.renderLoop?.destroy?.(animationProps);\n }\n}\n"],"file":"render-loop.js"}
1
+ {"version":3,"sources":["../../src/lib/render-loop.ts"],"names":["AnimationLoop","RenderLoop","constructor","animationProps","destroy","animationLoop","onInitialize","run","RenderLoopConstructor","deviceProps","renderLoop","onRender","onFinalize","getInfo","info"],"mappings":";AAEA,SAAQA,aAAR,QAA4B,kBAA5B;AAOA,OAAO,MAAeC,UAAf,CAA0B;AAI/BC,EAAAA,WAAW,CAACC,cAAD,EAAkC;AAAA;AAAE;;AAE/CC,EAAAA,OAAO,GAAS;AAAA;;AACd,gCAAKC,aAAL,4EAAoBD,OAApB;AACD;;AAEiB,QAAZE,YAAY,CAACH,cAAD,EAAmD;AAAE,WAAO,IAAP;AAAc;;AAK3E,SAAHI,GAAG,CAACC,qBAAD,EAA2CC,WAA3C,EAAqF;AAC7F,QAAIC,UAA6B,GAAG,IAApC;AAGA,UAAML,aAAa,GAAG,IAAIL,aAAJ,CAAkB;AACtCS,MAAAA,WADsC;;AAGtC,YAAMH,YAAN,CAAmBH,cAAnB,EAAqE;AAAA;;AAEnEO,QAAAA,UAAU,GAAG,IAAIF,qBAAJ,CAA0BL,cAA1B,CAAb;AACAO,QAAAA,UAAU,CAAEL,aAAZ,GAA4BA,aAA5B;AAEA,eAAO,sBAAMK,UAAN,gDAAM,YAAYJ,YAAZ,CAAyBH,cAAzB,CAAN,CAAP;AACD,OATqC;;AAWtCQ,MAAAA,QAAQ,CAACR,cAAD,EAAiC;AAAA;;AACvC,wBAAAO,UAAU,UAAV,oDAAYC,QAAZ,CAAqBR,cAArB;AACD,OAbqC;;AAetCS,MAAAA,UAAU,CAACT,cAAD,EAAiC;AAAA;;AACzC,wBAAAO,UAAU,UAAV,oDAAYE,UAAZ,CAAuBT,cAAvB;AACD;;AAjBqC,KAAlB,CAAtB;;AAqBAE,IAAAA,aAAa,CAACQ,OAAd,GAAwB,MAAM;AAE5B,aAAO,KAAKL,qBAAL,CAA2BM,IAAlC;AACD,KAHD;;AAQA,WAAOT,aAAP;AACD;;AAjD8B","sourcesContent":["import type {DeviceProps} from '@luma.gl/api';\nimport type {AnimationProps} from '../lib/animation-props';\nimport {AnimationLoop} from './animation-loop';\n\n/**\n * Minimal animation loop that initializes models in constructor\n * Simplifying type management\n * v9 API\n */\nexport abstract class RenderLoop {\n\n animationLoop?: AnimationLoop;\n\n constructor(animationProps?: AnimationProps) {}\n\n destroy(): void {\n this.animationLoop?.destroy();\n }\n\n async onInitialize(animationProps: AnimationProps): Promise<unknown> { return null; }\n abstract onRender(animationProps: AnimationProps): unknown;\n abstract onFinalize(animationProps: AnimationProps): void;\n\n /** Instantiates and runs the render loop */\n static run(RenderLoopConstructor: typeof RenderLoop, deviceProps?: DeviceProps): AnimationLoop {\n let renderLoop: RenderLoop | null = null;\n\n // Create an animation loop;\n const animationLoop = new AnimationLoop({\n deviceProps,\n\n async onInitialize(animationProps: AnimationProps): Promise<unknown> {\n // @ts-expect-error abstract to prevent instantiation\n renderLoop = new RenderLoopConstructor(animationProps);\n renderLoop!.animationLoop = animationLoop;\n // Any async loading can be handled here\n return await renderLoop?.onInitialize(animationProps);\n },\n \n onRender(animationProps: AnimationProps) {\n renderLoop?.onRender(animationProps);\n },\n \n onFinalize(animationProps: AnimationProps) {\n renderLoop?.onFinalize(animationProps);\n }\n });\n\n // @ts-expect-error Hack: adds info for the website to find\n animationLoop.getInfo = () => {\n // @ts-ignore\n return this.RenderLoopConstructor.info;\n }\n\n // Start the loop automatically\n // animationLoop.start();\n\n return animationLoop;\n }\n}\n"],"file":"render-loop.js"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luma.gl/engine",
3
- "version": "9.0.0-alpha.10",
3
+ "version": "9.0.0-alpha.11",
4
4
  "description": "WebGL2 Components for High Performance Rendering and Computation",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -31,12 +31,12 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@babel/runtime": "^7.0.0",
34
- "@luma.gl/constants": "9.0.0-alpha.10",
35
- "@luma.gl/shadertools": "9.0.0-alpha.10",
36
- "@luma.gl/webgl": "9.0.0-alpha.10",
34
+ "@luma.gl/constants": "9.0.0-alpha.11",
35
+ "@luma.gl/shadertools": "9.0.0-alpha.11",
36
+ "@luma.gl/webgl": "9.0.0-alpha.11",
37
37
  "@math.gl/core": "^3.5.0",
38
38
  "@probe.gl/log": "^3.5.0",
39
39
  "@probe.gl/stats": "^3.5.2"
40
40
  },
41
- "gitHead": "eaa912a3f1479a58e6b9915e905a9274e11d9c34"
41
+ "gitHead": "3a99f57970a609d50948851aec7a56d56685ab62"
42
42
  }
@@ -1,3 +1,5 @@
1
+ // luma.gl, MIT license
2
+
1
3
  import {luma, Device, DeviceProps} from '@luma.gl/api';
2
4
  import {requestAnimationFrame, cancelAnimationFrame} from '@luma.gl/api';
3
5
  import {Timeline} from '../animation/timeline';
@@ -15,8 +17,8 @@ type ContextProps = DeviceProps;
15
17
  export type AnimationLoopProps = {
16
18
  onCreateDevice?: (props: DeviceProps) => Promise<Device>;
17
19
  onAddHTML?: (div: HTMLDivElement) => string; // innerHTML
18
- onInitialize?: (animationProps: AnimationProps) => void;
19
- onRender?: (animationProps: AnimationProps) => void;
20
+ onInitialize?: (animationProps: AnimationProps) => Promise<unknown>;
21
+ onRender?: (animationProps: AnimationProps) => unknown;
20
22
  onFinalize?: (animationProps: AnimationProps) => void;
21
23
  onError?: (reason: Error) => void;
22
24
 
@@ -25,7 +27,6 @@ export type AnimationLoopProps = {
25
27
  stats?: Stats;
26
28
 
27
29
  // view parameters
28
- debug?: boolean;
29
30
  autoResizeViewport?: boolean;
30
31
  autoResizeDrawingBuffer?: boolean;
31
32
  useDevicePixels?: number | boolean;
@@ -34,14 +35,13 @@ export type AnimationLoopProps = {
34
35
  const DEFAULT_ANIMATION_LOOP_PROPS: Required<AnimationLoopProps> = {
35
36
  onCreateDevice: (props: DeviceProps): Promise<Device> => luma.createDevice(props),
36
37
  onAddHTML: () => '',
37
- onInitialize: () => ({}),
38
+ onInitialize: async () => { return null; },
38
39
  onRender: () => {},
39
40
  onFinalize: () => {},
40
41
  onError: (error) => console.error(error), // eslint-disable-line no-console
41
42
 
42
43
  device: null,
43
44
  deviceProps: {},
44
- debug: false,
45
45
  stats: luma.stats.get(`animation-loop-${statIdCounter++}`),
46
46
 
47
47
  // view parameters
@@ -326,15 +326,15 @@ export class AnimationLoop {
326
326
 
327
327
  // Called on each frame, can be overridden to call onRender multiple times
328
328
  // to support e.g. stereoscopic rendering
329
- _renderFrame(props: AnimationProps) {
329
+ _renderFrame(animationProps: AnimationProps) {
330
330
  // Allow e.g. VR display to render multiple frames.
331
331
  if (this.display) {
332
- this.display._renderFrame(props);
332
+ this.display._renderFrame(animationProps);
333
333
  return;
334
334
  }
335
335
 
336
336
  // call callback
337
- this.props.onRender(props);
337
+ this.props.onRender(this._getAnimationProps());
338
338
  // end callback
339
339
  }
340
340
 
@@ -8,12 +8,49 @@ import {AnimationLoop} from './animation-loop';
8
8
  * v9 API
9
9
  */
10
10
  export abstract class RenderLoop {
11
+
12
+ animationLoop?: AnimationLoop;
13
+
11
14
  constructor(animationProps?: AnimationProps) {}
12
15
 
16
+ destroy(): void {
17
+ this.animationLoop?.destroy();
18
+ }
19
+
20
+ async onInitialize(animationProps: AnimationProps): Promise<unknown> { return null; }
21
+ abstract onRender(animationProps: AnimationProps): unknown;
22
+ abstract onFinalize(animationProps: AnimationProps): void;
23
+
13
24
  /** Instantiates and runs the render loop */
14
25
  static run(RenderLoopConstructor: typeof RenderLoop, deviceProps?: DeviceProps): AnimationLoop {
26
+ let renderLoop: RenderLoop | null = null;
27
+
15
28
  // Create an animation loop;
16
- const animationLoop = new SyncInitAnimationLoop(RenderLoopConstructor, deviceProps);
29
+ const animationLoop = new AnimationLoop({
30
+ deviceProps,
31
+
32
+ async onInitialize(animationProps: AnimationProps): Promise<unknown> {
33
+ // @ts-expect-error abstract to prevent instantiation
34
+ renderLoop = new RenderLoopConstructor(animationProps);
35
+ renderLoop!.animationLoop = animationLoop;
36
+ // Any async loading can be handled here
37
+ return await renderLoop?.onInitialize(animationProps);
38
+ },
39
+
40
+ onRender(animationProps: AnimationProps) {
41
+ renderLoop?.onRender(animationProps);
42
+ },
43
+
44
+ onFinalize(animationProps: AnimationProps) {
45
+ renderLoop?.onFinalize(animationProps);
46
+ }
47
+ });
48
+
49
+ // @ts-expect-error Hack: adds info for the website to find
50
+ animationLoop.getInfo = () => {
51
+ // @ts-ignore
52
+ return this.RenderLoopConstructor.info;
53
+ }
17
54
 
18
55
  // Start the loop automatically
19
56
  // animationLoop.start();
@@ -21,38 +58,3 @@ export abstract class RenderLoop {
21
58
  return animationLoop;
22
59
  }
23
60
  }
24
-
25
- /** Instantiates the RenderLoop once the device is created */
26
- class SyncInitAnimationLoop extends AnimationLoop {
27
- RenderLoopConstructor: typeof RenderLoop;
28
- renderLoop: RenderLoop | null = null;
29
-
30
- getInfo() {
31
- // @ts-ignore
32
- return this.RenderLoopConstructor.info;
33
- }
34
-
35
- constructor(RenderLoopConstructor: typeof RenderLoop, deviceProps?: DeviceProps) {
36
- super({deviceProps});
37
- this.RenderLoopConstructor = RenderLoopConstructor;
38
- }
39
-
40
- onInitialize(animationProps: AnimationProps) {
41
- // @ts-expect-error abstract to prevent instantiation
42
- this.renderLoop = new this.RenderLoopConstructor(animationProps);
43
- }
44
-
45
- onRender(animationProps: AnimationProps) {
46
- // @ts-expect-error API still TBD
47
- this.renderLoop?.onRender?.(animationProps);
48
- // @ts-expect-error API still TBD
49
- this.renderLoop?.frame?.(animationProps);
50
- }
51
-
52
- onFinalize(animationProps: AnimationProps) {
53
- // @ts-expect-error API still TBD
54
- this.renderLoop?.onFinalize?.(animationProps);
55
- // @ts-expect-error API still TBD
56
- this.renderLoop?.destroy?.(animationProps);
57
- }
58
- }