ReflectorForSSRPass.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import {
  2. Color,
  3. Matrix4,
  4. Mesh,
  5. PerspectiveCamera,
  6. ShaderMaterial,
  7. UniformsUtils,
  8. Vector2,
  9. Vector3,
  10. WebGLRenderTarget,
  11. DepthTexture,
  12. UnsignedShortType,
  13. NearestFilter,
  14. Plane
  15. } from 'three';
  16. class ReflectorForSSRPass extends Mesh {
  17. constructor( geometry, options = {} ) {
  18. super( geometry );
  19. this.type = 'ReflectorForSSRPass';
  20. const scope = this;
  21. const color = ( options.color !== undefined ) ? new Color( options.color ) : new Color( 0x7F7F7F );
  22. const textureWidth = options.textureWidth || 512;
  23. const textureHeight = options.textureHeight || 512;
  24. const clipBias = options.clipBias || 0;
  25. const shader = options.shader || ReflectorForSSRPass.ReflectorShader;
  26. const useDepthTexture = options.useDepthTexture === true;
  27. const yAxis = new Vector3( 0, 1, 0 );
  28. const vecTemp0 = new Vector3();
  29. const vecTemp1 = new Vector3();
  30. //
  31. scope.needsUpdate = false;
  32. scope.maxDistance = ReflectorForSSRPass.ReflectorShader.uniforms.maxDistance.value;
  33. scope.opacity = ReflectorForSSRPass.ReflectorShader.uniforms.opacity.value;
  34. scope.color = color;
  35. scope.resolution = options.resolution || new Vector2( window.innerWidth, window.innerHeight );
  36. scope._distanceAttenuation = ReflectorForSSRPass.ReflectorShader.defines.DISTANCE_ATTENUATION;
  37. Object.defineProperty( scope, 'distanceAttenuation', {
  38. get() {
  39. return scope._distanceAttenuation;
  40. },
  41. set( val ) {
  42. if ( scope._distanceAttenuation === val ) return;
  43. scope._distanceAttenuation = val;
  44. scope.material.defines.DISTANCE_ATTENUATION = val;
  45. scope.material.needsUpdate = true;
  46. }
  47. } );
  48. scope._fresnel = ReflectorForSSRPass.ReflectorShader.defines.FRESNEL;
  49. Object.defineProperty( scope, 'fresnel', {
  50. get() {
  51. return scope._fresnel;
  52. },
  53. set( val ) {
  54. if ( scope._fresnel === val ) return;
  55. scope._fresnel = val;
  56. scope.material.defines.FRESNEL = val;
  57. scope.material.needsUpdate = true;
  58. }
  59. } );
  60. const normal = new Vector3();
  61. const reflectorWorldPosition = new Vector3();
  62. const cameraWorldPosition = new Vector3();
  63. const rotationMatrix = new Matrix4();
  64. const lookAtPosition = new Vector3( 0, 0, - 1 );
  65. const view = new Vector3();
  66. const target = new Vector3();
  67. const textureMatrix = new Matrix4();
  68. const virtualCamera = new PerspectiveCamera();
  69. let depthTexture;
  70. if ( useDepthTexture ) {
  71. depthTexture = new DepthTexture();
  72. depthTexture.type = UnsignedShortType;
  73. depthTexture.minFilter = NearestFilter;
  74. depthTexture.magFilter = NearestFilter;
  75. }
  76. const parameters = {
  77. depthTexture: useDepthTexture ? depthTexture : null,
  78. };
  79. const renderTarget = new WebGLRenderTarget( textureWidth, textureHeight, parameters );
  80. const material = new ShaderMaterial( {
  81. transparent: useDepthTexture,
  82. defines: Object.assign( {}, ReflectorForSSRPass.ReflectorShader.defines, {
  83. useDepthTexture
  84. } ),
  85. uniforms: UniformsUtils.clone( shader.uniforms ),
  86. fragmentShader: shader.fragmentShader,
  87. vertexShader: shader.vertexShader
  88. } );
  89. material.uniforms[ 'tDiffuse' ].value = renderTarget.texture;
  90. material.uniforms[ 'color' ].value = scope.color;
  91. material.uniforms[ 'textureMatrix' ].value = textureMatrix;
  92. if ( useDepthTexture ) {
  93. material.uniforms[ 'tDepth' ].value = renderTarget.depthTexture;
  94. }
  95. this.material = material;
  96. const globalPlane = new Plane( new Vector3( 0, 1, 0 ), clipBias );
  97. const globalPlanes = [ globalPlane ];
  98. this.doRender = function ( renderer, scene, camera ) {
  99. material.uniforms[ 'maxDistance' ].value = scope.maxDistance;
  100. material.uniforms[ 'color' ].value = scope.color;
  101. material.uniforms[ 'opacity' ].value = scope.opacity;
  102. vecTemp0.copy( camera.position ).normalize();
  103. vecTemp1.copy( vecTemp0 ).reflect( yAxis );
  104. material.uniforms[ 'fresnelCoe' ].value = ( vecTemp0.dot( vecTemp1 ) + 1. ) / 2.; // TODO: Also need to use glsl viewPosition and viewNormal per pixel.
  105. reflectorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  106. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  107. rotationMatrix.extractRotation( scope.matrixWorld );
  108. normal.set( 0, 0, 1 );
  109. normal.applyMatrix4( rotationMatrix );
  110. view.subVectors( reflectorWorldPosition, cameraWorldPosition );
  111. // Avoid rendering when reflector is facing away
  112. if ( view.dot( normal ) > 0 ) return;
  113. view.reflect( normal ).negate();
  114. view.add( reflectorWorldPosition );
  115. rotationMatrix.extractRotation( camera.matrixWorld );
  116. lookAtPosition.set( 0, 0, - 1 );
  117. lookAtPosition.applyMatrix4( rotationMatrix );
  118. lookAtPosition.add( cameraWorldPosition );
  119. target.subVectors( reflectorWorldPosition, lookAtPosition );
  120. target.reflect( normal ).negate();
  121. target.add( reflectorWorldPosition );
  122. virtualCamera.position.copy( view );
  123. virtualCamera.up.set( 0, 1, 0 );
  124. virtualCamera.up.applyMatrix4( rotationMatrix );
  125. virtualCamera.up.reflect( normal );
  126. virtualCamera.lookAt( target );
  127. virtualCamera.far = camera.far; // Used in WebGLBackground
  128. virtualCamera.updateMatrixWorld();
  129. virtualCamera.projectionMatrix.copy( camera.projectionMatrix );
  130. material.uniforms[ 'virtualCameraNear' ].value = camera.near;
  131. material.uniforms[ 'virtualCameraFar' ].value = camera.far;
  132. material.uniforms[ 'virtualCameraMatrixWorld' ].value = virtualCamera.matrixWorld;
  133. material.uniforms[ 'virtualCameraProjectionMatrix' ].value = camera.projectionMatrix;
  134. material.uniforms[ 'virtualCameraProjectionMatrixInverse' ].value = camera.projectionMatrixInverse;
  135. material.uniforms[ 'resolution' ].value = scope.resolution;
  136. // Update the texture matrix
  137. textureMatrix.set(
  138. 0.5, 0.0, 0.0, 0.5,
  139. 0.0, 0.5, 0.0, 0.5,
  140. 0.0, 0.0, 0.5, 0.5,
  141. 0.0, 0.0, 0.0, 1.0
  142. );
  143. textureMatrix.multiply( virtualCamera.projectionMatrix );
  144. textureMatrix.multiply( virtualCamera.matrixWorldInverse );
  145. textureMatrix.multiply( scope.matrixWorld );
  146. // Render
  147. renderTarget.texture.encoding = renderer.outputEncoding;
  148. // scope.visible = false;
  149. const currentRenderTarget = renderer.getRenderTarget();
  150. const currentXrEnabled = renderer.xr.enabled;
  151. const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  152. const currentClippingPlanes = renderer.clippingPlanes;
  153. renderer.xr.enabled = false; // Avoid camera modification
  154. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  155. renderer.clippingPlanes = globalPlanes;
  156. renderer.setRenderTarget( renderTarget );
  157. renderer.state.buffers.depth.setMask( true ); // make sure the depth buffer is writable so it can be properly cleared, see #18897
  158. if ( renderer.autoClear === false ) renderer.clear();
  159. renderer.render( scene, virtualCamera );
  160. renderer.xr.enabled = currentXrEnabled;
  161. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  162. renderer.clippingPlanes = currentClippingPlanes;
  163. renderer.setRenderTarget( currentRenderTarget );
  164. // Restore viewport
  165. const viewport = camera.viewport;
  166. if ( viewport !== undefined ) {
  167. renderer.state.viewport( viewport );
  168. }
  169. // scope.visible = true;
  170. };
  171. this.getRenderTarget = function () {
  172. return renderTarget;
  173. };
  174. }
  175. }
  176. ReflectorForSSRPass.prototype.isReflectorForSSRPass = true;
  177. ReflectorForSSRPass.ReflectorShader = {
  178. defines: {
  179. DISTANCE_ATTENUATION: true,
  180. FRESNEL: true,
  181. },
  182. uniforms: {
  183. color: { value: null },
  184. tDiffuse: { value: null },
  185. tDepth: { value: null },
  186. textureMatrix: { value: new Matrix4() },
  187. maxDistance: { value: 180 },
  188. opacity: { value: 0.5 },
  189. fresnelCoe: { value: null },
  190. virtualCameraNear: { value: null },
  191. virtualCameraFar: { value: null },
  192. virtualCameraProjectionMatrix: { value: new Matrix4() },
  193. virtualCameraMatrixWorld: { value: new Matrix4() },
  194. virtualCameraProjectionMatrixInverse: { value: new Matrix4() },
  195. resolution: { value: new Vector2() },
  196. },
  197. vertexShader: /* glsl */`
  198. uniform mat4 textureMatrix;
  199. varying vec4 vUv;
  200. void main() {
  201. vUv = textureMatrix * vec4( position, 1.0 );
  202. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  203. }`,
  204. fragmentShader: /* glsl */`
  205. uniform vec3 color;
  206. uniform sampler2D tDiffuse;
  207. uniform sampler2D tDepth;
  208. uniform float maxDistance;
  209. uniform float opacity;
  210. uniform float fresnelCoe;
  211. uniform float virtualCameraNear;
  212. uniform float virtualCameraFar;
  213. uniform mat4 virtualCameraProjectionMatrix;
  214. uniform mat4 virtualCameraProjectionMatrixInverse;
  215. uniform mat4 virtualCameraMatrixWorld;
  216. uniform vec2 resolution;
  217. varying vec4 vUv;
  218. #include <packing>
  219. float blendOverlay( float base, float blend ) {
  220. return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );
  221. }
  222. vec3 blendOverlay( vec3 base, vec3 blend ) {
  223. return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );
  224. }
  225. float getDepth( const in vec2 uv ) {
  226. return texture2D( tDepth, uv ).x;
  227. }
  228. float getViewZ( const in float depth ) {
  229. return perspectiveDepthToViewZ( depth, virtualCameraNear, virtualCameraFar );
  230. }
  231. vec3 getViewPosition( const in vec2 uv, const in float depth/*clip space*/, const in float clipW ) {
  232. vec4 clipPosition = vec4( ( vec3( uv, depth ) - 0.5 ) * 2.0, 1.0 );//ndc
  233. clipPosition *= clipW; //clip
  234. return ( virtualCameraProjectionMatrixInverse * clipPosition ).xyz;//view
  235. }
  236. void main() {
  237. vec4 base = texture2DProj( tDiffuse, vUv );
  238. #ifdef useDepthTexture
  239. vec2 uv=(gl_FragCoord.xy-.5)/resolution.xy;
  240. uv.x=1.-uv.x;
  241. float depth = texture2DProj( tDepth, vUv ).r;
  242. float viewZ = getViewZ( depth );
  243. float clipW = virtualCameraProjectionMatrix[2][3] * viewZ+virtualCameraProjectionMatrix[3][3];
  244. vec3 viewPosition=getViewPosition( uv, depth, clipW );
  245. vec3 worldPosition=(virtualCameraMatrixWorld*vec4(viewPosition,1)).xyz;
  246. if(worldPosition.y>maxDistance) discard;
  247. float op=opacity;
  248. #ifdef DISTANCE_ATTENUATION
  249. float ratio=1.-(worldPosition.y/maxDistance);
  250. float attenuation=ratio*ratio;
  251. op=opacity*attenuation;
  252. #endif
  253. #ifdef FRESNEL
  254. op*=fresnelCoe;
  255. #endif
  256. gl_FragColor = vec4( blendOverlay( base.rgb, color ), op );
  257. #else
  258. gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );
  259. #endif
  260. }
  261. `,
  262. };
  263. export { ReflectorForSSRPass };