Mirror.js 8.4 KB

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