Mirror.js 6.5 KB

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