Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. float SierpinskiTriangle2(float3 p)
  2. {
  3. float Scale = 2.0;
  4. float3 Offset = float3(1, 1, 1);
  5. int n = 0;
  6. while (n < 1) {
  7. if (p.x + p.y < 0)
  8. p.xy = -p.yx; // fold 1
  9. if (p.x + p.z < 0)
  10. p.xz = -p.zx; // fold 2
  11. if (p.y + p.z < 0)
  12. p.zy = -p.yz; // fold 3
  13. p = p * Scale - Offset * (Scale - 1.0);
  14. n++;
  15. }
  16. return (length(p)) * pow(Scale, -float(n));
  17. }
  18. ...
  19. float DistanceField(float3 p)
  20. {
  21. return sierpinski3d(p);
  22. }
  23. ...
  24. fixed4 RayMarch(float3 ro, float3 rd, float depth)
  25. {
  26. fixed4 result = fixed4(1,1,1,1);
  27. const int max_iteration = _MaxIterations;
  28. float t = 0; //distance travelled along the ray direction
  29.  
  30. for (int i = 0; i < max_iteration; i++)
  31. {
  32. if (t > _maxDistance || t >= depth)
  33. {
  34. result = fixed4(rd,0);
  35. break;
  36. }
  37.  
  38. float3 p = ro + rd * t;
  39. float d = DistanceField(p);
  40. if (d < _Accuracy)
  41. {
  42. float3 n = GetNormal(p);
  43. float3 s = Shading(p,n);
  44. result = fixed4(s,1);
  45. break;
  46. }
  47. t += d;
  48. }
  49. return result;
  50. }
  51. ...
  52. fixed4 frag(v2f i) : SV_Target
  53. {
  54. float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, i.uv).r);
  55. depth *= length(i.ray);
  56. fixed3 col = tex2D(_MainTex, i.uv);
  57. float3 rayDirection = normalize(i.ray.xyz);
  58. float3 rayOrigin = _WorldSpaceCameraPos;
  59. fixed4 result = RayMarch(rayOrigin, rayDirection, depth);
  60. return fixed4(col * (1.0 - result.w) + result.xyz * result.w,1.0);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement