Browse Source

Mirror: Clean up.

Mr.doob 8 years ago
parent
commit
97331bd037
1 changed files with 47 additions and 55 deletions
  1. 47 55
      examples/js/Mirror.js

+ 47 - 55
examples/js/Mirror.js

@@ -2,56 +2,6 @@
  * @author Slayvin / http://slayvin.net
  * @author Slayvin / http://slayvin.net
  */
  */
 
 
-THREE.ShaderLib[ 'mirror' ] = {
-
-	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" )
-
-};
-
 THREE.Mirror = function ( renderer, camera, options ) {
 THREE.Mirror = function ( renderer, camera, options ) {
 
 
 	THREE.Object3D.call( this );
 	THREE.Object3D.call( this );
@@ -118,7 +68,47 @@ THREE.Mirror = function ( renderer, camera, options ) {
 	this.renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );
 	this.renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );
 	this.renderTarget2 = new THREE.WebGLRenderTarget( width, height, parameters );
 	this.renderTarget2 = new THREE.WebGLRenderTarget( width, height, parameters );
 
 
-	var mirrorShader = THREE.ShaderLib[ "mirror" ];
+	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' )
+
+	};
+
 	var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
 	var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
 
 
 	this.material = new THREE.ShaderMaterial( {
 	this.material = new THREE.ShaderMaterial( {
@@ -215,10 +205,12 @@ THREE.Mirror.prototype.updateTextureMatrix = function () {
 	this.mirrorCamera.matrixWorldInverse.getInverse( this.mirrorCamera.matrixWorld );
 	this.mirrorCamera.matrixWorldInverse.getInverse( this.mirrorCamera.matrixWorld );
 
 
 	// Update the texture matrix
 	// 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.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.projectionMatrix );
 	this.textureMatrix.multiply( this.mirrorCamera.matrixWorldInverse );
 	this.textureMatrix.multiply( this.mirrorCamera.matrixWorldInverse );