AnaglyphEffect.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. class AnaglyphEffect {
  15. constructor( renderer, width = 512, height = 512 ) {
  16. // Dubois matrices from https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.7.6968&rep=rep1&type=pdf#page=4
  17. this.colorMatrixLeft = new Matrix3().fromArray( [
  18. 0.456100, - 0.0400822, - 0.0152161,
  19. 0.500484, - 0.0378246, - 0.0205971,
  20. 0.176381, - 0.0157589, - 0.00546856
  21. ] );
  22. this.colorMatrixRight = new Matrix3().fromArray( [
  23. - 0.0434706, 0.378476, - 0.0721527,
  24. - 0.0879388, 0.73364, - 0.112961,
  25. - 0.00155529, - 0.0184503, 1.2264
  26. ] );
  27. const _camera = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  28. const _scene = new Scene();
  29. const _stereo = new StereoCamera();
  30. const _params = { minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat };
  31. const _renderTargetL = new WebGLRenderTarget( width, height, _params );
  32. const _renderTargetR = new WebGLRenderTarget( width, height, _params );
  33. const _material = new ShaderMaterial( {
  34. uniforms: {
  35. 'mapLeft': { value: _renderTargetL.texture },
  36. 'mapRight': { value: _renderTargetR.texture },
  37. 'colorMatrixLeft': { value: this.colorMatrixLeft },
  38. 'colorMatrixRight': { value: this.colorMatrixRight }
  39. },
  40. vertexShader: [
  41. 'varying vec2 vUv;',
  42. 'void main() {',
  43. ' vUv = vec2( uv.x, uv.y );',
  44. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  45. '}'
  46. ].join( '\n' ),
  47. fragmentShader: [
  48. 'uniform sampler2D mapLeft;',
  49. 'uniform sampler2D mapRight;',
  50. 'varying vec2 vUv;',
  51. 'uniform mat3 colorMatrixLeft;',
  52. 'uniform mat3 colorMatrixRight;',
  53. // These functions implement sRGB linearization and gamma correction
  54. 'float lin( float c ) {',
  55. ' return c <= 0.04045 ? c * 0.0773993808 :',
  56. ' pow( c * 0.9478672986 + 0.0521327014, 2.4 );',
  57. '}',
  58. 'vec4 lin( vec4 c ) {',
  59. ' return vec4( lin( c.r ), lin( c.g ), lin( c.b ), c.a );',
  60. '}',
  61. 'float dev( float c ) {',
  62. ' return c <= 0.0031308 ? c * 12.92',
  63. ' : pow( c, 0.41666 ) * 1.055 - 0.055;',
  64. '}',
  65. 'void main() {',
  66. ' vec2 uv = vUv;',
  67. ' vec4 colorL = lin( texture2D( mapLeft, uv ) );',
  68. ' vec4 colorR = lin( texture2D( mapRight, uv ) );',
  69. ' vec3 color = clamp(',
  70. ' colorMatrixLeft * colorL.rgb +',
  71. ' colorMatrixRight * colorR.rgb, 0., 1. );',
  72. ' gl_FragColor = vec4(',
  73. ' dev( color.r ), dev( color.g ), dev( color.b ),',
  74. ' max( colorL.a, colorR.a ) );',
  75. '}'
  76. ].join( '\n' )
  77. } );
  78. const _mesh = new Mesh( new PlaneGeometry( 2, 2 ), _material );
  79. _scene.add( _mesh );
  80. this.setSize = function ( width, height ) {
  81. renderer.setSize( width, height );
  82. const pixelRatio = renderer.getPixelRatio();
  83. _renderTargetL.setSize( width * pixelRatio, height * pixelRatio );
  84. _renderTargetR.setSize( width * pixelRatio, height * pixelRatio );
  85. };
  86. this.render = function ( scene, camera ) {
  87. const currentRenderTarget = renderer.getRenderTarget();
  88. scene.updateMatrixWorld();
  89. if ( camera.parent === null ) camera.updateMatrixWorld();
  90. _stereo.update( camera );
  91. renderer.setRenderTarget( _renderTargetL );
  92. renderer.clear();
  93. renderer.render( scene, _stereo.cameraL );
  94. renderer.setRenderTarget( _renderTargetR );
  95. renderer.clear();
  96. renderer.render( scene, _stereo.cameraR );
  97. renderer.setRenderTarget( null );
  98. renderer.render( _scene, _camera );
  99. renderer.setRenderTarget( currentRenderTarget );
  100. };
  101. this.dispose = function () {
  102. if ( _renderTargetL ) _renderTargetL.dispose();
  103. if ( _renderTargetR ) _renderTargetR.dispose();
  104. if ( _mesh ) _mesh.geometry.dispose();
  105. if ( _material ) _material.dispose();
  106. };
  107. }
  108. }
  109. export { AnaglyphEffect };