2
0

AnaglyphEffect.js 3.8 KB

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