Mirror.js 8.9 KB

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