Mirror.js 9.2 KB
Newer Older
M
r61  
Mr.doob 已提交
1 2 3 4
/**
 * @author Slayvin / http://slayvin.net
 */

M
r72  
Mr.doob 已提交
5
THREE.ShaderLib[ 'mirror' ] = {
M
r61  
Mr.doob 已提交
6

M
r78  
Mr.doob 已提交
7 8 9 10
	uniforms: {
		"mirrorColor": { value: new THREE.Color( 0x7F7F7F ) },
		"mirrorSampler": { value: null },
		"textureMatrix" : { value: new THREE.Matrix4() }
M
r61  
Mr.doob 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	},

	vertexShader: [

		"uniform mat4 textureMatrix;",

		"varying vec4 mirrorCoord;",

		"void main() {",

			"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
			"vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
			"mirrorCoord = textureMatrix * worldPosition;",

			"gl_Position = projectionMatrix * mvPosition;",

		"}"

M
r72  
Mr.doob 已提交
29
	].join( "\n" ),
M
r61  
Mr.doob 已提交
30 31 32 33 34 35 36 37 38 39 40

	fragmentShader: [

		"uniform vec3 mirrorColor;",
		"uniform sampler2D mirrorSampler;",

		"varying vec4 mirrorCoord;",

		"float blendOverlay(float base, float blend) {",
			"return( base < 0.5 ? ( 2.0 * base * blend ) : (1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );",
		"}",
M
r72  
Mr.doob 已提交
41

M
r61  
Mr.doob 已提交
42 43 44 45 46 47 48 49 50
		"void main() {",

			"vec4 color = texture2DProj(mirrorSampler, mirrorCoord);",
			"color = vec4(blendOverlay(mirrorColor.r, color.r), blendOverlay(mirrorColor.g, color.g), blendOverlay(mirrorColor.b, color.b), 1.0);",

			"gl_FragColor = color;",

		"}"

M
r72  
Mr.doob 已提交
51
	].join( "\n" )
M
r61  
Mr.doob 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

};

THREE.Mirror = function ( renderer, camera, options ) {

	THREE.Object3D.call( this );

	this.name = 'mirror_' + this.id;

	options = options || {};

	this.matrixNeedsUpdate = true;

	var width = options.textureWidth !== undefined ? options.textureWidth : 512;
	var height = options.textureHeight !== undefined ? options.textureHeight : 512;

	this.clipBias = options.clipBias !== undefined ? options.clipBias : 0.0;

M
r72  
Mr.doob 已提交
70
	var mirrorColor = options.color !== undefined ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
M
r61  
Mr.doob 已提交
71 72 73 74 75 76 77

	this.renderer = renderer;
	this.mirrorPlane = new THREE.Plane();
	this.normal = new THREE.Vector3( 0, 0, 1 );
	this.mirrorWorldPosition = new THREE.Vector3();
	this.cameraWorldPosition = new THREE.Vector3();
	this.rotationMatrix = new THREE.Matrix4();
M
r72  
Mr.doob 已提交
78
	this.lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
M
r61  
Mr.doob 已提交
79
	this.clipPlane = new THREE.Vector4();
M
r72  
Mr.doob 已提交
80

M
r61  
Mr.doob 已提交
81 82 83 84 85
	// For debug only, show the normal and plane of the mirror
	var debugMode = options.debugMode !== undefined ? options.debugMode : false;

	if ( debugMode ) {

M
r72  
Mr.doob 已提交
86
		var arrow = new THREE.ArrowHelper( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 0, 0, 0 ), 10, 0xffff80 );
M
r61  
Mr.doob 已提交
87
		var planeGeometry = new THREE.Geometry();
M
r72  
Mr.doob 已提交
88 89
		planeGeometry.vertices.push( new THREE.Vector3( - 10, - 10, 0 ) );
		planeGeometry.vertices.push( new THREE.Vector3( 10, - 10, 0 ) );
M
r61  
Mr.doob 已提交
90
		planeGeometry.vertices.push( new THREE.Vector3( 10, 10, 0 ) );
M
r72  
Mr.doob 已提交
91 92
		planeGeometry.vertices.push( new THREE.Vector3( - 10, 10, 0 ) );
		planeGeometry.vertices.push( planeGeometry.vertices[ 0 ] );
M
r61  
Mr.doob 已提交
93 94
		var plane = new THREE.Line( planeGeometry, new THREE.LineBasicMaterial( { color: 0xffff80 } ) );

M
r72  
Mr.doob 已提交
95 96
		this.add( arrow );
		this.add( plane );
M
r61  
Mr.doob 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

	}

	if ( camera instanceof THREE.PerspectiveCamera ) {

		this.camera = camera;

	} else {

		this.camera = new THREE.PerspectiveCamera();
		console.log( this.name + ': camera is not a Perspective Camera!' );

	}

	this.textureMatrix = new THREE.Matrix4();

	this.mirrorCamera = this.camera.clone();
M
Mr.doob 已提交
114
	this.mirrorCamera.matrixAutoUpdate = true;
M
r61  
Mr.doob 已提交
115

M
r72  
Mr.doob 已提交
116 117
	var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };

M
r77  
Mr.doob 已提交
118 119
	this.renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );
	this.renderTarget2 = new THREE.WebGLRenderTarget( width, height, parameters );
M
r61  
Mr.doob 已提交
120 121 122 123 124 125 126 127 128 129 130 131

	var mirrorShader = THREE.ShaderLib[ "mirror" ];
	var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );

	this.material = new THREE.ShaderMaterial( {

		fragmentShader: mirrorShader.fragmentShader,
		vertexShader: mirrorShader.vertexShader,
		uniforms: mirrorUniforms

	} );

M
r77  
Mr.doob 已提交
132
	this.material.uniforms.mirrorSampler.value = this.renderTarget.texture;
M
r61  
Mr.doob 已提交
133 134 135
	this.material.uniforms.mirrorColor.value = mirrorColor;
	this.material.uniforms.textureMatrix.value = this.textureMatrix;

M
r72  
Mr.doob 已提交
136
	if ( ! THREE.Math.isPowerOfTwo( width ) || ! THREE.Math.isPowerOfTwo( height ) ) {
M
r61  
Mr.doob 已提交
137

M
r77  
Mr.doob 已提交
138 139
		this.renderTarget.texture.generateMipmaps = false;
		this.renderTarget2.texture.generateMipmaps = false;
M
r61  
Mr.doob 已提交
140 141 142 143 144 145 146 147 148

	}

	this.updateTextureMatrix();
	this.render();

};

THREE.Mirror.prototype = Object.create( THREE.Object3D.prototype );
M
Mr.doob 已提交
149
THREE.Mirror.prototype.constructor = THREE.Mirror;
M
r61  
Mr.doob 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162

THREE.Mirror.prototype.renderWithMirror = function ( otherMirror ) {

	// update the mirror matrix to mirror the current view
	this.updateTextureMatrix();
	this.matrixNeedsUpdate = false;

	// set the camera of the other mirror so the mirrored view is the reference view
	var tempCamera = otherMirror.camera;
	otherMirror.camera = this.mirrorCamera;

	// render the other mirror in temp texture
	otherMirror.renderTemp();
M
r77  
Mr.doob 已提交
163
	otherMirror.material.uniforms.mirrorSampler.value = otherMirror.renderTarget2.texture;
M
r61  
Mr.doob 已提交
164 165 166 167 168 169

	// render the current mirror
	this.render();
	this.matrixNeedsUpdate = true;

	// restore material and camera of other mirror
M
r77  
Mr.doob 已提交
170
	otherMirror.material.uniforms.mirrorSampler.value = otherMirror.renderTarget.texture;
M
r61  
Mr.doob 已提交
171 172 173 174
	otherMirror.camera = tempCamera;

	// restore texture matrix of other mirror
	otherMirror.updateTextureMatrix();
M
r72  
Mr.doob 已提交
175

M
r61  
Mr.doob 已提交
176 177 178 179 180 181 182
};

THREE.Mirror.prototype.updateTextureMatrix = function () {

	this.updateMatrixWorld();
	this.camera.updateMatrixWorld();

M
r64  
Mr.doob 已提交
183 184
	this.mirrorWorldPosition.setFromMatrixPosition( this.matrixWorld );
	this.cameraWorldPosition.setFromMatrixPosition( this.camera.matrixWorld );
M
r61  
Mr.doob 已提交
185 186 187 188 189 190 191

	this.rotationMatrix.extractRotation( this.matrixWorld );

	this.normal.set( 0, 0, 1 );
	this.normal.applyMatrix4( this.rotationMatrix );

	var view = this.mirrorWorldPosition.clone().sub( this.cameraWorldPosition );
M
r66  
Mr.doob 已提交
192 193
	view.reflect( this.normal ).negate();
	view.add( this.mirrorWorldPosition );
M
r61  
Mr.doob 已提交
194 195 196

	this.rotationMatrix.extractRotation( this.camera.matrixWorld );

M
r72  
Mr.doob 已提交
197
	this.lookAtPosition.set( 0, 0, - 1 );
M
r61  
Mr.doob 已提交
198 199 200 201
	this.lookAtPosition.applyMatrix4( this.rotationMatrix );
	this.lookAtPosition.add( this.cameraWorldPosition );

	var target = this.mirrorWorldPosition.clone().sub( this.lookAtPosition );
M
r66  
Mr.doob 已提交
202 203
	target.reflect( this.normal ).negate();
	target.add( this.mirrorWorldPosition );
M
r61  
Mr.doob 已提交
204

M
r72  
Mr.doob 已提交
205
	this.up.set( 0, - 1, 0 );
M
r61  
Mr.doob 已提交
206
	this.up.applyMatrix4( this.rotationMatrix );
M
r66  
Mr.doob 已提交
207
	this.up.reflect( this.normal ).negate();
M
r61  
Mr.doob 已提交
208

M
r66  
Mr.doob 已提交
209 210 211
	this.mirrorCamera.position.copy( view );
	this.mirrorCamera.up = this.up;
	this.mirrorCamera.lookAt( target );
M
r61  
Mr.doob 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

	this.mirrorCamera.updateProjectionMatrix();
	this.mirrorCamera.updateMatrixWorld();
	this.mirrorCamera.matrixWorldInverse.getInverse( this.mirrorCamera.matrixWorld );

	// Update the texture matrix
	this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
							0.0, 0.5, 0.0, 0.5,
							0.0, 0.0, 0.5, 0.5,
							0.0, 0.0, 0.0, 1.0 );
	this.textureMatrix.multiply( this.mirrorCamera.projectionMatrix );
	this.textureMatrix.multiply( this.mirrorCamera.matrixWorldInverse );

	// Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
	// Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
	this.mirrorPlane.setFromNormalAndCoplanarPoint( this.normal, this.mirrorWorldPosition );
	this.mirrorPlane.applyMatrix4( this.mirrorCamera.matrixWorldInverse );

	this.clipPlane.set( this.mirrorPlane.normal.x, this.mirrorPlane.normal.y, this.mirrorPlane.normal.z, this.mirrorPlane.constant );

	var q = new THREE.Vector4();
	var projectionMatrix = this.mirrorCamera.projectionMatrix;

M
r72  
Mr.doob 已提交
235 236
	q.x = ( Math.sign( this.clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
	q.y = ( Math.sign( this.clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
M
r61  
Mr.doob 已提交
237
	q.z = - 1.0;
M
r72  
Mr.doob 已提交
238
	q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
M
r61  
Mr.doob 已提交
239 240 241

	// Calculate the scaled plane vector
	var c = new THREE.Vector4();
M
r72  
Mr.doob 已提交
242
	c = this.clipPlane.multiplyScalar( 2.0 / this.clipPlane.dot( q ) );
M
r61  
Mr.doob 已提交
243 244

	// Replacing the third row of the projection matrix
M
r72  
Mr.doob 已提交
245 246 247 248
	projectionMatrix.elements[ 2 ] = c.x;
	projectionMatrix.elements[ 6 ] = c.y;
	projectionMatrix.elements[ 10 ] = c.z + 1.0 - this.clipBias;
	projectionMatrix.elements[ 14 ] = c.w;
M
r61  
Mr.doob 已提交
249 250 251 252 253 254 255 256 257 258 259 260

};

THREE.Mirror.prototype.render = function () {

	if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();

	this.matrixNeedsUpdate = true;

	// Render the mirrored view of the current scene into the target texture
	var scene = this;

M
r72  
Mr.doob 已提交
261
	while ( scene.parent !== null ) {
M
r61  
Mr.doob 已提交
262 263 264 265 266

		scene = scene.parent;

	}

M
r72  
Mr.doob 已提交
267 268 269 270 271
	if ( scene !== undefined && scene instanceof THREE.Scene ) {

		// We can't render ourself to ourself
		var visible = this.material.visible;
		this.material.visible = false;
M
r61  
Mr.doob 已提交
272

M
r77  
Mr.doob 已提交
273
		this.renderer.render( scene, this.mirrorCamera, this.renderTarget, true );
M
r61  
Mr.doob 已提交
274

M
r72  
Mr.doob 已提交
275 276
		this.material.visible = visible;

M
r61  
Mr.doob 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289
	}

};

THREE.Mirror.prototype.renderTemp = function () {

	if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();

	this.matrixNeedsUpdate = true;

	// Render the mirrored view of the current scene into the target texture
	var scene = this;

M
r72  
Mr.doob 已提交
290
	while ( scene.parent !== null ) {
M
r61  
Mr.doob 已提交
291 292 293 294 295

		scene = scene.parent;

	}

M
r72  
Mr.doob 已提交
296
	if ( scene !== undefined && scene instanceof THREE.Scene ) {
M
r61  
Mr.doob 已提交
297

M
r77  
Mr.doob 已提交
298
		this.renderer.render( scene, this.mirrorCamera, this.renderTarget2, true );
M
r61  
Mr.doob 已提交
299 300 301 302

	}

};