2
0

SSRShader.js 8.9 KB

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