Reflector.js 6.9 KB

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