Mirror.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.far = camera.far; // Used in WebGLBackground
  107. mirrorCamera.updateMatrixWorld();
  108. mirrorCamera.projectionMatrix.copy( camera.projectionMatrix );
  109. mirrorCamera.userData.recursion = 0;
  110. // Update the texture matrix
  111. textureMatrix.set(
  112. 0.5, 0.0, 0.0, 0.5,
  113. 0.0, 0.5, 0.0, 0.5,
  114. 0.0, 0.0, 0.5, 0.5,
  115. 0.0, 0.0, 0.0, 1.0
  116. );
  117. textureMatrix.multiply( mirrorCamera.projectionMatrix );
  118. textureMatrix.multiply( mirrorCamera.matrixWorldInverse );
  119. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  120. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  121. mirrorPlane.setFromNormalAndCoplanarPoint( normal, mirrorWorldPosition );
  122. mirrorPlane.applyMatrix4( mirrorCamera.matrixWorldInverse );
  123. clipPlane.set( mirrorPlane.normal.x, mirrorPlane.normal.y, mirrorPlane.normal.z, mirrorPlane.constant );
  124. var projectionMatrix = mirrorCamera.projectionMatrix;
  125. q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  126. q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  127. q.z = - 1.0;
  128. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  129. // Calculate the scaled plane vector
  130. clipPlane.multiplyScalar( 2.0 / clipPlane.dot( q ) );
  131. // Replacing the third row of the projection matrix
  132. projectionMatrix.elements[ 2 ] = clipPlane.x;
  133. projectionMatrix.elements[ 6 ] = clipPlane.y;
  134. projectionMatrix.elements[ 10 ] = clipPlane.z + 1.0 - clipBias;
  135. projectionMatrix.elements[ 14 ] = clipPlane.w;
  136. // Render
  137. scope.visible = false;
  138. var currentRenderTarget = renderer.getRenderTarget();
  139. var currentVrEnabled = renderer.vr.enabled;
  140. var currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  141. renderer.vr.enabled = false; // Avoid camera modification and recursion
  142. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  143. renderer.render( scene, mirrorCamera, renderTarget, true );
  144. renderer.vr.enabled = currentVrEnabled;
  145. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  146. renderer.setRenderTarget( currentRenderTarget );
  147. // Restore viewport
  148. var bounds = camera.bounds;
  149. if ( bounds !== undefined ) {
  150. var size = renderer.getSize();
  151. var pixelRatio = renderer.getPixelRatio();
  152. viewport.x = bounds.x * size.width * pixelRatio;
  153. viewport.y = bounds.y * size.height * pixelRatio;
  154. viewport.z = bounds.z * size.width * pixelRatio;
  155. viewport.w = bounds.w * size.height * pixelRatio;
  156. renderer.state.viewport( viewport );
  157. }
  158. scope.visible = true;
  159. };
  160. };
  161. THREE.Mirror.prototype = Object.create( THREE.Mesh.prototype );