1
0

Reflector.js 6.9 KB

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