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