Mirror.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. scope.isVisible = function ( camera ) {
  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. return view.dot( normal ) < 0;
  85. };
  86. scope.onBeforeRender = function ( renderer, scene, camera ) {
  87. mirrorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  88. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  89. rotationMatrix.extractRotation( scope.matrixWorld );
  90. normal.set( 0, 0, 1 );
  91. normal.applyMatrix4( rotationMatrix );
  92. view.subVectors( mirrorWorldPosition, cameraWorldPosition );
  93. // Avoid rendering when mirror is facing away
  94. if ( view.dot( normal ) > 0 ) return;
  95. view.reflect( normal ).negate();
  96. view.add( mirrorWorldPosition );
  97. rotationMatrix.extractRotation( camera.matrixWorld );
  98. lookAtPosition.set( 0, 0, - 1 );
  99. lookAtPosition.applyMatrix4( rotationMatrix );
  100. lookAtPosition.add( cameraWorldPosition );
  101. target.subVectors( mirrorWorldPosition, lookAtPosition );
  102. target.reflect( normal ).negate();
  103. target.add( mirrorWorldPosition );
  104. mirrorCamera.position.copy( view );
  105. mirrorCamera.up.set( 0, 1, 0 );
  106. mirrorCamera.up.applyMatrix4( rotationMatrix );
  107. mirrorCamera.up.reflect( normal );
  108. mirrorCamera.lookAt( target );
  109. mirrorCamera.aspect = camera.aspect;
  110. mirrorCamera.near = camera.near;
  111. mirrorCamera.far = camera.far;
  112. mirrorCamera.updateMatrixWorld();
  113. mirrorCamera.updateProjectionMatrix();
  114. // Update the texture matrix
  115. textureMatrix.set(
  116. 0.5, 0.0, 0.0, 0.5,
  117. 0.0, 0.5, 0.0, 0.5,
  118. 0.0, 0.0, 0.5, 0.5,
  119. 0.0, 0.0, 0.0, 1.0
  120. );
  121. textureMatrix.multiply( mirrorCamera.projectionMatrix );
  122. textureMatrix.multiply( mirrorCamera.matrixWorldInverse );
  123. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  124. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  125. mirrorPlane.setFromNormalAndCoplanarPoint( normal, mirrorWorldPosition );
  126. mirrorPlane.applyMatrix4( mirrorCamera.matrixWorldInverse );
  127. clipPlane.set( mirrorPlane.normal.x, mirrorPlane.normal.y, mirrorPlane.normal.z, mirrorPlane.constant );
  128. var projectionMatrix = mirrorCamera.projectionMatrix;
  129. q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  130. q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  131. q.z = - 1.0;
  132. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  133. // Calculate the scaled plane vector
  134. clipPlane.multiplyScalar( 2.0 / clipPlane.dot( q ) );
  135. // Replacing the third row of the projection matrix
  136. projectionMatrix.elements[ 2 ] = clipPlane.x;
  137. projectionMatrix.elements[ 6 ] = clipPlane.y;
  138. projectionMatrix.elements[ 10 ] = clipPlane.z + 1.0 - clipBias;
  139. projectionMatrix.elements[ 14 ] = clipPlane.w;
  140. // Render
  141. scope.visible = false;
  142. var currentRenderTarget = renderer.getRenderTarget();
  143. var currentVrEnabled = renderer.vr.enabled;
  144. var currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  145. renderer.vr.enabled = false; // Avoid camera modification and recursion
  146. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  147. renderer.render( scene, mirrorCamera, renderTarget, true );
  148. renderer.vr.enabled = currentVrEnabled;
  149. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  150. renderer.setRenderTarget( currentRenderTarget );
  151. // Restore viewport
  152. var bounds = camera.bounds;
  153. if ( bounds !== undefined ) {
  154. var size = renderer.getSize();
  155. var pixelRatio = renderer.getPixelRatio();
  156. viewport.x = bounds.x * size.width * pixelRatio;
  157. viewport.y = bounds.y * size.height * pixelRatio;
  158. viewport.z = bounds.z * size.width * pixelRatio;
  159. viewport.w = bounds.w * size.height * pixelRatio;
  160. renderer.state.viewport( viewport );
  161. }
  162. scope.visible = true;
  163. };
  164. };
  165. THREE.Mirror.prototype = Object.create( THREE.Mesh.prototype );
粤ICP备19079148号