ParallaxBarrierWebGLRenderer.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author marklundin / http://mark-lundin.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. */
  6. if ( THREE.WebGLRenderer ) {
  7. THREE.ParallaxBarrierWebGLRenderer = function ( parameters ) {
  8. THREE.WebGLRenderer.call( this, parameters );
  9. this.autoUpdateScene = false;
  10. var _this = this, _setSize = this.setSize, _render = this.render;
  11. var _cameraL = new THREE.PerspectiveCamera(),
  12. _cameraR = new THREE.PerspectiveCamera();
  13. var eyeRight = new THREE.Matrix4(),
  14. eyeLeft = new THREE.Matrix4(),
  15. focalLength = 125,
  16. _aspect, _near, _far, _fov;
  17. _cameraL.matrixAutoUpdate = _cameraR.matrixAutoUpdate = false;
  18. var _params = { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
  19. var _renderTargetL = new THREE.WebGLRenderTarget( 512, 512, _params ),
  20. _renderTargetR = new THREE.WebGLRenderTarget( 512, 512, _params );
  21. var _camera = new THREE.PerspectiveCamera( 53, 1, 1, 10000 );
  22. _camera.position.z = 2;
  23. var _material = new THREE.ShaderMaterial( {
  24. uniforms: {
  25. "mapLeft": { type: "t", value: 0, texture: _renderTargetL },
  26. "mapRight": { type: "t", value: 1, texture: _renderTargetR }
  27. },
  28. vertexShader: [
  29. "varying vec2 vUv;",
  30. "void main() {",
  31. "vUv = vec2( uv.x, 1.0 - uv.y );",
  32. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  33. "}"
  34. ].join("\n"),
  35. fragmentShader: [
  36. "uniform sampler2D mapLeft;",
  37. "uniform sampler2D mapRight;",
  38. "varying vec2 vUv;",
  39. "void main() {",
  40. "vec2 uv = vUv;",
  41. "if ( ( mod(gl_FragCoord.x, 2.0) ) > 1.00 ) {",
  42. "gl_FragColor = texture2D( mapLeft, uv );",
  43. "} else {",
  44. "gl_FragColor = texture2D( mapRight, uv );",
  45. "}",
  46. "}"
  47. ].join("\n")
  48. } );
  49. var _scene = new THREE.Scene();
  50. var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), _material );
  51. mesh.rotation.x = Math.PI / 2;
  52. _scene.add( mesh );
  53. _scene.add( _camera );
  54. this.setSize = function ( width, height ) {
  55. _setSize.call( _this, width, height );
  56. _renderTargetL.width = width;
  57. _renderTargetL.height = height;
  58. _renderTargetR.width = width;
  59. _renderTargetR.height = height;
  60. };
  61. /*
  62. * Renderer now uses an asymmetric perspective projection (http://paulbourke.net/miscellaneous/stereographics/stereorender/).
  63. * Each camera is offset by the eye seperation and its projection matrix is also skewed asymetrically back to converge on the same
  64. * projection plane. Added a focal length parameter to, this is where the parallax is equal to 0.
  65. */
  66. this.render = function ( scene, camera, renderTarget, forceClear ) {
  67. scene.updateMatrixWorld();
  68. var hasCameraChanged = ( _aspect !== camera.aspect ) || ( _near !== camera.near ) || ( _far !== camera.far ) || ( _fov !== camera.fov );
  69. if( hasCameraChanged ) {
  70. _aspect = camera.aspect;
  71. _near = camera.near;
  72. _far = camera.far;
  73. _fov = camera.fov;
  74. var projectionMatrix = camera.projectionMatrix.clone(),
  75. eyeSep = focalLength / 30 * 0.5,
  76. eyeSepOnProjection = eyeSep * _near / focalLength,
  77. ymax = _near * Math.tan( _fov * Math.PI / 360 ),
  78. xmin, xmax;
  79. // translate xOffset
  80. eyeRight.n14 = eyeSep;
  81. eyeLeft.n14 = -eyeSep;
  82. // for left eye
  83. xmin = -ymax * _aspect + eyeSepOnProjection;
  84. xmax = ymax * _aspect + eyeSepOnProjection;
  85. projectionMatrix.n11 = 2 * _near / ( xmax - xmin );
  86. projectionMatrix.n13 = ( xmax + xmin ) / ( xmax - xmin );
  87. _cameraL.projectionMatrix.copy( projectionMatrix );
  88. // for right eye
  89. xmin = -ymax * _aspect - eyeSepOnProjection;
  90. xmax = ymax * _aspect - eyeSepOnProjection;
  91. projectionMatrix.n11 = 2 * _near / ( xmax - xmin );
  92. projectionMatrix.n13 = ( xmax + xmin ) / ( xmax - xmin );
  93. _cameraR.projectionMatrix.copy( projectionMatrix );
  94. }
  95. _cameraL.matrixWorld.copy( camera.matrixWorld ).multiplySelf( eyeLeft );
  96. _cameraL.position.copy( camera.position );
  97. _cameraL.near = camera.near;
  98. _cameraL.far = camera.far;
  99. _render.call( _this, scene, _cameraL, _renderTargetL, true );
  100. _cameraR.matrixWorld.copy( camera.matrixWorld ).multiplySelf( eyeRight );
  101. _cameraR.position.copy( camera.position );
  102. _cameraR.near = camera.near;
  103. _cameraR.far = camera.far;
  104. _render.call( _this, scene, _cameraR, _renderTargetR, true );
  105. _scene.updateMatrixWorld();
  106. _render.call( _this, _scene, _camera );
  107. };
  108. };
  109. };