iq_sdf.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // References:
  3. // Modeling with distance functions
  4. // http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
  5. //
  6. // primitives
  7. float sdSphere(vec3 _pos, float _radius)
  8. {
  9. return length(_pos) - _radius;
  10. }
  11. float udBox(vec3 _pos, vec3 _extents)
  12. {
  13. return length(max(abs(_pos) - _extents, 0.0) );
  14. }
  15. float udRoundBox(vec3 _pos, vec3 _extents, float r)
  16. {
  17. return length(max(abs(_pos) - _extents, 0.0) ) - r;
  18. }
  19. float sdBox(vec3 _pos, vec3 _extents)
  20. {
  21. vec3 d = abs(_pos) - _extents;
  22. return min(max(d.x, max(d.y, d.z) ), 0.0) +
  23. length(max(d, 0.0) );
  24. }
  25. float sdTorus(vec3 _pos, vec2 t)
  26. {
  27. vec2 q = vec2(length(_pos.xz) - t.x, _pos.y);
  28. return length(q) - t.y;
  29. }
  30. float sdCylinder(vec3 _pos, vec3 c)
  31. {
  32. return length(_pos.xz - c.xy) - c.z;
  33. }
  34. float sdCone(vec3 _pos, vec2 c)
  35. {
  36. // c must be normalized
  37. float q = length(_pos.xy);
  38. return dot(c, vec2(q, _pos.z) );
  39. }
  40. float sdPlane(vec3 _pos, vec4 n)
  41. {
  42. // n must be normalized
  43. return dot(_pos, n.xyz) + n.w;
  44. }
  45. float sdHexPrism(vec3 _pos, vec2 h)
  46. {
  47. vec3 q = abs(_pos);
  48. return max(q.z - h.y, max(q.x + q.y * 0.57735, q.y * 1.1547) - h.x);
  49. }
  50. float sdTriPrism(vec3 _pos, vec2 h)
  51. {
  52. vec3 q = abs(_pos);
  53. return max(q.z - h.y, max(q.x * 0.866025 + _pos.y * 0.5, -_pos.y) - h.x * 0.5);
  54. }
  55. // domain operations
  56. float opUnion(float d1, float d2)
  57. {
  58. return min(d1, d2);
  59. }
  60. float opSubtract(float d1, float d2)
  61. {
  62. return max(-d1, d2);
  63. }
  64. float opIntersect(float d1, float d2)
  65. {
  66. return max(d1, d2);
  67. }