ReflectorForSSRPass.js 10 KB

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