AnaglyphEffect.js 4.3 KB

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