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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/**
* @author mrdoob / http://mrdoob.com/
* @author marklundin / http://mark-lundin.com/
* @author alteredq / http://alteredqualia.com/
* @author tschw
*/
import {
LinearFilter,
Matrix3,
Mesh,
NearestFilter,
OrthographicCamera,
PlaneBufferGeometry,
RGBAFormat,
Scene,
ShaderMaterial,
StereoCamera,
WebGLRenderTarget
} from "../../../build/three.module.js";
var AnaglyphEffect = function ( renderer, width, height ) {
// Matrices generated with angler.js https://github.com/tschw/angler.js/
// (in column-major element order, as accepted by WebGL)
this.colorMatrixLeft = new Matrix3().fromArray( [
1.0671679973602295, - 0.0016435992438346148, 0.0001777536963345483, // r out
- 0.028107794001698494, - 0.00019593400065787137, - 0.0002875397040043026, // g out
- 0.04279090091586113, 0.000015809757314855233, - 0.00024287120322696865 // b out
] );
// red green blue in
this.colorMatrixRight = new Matrix3().fromArray( [
- 0.0355340838432312, - 0.06440307199954987, 0.018319187685847282, // r out
- 0.10269022732973099, 0.8079727292060852, - 0.04835830628871918, // g out
0.0001224992738571018, - 0.009558862075209618, 0.567823588848114 // b out
] );
var _camera = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
var _scene = new Scene();
var _stereo = new StereoCamera();
var _params = { minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat };
if ( width === undefined ) width = 512;
if ( height === undefined ) height = 512;
var _renderTargetL = new WebGLRenderTarget( width, height, _params );
var _renderTargetR = new WebGLRenderTarget( width, height, _params );
var _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;",
// These functions implement sRGB linearization and gamma correction
"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" )
} );
var _mesh = new Mesh( new PlaneBufferGeometry( 2, 2 ), _material );
_scene.add( _mesh );
this.setSize = function ( width, height ) {
renderer.setSize( width, height );
var pixelRatio = renderer.getPixelRatio();
_renderTargetL.setSize( width * pixelRatio, height * pixelRatio );
_renderTargetR.setSize( width * pixelRatio, height * pixelRatio );
};
this.render = function ( scene, camera ) {
var currentRenderTarget = renderer.getRenderTarget();
scene.updateMatrixWorld();
if ( camera.parent === null ) 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 );
renderer.render( _scene, _camera );
renderer.setRenderTarget( currentRenderTarget );
};
this.dispose = function () {
if ( _renderTargetL ) _renderTargetL.dispose();
if ( _renderTargetR ) _renderTargetR.dispose();
if ( _mesh ) _mesh.geometry.dispose();
if ( _material ) _material.dispose();
};
};
export { AnaglyphEffect };
|