Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float SierpinskiTriangle2(float3 p)
- {
- float Scale = 2.0;
- float3 Offset = float3(1, 1, 1);
- int n = 0;
- while (n < 1) {
- if (p.x + p.y < 0)
- p.xy = -p.yx; // fold 1
- if (p.x + p.z < 0)
- p.xz = -p.zx; // fold 2
- if (p.y + p.z < 0)
- p.zy = -p.yz; // fold 3
- p = p * Scale - Offset * (Scale - 1.0);
- n++;
- }
- return (length(p)) * pow(Scale, -float(n));
- }
- ...
- float DistanceField(float3 p)
- {
- return sierpinski3d(p);
- }
- ...
- fixed4 RayMarch(float3 ro, float3 rd, float depth)
- {
- fixed4 result = fixed4(1,1,1,1);
- const int max_iteration = _MaxIterations;
- float t = 0; //distance travelled along the ray direction
- for (int i = 0; i < max_iteration; i++)
- {
- if (t > _maxDistance || t >= depth)
- {
- result = fixed4(rd,0);
- break;
- }
- float3 p = ro + rd * t;
- float d = DistanceField(p);
- if (d < _Accuracy)
- {
- float3 n = GetNormal(p);
- float3 s = Shading(p,n);
- result = fixed4(s,1);
- break;
- }
- t += d;
- }
- return result;
- }
- ...
- fixed4 frag(v2f i) : SV_Target
- {
- float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, i.uv).r);
- depth *= length(i.ray);
- fixed3 col = tex2D(_MainTex, i.uv);
- float3 rayDirection = normalize(i.ray.xyz);
- float3 rayOrigin = _WorldSpaceCameraPos;
- fixed4 result = RayMarch(rayOrigin, rayDirection, depth);
- return fixed4(col * (1.0 - result.w) + result.xyz * result.w,1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement