Reflector.js 7.2 KB

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