Mirror.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @author Slayvin / http://slayvin.net
  3. */
  4. THREE.Mirror = function ( width, height, options ) {
  5. THREE.Mesh.call( this, new THREE.PlaneBufferGeometry( width, height ) );
  6. var scope = this;
  7. scope.name = 'mirror_' + scope.id;
  8. scope.matrixNeedsUpdate = true;
  9. options = options || {};
  10. var viewport = new THREE.Vector4();
  11. var textureWidth = options.textureWidth !== undefined ? options.textureWidth : 512;
  12. var textureHeight = options.textureHeight !== undefined ? options.textureHeight : 512;
  13. var clipBias = options.clipBias !== undefined ? options.clipBias : 0.0;
  14. var mirrorColor = options.color !== undefined ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
  15. var recursion = options.recursion !== undefined ? options.recursion : 0;
  16. //
  17. var mirrorPlane = new THREE.Plane();
  18. var normal = new THREE.Vector3();
  19. var mirrorWorldPosition = new THREE.Vector3();
  20. var cameraWorldPosition = new THREE.Vector3();
  21. var rotationMatrix = new THREE.Matrix4();
  22. var lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
  23. var clipPlane = new THREE.Vector4();
  24. var view = new THREE.Vector3();
  25. var target = new THREE.Vector3();
  26. var q = new THREE.Vector4();
  27. var textureMatrix = new THREE.Matrix4();
  28. var mirrorCamera = new THREE.PerspectiveCamera();
  29. var parameters = {
  30. minFilter: THREE.LinearFilter,
  31. magFilter: THREE.LinearFilter,
  32. format: THREE.RGBFormat,
  33. stencilBuffer: false
  34. };
  35. var renderTarget = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  36. if ( ! THREE.Math.isPowerOfTwo( textureWidth ) || ! THREE.Math.isPowerOfTwo( textureHeight ) ) {
  37. renderTarget.texture.generateMipmaps = false;
  38. }
  39. var mirrorShader = {
  40. uniforms: {
  41. mirrorColor: { value: new THREE.Color( 0x7F7F7F ) },
  42. mirrorSampler: { value: null },
  43. textureMatrix: { value: new THREE.Matrix4() }
  44. },
  45. vertexShader: [
  46. 'uniform mat4 textureMatrix;',
  47. 'varying vec4 mirrorCoord;',
  48. 'void main() {',
  49. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  50. ' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );',
  51. ' mirrorCoord = textureMatrix * worldPosition;',
  52. ' gl_Position = projectionMatrix * mvPosition;',
  53. '}'
  54. ].join( '\n' ),
  55. fragmentShader: [
  56. 'uniform vec3 mirrorColor;',
  57. 'uniform sampler2D mirrorSampler;',
  58. 'varying vec4 mirrorCoord;',
  59. 'float blendOverlay(float base, float blend) {',
  60. ' return( base < 0.5 ? ( 2.0 * base * blend ) : (1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );',
  61. '}',
  62. 'void main() {',
  63. ' vec4 color = texture2DProj(mirrorSampler, mirrorCoord);',
  64. ' color = vec4(blendOverlay(mirrorColor.r, color.r), blendOverlay(mirrorColor.g, color.g), blendOverlay(mirrorColor.b, color.b), 1.0);',
  65. ' gl_FragColor = color;',
  66. '}'
  67. ].join( '\n' )
  68. };
  69. var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
  70. var material = new THREE.ShaderMaterial( {
  71. fragmentShader: mirrorShader.fragmentShader,
  72. vertexShader: mirrorShader.vertexShader,
  73. uniforms: mirrorUniforms
  74. } );
  75. material.uniforms.mirrorSampler.value = renderTarget.texture;
  76. material.uniforms.mirrorColor.value = mirrorColor;
  77. material.uniforms.textureMatrix.value = textureMatrix;
  78. scope.material = material;
  79. scope.onBeforeRender = function ( renderer, scene, camera ) {
  80. if ( 'recursion' in camera.userData ) {
  81. if ( camera.userData.recursion === recursion ) return;
  82. camera.userData.recursion ++;
  83. }
  84. mirrorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  85. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  86. rotationMatrix.extractRotation( scope.matrixWorld );
  87. normal.set( 0, 0, 1 );
  88. normal.applyMatrix4( rotationMatrix );
  89. view.subVectors( mirrorWorldPosition, cameraWorldPosition );
  90. // Avoid rendering when mirror is facing away
  91. if ( view.dot( normal ) > 0 ) return;
  92. view.reflect( normal ).negate();
  93. view.add( mirrorWorldPosition );
  94. rotationMatrix.extractRotation( camera.matrixWorld );
  95. lookAtPosition.set( 0, 0, - 1 );
  96. lookAtPosition.applyMatrix4( rotationMatrix );
  97. lookAtPosition.add( cameraWorldPosition );
  98. target.subVectors( mirrorWorldPosition, lookAtPosition );
  99. target.reflect( normal ).negate();
  100. target.add( mirrorWorldPosition );
  101. mirrorCamera.position.copy( view );
  102. mirrorCamera.up.set( 0, 1, 0 );
  103. mirrorCamera.up.applyMatrix4( rotationMatrix );
  104. mirrorCamera.up.reflect( normal );
  105. mirrorCamera.lookAt( target );
  106. mirrorCamera.aspect = camera.aspect;
  107. mirrorCamera.near = camera.near;
  108. mirrorCamera.far = camera.far;
  109. mirrorCamera.updateMatrixWorld();
  110. mirrorCamera.updateProjectionMatrix();
  111. mirrorCamera.userData.recursion = 0;
  112. // Update the texture matrix
  113. textureMatrix.set(
  114. 0.5, 0.0, 0.0, 0.5,
  115. 0.0, 0.5, 0.0, 0.5,
  116. 0.0, 0.0, 0.5, 0.5,
  117. 0.0, 0.0, 0.0, 1.0
  118. );
  119. textureMatrix.multiply( mirrorCamera.projectionMatrix );
  120. textureMatrix.multiply( mirrorCamera.matrixWorldInverse );
  121. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  122. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  123. mirrorPlane.setFromNormalAndCoplanarPoint( normal, mirrorWorldPosition );
  124. mirrorPlane.applyMatrix4( mirrorCamera.matrixWorldInverse );
  125. clipPlane.set( mirrorPlane.normal.x, mirrorPlane.normal.y, mirrorPlane.normal.z, mirrorPlane.constant );
  126. var projectionMatrix = mirrorCamera.projectionMatrix;
  127. q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  128. q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  129. q.z = - 1.0;
  130. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  131. // Calculate the scaled plane vector
  132. clipPlane.multiplyScalar( 2.0 / clipPlane.dot( q ) );
  133. // Replacing the third row of the projection matrix
  134. projectionMatrix.elements[ 2 ] = clipPlane.x;
  135. projectionMatrix.elements[ 6 ] = clipPlane.y;
  136. projectionMatrix.elements[ 10 ] = clipPlane.z + 1.0 - clipBias;
  137. projectionMatrix.elements[ 14 ] = clipPlane.w;
  138. // Render
  139. scope.visible = false;
  140. var currentRenderTarget = renderer.getRenderTarget();
  141. var currentVrEnabled = renderer.vr.enabled;
  142. var currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  143. renderer.vr.enabled = false; // Avoid camera modification and recursion
  144. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  145. renderer.render( scene, mirrorCamera, renderTarget, true );
  146. renderer.vr.enabled = currentVrEnabled;
  147. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  148. renderer.setRenderTarget( currentRenderTarget );
  149. // Restore viewport
  150. var bounds = camera.bounds;
  151. if ( bounds !== undefined ) {
  152. var size = renderer.getSize();
  153. var pixelRatio = renderer.getPixelRatio();
  154. viewport.x = bounds.x * size.width * pixelRatio;
  155. viewport.y = bounds.y * size.height * pixelRatio;
  156. viewport.z = bounds.z * size.width * pixelRatio;
  157. viewport.w = bounds.w * size.height * pixelRatio;
  158. renderer.state.viewport( viewport );
  159. }
  160. scope.visible = true;
  161. };
  162. };
  163. THREE.Mirror.prototype = Object.create( THREE.Mesh.prototype );