Skip to content

Commit

Permalink
Merge bddb4f9 into cfe1298
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Apr 8, 2019
2 parents cfe1298 + bddb4f9 commit 3de7d03
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 59 deletions.
60 changes: 15 additions & 45 deletions modules/addons/src/webvr/display-animation-loop.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,30 @@
import {AnimationLoop, withParameters} from '@luma.gl/core';
import {AnimationLoop} from '@luma.gl/core';

// This code could be folded into the core animation loop
export default class DisplayAnimationLoop extends AnimationLoop {
setDisplay(display) {
if (this.display !== display) {
if (this.display) {
this.display.detachDisplay();
}

this.display = display;

if (this.display && this.gl) {
this.display.attachDisplay(this.gl);
}
}
finalize() {
this._setDisplay(null);
}

onFrame(...args) {
const views = this.display && this.display.getViews();
if (!views) {
return super.onFrame(...args);
_setDisplay(display) {
// store anumation loop on the display
if (display) {
display.animationLoop = this;
}

const options = args[0];
const {width, height} = options;

// Need both vrPresenting and vrFrame
// to avoid race conditions when we exit VR
// after we schedule an animation frame
for (const view of views) {
const [x, y, w, h] = view.viewport;
const viewRect = [x * width, y * height, w * width, h * height];

withParameters(
this.gl,
{
viewport: viewRect,
scissor: viewRect,
scissorTest: true
},
() => super.onRender({...options, ...view})
);
if (!display && this.display) {
this.display.finalize();
this.display.animationLoop = null;
}

this.display.submitFrame();
this.gl.viewport(0, 0, width, height);
return null;
this.display = display;
}

_createWebGLContext(...args) {
const before = this.gl;
super._createWebGLContext(...args);

if (before !== this.gl && this.display) {
this.display.attachDisplay(this.gl);
_renderFrame(...args) {
if (this.display) {
return this.display._renderFrame();
}
return super.onFrame(...args);
}

_requestAnimationFrame(renderFrame) {
Expand Down
36 changes: 36 additions & 0 deletions modules/addons/src/webvr/display.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {withParameters} from '@luma.gl/core';

export default class Display {
attachDisplay(gl) {
this.gl = gl;
Expand Down Expand Up @@ -30,4 +32,38 @@ export default class Display {
requestAnimationFrame() {
return false;
}

// AnimationLoop calls this API

_renderFrame(...args) {
const views = this.getViews();
if (!views) {
return false;
}

const options = args[0];
const {width, height} = options;

// Need both vrPresenting and vrFrame
// to avoid race conditions when we exit VR
// after we schedule an animation frame
for (const view of views) {
const [x, y, w, h] = view.viewport;
const viewRect = [x * width, y * height, w * width, h * height];

withParameters(
this.gl,
{
viewport: viewRect,
scissor: viewRect,
scissorTest: true
},
() => this.animationLoop.onRender({...options, ...view})
);
}

this.submitFrame();
this.gl.viewport(0, 0, width, height);
return true;
}
}
31 changes: 21 additions & 10 deletions modules/addons/src/webvr/vr-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ export default class VRDisplay extends Display {
}
}

attachDisplay(gl) {
super.attachDisplay(gl);
this._addVRButton();
}

detachDisplay() {
super.detachDisplay();
finalize() {
this._removeVRButton();
}

getViews(options) {
this._addVRButton();

// Need both vrPresenting and vrFrame
// to avoid race conditions when we exit VR
// after we schedule an animation frame
Expand Down Expand Up @@ -90,27 +86,42 @@ export default class VRDisplay extends Display {
// TODO: Consider resizing canvas to match vrDisplay.getEyeParameters()
// TODO: Maybe allow to select display?
async _addVRButton() {
if (this.vrButton) {
return;
}

const canvas = this._getCanvas();
if (!canvas) {
return;
}

const displays = await navigator.getVRDisplays();
if (displays && displays.length) {
log.info(2, 'Found VR Displays', displays)();

this.vrDisplay = displays[0];
this.vrButton = createEnterVRButton({
canvas: this.gl.canvas,
canvas,
title: `Enter VR (${this.vrDisplay.displayName})`
});
this.vrButton.onclick = () => this._startDisplay();
}
}

_getCanvas() {
return this.animationLoop.canvas || (this.animationLoop.gl || this.animationLoop.gl.canvas);
}

_removeVRButton() {
// TODO
if (this.vrButton) {
// TODO
}
}

_startDisplay() {
this.vrDisplay.requestPresent([
{
source: this.gl.canvas
source: this.getCanvas()
}
]);
}
Expand Down
8 changes: 4 additions & 4 deletions modules/core/src/lib/animation-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ export default class AnimationLoop {
this._setupFrame();
this._updateCallbackData();

// call callback
this.onFrame(this.animationProps);
// end callback
this._renderFrame(this.animationProps);

// clear needsRedraw flag
this._clearNeedsRedraw();
Expand Down Expand Up @@ -230,8 +228,10 @@ export default class AnimationLoop {

// Called on each frame, can be overridden to call onRender multiple times
// to support e.g. stereoscopic rendering
onFrame(...args) {
_renderFrame(...args) {
// call callback
this.onRender(...args);
// end callback
}

onRender(...args) {
Expand Down

0 comments on commit 3de7d03

Please sign in to comment.