VREffect.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. * @author dmarcos / https://github.com/dmarcos
  3. * @author mrdoob / http://mrdoob.com
  4. *
  5. * WebVR Spec: http://mozvr.github.io/webvr-spec/webvr.html
  6. *
  7. * Firefox: http://mozvr.com/downloads/
  8. * Chromium: https://webvr.info/get-chrome
  9. *
  10. */
  11. THREE.VREffect = function( renderer, onError ) {
  12. var vrDisplay, vrDisplays;
  13. var eyeTranslationL = new THREE.Vector3();
  14. var eyeTranslationR = new THREE.Vector3();
  15. var renderRectL, renderRectR;
  16. var frameData = null;
  17. if ( 'VRFrameData' in window ) {
  18. frameData = new window.VRFrameData();
  19. }
  20. function gotVRDisplays( displays ) {
  21. vrDisplays = displays;
  22. if ( displays.length > 0 ) {
  23. vrDisplay = displays[ 0 ];
  24. } else {
  25. if ( onError ) onError( 'HMD not available' );
  26. }
  27. }
  28. if ( navigator.getVRDisplays ) {
  29. navigator.getVRDisplays().then( gotVRDisplays ).catch( function() {
  30. console.warn( 'THREE.VREffect: Unable to get VR Displays' );
  31. } );
  32. }
  33. //
  34. this.isPresenting = false;
  35. this.scale = 1;
  36. var scope = this;
  37. var rendererSize = renderer.getSize();
  38. var rendererUpdateStyle = false;
  39. var rendererPixelRatio = renderer.getPixelRatio();
  40. this.getVRDisplay = function() {
  41. return vrDisplay;
  42. };
  43. this.setVRDisplay = function( value ) {
  44. vrDisplay = value;
  45. };
  46. this.getVRDisplays = function() {
  47. console.warn( 'THREE.VREffect: getVRDisplays() is being deprecated.' );
  48. return vrDisplays;
  49. };
  50. this.setSize = function( width, height, updateStyle ) {
  51. rendererSize = { width: width, height: height };
  52. rendererUpdateStyle = updateStyle;
  53. if ( scope.isPresenting ) {
  54. var eyeParamsL = vrDisplay.getEyeParameters( 'left' );
  55. renderer.setPixelRatio( 1 );
  56. renderer.setSize( eyeParamsL.renderWidth * 2, eyeParamsL.renderHeight, false );
  57. } else {
  58. renderer.setPixelRatio( rendererPixelRatio );
  59. renderer.setSize( width, height, updateStyle );
  60. }
  61. };
  62. // VR presentation
  63. var canvas = renderer.domElement;
  64. var defaultLeftBounds = [ 0.0, 0.0, 0.5, 1.0 ];
  65. var defaultRightBounds = [ 0.5, 0.0, 0.5, 1.0 ];
  66. function onVRDisplayPresentChange() {
  67. var wasPresenting = scope.isPresenting;
  68. scope.isPresenting = vrDisplay !== undefined && vrDisplay.isPresenting;
  69. if ( scope.isPresenting ) {
  70. var eyeParamsL = vrDisplay.getEyeParameters( 'left' );
  71. var eyeWidth = eyeParamsL.renderWidth;
  72. var eyeHeight = eyeParamsL.renderHeight;
  73. if ( ! wasPresenting ) {
  74. rendererPixelRatio = renderer.getPixelRatio();
  75. rendererSize = renderer.getSize();
  76. renderer.setPixelRatio( 1 );
  77. renderer.setSize( eyeWidth * 2, eyeHeight, false );
  78. }
  79. } else if ( wasPresenting ) {
  80. renderer.setPixelRatio( rendererPixelRatio );
  81. renderer.setSize( rendererSize.width, rendererSize.height, rendererUpdateStyle );
  82. }
  83. }
  84. window.addEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange, false );
  85. this.setFullScreen = function( boolean ) {
  86. return new Promise( function( resolve, reject ) {
  87. if ( vrDisplay === undefined ) {
  88. reject( new Error( 'No VR hardware found.' ) );
  89. return;
  90. }
  91. if ( scope.isPresenting === boolean ) {
  92. resolve();
  93. return;
  94. }
  95. if ( boolean ) {
  96. resolve( vrDisplay.requestPresent( [ { source: canvas } ] ) );
  97. } else {
  98. resolve( vrDisplay.exitPresent() );
  99. }
  100. } );
  101. };
  102. this.requestPresent = function() {
  103. return this.setFullScreen( true );
  104. };
  105. this.exitPresent = function() {
  106. return this.setFullScreen( false );
  107. };
  108. this.requestAnimationFrame = function( f ) {
  109. if ( vrDisplay !== undefined ) {
  110. return vrDisplay.requestAnimationFrame( f );
  111. } else {
  112. return window.requestAnimationFrame( f );
  113. }
  114. };
  115. this.cancelAnimationFrame = function( h ) {
  116. if ( vrDisplay !== undefined ) {
  117. vrDisplay.cancelAnimationFrame( h );
  118. } else {
  119. window.cancelAnimationFrame( h );
  120. }
  121. };
  122. this.submitFrame = function() {
  123. if ( vrDisplay !== undefined && scope.isPresenting ) {
  124. vrDisplay.submitFrame();
  125. }
  126. };
  127. this.autoSubmitFrame = true;
  128. // render
  129. var cameraL = new THREE.PerspectiveCamera();
  130. cameraL.layers.enable( 1 );
  131. var cameraR = new THREE.PerspectiveCamera();
  132. cameraR.layers.enable( 2 );
  133. this.render = function( scene, camera, renderTarget, forceClear ) {
  134. if ( vrDisplay && scope.isPresenting ) {
  135. var autoUpdate = scene.autoUpdate;
  136. if ( autoUpdate ) {
  137. scene.updateMatrixWorld();
  138. scene.autoUpdate = false;
  139. }
  140. var eyeParamsL = vrDisplay.getEyeParameters( 'left' );
  141. var eyeParamsR = vrDisplay.getEyeParameters( 'right' );
  142. eyeTranslationL.fromArray( eyeParamsL.offset );
  143. eyeTranslationR.fromArray( eyeParamsR.offset );
  144. if ( Array.isArray( scene ) ) {
  145. console.warn( 'THREE.VREffect.render() no longer supports arrays. Use object.layers instead.' );
  146. scene = scene[ 0 ];
  147. }
  148. // When rendering we don't care what the recommended size is, only what the actual size
  149. // of the backbuffer is.
  150. var size = renderer.getSize();
  151. var layers = vrDisplay.getLayers();
  152. var leftBounds;
  153. var rightBounds;
  154. if ( layers.length ) {
  155. var layer = layers[ 0 ];
  156. leftBounds = layer.leftBounds !== null && layer.leftBounds.length === 4 ? layer.leftBounds : defaultLeftBounds;
  157. rightBounds = layer.rightBounds !== null && layer.rightBounds.length === 4 ? layer.rightBounds : defaultRightBounds;
  158. } else {
  159. leftBounds = defaultLeftBounds;
  160. rightBounds = defaultRightBounds;
  161. }
  162. renderRectL = {
  163. x: Math.round( size.width * leftBounds[ 0 ] ),
  164. y: Math.round( size.height * leftBounds[ 1 ] ),
  165. width: Math.round( size.width * leftBounds[ 2 ] ),
  166. height: Math.round( size.height * leftBounds[ 3 ] )
  167. };
  168. renderRectR = {
  169. x: Math.round( size.width * rightBounds[ 0 ] ),
  170. y: Math.round( size.height * rightBounds[ 1 ] ),
  171. width: Math.round( size.width * rightBounds[ 2 ] ),
  172. height: Math.round( size.height * rightBounds[ 3 ] )
  173. };
  174. if ( renderTarget ) {
  175. renderer.setRenderTarget( renderTarget );
  176. renderTarget.scissorTest = true;
  177. } else {
  178. renderer.setRenderTarget( null );
  179. renderer.setScissorTest( true );
  180. }
  181. if ( renderer.autoClear || forceClear ) renderer.clear();
  182. if ( camera.parent === null ) camera.updateMatrixWorld();
  183. camera.matrixWorld.decompose( cameraL.position, cameraL.quaternion, cameraL.scale );
  184. camera.matrixWorld.decompose( cameraR.position, cameraR.quaternion, cameraR.scale );
  185. var scale = this.scale;
  186. cameraL.translateOnAxis( eyeTranslationL, scale );
  187. cameraR.translateOnAxis( eyeTranslationR, scale );
  188. if ( vrDisplay.getFrameData ) {
  189. vrDisplay.depthNear = camera.near;
  190. vrDisplay.depthFar = camera.far;
  191. vrDisplay.getFrameData( frameData );
  192. cameraL.projectionMatrix.elements = frameData.leftProjectionMatrix;
  193. cameraR.projectionMatrix.elements = frameData.rightProjectionMatrix;
  194. } else {
  195. cameraL.projectionMatrix = fovToProjection( eyeParamsL.fieldOfView, true, camera.near, camera.far );
  196. cameraR.projectionMatrix = fovToProjection( eyeParamsR.fieldOfView, true, camera.near, camera.far );
  197. }
  198. // render left eye
  199. if ( renderTarget ) {
  200. renderTarget.viewport.set( renderRectL.x, renderRectL.y, renderRectL.width, renderRectL.height );
  201. renderTarget.scissor.set( renderRectL.x, renderRectL.y, renderRectL.width, renderRectL.height );
  202. } else {
  203. renderer.setViewport( renderRectL.x, renderRectL.y, renderRectL.width, renderRectL.height );
  204. renderer.setScissor( renderRectL.x, renderRectL.y, renderRectL.width, renderRectL.height );
  205. }
  206. renderer.render( scene, cameraL, renderTarget, forceClear );
  207. // render right eye
  208. if ( renderTarget ) {
  209. renderTarget.viewport.set( renderRectR.x, renderRectR.y, renderRectR.width, renderRectR.height );
  210. renderTarget.scissor.set( renderRectR.x, renderRectR.y, renderRectR.width, renderRectR.height );
  211. } else {
  212. renderer.setViewport( renderRectR.x, renderRectR.y, renderRectR.width, renderRectR.height );
  213. renderer.setScissor( renderRectR.x, renderRectR.y, renderRectR.width, renderRectR.height );
  214. }
  215. renderer.render( scene, cameraR, renderTarget, forceClear );
  216. if ( renderTarget ) {
  217. renderTarget.viewport.set( 0, 0, size.width, size.height );
  218. renderTarget.scissor.set( 0, 0, size.width, size.height );
  219. renderTarget.scissorTest = false;
  220. renderer.setRenderTarget( null );
  221. } else {
  222. renderer.setViewport( 0, 0, size.width, size.height );
  223. renderer.setScissorTest( false );
  224. }
  225. if ( autoUpdate ) {
  226. scene.autoUpdate = true;
  227. }
  228. if ( scope.autoSubmitFrame ) {
  229. scope.submitFrame();
  230. }
  231. return;
  232. }
  233. // Regular render mode if not HMD
  234. renderer.render( scene, camera, renderTarget, forceClear );
  235. };
  236. this.dispose = function() {
  237. window.removeEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange, false );
  238. };
  239. //
  240. function fovToNDCScaleOffset( fov ) {
  241. var pxscale = 2.0 / ( fov.leftTan + fov.rightTan );
  242. var pxoffset = ( fov.leftTan - fov.rightTan ) * pxscale * 0.5;
  243. var pyscale = 2.0 / ( fov.upTan + fov.downTan );
  244. var pyoffset = ( fov.upTan - fov.downTan ) * pyscale * 0.5;
  245. return { scale: [ pxscale, pyscale ], offset: [ pxoffset, pyoffset ] };
  246. }
  247. function fovPortToProjection( fov, rightHanded, zNear, zFar ) {
  248. rightHanded = rightHanded === undefined ? true : rightHanded;
  249. zNear = zNear === undefined ? 0.01 : zNear;
  250. zFar = zFar === undefined ? 10000.0 : zFar;
  251. var handednessScale = rightHanded ? - 1.0 : 1.0;
  252. // start with an identity matrix
  253. var mobj = new THREE.Matrix4();
  254. var m = mobj.elements;
  255. // and with scale/offset info for normalized device coords
  256. var scaleAndOffset = fovToNDCScaleOffset( fov );
  257. // X result, map clip edges to [-w,+w]
  258. m[ 0 * 4 + 0 ] = scaleAndOffset.scale[ 0 ];
  259. m[ 0 * 4 + 1 ] = 0.0;
  260. m[ 0 * 4 + 2 ] = scaleAndOffset.offset[ 0 ] * handednessScale;
  261. m[ 0 * 4 + 3 ] = 0.0;
  262. // Y result, map clip edges to [-w,+w]
  263. // Y offset is negated because this proj matrix transforms from world coords with Y=up,
  264. // but the NDC scaling has Y=down (thanks D3D?)
  265. m[ 1 * 4 + 0 ] = 0.0;
  266. m[ 1 * 4 + 1 ] = scaleAndOffset.scale[ 1 ];
  267. m[ 1 * 4 + 2 ] = - scaleAndOffset.offset[ 1 ] * handednessScale;
  268. m[ 1 * 4 + 3 ] = 0.0;
  269. // Z result (up to the app)
  270. m[ 2 * 4 + 0 ] = 0.0;
  271. m[ 2 * 4 + 1 ] = 0.0;
  272. m[ 2 * 4 + 2 ] = zFar / ( zNear - zFar ) * - handednessScale;
  273. m[ 2 * 4 + 3 ] = ( zFar * zNear ) / ( zNear - zFar );
  274. // W result (= Z in)
  275. m[ 3 * 4 + 0 ] = 0.0;
  276. m[ 3 * 4 + 1 ] = 0.0;
  277. m[ 3 * 4 + 2 ] = handednessScale;
  278. m[ 3 * 4 + 3 ] = 0.0;
  279. mobj.transpose();
  280. return mobj;
  281. }
  282. function fovToProjection( fov, rightHanded, zNear, zFar ) {
  283. var DEG2RAD = Math.PI / 180.0;
  284. var fovPort = {
  285. upTan: Math.tan( fov.upDegrees * DEG2RAD ),
  286. downTan: Math.tan( fov.downDegrees * DEG2RAD ),
  287. leftTan: Math.tan( fov.leftDegrees * DEG2RAD ),
  288. rightTan: Math.tan( fov.rightDegrees * DEG2RAD )
  289. };
  290. return fovPortToProjection( fovPort, rightHanded, zNear, zFar );
  291. }
  292. };