Mirror.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @author Slayvin / http://slayvin.net
  3. */
  4. THREE.ShaderLib['mirror'] = {
  5. uniforms: { "mirrorColor": { type: "c", value: new THREE.Color(0x7F7F7F) },
  6. "mirrorSampler": { type: "t", value: null },
  7. "textureMatrix" : { type: "m4", value: new THREE.Matrix4() }
  8. },
  9. vertexShader: [
  10. "uniform mat4 textureMatrix;",
  11. "varying vec4 mirrorCoord;",
  12. "void main() {",
  13. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  14. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  15. "mirrorCoord = textureMatrix * worldPosition;",
  16. "gl_Position = projectionMatrix * mvPosition;",
  17. "}"
  18. ].join("\n"),
  19. fragmentShader: [
  20. "uniform vec3 mirrorColor;",
  21. "uniform sampler2D mirrorSampler;",
  22. "varying vec4 mirrorCoord;",
  23. "float blendOverlay(float base, float blend) {",
  24. "return( base < 0.5 ? ( 2.0 * base * blend ) : (1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );",
  25. "}",
  26. "void main() {",
  27. "vec4 color = texture2DProj(mirrorSampler, mirrorCoord);",
  28. "color = vec4(blendOverlay(mirrorColor.r, color.r), blendOverlay(mirrorColor.g, color.g), blendOverlay(mirrorColor.b, color.b), 1.0);",
  29. "gl_FragColor = color;",
  30. "}"
  31. ].join("\n")
  32. };
  33. THREE.Mirror = function ( renderer, camera, options ) {
  34. THREE.Object3D.call( this );
  35. this.name = 'mirror_' + this.id;
  36. options = options || {};
  37. this.matrixNeedsUpdate = true;
  38. var width = options.textureWidth !== undefined ? options.textureWidth : 512;
  39. var height = options.textureHeight !== undefined ? options.textureHeight : 512;
  40. this.clipBias = options.clipBias !== undefined ? options.clipBias : 0.0;
  41. var mirrorColor = options.color !== undefined ? new THREE.Color(options.color) : new THREE.Color(0x7F7F7F);
  42. this.renderer = renderer;
  43. this.mirrorPlane = new THREE.Plane();
  44. this.normal = new THREE.Vector3( 0, 0, 1 );
  45. this.mirrorWorldPosition = new THREE.Vector3();
  46. this.cameraWorldPosition = new THREE.Vector3();
  47. this.rotationMatrix = new THREE.Matrix4();
  48. this.lookAtPosition = new THREE.Vector3(0, 0, -1);
  49. this.clipPlane = new THREE.Vector4();
  50. // For debug only, show the normal and plane of the mirror
  51. var debugMode = options.debugMode !== undefined ? options.debugMode : false;
  52. if ( debugMode ) {
  53. var arrow = new THREE.ArrowHelper(new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 0, 0, 0 ), 10, 0xffff80 );
  54. var planeGeometry = new THREE.Geometry();
  55. planeGeometry.vertices.push( new THREE.Vector3( -10, -10, 0 ) );
  56. planeGeometry.vertices.push( new THREE.Vector3( 10, -10, 0 ) );
  57. planeGeometry.vertices.push( new THREE.Vector3( 10, 10, 0 ) );
  58. planeGeometry.vertices.push( new THREE.Vector3( -10, 10, 0 ) );
  59. planeGeometry.vertices.push( planeGeometry.vertices[0] );
  60. var plane = new THREE.Line( planeGeometry, new THREE.LineBasicMaterial( { color: 0xffff80 } ) );
  61. this.add(arrow);
  62. this.add(plane);
  63. }
  64. if ( camera instanceof THREE.PerspectiveCamera ) {
  65. this.camera = camera;
  66. } else {
  67. this.camera = new THREE.PerspectiveCamera();
  68. console.log( this.name + ': camera is not a Perspective Camera!' );
  69. }
  70. this.textureMatrix = new THREE.Matrix4();
  71. this.mirrorCamera = this.camera.clone();
  72. this.texture = new THREE.WebGLRenderTarget( width, height );
  73. this.tempTexture = new THREE.WebGLRenderTarget( width, height );
  74. var mirrorShader = THREE.ShaderLib[ "mirror" ];
  75. var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
  76. this.material = new THREE.ShaderMaterial( {
  77. fragmentShader: mirrorShader.fragmentShader,
  78. vertexShader: mirrorShader.vertexShader,
  79. uniforms: mirrorUniforms
  80. } );
  81. this.material.uniforms.mirrorSampler.value = this.texture;
  82. this.material.uniforms.mirrorColor.value = mirrorColor;
  83. this.material.uniforms.textureMatrix.value = this.textureMatrix;
  84. if ( !THREE.Math.isPowerOfTwo(width) || !THREE.Math.isPowerOfTwo( height ) ) {
  85. this.texture.generateMipmaps = false;
  86. this.tempTexture.generateMipmaps = false;
  87. }
  88. this.updateTextureMatrix();
  89. this.render();
  90. };
  91. THREE.Mirror.prototype = Object.create( THREE.Object3D.prototype );
  92. THREE.Mirror.prototype.renderWithMirror = function ( otherMirror ) {
  93. // update the mirror matrix to mirror the current view
  94. this.updateTextureMatrix();
  95. this.matrixNeedsUpdate = false;
  96. // set the camera of the other mirror so the mirrored view is the reference view
  97. var tempCamera = otherMirror.camera;
  98. otherMirror.camera = this.mirrorCamera;
  99. // render the other mirror in temp texture
  100. otherMirror.renderTemp();
  101. otherMirror.material.uniforms.mirrorSampler.value = otherMirror.tempTexture;
  102. // render the current mirror
  103. this.render();
  104. this.matrixNeedsUpdate = true;
  105. // restore material and camera of other mirror
  106. otherMirror.material.uniforms.mirrorSampler.value = otherMirror.texture;
  107. otherMirror.camera = tempCamera;
  108. // restore texture matrix of other mirror
  109. otherMirror.updateTextureMatrix();
  110. };
  111. THREE.Mirror.prototype.updateTextureMatrix = function () {
  112. this.updateMatrixWorld();
  113. this.camera.updateMatrixWorld();
  114. this.mirrorWorldPosition.setFromMatrixPosition( this.matrixWorld );
  115. this.cameraWorldPosition.setFromMatrixPosition( this.camera.matrixWorld );
  116. this.rotationMatrix.extractRotation( this.matrixWorld );
  117. this.normal.set( 0, 0, 1 );
  118. this.normal.applyMatrix4( this.rotationMatrix );
  119. var view = this.mirrorWorldPosition.clone().sub( this.cameraWorldPosition );
  120. view.reflect( this.normal ).negate();
  121. view.add( this.mirrorWorldPosition );
  122. this.rotationMatrix.extractRotation( this.camera.matrixWorld );
  123. this.lookAtPosition.set(0, 0, -1);
  124. this.lookAtPosition.applyMatrix4( this.rotationMatrix );
  125. this.lookAtPosition.add( this.cameraWorldPosition );
  126. var target = this.mirrorWorldPosition.clone().sub( this.lookAtPosition );
  127. target.reflect( this.normal ).negate();
  128. target.add( this.mirrorWorldPosition );
  129. this.up.set( 0, -1, 0 );
  130. this.up.applyMatrix4( this.rotationMatrix );
  131. this.up.reflect( this.normal ).negate();
  132. this.mirrorCamera.position.copy( view );
  133. this.mirrorCamera.up = this.up;
  134. this.mirrorCamera.lookAt( target );
  135. this.mirrorCamera.updateProjectionMatrix();
  136. this.mirrorCamera.updateMatrixWorld();
  137. this.mirrorCamera.matrixWorldInverse.getInverse( this.mirrorCamera.matrixWorld );
  138. // Update the texture matrix
  139. this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
  140. 0.0, 0.5, 0.0, 0.5,
  141. 0.0, 0.0, 0.5, 0.5,
  142. 0.0, 0.0, 0.0, 1.0 );
  143. this.textureMatrix.multiply( this.mirrorCamera.projectionMatrix );
  144. this.textureMatrix.multiply( this.mirrorCamera.matrixWorldInverse );
  145. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  146. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  147. this.mirrorPlane.setFromNormalAndCoplanarPoint( this.normal, this.mirrorWorldPosition );
  148. this.mirrorPlane.applyMatrix4( this.mirrorCamera.matrixWorldInverse );
  149. this.clipPlane.set( this.mirrorPlane.normal.x, this.mirrorPlane.normal.y, this.mirrorPlane.normal.z, this.mirrorPlane.constant );
  150. var q = new THREE.Vector4();
  151. var projectionMatrix = this.mirrorCamera.projectionMatrix;
  152. q.x = ( Math.sign(this.clipPlane.x) + projectionMatrix.elements[8] ) / projectionMatrix.elements[0];
  153. q.y = ( Math.sign(this.clipPlane.y) + projectionMatrix.elements[9] ) / projectionMatrix.elements[5];
  154. q.z = - 1.0;
  155. q.w = ( 1.0 + projectionMatrix.elements[10] ) / projectionMatrix.elements[14];
  156. // Calculate the scaled plane vector
  157. var c = new THREE.Vector4();
  158. c = this.clipPlane.multiplyScalar( 2.0 / this.clipPlane.dot(q) );
  159. // Replacing the third row of the projection matrix
  160. projectionMatrix.elements[2] = c.x;
  161. projectionMatrix.elements[6] = c.y;
  162. projectionMatrix.elements[10] = c.z + 1.0 - this.clipBias;
  163. projectionMatrix.elements[14] = c.w;
  164. };
  165. THREE.Mirror.prototype.render = function () {
  166. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  167. this.matrixNeedsUpdate = true;
  168. // Render the mirrored view of the current scene into the target texture
  169. var scene = this;
  170. while ( scene.parent !== undefined ) {
  171. scene = scene.parent;
  172. }
  173. if ( scene !== undefined && scene instanceof THREE.Scene) {
  174. this.renderer.render( scene, this.mirrorCamera, this.texture, true );
  175. }
  176. };
  177. THREE.Mirror.prototype.renderTemp = function () {
  178. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  179. this.matrixNeedsUpdate = true;
  180. // Render the mirrored view of the current scene into the target texture
  181. var scene = this;
  182. while ( scene.parent !== undefined ) {
  183. scene = scene.parent;
  184. }
  185. if ( scene !== undefined && scene instanceof THREE.Scene) {
  186. this.renderer.render( scene, this.mirrorCamera, this.tempTexture, true );
  187. }
  188. };