AnaglyphWebGLRenderer.js 4.3 KB

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