summaryrefslogtreecommitdiffstats
path: root/quilez.cginc
blob: 8deb61a39075b1d004de90760a4d25d213c132ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "pema99.cginc"

#ifndef __QUILEZ_INC
#define __QUILEZ_INC

// The MIT License
// Copyright © 2019-2024 Inigo Quilez
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

float distance_from_octahedron(in float3 p)
{
  float s = 1.0;
  float3 pp = abs(p);
  float m = pp.x+pp.y+pp.z-s;
  float3 q;
  if( 3.0*pp.x < m ) q = pp.xyz;
  else if( 3.0*pp.y < m ) q = pp.yzx;
  else if( 3.0*pp.z < m ) q = pp.zxy;
  else return m*0.57735027;

  float k = clamp(0.5*(q.z-q.y+s),0.0,s);
  return length(float3(q.x,q.y-s+k,q.z-k));
}

float distance_from_sphere(float3 p, float r)
{
    return length(p) - r;
}

float distance_from_torus(float3 p, float2 t)
{
  float2 q = float2(length(p.xz) - t.x, p.y);
  return length(q) - t.y;
}

float distance_from_capped_torus(float3 p, float2 sc, float ra, float rb)
{
  p.x = abs(p.x);
  float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy);
  return sqrt(dot(p,p) + ra*ra - 2.0*ra*k) - rb;
}

float distance_from_cut_sphere( in float3 p, in float r, in float h )
{
  float w = sqrt(r*r-h*h); // constant for a given shape

  float2 q = float2( length(p.xz), p.y );

  float s = max( (h-r)*q.x*q.x+w*w*(h+r-2.0*q.y), h*q.x-w*q.y );

  return (s<0.0) ? length(q)-r :
    (q.x<w) ? h - q.y     :
    length(q-float2(w,h));
}

float distance_from_cut_hollow_sphere( float3 p, float r, float h, float t )
{
  float2 q = float2( length(p.xz), p.y );

  float w = sqrt(r*r-h*h);

  return ((h*q.x<w*q.y) ? length(q-float2(w,h)) :
      abs(length(q)-r) ) - t;
}

float distance_from_box(float3 p, float3 b)
{
  float3 q = abs(p) - b;
  return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}

float distance_from_round_box(float3 p, float3 b, float r)
{
  float3 q = abs(p) - b + r;
  return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r;
}

float distance_from_box_frame(float3 p, float3 b, float e)
{
  p = abs(p)-b;
  float3 q = abs(p+e)-e;

  return min(min(
        length(max(float3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0),
        length(max(float3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)),
      length(max(float3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0));
}

float distance_from_pyramid(float3 p, float h)
{
  float m2 = h*h + 0.25;

  // symmetry
  p.xz = abs(p.xz); // do p=abs(p) instead for double pyramid
  p.xz = (p.z>p.x) ? p.zx : p.xz;
  p.xz -= 0.5;

  // project into face plane (2D)
  float3 q = float3( p.z, h*p.y-0.5*p.x, h*p.x+0.5*p.y);

  float s = max(-q.x,0.0);
  float t = clamp( (q.y-0.5*q.x)/(m2+0.25), 0.0, 1.0 );

  float a = m2*(q.x+s)*(q.x+s) + q.y*q.y;
  float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t);

  float d2 = max(-q.y,q.x*m2+q.y*0.5) < 0.0 ? 0.0 : min(a,b);

  // recover 3D and scale, and add sign
  return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));;
}

float distance_from_plane(float3 p, float3 n, float h)
{
  // n must be normalized
  return dot(p,n) + h;
}

float distance_from_cylinder(float3 p, float3 c)
{
  return length(p.xz-c.xy)-c.z;
}

float distance_from_capped_cylinder(float3 p, float h, float r)
{
  float2 d = abs(float2(length(p.xz),p.y)) - float2(r,h);
  return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}

float distance_from_hex_prism(float3 p, float2 h)
{
  float3 q = abs(p);

  const float3 k = float3(-0.8660254, 0.5, 0.57735);
  p = abs(p);
  p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy;
  float2 d = float2(
      length(p.xy - float2(clamp(p.x, -k.z*h.x, k.z*h.x), h.x))*sign(p.y - h.x),
      p.z-h.y );
  return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}

float distance_from_capsule(float3 p, float3 a, float3 b, float r)
{
  float3 pa = p - a, ba = b - a;
  float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
  return length( pa - ba*h ) - r;
}

// https://iquilezles.org/articles/ellipsoids/
float distance_from_ellipsoid(float3 p, float3 r)
{
    float k1 = length(p/r);
    float k2 = length(p/(r*r));
    return k1*(k1-1.0)/k2;
}

float3 op_rep(in float3 p, in float3 c)
{
  return glsl_mod(p+0.5*c,c)-0.5*c;
}

// compute d1 - d2
float op_sub(float d1, float d2)
{
  return max(d1,-d2);
}

// End licensed section

#endif  // __QUILEZ_INC