Reflector.js 6.9 KB

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