提交 06903c44 编写于 作者: M Mr.doob

WebGLRenderer: Added WebGLAnimation.

上级 b5537401
...@@ -49,6 +49,8 @@ var WEBVR = { ...@@ -49,6 +49,8 @@ var WEBVR = {
function onSessionEnded( event ) { function onSessionEnded( event ) {
currentSession.removeEventListener( 'end', onSessionEnded );
renderer.vr.setSession( null ); renderer.vr.setSession( null );
button.textContent = 'ENTER XR'; button.textContent = 'ENTER XR';
......
...@@ -20,6 +20,7 @@ import { UniformsLib } from './shaders/UniformsLib.js'; ...@@ -20,6 +20,7 @@ import { UniformsLib } from './shaders/UniformsLib.js';
import { UniformsUtils } from './shaders/UniformsUtils.js'; import { UniformsUtils } from './shaders/UniformsUtils.js';
import { Vector3 } from '../math/Vector3.js'; import { Vector3 } from '../math/Vector3.js';
import { Vector4 } from '../math/Vector4.js'; import { Vector4 } from '../math/Vector4.js';
import { WebGLAnimation } from './webgl/WebGLAnimation.js';
import { WebGLAttributes } from './webgl/WebGLAttributes.js'; import { WebGLAttributes } from './webgl/WebGLAttributes.js';
import { WebGLBackground } from './webgl/WebGLBackground.js'; import { WebGLBackground } from './webgl/WebGLBackground.js';
import { WebGLBufferRenderer } from './webgl/WebGLBufferRenderer.js'; import { WebGLBufferRenderer } from './webgl/WebGLBufferRenderer.js';
...@@ -1015,53 +1016,25 @@ function WebGLRenderer( parameters ) { ...@@ -1015,53 +1016,25 @@ function WebGLRenderer( parameters ) {
// Animation Loop // Animation Loop
var isAnimating = false; var onAnimationFrameCallback = null;
var onAnimationFrame = null;
function startAnimation() { function onAnimationFrame() {
if ( isAnimating ) return; if ( vr.isPresenting() ) return;
if ( onAnimationFrameCallback ) onAnimationFrameCallback();
requestAnimationLoopFrame();
isAnimating = true;
}
function stopAnimation() {
isAnimating = false;
} }
function requestAnimationLoopFrame() { var animation = new WebGLAnimation();
animation.setAnimationLoop( onAnimationFrame );
if ( vr.isPresenting() ) { animation.setContext( window );
vr.requestAnimationFrame( animationLoop );
} else {
window.requestAnimationFrame( animationLoop );
}
}
function animationLoop( time ) {
if ( isAnimating === false ) return;
onAnimationFrame( time );
requestAnimationLoopFrame();
}
this.setAnimationLoop = function ( callback ) { this.setAnimationLoop = function ( callback ) {
onAnimationFrame = callback; onAnimationFrameCallback = callback;
onAnimationFrame !== null ? startAnimation() : stopAnimation(); animation.start();
vr.setAnimationLoop( callback );
}; };
......
/**
* @author mrdoob / http://mrdoob.com/
*/
function WebGLAnimation() {
var context = null;
var isAnimating = false;
var animationLoop = null;
function onAnimationFrame( time, frame ) {
if ( isAnimating === false ) return;
animationLoop( time, frame );
context.requestAnimationFrame( onAnimationFrame );
}
return {
start: function () {
if ( isAnimating === true ) return;
if ( animationLoop === null ) return;
context.requestAnimationFrame( onAnimationFrame );
isAnimating = true;
},
stop: function () {
isAnimating = false;
},
setAnimationLoop: function ( callback ) {
animationLoop = callback;
},
setContext: function ( value ) {
context = value;
}
}
}
export { WebGLAnimation };
...@@ -8,6 +8,7 @@ import { Vector4 } from '../../math/Vector4.js'; ...@@ -8,6 +8,7 @@ import { Vector4 } from '../../math/Vector4.js';
import { Quaternion } from '../../math/Quaternion.js'; import { Quaternion } from '../../math/Quaternion.js';
import { ArrayCamera } from '../../cameras/ArrayCamera.js'; import { ArrayCamera } from '../../cameras/ArrayCamera.js';
import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js'; import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
function WebVRManager( renderer ) { function WebVRManager( renderer ) {
...@@ -67,10 +68,14 @@ function WebVRManager( renderer ) { ...@@ -67,10 +68,14 @@ function WebVRManager( renderer ) {
renderer.setDrawingBufferSize( renderWidth * 2, renderHeight, 1 ); renderer.setDrawingBufferSize( renderWidth * 2, renderHeight, 1 );
animation.start();
} else if ( scope.enabled ) { } else if ( scope.enabled ) {
renderer.setDrawingBufferSize( currentSize.width, currentSize.height, currentPixelRatio ); renderer.setDrawingBufferSize( currentSize.width, currentSize.height, currentPixelRatio );
animation.stop();
} }
} }
...@@ -90,6 +95,8 @@ function WebVRManager( renderer ) { ...@@ -90,6 +95,8 @@ function WebVRManager( renderer ) {
if ( value !== undefined ) device = value; if ( value !== undefined ) device = value;
animation.setContext( value );
}; };
this.setPoseTarget = function ( object ) { this.setPoseTarget = function ( object ) {
...@@ -228,9 +235,13 @@ function WebVRManager( renderer ) { ...@@ -228,9 +235,13 @@ function WebVRManager( renderer ) {
this.isPresenting = isPresenting; this.isPresenting = isPresenting;
this.requestAnimationFrame = function ( callback ) { // Animation Loop
var animation = new WebGLAnimation();
this.setAnimationLoop = function ( callback ) {
device.requestAnimationFrame( callback ); animation.setAnimationLoop( callback );
}; };
...@@ -250,6 +261,14 @@ function WebVRManager( renderer ) { ...@@ -250,6 +261,14 @@ function WebVRManager( renderer ) {
}; };
// DEPRECATED
this.requestAnimationFrame = function ( callback ) {
// device.requestAnimationFrame( callback );
};
} }
export { WebVRManager }; export { WebVRManager };
...@@ -8,6 +8,7 @@ import { Vector3 } from '../../math/Vector3.js'; ...@@ -8,6 +8,7 @@ import { Vector3 } from '../../math/Vector3.js';
import { Quaternion } from '../../math/Quaternion.js'; import { Quaternion } from '../../math/Quaternion.js';
import { ArrayCamera } from '../../cameras/ArrayCamera.js'; import { ArrayCamera } from '../../cameras/ArrayCamera.js';
import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js'; import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
function WebXRManager( gl ) { function WebXRManager( gl ) {
...@@ -59,18 +60,30 @@ function WebXRManager( gl ) { ...@@ -59,18 +60,30 @@ function WebXRManager( gl ) {
}; };
//
this.setSession = function ( value ) { this.setSession = function ( value ) {
session = value; session = value;
if ( session !== null ) { if ( session !== null ) {
session.addEventListener( 'end', function () {
gl.bindFramebuffer( gl.FRAMEBUFFER, null );
animation.stop();
} );
session.baseLayer = new XRWebGLLayer( session, gl ); session.baseLayer = new XRWebGLLayer( session, gl );
session.requestFrameOfReference( 'stage' ).then( function ( value ) { session.requestFrameOfReference( 'stage' ).then( function ( value ) {
frameOfRef = value; frameOfRef = value;
isExclusive = session.exclusive; isExclusive = session.exclusive;
animation.setContext( session );
animation.start();
} ); } );
} }
...@@ -85,48 +98,55 @@ function WebXRManager( gl ) { ...@@ -85,48 +98,55 @@ function WebXRManager( gl ) {
this.isPresenting = isPresenting; this.isPresenting = isPresenting;
this.requestAnimationFrame = function ( callback ) { // Animation Loop
function onFrame( time, frame ) { var onAnimationFrameCallback = null;
pose = frame.getDevicePose( frameOfRef ); function onAnimationFrame( time, frame ) {
var layer = session.baseLayer; pose = frame.getDevicePose( frameOfRef );
var views = frame.views;
for ( var i = 0; i < views.length; i ++ ) { var layer = session.baseLayer;
var views = frame.views;
var view = views[ i ]; for ( var i = 0; i < views.length; i ++ ) {
var viewport = layer.getViewport( view );
var viewMatrix = pose.getViewMatrix( view );
var camera = cameraVR.cameras[ i ]; var view = views[ i ];
camera.projectionMatrix.fromArray( view.projectionMatrix ); var viewport = layer.getViewport( view );
camera.matrixWorldInverse.fromArray( viewMatrix ); var viewMatrix = pose.getViewMatrix( view );
camera.matrixWorld.getInverse( camera.matrixWorldInverse );
camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
if ( i === 0 ) { var camera = cameraVR.cameras[ i ];
camera.projectionMatrix.fromArray( view.projectionMatrix );
camera.matrixWorldInverse.fromArray( viewMatrix );
camera.matrixWorld.getInverse( camera.matrixWorldInverse );
camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
cameraVR.matrixWorld.copy( camera.matrixWorld ); if ( i === 0 ) {
cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
// HACK (mrdoob) cameraVR.matrixWorld.copy( camera.matrixWorld );
// https://github.com/w3c/webvr/issues/203 cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
cameraVR.projectionMatrix.copy( camera.projectionMatrix ); // HACK (mrdoob)
// https://github.com/w3c/webvr/issues/203
} cameraVR.projectionMatrix.copy( camera.projectionMatrix );
} }
gl.bindFramebuffer( gl.FRAMEBUFFER, session.baseLayer.framebuffer ); }
callback(); gl.bindFramebuffer( gl.FRAMEBUFFER, session.baseLayer.framebuffer );
} if ( onAnimationFrameCallback ) onAnimationFrameCallback();
}
session.requestAnimationFrame( onFrame ); var animation = new WebGLAnimation();
animation.setAnimationLoop( onAnimationFrame );
this.setAnimationLoop = function ( callback ) {
onAnimationFrameCallback = callback;
}; };
...@@ -139,6 +159,8 @@ function WebXRManager( gl ) { ...@@ -139,6 +159,8 @@ function WebXRManager( gl ) {
}; };
this.requestAnimationFrame = function () {};
this.submitFrame = function () {}; this.submitFrame = function () {};
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册