AnaglyphEffect.js 4.1 KB

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