Reflector.js 6.9 KB

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