Reflector.js 6.9 KB

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