Spaces:
Running
Running
File size: 3,374 Bytes
a28eca3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import {
LinearFilter,
Matrix3,
NearestFilter,
RGBAFormat,
ShaderMaterial,
StereoCamera,
WebGLRenderTarget
} from 'three';
import { FullScreenQuad } from '../postprocessing/Pass.js';
class AnaglyphEffect {
constructor( renderer, width = 512, height = 512 ) {
// Dubois matrices from https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.7.6968&rep=rep1&type=pdf#page=4
this.colorMatrixLeft = new Matrix3().fromArray( [
0.456100, - 0.0400822, - 0.0152161,
0.500484, - 0.0378246, - 0.0205971,
0.176381, - 0.0157589, - 0.00546856
] );
this.colorMatrixRight = new Matrix3().fromArray( [
- 0.0434706, 0.378476, - 0.0721527,
- 0.0879388, 0.73364, - 0.112961,
- 0.00155529, - 0.0184503, 1.2264
] );
const _stereo = new StereoCamera();
const _params = { minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat };
const _renderTargetL = new WebGLRenderTarget( width, height, _params );
const _renderTargetR = new WebGLRenderTarget( width, height, _params );
const _material = new ShaderMaterial( {
uniforms: {
'mapLeft': { value: _renderTargetL.texture },
'mapRight': { value: _renderTargetR.texture },
'colorMatrixLeft': { value: this.colorMatrixLeft },
'colorMatrixRight': { value: this.colorMatrixRight }
},
vertexShader: [
'varying vec2 vUv;',
'void main() {',
' vUv = vec2( uv.x, uv.y );',
' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
'}'
].join( '\n' ),
fragmentShader: [
'uniform sampler2D mapLeft;',
'uniform sampler2D mapRight;',
'varying vec2 vUv;',
'uniform mat3 colorMatrixLeft;',
'uniform mat3 colorMatrixRight;',
'void main() {',
' vec2 uv = vUv;',
' vec4 colorL = texture2D( mapLeft, uv );',
' vec4 colorR = texture2D( mapRight, uv );',
' vec3 color = clamp(',
' colorMatrixLeft * colorL.rgb +',
' colorMatrixRight * colorR.rgb, 0., 1. );',
' gl_FragColor = vec4(',
' color.r, color.g, color.b,',
' max( colorL.a, colorR.a ) );',
' #include <tonemapping_fragment>',
' #include <colorspace_fragment>',
'}'
].join( '\n' )
} );
const _quad = new FullScreenQuad( _material );
this.setSize = function ( width, height ) {
renderer.setSize( width, height );
const pixelRatio = renderer.getPixelRatio();
_renderTargetL.setSize( width * pixelRatio, height * pixelRatio );
_renderTargetR.setSize( width * pixelRatio, height * pixelRatio );
};
this.render = function ( scene, camera ) {
const currentRenderTarget = renderer.getRenderTarget();
if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
_stereo.update( camera );
renderer.setRenderTarget( _renderTargetL );
renderer.clear();
renderer.render( scene, _stereo.cameraL );
renderer.setRenderTarget( _renderTargetR );
renderer.clear();
renderer.render( scene, _stereo.cameraR );
renderer.setRenderTarget( null );
_quad.render( renderer );
renderer.setRenderTarget( currentRenderTarget );
};
this.dispose = function () {
_renderTargetL.dispose();
_renderTargetR.dispose();
_material.dispose();
_quad.dispose();
};
}
}
export { AnaglyphEffect };
|