Reflector.js 6.9 KB

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