Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float GetDist(float3 p)
- {
- float d = 1e10;
- //sdf shaped and stuff in here
- return d;
- }
- float3 GetNormal(float3 p)
- {
- float2 e = float2(1e-2, 0);
- float3 n = GetDist(p) - float3
- (
- GetDist(p - e.xyy),
- GetDist(p - e.yxy),
- GetDist(p - e.yyx)
- );
- return normalize(n);
- }
- float RayMarch(float3 ro, float3 rd)
- {
- float d0 = 0;
- float dS;
- for(int i = 0; i < _MaxSteps; i++)
- {
- float3 p = ro + d0 * rd;
- dS = GetDist(p);
- d0 += dS;
- if(dS <_SurfaceDistance || d0 > _MaxDistance)
- {
- break;
- }
- }
- return d0;
- }
- fixed4 frag(v2f i) : SV_Target
- {
- fixed4 col = 0;
- float3 ro = i.ro;
- float3 rd = normalize(i.hitPos - ro);
- float d = RayMarch(ro, rd);
- if(d >= _MaxDistance)
- {
- discard;
- }
- else
- {
- float3 p = ro + rd * d; //point/position
- float3 N = GetNormal(p); //normal
- float3 L = _WorldSpaceLightPos0.xyz; //direction from mesh to light
- float3 diffuseLight = dot(N, L) * _LightColor0.xyz;
- diffuseLight += _AmbientLight;
- //specular lighting/Bling-Phong
- float3 V = normalize(_WorldSpaceCameraPos - p); //view vector
- float3 H = normalize(L + V);
- float3 specularLight = saturate(dot(H, N));
- float specularExponent = exp2(_Gloss * 6) + 2; //2^Gloss... numbers from freya holmer
- specularLight = pow(specularLight, specularExponent); //specular exponent
- specularLight *= _LightColor0.xyz;
- //composite lights + colors
- col.rgb = diffuseLight * _Color + specularLight;
- }
- return col;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement