AnaglyphEffect.js 4.4 KB

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