Mirror.js 7.0 KB

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