Advertisement
BeneFager

Untitled

Dec 16th, 2022
1,963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float GetDist(float3 p)
  2. {
  3.     float d = 1e10;
  4.     //sdf shaped and stuff in here
  5.     return d;
  6. }
  7.  
  8. float3 GetNormal(float3 p)
  9. {
  10.     float2 e = float2(1e-2, 0);
  11.     float3 n = GetDist(p) - float3
  12.     (
  13.     GetDist(p - e.xyy),
  14.     GetDist(p - e.yxy),
  15.     GetDist(p - e.yyx)
  16.     );
  17. return normalize(n);
  18. }
  19.  
  20. float RayMarch(float3 ro, float3 rd)
  21. {
  22.     float d0 = 0;
  23.     float dS;
  24.  
  25.     for(int i = 0; i < _MaxSteps; i++)
  26.     {
  27.         float3 p = ro + d0 * rd;
  28.         dS = GetDist(p);
  29.         d0 += dS;
  30.         if(dS <_SurfaceDistance || d0 > _MaxDistance)
  31.         {
  32.             break;
  33.         }
  34.     }
  35.     return d0;
  36. }
  37.  
  38. fixed4 frag(v2f i) : SV_Target
  39. {
  40.     fixed4 col = 0;
  41.  
  42.     float3 ro = i.ro;
  43.     float3 rd = normalize(i.hitPos - ro);
  44.  
  45.     float d = RayMarch(ro, rd);
  46.  
  47.     if(d >= _MaxDistance)
  48.     {
  49.         discard;
  50.     }
  51.     else
  52.     {
  53.         float3 p = ro + rd * d; //point/position
  54.         float3 N = GetNormal(p); //normal
  55.         float3 L = _WorldSpaceLightPos0.xyz; //direction from mesh to light
  56.  
  57.         float3 diffuseLight = dot(N, L) * _LightColor0.xyz;
  58.  
  59.         diffuseLight += _AmbientLight;
  60.  
  61.         //specular lighting/Bling-Phong
  62.         float3 V = normalize(_WorldSpaceCameraPos - p); //view vector
  63.         float3 H = normalize(L + V);
  64.         float3 specularLight = saturate(dot(H, N));
  65.  
  66.         float specularExponent = exp2(_Gloss * 6) + 2; //2^Gloss... numbers from freya holmer
  67.         specularLight = pow(specularLight, specularExponent); //specular exponent
  68.         specularLight *= _LightColor0.xyz;
  69.  
  70.         //composite lights + colors
  71.         col.rgb = diffuseLight * _Color + specularLight;
  72.     }
  73. return col;
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement