AnaglyphEffect.js 4.5 KB

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