Reflector.js 6.8 KB

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