ReflectorForSSRPass.js 10 KB

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