Reflector.js 6.8 KB

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