AnaglyphEffect.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. class AnaglyphEffect {
  12. constructor( renderer, width = 512, height = 512 ) {
  13. // Dubois matrices from https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.7.6968&rep=rep1&type=pdf#page=4
  14. this.colorMatrixLeft = new Matrix3().fromArray( [
  15. 0.456100, - 0.0400822, - 0.0152161,
  16. 0.500484, - 0.0378246, - 0.0205971,
  17. 0.176381, - 0.0157589, - 0.00546856
  18. ] );
  19. this.colorMatrixRight = new Matrix3().fromArray( [
  20. - 0.0434706, 0.378476, - 0.0721527,
  21. - 0.0879388, 0.73364, - 0.112961,
  22. - 0.00155529, - 0.0184503, 1.2264
  23. ] );
  24. const _stereo = new StereoCamera();
  25. const _params = { minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat };
  26. const _renderTargetL = new WebGLRenderTarget( width, height, _params );
  27. const _renderTargetR = new WebGLRenderTarget( width, height, _params );
  28. const _material = new ShaderMaterial( {
  29. uniforms: {
  30. 'mapLeft': { value: _renderTargetL.texture },
  31. 'mapRight': { value: _renderTargetR.texture },
  32. 'colorMatrixLeft': { value: this.colorMatrixLeft },
  33. 'colorMatrixRight': { value: this.colorMatrixRight }
  34. },
  35. vertexShader: [
  36. 'varying vec2 vUv;',
  37. 'void main() {',
  38. ' vUv = vec2( uv.x, uv.y );',
  39. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  40. '}'
  41. ].join( '\n' ),
  42. fragmentShader: [
  43. 'uniform sampler2D mapLeft;',
  44. 'uniform sampler2D mapRight;',
  45. 'varying vec2 vUv;',
  46. 'uniform mat3 colorMatrixLeft;',
  47. 'uniform mat3 colorMatrixRight;',
  48. 'void main() {',
  49. ' vec2 uv = vUv;',
  50. ' vec4 colorL = texture2D( mapLeft, uv );',
  51. ' vec4 colorR = texture2D( mapRight, uv );',
  52. ' vec3 color = clamp(',
  53. ' colorMatrixLeft * colorL.rgb +',
  54. ' colorMatrixRight * colorR.rgb, 0., 1. );',
  55. ' gl_FragColor = vec4(',
  56. ' color.r, color.g, color.b,',
  57. ' max( colorL.a, colorR.a ) );',
  58. ' #include <tonemapping_fragment>',
  59. ' #include <colorspace_fragment>',
  60. '}'
  61. ].join( '\n' )
  62. } );
  63. const _quad = new FullScreenQuad( _material );
  64. this.setSize = function ( width, height ) {
  65. renderer.setSize( width, height );
  66. const pixelRatio = renderer.getPixelRatio();
  67. _renderTargetL.setSize( width * pixelRatio, height * pixelRatio );
  68. _renderTargetR.setSize( width * pixelRatio, height * pixelRatio );
  69. };
  70. this.render = function ( scene, camera ) {
  71. const currentRenderTarget = renderer.getRenderTarget();
  72. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  73. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  74. _stereo.update( camera );
  75. renderer.setRenderTarget( _renderTargetL );
  76. renderer.clear();
  77. renderer.render( scene, _stereo.cameraL );
  78. renderer.setRenderTarget( _renderTargetR );
  79. renderer.clear();
  80. renderer.render( scene, _stereo.cameraR );
  81. renderer.setRenderTarget( null );
  82. _quad.render( renderer );
  83. renderer.setRenderTarget( currentRenderTarget );
  84. };
  85. this.dispose = function () {
  86. _renderTargetL.dispose();
  87. _renderTargetR.dispose();
  88. _material.dispose();
  89. _quad.dispose();
  90. };
  91. }
  92. }
  93. export { AnaglyphEffect };
粤ICP备19079148号