Mirror.js 8.4 KB

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