Reflector.js 6.8 KB

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