Reflector.js 7.0 KB

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