Reflector.js 7.1 KB

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