AnaglyphEffect.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {
  2. LinearFilter,
  3. Matrix3,
  4. NearestFilter,
  5. RGBAFormat,
  6. ShaderMaterial,
  7. StereoCamera,
  8. WebGLRenderTarget
  9. } from 'three';
  10. import { FullScreenQuad } from '../postprocessing/Pass.js';
  11. /**
  12. * A class that creates an anaglyph effect.
  13. *
  14. * Note that this class can only be used with {@link WebGLRenderer}.
  15. * When using {@link WebGPURenderer}, use {@link AnaglyphPassNode}.
  16. *
  17. * @three_import import { AnaglyphEffect } from 'three/addons/effects/AnaglyphEffect.js';
  18. */
  19. class AnaglyphEffect {
  20. /**
  21. * Constructs a new anaglyph effect.
  22. *
  23. * @param {WebGLRenderer} renderer - The renderer.
  24. * @param {number} width - The width of the effect in physical pixels.
  25. * @param {number} height - The height of the effect in physical pixels.
  26. */
  27. constructor( renderer, width = 512, height = 512 ) {
  28. // Dubois matrices from https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.7.6968&rep=rep1&type=pdf#page=4
  29. this.colorMatrixLeft = new Matrix3().fromArray( [
  30. 0.456100, - 0.0400822, - 0.0152161,
  31. 0.500484, - 0.0378246, - 0.0205971,
  32. 0.176381, - 0.0157589, - 0.00546856
  33. ] );
  34. this.colorMatrixRight = new Matrix3().fromArray( [
  35. - 0.0434706, 0.378476, - 0.0721527,
  36. - 0.0879388, 0.73364, - 0.112961,
  37. - 0.00155529, - 0.0184503, 1.2264
  38. ] );
  39. const _stereo = new StereoCamera();
  40. const _params = { minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat };
  41. const _renderTargetL = new WebGLRenderTarget( width, height, _params );
  42. const _renderTargetR = new WebGLRenderTarget( width, height, _params );
  43. const _material = new ShaderMaterial( {
  44. uniforms: {
  45. 'mapLeft': { value: _renderTargetL.texture },
  46. 'mapRight': { value: _renderTargetR.texture },
  47. 'colorMatrixLeft': { value: this.colorMatrixLeft },
  48. 'colorMatrixRight': { value: this.colorMatrixRight }
  49. },
  50. vertexShader: [
  51. 'varying vec2 vUv;',
  52. 'void main() {',
  53. ' vUv = vec2( uv.x, uv.y );',
  54. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  55. '}'
  56. ].join( '\n' ),
  57. fragmentShader: [
  58. 'uniform sampler2D mapLeft;',
  59. 'uniform sampler2D mapRight;',
  60. 'varying vec2 vUv;',
  61. 'uniform mat3 colorMatrixLeft;',
  62. 'uniform mat3 colorMatrixRight;',
  63. 'void main() {',
  64. ' vec2 uv = vUv;',
  65. ' vec4 colorL = texture2D( mapLeft, uv );',
  66. ' vec4 colorR = texture2D( mapRight, uv );',
  67. ' vec3 color = clamp(',
  68. ' colorMatrixLeft * colorL.rgb +',
  69. ' colorMatrixRight * colorR.rgb, 0., 1. );',
  70. ' gl_FragColor = vec4(',
  71. ' color.r, color.g, color.b,',
  72. ' max( colorL.a, colorR.a ) );',
  73. ' #include <tonemapping_fragment>',
  74. ' #include <colorspace_fragment>',
  75. '}'
  76. ].join( '\n' )
  77. } );
  78. const _quad = new FullScreenQuad( _material );
  79. /**
  80. * Resizes the effect.
  81. *
  82. * @param {number} width - The width of the effect in logical pixels.
  83. * @param {number} height - The height of the effect in logical pixels.
  84. */
  85. this.setSize = function ( width, height ) {
  86. renderer.setSize( width, height );
  87. const pixelRatio = renderer.getPixelRatio();
  88. _renderTargetL.setSize( width * pixelRatio, height * pixelRatio );
  89. _renderTargetR.setSize( width * pixelRatio, height * pixelRatio );
  90. };
  91. /**
  92. * When using this effect, this method should be called instead of the
  93. * default {@link WebGLRenderer#render}.
  94. *
  95. * @param {Object3D} scene - The scene to render.
  96. * @param {Camera} camera - The camera.
  97. */
  98. this.render = function ( scene, camera ) {
  99. const currentRenderTarget = renderer.getRenderTarget();
  100. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  101. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  102. _stereo.update( camera );
  103. renderer.setRenderTarget( _renderTargetL );
  104. renderer.clear();
  105. renderer.render( scene, _stereo.cameraL );
  106. renderer.setRenderTarget( _renderTargetR );
  107. renderer.clear();
  108. renderer.render( scene, _stereo.cameraR );
  109. renderer.setRenderTarget( null );
  110. _quad.render( renderer );
  111. renderer.setRenderTarget( currentRenderTarget );
  112. };
  113. /**
  114. * Frees internal resources. This method should be called
  115. * when the effect is no longer required.
  116. */
  117. this.dispose = function () {
  118. _renderTargetL.dispose();
  119. _renderTargetR.dispose();
  120. _material.dispose();
  121. _quad.dispose();
  122. };
  123. }
  124. }
  125. export { AnaglyphEffect };