AnaglyphEffect.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author marklundin / http://mark-lundin.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author tschw
  6. */
  7. THREE.AnaglyphEffect = function ( renderer, width, height ) {
  8. // Matrices generated with angler.js https://github.com/tschw/angler.js/
  9. // (in column-major element order, as accepted by WebGL)
  10. this.colorMatrixLeft = new THREE.Matrix3().fromArray( [
  11. 1.0671679973602295, -0.0016435992438346148, 0.0001777536963345483, // r out
  12. -0.028107794001698494, -0.00019593400065787137, -0.0002875397040043026, // g out
  13. -0.04279090091586113, 0.000015809757314855233, -0.00024287120322696865 // b out
  14. ] );
  15. // red green blue in
  16. this.colorMatrixRight = new THREE.Matrix3().fromArray( [
  17. -0.0355340838432312, -0.06440307199954987, 0.018319187685847282, // r out
  18. -0.10269022732973099, 0.8079727292060852, -0.04835830628871918, // g out
  19. 0.0001224992738571018, -0.009558862075209618, 0.567823588848114 // b out
  20. ] );
  21. var _camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  22. var _scene = new THREE.Scene();
  23. var _stereo = new THREE.StereoCamera();
  24. var _params = { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
  25. if ( width === undefined ) width = 512;
  26. if ( height === undefined ) height = 512;
  27. var _renderTargetL = new THREE.WebGLRenderTarget( width, height, _params );
  28. var _renderTargetR = new THREE.WebGLRenderTarget( width, height, _params );
  29. var _material = new THREE.ShaderMaterial( {
  30. uniforms: {
  31. "mapLeft": { value: _renderTargetL.texture },
  32. "mapRight": { value: _renderTargetR.texture },
  33. "colorMatrixLeft": { value: this.colorMatrixLeft },
  34. "colorMatrixRight": { value: this.colorMatrixRight }
  35. },
  36. vertexShader: [
  37. "varying vec2 vUv;",
  38. "void main() {",
  39. " vUv = vec2( uv.x, uv.y );",
  40. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  41. "}"
  42. ].join( "\n" ),
  43. fragmentShader: [
  44. "uniform sampler2D mapLeft;",
  45. "uniform sampler2D mapRight;",
  46. "varying vec2 vUv;",
  47. "uniform mat3 colorMatrixLeft;",
  48. "uniform mat3 colorMatrixRight;",
  49. // These functions implement sRGB linearization and gamma correction
  50. "float lin( float c ) {",
  51. " return c <= 0.04045 ? c * 0.0773993808 :",
  52. " pow( c * 0.9478672986 + 0.0521327014, 2.4 );",
  53. "}",
  54. "vec4 lin( vec4 c ) {",
  55. " return vec4( lin( c.r ), lin( c.g ), lin( c.b ), c.a );",
  56. "}",
  57. "float dev( float c ) {",
  58. " return c <= 0.0031308 ? c * 12.92",
  59. " : pow( c, 0.41666 ) * 1.055 - 0.055;",
  60. "}",
  61. "void main() {",
  62. " vec2 uv = vUv;",
  63. " vec4 colorL = lin( texture2D( mapLeft, uv ) );",
  64. " vec4 colorR = lin( texture2D( mapRight, uv ) );",
  65. " vec3 color = clamp(",
  66. " colorMatrixLeft * colorL.rgb +",
  67. " colorMatrixRight * colorR.rgb, 0., 1. );",
  68. " gl_FragColor = vec4(",
  69. " dev( color.r ), dev( color.g ), dev( color.b ),",
  70. " max( colorL.a, colorR.a ) );",
  71. "}"
  72. ].join( "\n" )
  73. } );
  74. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), _material );
  75. _scene.add( mesh );
  76. this.setSize = function ( width, height ) {
  77. renderer.setSize( width, height );
  78. var pixelRatio = renderer.getPixelRatio();
  79. _renderTargetL.setSize( width * pixelRatio, height * pixelRatio );
  80. _renderTargetR.setSize( width * pixelRatio, height * pixelRatio );
  81. };
  82. this.render = function ( scene, camera ) {
  83. scene.updateMatrixWorld();
  84. if ( camera.parent === null ) camera.updateMatrixWorld();
  85. _stereo.update( camera );
  86. renderer.render( scene, _stereo.cameraL, _renderTargetL, true );
  87. renderer.render( scene, _stereo.cameraR, _renderTargetR, true );
  88. renderer.render( _scene, _camera );
  89. };
  90. this.dispose = function() {
  91. if ( _renderTargetL ) _renderTargetL.dispose();
  92. if ( _renderTargetR ) _renderTargetR.dispose();
  93. };
  94. };