2
0

Mirror.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. // For debug only, show the normal and plane of the mirror
  22. var debugMode = options.debugMode !== undefined ? options.debugMode : false;
  23. if ( debugMode ) {
  24. var arrow = new THREE.ArrowHelper( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 0, 0, 0 ), 10, 0xffff80 );
  25. var planeGeometry = new THREE.Geometry();
  26. planeGeometry.vertices.push( new THREE.Vector3( - 10, - 10, 0 ) );
  27. planeGeometry.vertices.push( new THREE.Vector3( 10, - 10, 0 ) );
  28. planeGeometry.vertices.push( new THREE.Vector3( 10, 10, 0 ) );
  29. planeGeometry.vertices.push( new THREE.Vector3( - 10, 10, 0 ) );
  30. planeGeometry.vertices.push( planeGeometry.vertices[ 0 ] );
  31. var plane = new THREE.Line( planeGeometry, new THREE.LineBasicMaterial( { color: 0xffff80 } ) );
  32. this.add( arrow );
  33. this.add( plane );
  34. }
  35. if ( camera instanceof THREE.PerspectiveCamera ) {
  36. this.camera = camera;
  37. } else {
  38. this.camera = new THREE.PerspectiveCamera();
  39. console.log( this.name + ': camera is not a Perspective Camera!' );
  40. }
  41. this.textureMatrix = new THREE.Matrix4();
  42. this.mirrorCamera = this.camera.clone();
  43. this.mirrorCamera.matrixAutoUpdate = true;
  44. var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  45. this.renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );
  46. this.renderTarget2 = new THREE.WebGLRenderTarget( width, height, parameters );
  47. var mirrorShader = {
  48. uniforms: {
  49. mirrorColor: { value: new THREE.Color( 0x7F7F7F ) },
  50. mirrorSampler: { value: null },
  51. textureMatrix: { value: new THREE.Matrix4() }
  52. },
  53. vertexShader: [
  54. 'uniform mat4 textureMatrix;',
  55. 'varying vec4 mirrorCoord;',
  56. 'void main() {',
  57. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  58. ' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );',
  59. ' mirrorCoord = textureMatrix * worldPosition;',
  60. ' gl_Position = projectionMatrix * mvPosition;',
  61. '}'
  62. ].join( '\n' ),
  63. fragmentShader: [
  64. 'uniform vec3 mirrorColor;',
  65. 'uniform sampler2D mirrorSampler;',
  66. 'varying vec4 mirrorCoord;',
  67. 'float blendOverlay(float base, float blend) {',
  68. ' return( base < 0.5 ? ( 2.0 * base * blend ) : (1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );',
  69. '}',
  70. 'void main() {',
  71. ' vec4 color = texture2DProj(mirrorSampler, mirrorCoord);',
  72. ' color = vec4(blendOverlay(mirrorColor.r, color.r), blendOverlay(mirrorColor.g, color.g), blendOverlay(mirrorColor.b, color.b), 1.0);',
  73. ' gl_FragColor = color;',
  74. '}'
  75. ].join( '\n' )
  76. };
  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.renderTarget.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.renderTarget.texture.generateMipmaps = false;
  88. this.renderTarget2.texture.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.renderTarget2.texture;
  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.renderTarget.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(
  143. 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. );
  148. this.textureMatrix.multiply( this.mirrorCamera.projectionMatrix );
  149. this.textureMatrix.multiply( this.mirrorCamera.matrixWorldInverse );
  150. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  151. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  152. this.mirrorPlane.setFromNormalAndCoplanarPoint( this.normal, this.mirrorWorldPosition );
  153. this.mirrorPlane.applyMatrix4( this.mirrorCamera.matrixWorldInverse );
  154. this.clipPlane.set( this.mirrorPlane.normal.x, this.mirrorPlane.normal.y, this.mirrorPlane.normal.z, this.mirrorPlane.constant );
  155. var q = new THREE.Vector4();
  156. var projectionMatrix = this.mirrorCamera.projectionMatrix;
  157. q.x = ( Math.sign( this.clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  158. q.y = ( Math.sign( this.clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  159. q.z = - 1.0;
  160. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  161. // Calculate the scaled plane vector
  162. var c = new THREE.Vector4();
  163. c = this.clipPlane.multiplyScalar( 2.0 / this.clipPlane.dot( q ) );
  164. // Replacing the third row of the projection matrix
  165. projectionMatrix.elements[ 2 ] = c.x;
  166. projectionMatrix.elements[ 6 ] = c.y;
  167. projectionMatrix.elements[ 10 ] = c.z + 1.0 - this.clipBias;
  168. projectionMatrix.elements[ 14 ] = c.w;
  169. };
  170. THREE.Mirror.prototype.render = function () {
  171. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  172. this.matrixNeedsUpdate = true;
  173. // Render the mirrored view of the current scene into the target texture
  174. var scene = this;
  175. while ( scene.parent !== null ) {
  176. scene = scene.parent;
  177. }
  178. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  179. // We can't render ourself to ourself
  180. var visible = this.material.visible;
  181. this.material.visible = false;
  182. this.renderer.render( scene, this.mirrorCamera, this.renderTarget, true );
  183. this.material.visible = visible;
  184. }
  185. };
  186. THREE.Mirror.prototype.renderTemp = function () {
  187. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  188. this.matrixNeedsUpdate = true;
  189. // Render the mirrored view of the current scene into the target texture
  190. var scene = this;
  191. while ( scene.parent !== null ) {
  192. scene = scene.parent;
  193. }
  194. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  195. this.renderer.render( scene, this.mirrorCamera, this.renderTarget2, true );
  196. }
  197. };