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

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

	THREE.Object3D.call( this );
M
Mr.doob 已提交
8

M
Mr.doob 已提交
9 10 11 12 13 14 15 16 17 18 19
	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;

G
gero3 已提交
20
	var mirrorColor = options.color !== undefined ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
M
Mr.doob 已提交
21 22 23 24 25 26 27

	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();
G
gero3 已提交
28
	this.lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
M
Mr.doob 已提交
29
	this.clipPlane = new THREE.Vector4();
30

M
Mr.doob 已提交
31 32
	if ( camera instanceof THREE.PerspectiveCamera ) {

M
Mr.doob 已提交
33
		this.camera = camera;
M
Mr.doob 已提交
34 35 36

	} else {

M
Mr.doob 已提交
37
		this.camera = new THREE.PerspectiveCamera();
38
		console.log( this.name + ': camera is not a Perspective Camera!' );
M
Mr.doob 已提交
39

M
Mr.doob 已提交
40 41 42 43 44
	}

	this.textureMatrix = new THREE.Matrix4();

	this.mirrorCamera = this.camera.clone();
45
	this.mirrorCamera.matrixAutoUpdate = true;
M
Mr.doob 已提交
46

G
Gregg Tavares 已提交
47 48
	var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };

49 50
	this.renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );
	this.renderTarget2 = new THREE.WebGLRenderTarget( width, height, parameters );
M
Mr.doob 已提交
51

M
Mr.doob 已提交
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
	var mirrorShader = {

		uniforms: {
			mirrorColor: { value: new THREE.Color( 0x7F7F7F ) },
			mirrorSampler: { value: null },
			textureMatrix: { value: new THREE.Matrix4() }
		},

		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;',

			'}'
		].join( '\n' ),

		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 ) ) );',
			'}',

			'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;',
			'}'
		].join( '\n' )

	};

93
	var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
M
Mr.doob 已提交
94 95

	this.material = new THREE.ShaderMaterial( {
M
Mr.doob 已提交
96

M
Mr.doob 已提交
97 98 99
		fragmentShader: mirrorShader.fragmentShader,
		vertexShader: mirrorShader.vertexShader,
		uniforms: mirrorUniforms
M
Mr.doob 已提交
100

M
Mr.doob 已提交
101 102
	} );

103 104 105
	this.material.uniforms.mirrorSampler.value = this.renderTarget.texture;
	this.material.uniforms.mirrorColor.value = mirrorColor;
	this.material.uniforms.textureMatrix.value = this.textureMatrix;
M
Mr.doob 已提交
106

G
gero3 已提交
107
	if ( ! THREE.Math.isPowerOfTwo( width ) || ! THREE.Math.isPowerOfTwo( height ) ) {
M
Mr.doob 已提交
108

109 110
		this.renderTarget.texture.generateMipmaps = false;
		this.renderTarget2.texture.generateMipmaps = false;
M
Mr.doob 已提交
111 112 113 114 115 116 117 118 119 120

	}

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

};

THREE.Mirror.prototype = Object.create( THREE.Object3D.prototype );

M
Mr.doob 已提交
121
Object.assign( THREE.Mirror.prototype, {
M
Mr.doob 已提交
122

M
Mr.doob 已提交
123
	constructor: THREE.Mirror,
M
Mr.doob 已提交
124

M
Mr.doob 已提交
125
	renderWithMirror: function ( otherMirror ) {
M
Mr.doob 已提交
126

M
Mr.doob 已提交
127 128 129
		// update the mirror matrix to mirror the current view
		this.updateTextureMatrix();
		this.matrixNeedsUpdate = false;
M
Mr.doob 已提交
130

M
Mr.doob 已提交
131 132 133
		// set the camera of the other mirror so the mirrored view is the reference view
		var tempCamera = otherMirror.camera;
		otherMirror.camera = this.mirrorCamera;
M
Mr.doob 已提交
134

M
Mr.doob 已提交
135 136 137
		// render the other mirror in temp texture
		otherMirror.renderTemp();
		otherMirror.material.uniforms.mirrorSampler.value = otherMirror.renderTarget2.texture;
M
Mr.doob 已提交
138

M
Mr.doob 已提交
139 140 141
		// render the current mirror
		this.render();
		this.matrixNeedsUpdate = true;
G
gero3 已提交
142

M
Mr.doob 已提交
143 144 145
		// restore material and camera of other mirror
		otherMirror.material.uniforms.mirrorSampler.value = otherMirror.renderTarget.texture;
		otherMirror.camera = tempCamera;
M
Mr.doob 已提交
146

M
Mr.doob 已提交
147 148
		// restore texture matrix of other mirror
		otherMirror.updateTextureMatrix();
M
Mr.doob 已提交
149

M
Mr.doob 已提交
150
	},
M
Mr.doob 已提交
151

M
Mr.doob 已提交
152
	updateTextureMatrix: function () {
M
Mr.doob 已提交
153

M
Mr.doob 已提交
154 155
		this.updateMatrixWorld();
		this.camera.updateMatrixWorld();
M
Mr.doob 已提交
156

M
Mr.doob 已提交
157 158
		this.mirrorWorldPosition.setFromMatrixPosition( this.matrixWorld );
		this.cameraWorldPosition.setFromMatrixPosition( this.camera.matrixWorld );
M
Mr.doob 已提交
159

M
Mr.doob 已提交
160
		this.rotationMatrix.extractRotation( this.matrixWorld );
M
Mr.doob 已提交
161

M
Mr.doob 已提交
162 163
		this.normal.set( 0, 0, 1 );
		this.normal.applyMatrix4( this.rotationMatrix );
M
Mr.doob 已提交
164

M
Mr.doob 已提交
165 166 167
		var view = this.mirrorWorldPosition.clone().sub( this.cameraWorldPosition );
		view.reflect( this.normal ).negate();
		view.add( this.mirrorWorldPosition );
M
Mr.doob 已提交
168

M
Mr.doob 已提交
169
		this.rotationMatrix.extractRotation( this.camera.matrixWorld );
M
Mr.doob 已提交
170

M
Mr.doob 已提交
171 172 173
		this.lookAtPosition.set( 0, 0, - 1 );
		this.lookAtPosition.applyMatrix4( this.rotationMatrix );
		this.lookAtPosition.add( this.cameraWorldPosition );
M
Mr.doob 已提交
174

M
Mr.doob 已提交
175 176 177
		var target = this.mirrorWorldPosition.clone().sub( this.lookAtPosition );
		target.reflect( this.normal ).negate();
		target.add( this.mirrorWorldPosition );
M
Mr.doob 已提交
178

M
Mr.doob 已提交
179 180 181
		this.up.set( 0, - 1, 0 );
		this.up.applyMatrix4( this.rotationMatrix );
		this.up.reflect( this.normal ).negate();
M
Mr.doob 已提交
182

M
Mr.doob 已提交
183 184 185
		this.mirrorCamera.position.copy( view );
		this.mirrorCamera.up = this.up;
		this.mirrorCamera.lookAt( target );
M
Mr.doob 已提交
186

M
Mr.doob 已提交
187 188 189
		this.mirrorCamera.updateProjectionMatrix();
		this.mirrorCamera.updateMatrixWorld();
		this.mirrorCamera.matrixWorldInverse.getInverse( this.mirrorCamera.matrixWorld );
M
Mr.doob 已提交
190

M
Mr.doob 已提交
191 192 193 194 195 196 197 198 199
		// 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 );
M
Mr.doob 已提交
200

M
Mr.doob 已提交
201 202 203 204
		// 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 );
M
Mr.doob 已提交
205

M
Mr.doob 已提交
206
		this.clipPlane.set( this.mirrorPlane.normal.x, this.mirrorPlane.normal.y, this.mirrorPlane.normal.z, this.mirrorPlane.constant );
M
Mr.doob 已提交
207

M
Mr.doob 已提交
208 209
		var q = new THREE.Vector4();
		var projectionMatrix = this.mirrorCamera.projectionMatrix;
M
Mr.doob 已提交
210

M
Mr.doob 已提交
211 212 213 214
		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 ];
		q.z = - 1.0;
		q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
M
Mr.doob 已提交
215

M
Mr.doob 已提交
216 217 218
		// Calculate the scaled plane vector
		var c = new THREE.Vector4();
		c = this.clipPlane.multiplyScalar( 2.0 / this.clipPlane.dot( q ) );
M
Mr.doob 已提交
219

M
Mr.doob 已提交
220 221 222 223 224
		// Replacing the third row of the projection matrix
		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
Mr.doob 已提交
225

M
Mr.doob 已提交
226
	},
M
Mr.doob 已提交
227

M
Mr.doob 已提交
228
	render: function () {
M
Mr.doob 已提交
229

M
Mr.doob 已提交
230
		if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
M
Mr.doob 已提交
231

M
Mr.doob 已提交
232
		this.matrixNeedsUpdate = true;
M
Mr.doob 已提交
233

M
Mr.doob 已提交
234 235
		// Render the mirrored view of the current scene into the target texture
		var scene = this;
M
Mr.doob 已提交
236

M
Mr.doob 已提交
237
		while ( scene.parent !== null ) {
M
Mr.doob 已提交
238

M
Mr.doob 已提交
239
			scene = scene.parent;
M
Mr.doob 已提交
240

M
Mr.doob 已提交
241
		}
G
Gregg Tavares 已提交
242

M
Mr.doob 已提交
243
		if ( scene !== undefined && scene instanceof THREE.Scene ) {
M
Mr.doob 已提交
244

M
Mr.doob 已提交
245 246 247
			// We can't render ourself to ourself
			var visible = this.material.visible;
			this.material.visible = false;
G
Gregg Tavares 已提交
248

M
Mr.doob 已提交
249
			this.renderer.render( scene, this.mirrorCamera, this.renderTarget, true );
M
Mr.doob 已提交
250

M
Mr.doob 已提交
251
			this.material.visible = visible;
M
Mr.doob 已提交
252

M
Mr.doob 已提交
253
		}
M
Mr.doob 已提交
254

M
Mr.doob 已提交
255
	},
M
Mr.doob 已提交
256

M
Mr.doob 已提交
257
	renderTemp: function () {
M
Mr.doob 已提交
258

M
Mr.doob 已提交
259
		if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
M
Mr.doob 已提交
260

M
Mr.doob 已提交
261
		this.matrixNeedsUpdate = true;
M
Mr.doob 已提交
262

M
Mr.doob 已提交
263 264
		// Render the mirrored view of the current scene into the target texture
		var scene = this;
M
Mr.doob 已提交
265

M
Mr.doob 已提交
266 267 268
		while ( scene.parent !== null ) {

			scene = scene.parent;
M
Mr.doob 已提交
269

M
Mr.doob 已提交
270
		}
M
Mr.doob 已提交
271

M
Mr.doob 已提交
272 273 274 275 276
		if ( scene !== undefined && scene instanceof THREE.Scene ) {

			this.renderer.render( scene, this.mirrorCamera, this.renderTarget2, true );

		}
M
Mr.doob 已提交
277 278 279

	}

M
Mr.doob 已提交
280
} );