Reflector.js 6.6 KB

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