SSRShader.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import {
  2. Matrix4,
  3. Vector2
  4. } from '../../../build/three.module.js';
  5. /**
  6. * References:
  7. * https://lettier.github.io/3d-game-shaders-for-beginners/screen-space-reflection.html
  8. */
  9. var SSRShader = {
  10. defines: {
  11. MAX_STEP: 0,
  12. PERSPECTIVE_CAMERA: true,
  13. DISTANCE_ATTENUATION: true,
  14. FRESNEL: true,
  15. INFINITE_THICK: false,
  16. SELECTIVE: false,
  17. },
  18. uniforms: {
  19. 'tDiffuse': { value: null },
  20. 'tNormal': { value: null },
  21. 'tMetalness': { value: null },
  22. 'tDepth': { value: null },
  23. 'cameraNear': { value: null },
  24. 'cameraFar': { value: null },
  25. 'resolution': { value: new Vector2() },
  26. 'cameraProjectionMatrix': { value: new Matrix4() },
  27. 'cameraInverseProjectionMatrix': { value: new Matrix4() },
  28. 'opacity': { value: .5 },
  29. 'maxDistance': { value: 180 },
  30. 'cameraRange': { value: 0 },
  31. 'thickness': { value: .018 },
  32. },
  33. vertexShader: /* glsl */`
  34. varying vec2 vUv;
  35. void main() {
  36. vUv = uv;
  37. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  38. }
  39. `,
  40. fragmentShader: /* glsl */`
  41. // precision highp float;
  42. precision highp sampler2D;
  43. varying vec2 vUv;
  44. uniform sampler2D tDepth;
  45. uniform sampler2D tNormal;
  46. uniform sampler2D tMetalness;
  47. uniform sampler2D tDiffuse;
  48. uniform float cameraRange;
  49. uniform vec2 resolution;
  50. uniform float opacity;
  51. uniform float cameraNear;
  52. uniform float cameraFar;
  53. uniform float maxDistance;
  54. uniform float thickness;
  55. uniform mat4 cameraProjectionMatrix;
  56. uniform mat4 cameraInverseProjectionMatrix;
  57. #include <packing>
  58. float pointToLineDistance(vec3 x0, vec3 x1, vec3 x2) {
  59. //x0: point, x1: linePointA, x2: linePointB
  60. //https://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
  61. return length(cross(x0-x1,x0-x2))/length(x2-x1);
  62. }
  63. float pointPlaneDistance(vec3 point,vec3 planePoint,vec3 planeNormal){
  64. // https://mathworld.wolfram.com/Point-PlaneDistance.html
  65. //// https://en.wikipedia.org/wiki/Plane_(geometry)
  66. //// http://paulbourke.net/geometry/pointlineplane/
  67. float a=planeNormal.x,b=planeNormal.y,c=planeNormal.z;
  68. float x0=point.x,y0=point.y,z0=point.z;
  69. float x=planePoint.x,y=planePoint.y,z=planePoint.z;
  70. float d=-(a*x+b*y+c*z);
  71. float distance=(a*x0+b*y0+c*z0+d)/sqrt(a*a+b*b+c*c);
  72. return distance;
  73. }
  74. float getDepth( const in vec2 uv ) {
  75. return texture2D( tDepth, uv ).x;
  76. }
  77. float getViewZ( const in float depth ) {
  78. #ifdef PERSPECTIVE_CAMERA
  79. return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );
  80. #else
  81. return orthographicDepthToViewZ( depth, cameraNear, cameraFar );
  82. #endif
  83. }
  84. vec3 getViewPosition( const in vec2 uv, const in float depth/*clip space*/, const in float clipW ) {
  85. vec4 clipPosition = vec4( ( vec3( uv, depth ) - 0.5 ) * 2.0, 1.0 );//ndc
  86. clipPosition *= clipW; //clip
  87. return ( cameraInverseProjectionMatrix * clipPosition ).xyz;//view
  88. }
  89. vec3 getViewNormal( const in vec2 uv ) {
  90. return unpackRGBToNormal( texture2D( tNormal, uv ).xyz );
  91. }
  92. vec2 viewPositionToXY(vec3 viewPosition){
  93. vec2 xy;
  94. vec4 clip=cameraProjectionMatrix*vec4(viewPosition,1);
  95. xy=clip.xy;//clip
  96. float clipW=clip.w;
  97. xy/=clipW;//NDC
  98. xy=(xy+1.)/2.;//uv
  99. xy*=resolution;//screen
  100. return xy;
  101. }
  102. void main(){
  103. #ifdef SELECTIVE
  104. float metalness=texture2D(tMetalness,vUv).r;
  105. if(metalness==0.) return;
  106. #endif
  107. float depth = getDepth( vUv );
  108. float viewZ = getViewZ( depth );
  109. if(-viewZ>=cameraFar) return;
  110. float clipW = cameraProjectionMatrix[2][3] * viewZ+cameraProjectionMatrix[3][3];
  111. vec3 viewPosition=getViewPosition( vUv, depth, clipW );
  112. vec2 d0=gl_FragCoord.xy;
  113. vec2 d1;
  114. vec3 viewNormal=getViewNormal( vUv );
  115. #ifdef PERSPECTIVE_CAMERA
  116. vec3 viewIncidentDir=normalize(viewPosition);
  117. vec3 viewReflectDir=reflect(viewIncidentDir,viewNormal);
  118. #else
  119. vec3 viewIncidentDir=vec3(0,0,-1);
  120. vec3 viewReflectDir=reflect(viewIncidentDir,viewNormal);
  121. #endif
  122. float maxReflectRayLen=maxDistance/dot(-viewIncidentDir,viewNormal);
  123. // dot(a,b)==length(a)*length(b)*cos(theta) // https://www.mathsisfun.com/algebra/vectors-dot-product.html
  124. // if(a.isNormalized&&b.isNormalized) dot(a,b)==cos(theta)
  125. // maxDistance/maxReflectRayLen=cos(theta)
  126. // maxDistance/maxReflectRayLen==dot(a,b)
  127. // maxReflectRayLen==maxDistance/dot(a,b)
  128. vec3 d1viewPosition=viewPosition+viewReflectDir*maxReflectRayLen;
  129. #ifdef PERSPECTIVE_CAMERA
  130. if(d1viewPosition.z>-cameraNear){
  131. //https://tutorial.math.lamar.edu/Classes/CalcIII/EqnsOfLines.aspx
  132. float t=(-cameraNear-viewPosition.z)/viewReflectDir.z;
  133. d1viewPosition=viewPosition+viewReflectDir*t;
  134. }
  135. #endif
  136. d1=viewPositionToXY(d1viewPosition);
  137. float totalLen=length(d1-d0);
  138. float xLen=d1.x-d0.x;
  139. float yLen=d1.y-d0.y;
  140. float totalStep=max(abs(xLen),abs(yLen));
  141. float xSpan=xLen/totalStep;
  142. float ySpan=yLen/totalStep;
  143. for(float i=0.;i<float(MAX_STEP);i++){
  144. if(i>=totalStep) break;
  145. vec2 xy=vec2(d0.x+i*xSpan,d0.y+i*ySpan);
  146. if(xy.x<0.||xy.x>resolution.x||xy.y<0.||xy.y>resolution.y) break;
  147. float s=length(xy-d0)/totalLen;
  148. vec2 uv=xy/resolution;
  149. float d = getDepth(uv);
  150. float vZ = getViewZ( d );
  151. if(-vZ>=cameraFar) continue;
  152. float cW = cameraProjectionMatrix[2][3] * vZ+cameraProjectionMatrix[3][3];
  153. vec3 vP=getViewPosition( uv, d, cW );
  154. #ifdef PERSPECTIVE_CAMERA
  155. // https://www.comp.nus.edu.sg/~lowkl/publications/lowk_persp_interp_techrep.pdf
  156. float recipVPZ=1./viewPosition.z;
  157. float viewReflectRayZ=1./(recipVPZ+s*(1./d1viewPosition.z-recipVPZ));
  158. #else
  159. float viewReflectRayZ=viewPosition.z+s*(d1viewPosition.z-viewPosition.z);
  160. #endif
  161. if(viewReflectRayZ>vZ) continue;
  162. bool hit;
  163. #ifdef INFINITE_THICK
  164. hit=true;
  165. #else
  166. float away=pointToLineDistance(vP,viewPosition,d1viewPosition);
  167. float minThickness;
  168. vec2 xyNeighbor=xy;
  169. xyNeighbor.x+=1.;
  170. vec2 uvNeighbor=xyNeighbor/resolution;
  171. vec3 vPNeighbor=getViewPosition(uvNeighbor,d,cW);
  172. minThickness=vPNeighbor.x-vP.x;
  173. minThickness*=3.;
  174. float tk=max(minThickness,thickness);
  175. hit=away<=tk;
  176. #endif
  177. if(hit){
  178. vec3 vN=getViewNormal( uv );
  179. if(dot(viewReflectDir,vN)>=0.) continue;
  180. float distance=pointPlaneDistance(vP,viewPosition,viewNormal);
  181. if(distance>maxDistance) break;
  182. float op=opacity;
  183. #ifdef DISTANCE_ATTENUATION
  184. float ratio=1.-(distance/maxDistance);
  185. float attenuation=ratio*ratio;
  186. op=opacity*attenuation;
  187. #endif
  188. #ifdef FRESNEL
  189. float fresnelCoe=(dot(viewIncidentDir,viewReflectDir)+1.)/2.;
  190. op*=fresnelCoe;
  191. #endif
  192. vec4 reflectColor=texture2D(tDiffuse,uv);
  193. gl_FragColor.xyz=reflectColor.xyz;
  194. gl_FragColor.a=op;
  195. break;
  196. }
  197. }
  198. }
  199. `
  200. };
  201. var SSRDepthShader = {
  202. defines: {
  203. 'PERSPECTIVE_CAMERA': 1
  204. },
  205. uniforms: {
  206. 'tDepth': { value: null },
  207. 'cameraNear': { value: null },
  208. 'cameraFar': { value: null },
  209. },
  210. vertexShader: /* glsl */`
  211. varying vec2 vUv;
  212. void main() {
  213. vUv = uv;
  214. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  215. }
  216. `,
  217. fragmentShader: /* glsl */`
  218. uniform sampler2D tDepth;
  219. uniform float cameraNear;
  220. uniform float cameraFar;
  221. varying vec2 vUv;
  222. #include <packing>
  223. float getLinearDepth( const in vec2 uv ) {
  224. #if PERSPECTIVE_CAMERA == 1
  225. float fragCoordZ = texture2D( tDepth, uv ).x;
  226. float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
  227. return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
  228. #else
  229. return texture2D( tDepth, uv ).x;
  230. #endif
  231. }
  232. void main() {
  233. float depth = getLinearDepth( vUv );
  234. float d = 1.0 - depth;
  235. // d=(d-.999)*1000.;
  236. gl_FragColor = vec4( vec3( d ), 1.0 );
  237. }
  238. `
  239. };
  240. var SSRBlurShader = {
  241. uniforms: {
  242. 'tDiffuse': { value: null },
  243. 'resolution': { value: new Vector2() },
  244. 'opacity': { value: .5 },
  245. },
  246. vertexShader: /* glsl */`
  247. varying vec2 vUv;
  248. void main() {
  249. vUv = uv;
  250. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  251. }
  252. `,
  253. fragmentShader: /* glsl */`
  254. uniform sampler2D tDiffuse;
  255. uniform vec2 resolution;
  256. varying vec2 vUv;
  257. void main() {
  258. //reverse engineering from PhotoShop blur filter, then change coefficient
  259. vec2 texelSize = ( 1.0 / resolution );
  260. vec4 c=texture2D(tDiffuse,vUv);
  261. vec2 offset;
  262. offset=(vec2(-1,0))*texelSize;
  263. vec4 cl=texture2D(tDiffuse,vUv+offset);
  264. offset=(vec2(1,0))*texelSize;
  265. vec4 cr=texture2D(tDiffuse,vUv+offset);
  266. offset=(vec2(0,-1))*texelSize;
  267. vec4 cb=texture2D(tDiffuse,vUv+offset);
  268. offset=(vec2(0,1))*texelSize;
  269. vec4 ct=texture2D(tDiffuse,vUv+offset);
  270. // float coeCenter=.5;
  271. // float coeSide=.125;
  272. float coeCenter=.2;
  273. float coeSide=.2;
  274. float a=c.a*coeCenter+cl.a*coeSide+cr.a*coeSide+cb.a*coeSide+ct.a*coeSide;
  275. vec3 rgb=(c.rgb*c.a*coeCenter+cl.rgb*cl.a*coeSide+cr.rgb*cr.a*coeSide+cb.rgb*cb.a*coeSide+ct.rgb*ct.a*coeSide)/a;
  276. gl_FragColor=vec4(rgb,a);
  277. }
  278. `
  279. };
  280. export { SSRShader, SSRDepthShader, SSRBlurShader };