Reflector.js 6.7 KB

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