Mirror.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.mirrorCamera.matrixAutoUpdate = true;
  73. var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  74. this.texture = new THREE.WebGLRenderTarget( width, height, parameters );
  75. this.tempTexture = new THREE.WebGLRenderTarget( width, height, parameters );
  76. var mirrorShader = THREE.ShaderLib[ "mirror" ];
  77. var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
  78. this.material = new THREE.ShaderMaterial( {
  79. fragmentShader: mirrorShader.fragmentShader,
  80. vertexShader: mirrorShader.vertexShader,
  81. uniforms: mirrorUniforms
  82. } );
  83. this.material.uniforms.mirrorSampler.value = this.texture;
  84. this.material.uniforms.mirrorColor.value = mirrorColor;
  85. this.material.uniforms.textureMatrix.value = this.textureMatrix;
  86. if ( ! THREE.Math.isPowerOfTwo( width ) || ! THREE.Math.isPowerOfTwo( height ) ) {
  87. this.texture.generateMipmaps = false;
  88. this.tempTexture.generateMipmaps = false;
  89. }
  90. this.updateTextureMatrix();
  91. this.render();
  92. };
  93. THREE.Mirror.prototype = Object.create( THREE.Object3D.prototype );
  94. THREE.Mirror.prototype.constructor = THREE.Mirror;
  95. THREE.Mirror.prototype.renderWithMirror = function ( otherMirror ) {
  96. // update the mirror matrix to mirror the current view
  97. this.updateTextureMatrix();
  98. this.matrixNeedsUpdate = false;
  99. // set the camera of the other mirror so the mirrored view is the reference view
  100. var tempCamera = otherMirror.camera;
  101. otherMirror.camera = this.mirrorCamera;
  102. // render the other mirror in temp texture
  103. otherMirror.renderTemp();
  104. otherMirror.material.uniforms.mirrorSampler.value = otherMirror.tempTexture;
  105. // render the current mirror
  106. this.render();
  107. this.matrixNeedsUpdate = true;
  108. // restore material and camera of other mirror
  109. otherMirror.material.uniforms.mirrorSampler.value = otherMirror.texture;
  110. otherMirror.camera = tempCamera;
  111. // restore texture matrix of other mirror
  112. otherMirror.updateTextureMatrix();
  113. };
  114. THREE.Mirror.prototype.updateTextureMatrix = function () {
  115. this.updateMatrixWorld();
  116. this.camera.updateMatrixWorld();
  117. this.mirrorWorldPosition.setFromMatrixPosition( this.matrixWorld );
  118. this.cameraWorldPosition.setFromMatrixPosition( this.camera.matrixWorld );
  119. this.rotationMatrix.extractRotation( this.matrixWorld );
  120. this.normal.set( 0, 0, 1 );
  121. this.normal.applyMatrix4( this.rotationMatrix );
  122. var view = this.mirrorWorldPosition.clone().sub( this.cameraWorldPosition );
  123. view.reflect( this.normal ).negate();
  124. view.add( this.mirrorWorldPosition );
  125. this.rotationMatrix.extractRotation( this.camera.matrixWorld );
  126. this.lookAtPosition.set( 0, 0, - 1 );
  127. this.lookAtPosition.applyMatrix4( this.rotationMatrix );
  128. this.lookAtPosition.add( this.cameraWorldPosition );
  129. var target = this.mirrorWorldPosition.clone().sub( this.lookAtPosition );
  130. target.reflect( this.normal ).negate();
  131. target.add( this.mirrorWorldPosition );
  132. this.up.set( 0, - 1, 0 );
  133. this.up.applyMatrix4( this.rotationMatrix );
  134. this.up.reflect( this.normal ).negate();
  135. this.mirrorCamera.position.copy( view );
  136. this.mirrorCamera.up = this.up;
  137. this.mirrorCamera.lookAt( target );
  138. this.mirrorCamera.updateProjectionMatrix();
  139. this.mirrorCamera.updateMatrixWorld();
  140. this.mirrorCamera.matrixWorldInverse.getInverse( this.mirrorCamera.matrixWorld );
  141. // Update the texture matrix
  142. this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
  143. 0.0, 0.5, 0.0, 0.5,
  144. 0.0, 0.0, 0.5, 0.5,
  145. 0.0, 0.0, 0.0, 1.0 );
  146. this.textureMatrix.multiply( this.mirrorCamera.projectionMatrix );
  147. this.textureMatrix.multiply( this.mirrorCamera.matrixWorldInverse );
  148. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  149. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  150. this.mirrorPlane.setFromNormalAndCoplanarPoint( this.normal, this.mirrorWorldPosition );
  151. this.mirrorPlane.applyMatrix4( this.mirrorCamera.matrixWorldInverse );
  152. this.clipPlane.set( this.mirrorPlane.normal.x, this.mirrorPlane.normal.y, this.mirrorPlane.normal.z, this.mirrorPlane.constant );
  153. var q = new THREE.Vector4();
  154. var projectionMatrix = this.mirrorCamera.projectionMatrix;
  155. q.x = ( Math.sign( this.clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  156. q.y = ( Math.sign( this.clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  157. q.z = - 1.0;
  158. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  159. // Calculate the scaled plane vector
  160. var c = new THREE.Vector4();
  161. c = this.clipPlane.multiplyScalar( 2.0 / this.clipPlane.dot( q ) );
  162. // Replacing the third row of the projection matrix
  163. projectionMatrix.elements[ 2 ] = c.x;
  164. projectionMatrix.elements[ 6 ] = c.y;
  165. projectionMatrix.elements[ 10 ] = c.z + 1.0 - this.clipBias;
  166. projectionMatrix.elements[ 14 ] = c.w;
  167. };
  168. THREE.Mirror.prototype.render = function () {
  169. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  170. this.matrixNeedsUpdate = true;
  171. // Render the mirrored view of the current scene into the target texture
  172. var scene = this;
  173. while ( scene.parent !== null ) {
  174. scene = scene.parent;
  175. }
  176. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  177. // We can't render ourself to ourself
  178. var visible = this.material.visible;
  179. this.material.visible = false;
  180. this.renderer.render( scene, this.mirrorCamera, this.texture, true );
  181. this.material.visible = visible;
  182. }
  183. };
  184. THREE.Mirror.prototype.renderTemp = function () {
  185. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  186. this.matrixNeedsUpdate = true;
  187. // Render the mirrored view of the current scene into the target texture
  188. var scene = this;
  189. while ( scene.parent !== null ) {
  190. scene = scene.parent;
  191. }
  192. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  193. this.renderer.render( scene, this.mirrorCamera, this.tempTexture, true );
  194. }
  195. };