Reflector.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. console.warn( "THREE.Reflector: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.Reflector = function ( geometry, options ) {
  3. THREE.Mesh.call( this, geometry );
  4. this.type = 'Reflector';
  5. var scope = this;
  6. options = options || {};
  7. var color = ( options.color !== undefined ) ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
  8. var textureWidth = options.textureWidth || 512;
  9. var textureHeight = options.textureHeight || 512;
  10. var clipBias = options.clipBias || 0;
  11. var shader = options.shader || THREE.Reflector.ReflectorShader;
  12. //
  13. var reflectorPlane = new THREE.Plane();
  14. var normal = new THREE.Vector3();
  15. var reflectorWorldPosition = new THREE.Vector3();
  16. var cameraWorldPosition = new THREE.Vector3();
  17. var rotationMatrix = new THREE.Matrix4();
  18. var lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
  19. var clipPlane = new THREE.Vector4();
  20. var view = new THREE.Vector3();
  21. var target = new THREE.Vector3();
  22. var q = new THREE.Vector4();
  23. var textureMatrix = new THREE.Matrix4();
  24. var virtualCamera = new THREE.PerspectiveCamera();
  25. var parameters = {
  26. minFilter: THREE.LinearFilter,
  27. magFilter: THREE.LinearFilter,
  28. format: THREE.RGBFormat,
  29. stencilBuffer: false
  30. };
  31. var renderTarget = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  32. if ( ! THREE.MathUtils.isPowerOfTwo( textureWidth ) || ! THREE.MathUtils.isPowerOfTwo( textureHeight ) ) {
  33. renderTarget.texture.generateMipmaps = false;
  34. }
  35. var material = new THREE.ShaderMaterial( {
  36. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  37. fragmentShader: shader.fragmentShader,
  38. vertexShader: shader.vertexShader
  39. } );
  40. material.uniforms[ "tDiffuse" ].value = renderTarget.texture;
  41. material.uniforms[ "color" ].value = color;
  42. material.uniforms[ "textureMatrix" ].value = textureMatrix;
  43. this.material = material;
  44. this.onBeforeRender = function ( renderer, scene, camera ) {
  45. reflectorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  46. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  47. rotationMatrix.extractRotation( scope.matrixWorld );
  48. normal.set( 0, 0, 1 );
  49. normal.applyMatrix4( rotationMatrix );
  50. view.subVectors( reflectorWorldPosition, cameraWorldPosition );
  51. // Avoid rendering when reflector is facing away
  52. if ( view.dot( normal ) > 0 ) return;
  53. view.reflect( normal ).negate();
  54. view.add( reflectorWorldPosition );
  55. rotationMatrix.extractRotation( camera.matrixWorld );
  56. lookAtPosition.set( 0, 0, - 1 );
  57. lookAtPosition.applyMatrix4( rotationMatrix );
  58. lookAtPosition.add( cameraWorldPosition );
  59. target.subVectors( reflectorWorldPosition, lookAtPosition );
  60. target.reflect( normal ).negate();
  61. target.add( reflectorWorldPosition );
  62. virtualCamera.position.copy( view );
  63. virtualCamera.up.set( 0, 1, 0 );
  64. virtualCamera.up.applyMatrix4( rotationMatrix );
  65. virtualCamera.up.reflect( normal );
  66. virtualCamera.lookAt( target );
  67. virtualCamera.far = camera.far; // Used in WebGLBackground
  68. virtualCamera.updateMatrixWorld();
  69. virtualCamera.projectionMatrix.copy( camera.projectionMatrix );
  70. // Update the texture matrix
  71. textureMatrix.set(
  72. 0.5, 0.0, 0.0, 0.5,
  73. 0.0, 0.5, 0.0, 0.5,
  74. 0.0, 0.0, 0.5, 0.5,
  75. 0.0, 0.0, 0.0, 1.0
  76. );
  77. textureMatrix.multiply( virtualCamera.projectionMatrix );
  78. textureMatrix.multiply( virtualCamera.matrixWorldInverse );
  79. textureMatrix.multiply( scope.matrixWorld );
  80. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  81. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  82. reflectorPlane.setFromNormalAndCoplanarPoint( normal, reflectorWorldPosition );
  83. reflectorPlane.applyMatrix4( virtualCamera.matrixWorldInverse );
  84. clipPlane.set( reflectorPlane.normal.x, reflectorPlane.normal.y, reflectorPlane.normal.z, reflectorPlane.constant );
  85. var projectionMatrix = virtualCamera.projectionMatrix;
  86. q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  87. q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  88. q.z = - 1.0;
  89. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  90. // Calculate the scaled plane vector
  91. clipPlane.multiplyScalar( 2.0 / clipPlane.dot( q ) );
  92. // Replacing the third row of the projection matrix
  93. projectionMatrix.elements[ 2 ] = clipPlane.x;
  94. projectionMatrix.elements[ 6 ] = clipPlane.y;
  95. projectionMatrix.elements[ 10 ] = clipPlane.z + 1.0 - clipBias;
  96. projectionMatrix.elements[ 14 ] = clipPlane.w;
  97. // Render
  98. renderTarget.texture.encoding = renderer.outputEncoding;
  99. scope.visible = false;
  100. var currentRenderTarget = renderer.getRenderTarget();
  101. var currentXrEnabled = renderer.xr.enabled;
  102. var currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  103. renderer.xr.enabled = false; // Avoid camera modification
  104. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  105. renderer.setRenderTarget( renderTarget );
  106. renderer.state.buffers.depth.setMask( true ); // make sure the depth buffer is writable so it can be properly cleared, see #18897
  107. if ( renderer.autoClear === false ) renderer.clear();
  108. renderer.render( scene, virtualCamera );
  109. renderer.xr.enabled = currentXrEnabled;
  110. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  111. renderer.setRenderTarget( currentRenderTarget );
  112. // Restore viewport
  113. var viewport = camera.viewport;
  114. if ( viewport !== undefined ) {
  115. renderer.state.viewport( viewport );
  116. }
  117. scope.visible = true;
  118. };
  119. this.getRenderTarget = function () {
  120. return renderTarget;
  121. };
  122. };
  123. THREE.Reflector.prototype = Object.create( THREE.Mesh.prototype );
  124. THREE.Reflector.prototype.constructor = THREE.Reflector;
  125. THREE.Reflector.ReflectorShader = {
  126. uniforms: {
  127. 'color': {
  128. value: null
  129. },
  130. 'tDiffuse': {
  131. value: null
  132. },
  133. 'textureMatrix': {
  134. value: null
  135. }
  136. },
  137. vertexShader: [
  138. 'uniform mat4 textureMatrix;',
  139. 'varying vec4 vUv;',
  140. 'void main() {',
  141. ' vUv = textureMatrix * vec4( position, 1.0 );',
  142. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  143. '}'
  144. ].join( '\n' ),
  145. fragmentShader: [
  146. 'uniform vec3 color;',
  147. 'uniform sampler2D tDiffuse;',
  148. 'varying vec4 vUv;',
  149. 'float blendOverlay( float base, float blend ) {',
  150. ' return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );',
  151. '}',
  152. 'vec3 blendOverlay( vec3 base, vec3 blend ) {',
  153. ' return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );',
  154. '}',
  155. 'void main() {',
  156. ' vec4 base = texture2DProj( tDiffuse, vUv );',
  157. ' gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );',
  158. '}'
  159. ].join( '\n' )
  160. };