Reflector.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. ( function () {
  2. class Reflector extends THREE.Mesh {
  3. constructor( geometry, options = {} ) {
  4. super( geometry );
  5. this.isReflector = true;
  6. this.type = 'Reflector';
  7. this.camera = new THREE.PerspectiveCamera();
  8. const scope = this;
  9. const color = options.color !== undefined ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
  10. const textureWidth = options.textureWidth || 512;
  11. const textureHeight = options.textureHeight || 512;
  12. const clipBias = options.clipBias || 0;
  13. const shader = options.shader || Reflector.ReflectorShader;
  14. const multisample = options.multisample !== undefined ? options.multisample : 4; //
  15. const reflectorPlane = new THREE.Plane();
  16. const normal = new THREE.Vector3();
  17. const reflectorWorldPosition = new THREE.Vector3();
  18. const cameraWorldPosition = new THREE.Vector3();
  19. const rotationMatrix = new THREE.Matrix4();
  20. const lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
  21. const clipPlane = new THREE.Vector4();
  22. const view = new THREE.Vector3();
  23. const target = new THREE.Vector3();
  24. const q = new THREE.Vector4();
  25. const textureMatrix = new THREE.Matrix4();
  26. const virtualCamera = this.camera;
  27. const renderTarget = new THREE.WebGLRenderTarget( textureWidth, textureHeight, {
  28. samples: multisample
  29. } );
  30. const material = new THREE.ShaderMaterial( {
  31. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  32. fragmentShader: shader.fragmentShader,
  33. vertexShader: shader.vertexShader
  34. } );
  35. material.uniforms[ 'tDiffuse' ].value = renderTarget.texture;
  36. material.uniforms[ 'color' ].value = color;
  37. material.uniforms[ 'textureMatrix' ].value = textureMatrix;
  38. this.material = material;
  39. this.onBeforeRender = function ( renderer, scene, camera ) {
  40. reflectorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  41. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  42. rotationMatrix.extractRotation( scope.matrixWorld );
  43. normal.set( 0, 0, 1 );
  44. normal.applyMatrix4( rotationMatrix );
  45. view.subVectors( reflectorWorldPosition, cameraWorldPosition ); // Avoid rendering when reflector is facing away
  46. if ( view.dot( normal ) > 0 ) return;
  47. view.reflect( normal ).negate();
  48. view.add( reflectorWorldPosition );
  49. rotationMatrix.extractRotation( camera.matrixWorld );
  50. lookAtPosition.set( 0, 0, - 1 );
  51. lookAtPosition.applyMatrix4( rotationMatrix );
  52. lookAtPosition.add( cameraWorldPosition );
  53. target.subVectors( reflectorWorldPosition, lookAtPosition );
  54. target.reflect( normal ).negate();
  55. target.add( reflectorWorldPosition );
  56. virtualCamera.position.copy( view );
  57. virtualCamera.up.set( 0, 1, 0 );
  58. virtualCamera.up.applyMatrix4( rotationMatrix );
  59. virtualCamera.up.reflect( normal );
  60. virtualCamera.lookAt( target );
  61. virtualCamera.far = camera.far; // Used in WebGLBackground
  62. virtualCamera.updateMatrixWorld();
  63. virtualCamera.projectionMatrix.copy( camera.projectionMatrix ); // Update the texture matrix
  64. 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 );
  65. textureMatrix.multiply( virtualCamera.projectionMatrix );
  66. textureMatrix.multiply( virtualCamera.matrixWorldInverse );
  67. textureMatrix.multiply( scope.matrixWorld ); // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  68. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  69. reflectorPlane.setFromNormalAndCoplanarPoint( normal, reflectorWorldPosition );
  70. reflectorPlane.applyMatrix4( virtualCamera.matrixWorldInverse );
  71. clipPlane.set( reflectorPlane.normal.x, reflectorPlane.normal.y, reflectorPlane.normal.z, reflectorPlane.constant );
  72. const projectionMatrix = virtualCamera.projectionMatrix;
  73. q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  74. q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  75. q.z = - 1.0;
  76. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ]; // Calculate the scaled plane vector
  77. clipPlane.multiplyScalar( 2.0 / clipPlane.dot( q ) ); // Replacing the third row of the projection matrix
  78. projectionMatrix.elements[ 2 ] = clipPlane.x;
  79. projectionMatrix.elements[ 6 ] = clipPlane.y;
  80. projectionMatrix.elements[ 10 ] = clipPlane.z + 1.0 - clipBias;
  81. projectionMatrix.elements[ 14 ] = clipPlane.w; // Render
  82. renderTarget.texture.encoding = renderer.outputEncoding;
  83. scope.visible = false;
  84. const currentRenderTarget = renderer.getRenderTarget();
  85. const currentXrEnabled = renderer.xr.enabled;
  86. const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  87. renderer.xr.enabled = false; // Avoid camera modification
  88. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  89. renderer.setRenderTarget( renderTarget );
  90. renderer.state.buffers.depth.setMask( true ); // make sure the depth buffer is writable so it can be properly cleared, see #18897
  91. if ( renderer.autoClear === false ) renderer.clear();
  92. renderer.render( scene, virtualCamera );
  93. renderer.xr.enabled = currentXrEnabled;
  94. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  95. renderer.setRenderTarget( currentRenderTarget ); // Restore viewport
  96. const viewport = camera.viewport;
  97. if ( viewport !== undefined ) {
  98. renderer.state.viewport( viewport );
  99. }
  100. scope.visible = true;
  101. };
  102. this.getRenderTarget = function () {
  103. return renderTarget;
  104. };
  105. this.dispose = function () {
  106. renderTarget.dispose();
  107. scope.material.dispose();
  108. };
  109. }
  110. }
  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. #include <encodings_fragment>
  153. }`
  154. };
  155. THREE.Reflector = Reflector;
  156. } )();