AnaglyphEffect.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ( function () {
  2. class AnaglyphEffect {
  3. constructor( renderer, width = 512, height = 512 ) {
  4. // Dubois matrices from https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.7.6968&rep=rep1&type=pdf#page=4
  5. this.colorMatrixLeft = new THREE.Matrix3().fromArray( [ 0.456100, - 0.0400822, - 0.0152161, 0.500484, - 0.0378246, - 0.0205971, 0.176381, - 0.0157589, - 0.00546856 ] );
  6. this.colorMatrixRight = new THREE.Matrix3().fromArray( [ - 0.0434706, 0.378476, - 0.0721527, - 0.0879388, 0.73364, - 0.112961, - 0.00155529, - 0.0184503, 1.2264 ] );
  7. const _camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  8. const _scene = new THREE.Scene();
  9. const _stereo = new THREE.StereoCamera();
  10. const _params = {
  11. minFilter: THREE.LinearFilter,
  12. magFilter: THREE.NearestFilter,
  13. format: THREE.RGBAFormat
  14. };
  15. const _renderTargetL = new THREE.WebGLRenderTarget( width, height, _params );
  16. const _renderTargetR = new THREE.WebGLRenderTarget( width, height, _params );
  17. const _material = new THREE.ShaderMaterial( {
  18. uniforms: {
  19. 'mapLeft': {
  20. value: _renderTargetL.texture
  21. },
  22. 'mapRight': {
  23. value: _renderTargetR.texture
  24. },
  25. 'colorMatrixLeft': {
  26. value: this.colorMatrixLeft
  27. },
  28. 'colorMatrixRight': {
  29. value: this.colorMatrixRight
  30. }
  31. },
  32. vertexShader: [ 'varying vec2 vUv;', 'void main() {', ' vUv = vec2( uv.x, uv.y );', ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', '}' ].join( '\n' ),
  33. fragmentShader: [ 'uniform sampler2D mapLeft;', 'uniform sampler2D mapRight;', 'varying vec2 vUv;', 'uniform mat3 colorMatrixLeft;', 'uniform mat3 colorMatrixRight;', // These functions implement sRGB linearization and gamma correction
  34. 'float lin( float c ) {', ' return c <= 0.04045 ? c * 0.0773993808 :', ' pow( c * 0.9478672986 + 0.0521327014, 2.4 );', '}', 'vec4 lin( vec4 c ) {', ' return vec4( lin( c.r ), lin( c.g ), lin( c.b ), c.a );', '}', 'float dev( float c ) {', ' return c <= 0.0031308 ? c * 12.92', ' : pow( c, 0.41666 ) * 1.055 - 0.055;', '}', 'void main() {', ' vec2 uv = vUv;', ' vec4 colorL = lin( texture2D( mapLeft, uv ) );', ' vec4 colorR = lin( texture2D( mapRight, uv ) );', ' vec3 color = clamp(', ' colorMatrixLeft * colorL.rgb +', ' colorMatrixRight * colorR.rgb, 0., 1. );', ' gl_FragColor = vec4(', ' dev( color.r ), dev( color.g ), dev( color.b ),', ' max( colorL.a, colorR.a ) );', '}' ].join( '\n' )
  35. } );
  36. const _mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), _material );
  37. _scene.add( _mesh );
  38. this.setSize = function ( width, height ) {
  39. renderer.setSize( width, height );
  40. const pixelRatio = renderer.getPixelRatio();
  41. _renderTargetL.setSize( width * pixelRatio, height * pixelRatio );
  42. _renderTargetR.setSize( width * pixelRatio, height * pixelRatio );
  43. };
  44. this.render = function ( scene, camera ) {
  45. const currentRenderTarget = renderer.getRenderTarget();
  46. scene.updateMatrixWorld();
  47. if ( camera.parent === null ) camera.updateMatrixWorld();
  48. _stereo.update( camera );
  49. renderer.setRenderTarget( _renderTargetL );
  50. renderer.clear();
  51. renderer.render( scene, _stereo.cameraL );
  52. renderer.setRenderTarget( _renderTargetR );
  53. renderer.clear();
  54. renderer.render( scene, _stereo.cameraR );
  55. renderer.setRenderTarget( null );
  56. renderer.render( _scene, _camera );
  57. renderer.setRenderTarget( currentRenderTarget );
  58. };
  59. this.dispose = function () {
  60. if ( _renderTargetL ) _renderTargetL.dispose();
  61. if ( _renderTargetR ) _renderTargetR.dispose();
  62. if ( _mesh ) _mesh.geometry.dispose();
  63. if ( _material ) _material.dispose();
  64. };
  65. }
  66. }
  67. THREE.AnaglyphEffect = AnaglyphEffect;
  68. } )();