AnaglyphEffect.js 4.3 KB

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