Reflector.js 6.6 KB

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