-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaymarchingUtils.cginc
More file actions
426 lines (361 loc) · 11.2 KB
/
RaymarchingUtils.cginc
File metadata and controls
426 lines (361 loc) · 11.2 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#ifndef RAYMARCHING_UTILS
#define RAYMARCHING_UTILS
#include "UnityCG.cginc"
#define PI 3.14159265358979323846
#define mod(x, y) (x - y * floor(x / y))
//DISTANCE FUNCTIONS from https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
float dot2( float2 v ) { return dot(v,v); }
float dot2( float3 v ) { return dot(v,v); }
float ndot( float2 a, float2 b ) { return a.x*b.x - a.y*b.y; }
float sdSphere( float3 p, float s )
{
return length(p)-s;
}
float sdBox( 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 sdRoundBox( float3 p, float3 b, float r )
{
float3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r;
}
float sdBoundingBox( 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 sdTorus( float3 p, float2 t )
{
float2 q = float2(length(p.xz)-t.x,p.y);
return length(q)-t.y;
}
float sdCappedTorus(float3 p, float2 sc, in float ra, in 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 sdLink( float3 p, float le, float r1, float r2 )
{
float3 q = float3( p.x, max(abs(p.y)-le,0.0), p.z );
return length(float2(length(q.xy)-r1,q.z)) - r2;
}
float sdCylinder( float3 p, float3 c )
{
return length(p.xz-c.xy)-c.z;
}
float sdCone( in float3 p, in float2 c, float h )
{
// c is the sin/cos of the angle, h is height
// Alternatively pass q instead of (c,h),
// which is the point at the base in 2D
float2 q = h*float2(c.x/c.y,-1.0);
float2 w = float2( length(p.xz), p.y );
float2 a = w - q*clamp( dot(w,q)/dot(q,q), 0.0, 1.0 );
float2 b = w - q*float2( clamp( w.x/q.x, 0.0, 1.0 ), 1.0 );
float k = sign( q.y );
float d = min(dot( a, a ),dot(b, b));
float s = max( k*(w.x*q.y-w.y*q.x),k*(w.y-q.y) );
return sqrt(d)*sign(s);
}
float sdConeInexact( float3 p, float2 c, float h )
{
float q = length(p.xz);
return max(dot(c.xy,float2(q,p.y)),-h-p.y);
}
float sdCone( float3 p, float2 c )
{
// c is the sin/cos of the angle
float2 q = float2( length(p.xz), -p.y );
float d = length(q-c*max(dot(q,c), 0.0));
return d * ((q.x*c.y-q.y*c.x<0.0)?-1.0:1.0);
}
float sdPlane( float3 p, float3 n, float h )
{
// n must be normalized
return dot(p,n) + h;
}
float sdHexPrism( float3 p, float2 h )
{
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 sdTriPrism( float3 p, float2 h )
{
float3 q = abs(p);
return max(q.z-h.y,max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5);
}
float sdCapsule( 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;
}
float sdVerticalCapsule( float3 p, float h, float r )
{
p.y -= clamp( p.y, 0.0, h );
return length( p ) - r;
}
float sdCappedCylinder( float3 p, float h, float r )
{
float2 d = abs(float2(length(p.xz),p.y)) - float2(h,r);
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}
float sdCappedCylinder(float3 p, float3 a, float3 b, float r)
{
float3 ba = b - a;
float3 pa = p - a;
float baba = dot(ba,ba);
float paba = dot(pa,ba);
float x = length(pa*baba-ba*paba) - r*baba;
float y = abs(paba-baba*0.5)-baba*0.5;
float x2 = x*x;
float y2 = y*y*baba;
float d = (max(x,y)<0.0)?-min(x2,y2):(((x>0.0)?x2:0.0)+((y>0.0)?y2:0.0));
return sign(d)*sqrt(abs(d))/baba;
}
float sdRoundedCylinder( float3 p, float ra, float rb, float h )
{
float2 d = float2( length(p.xz)-2.0*ra+rb, abs(p.y) - h );
return min(max(d.x,d.y),0.0) + length(max(d,0.0)) - rb;
}
float sdCappedCone( float3 p, float h, float r1, float r2 )
{
float2 q = float2( length(p.xz), p.y );
float2 k1 = float2(r2,h);
float2 k2 = float2(r2-r1,2.0*h);
float2 ca = float2(q.x-min(q.x,(q.y<0.0)?r1:r2), abs(q.y)-h);
float2 cb = q - k1 + k2*clamp( dot(k1-q,k2)/dot2(k2), 0.0, 1.0 );
float s = (cb.x<0.0 && ca.y<0.0) ? -1.0 : 1.0;
return s*sqrt( min(dot2(ca),dot2(cb)) );
}
float sdCappedCone(float3 p, float3 a, float3 b, float ra, float rb)
{
float rba = rb-ra;
float baba = dot(b-a,b-a);
float papa = dot(p-a,p-a);
float paba = dot(p-a,b-a)/baba;
float x = sqrt( papa - paba*paba*baba );
float cax = max(0.0,x-((paba<0.5)?ra:rb));
float cay = abs(paba-0.5)-0.5;
float k = rba*rba + baba;
float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 );
float cbx = x-ra - f*rba;
float cby = paba - f;
float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(cax*cax + cay*cay*baba,
cbx*cbx + cby*cby*baba) );
}
float sdSolidAngle(float3 p, float2 c, float ra)
{
// c is the sin/cos of the angle
float2 q = float2( length(p.xz), p.y );
float l = length(q) - ra;
float m = length(q - c*clamp(dot(q,c),0.0,ra) );
return max(l,m*sign(c.y*q.x-c.x*q.y));
}
float sdRoundCone( float3 p, float r1, float r2, float h )
{
float2 q = float2( length(p.xz), p.y );
float b = (r1-r2)/h;
float a = sqrt(1.0-b*b);
float k = dot(q,float2(-b,a));
if( k < 0.0 ) return length(q) - r1;
if( k > a*h ) return length(q-float2(0.0,h)) - r2;
return dot(q, float2(a,b) ) - r1;
}
float sdRoundCone(float3 p, float3 a, float3 b, float r1, float r2)
{
// sampling independent computations (only depend on shape)
float3 ba = b - a;
float l2 = dot(ba,ba);
float rr = r1 - r2;
float a2 = l2 - rr*rr;
float il2 = 1.0/l2;
// sampling dependant computations
float3 pa = p - a;
float y = dot(pa,ba);
float z = y - l2;
float x2 = dot2( pa*l2 - ba*y );
float y2 = y*y*l2;
float z2 = z*z*l2;
// single square root!
float k = sign(rr)*rr*rr*x2;
if( sign(z)*a2*z2 > k ) return sqrt(x2 + z2) *il2 - r2;
if( sign(y)*a2*y2 < k ) return sqrt(x2 + y2) *il2 - r1;
return (sqrt(x2*a2*il2)+y*rr)*il2 - r1;
}
float sdEllipsoid( float3 p, float3 r )
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
float sdRhombus(float3 p, float la, float lb, float h, float ra)
{
p = abs(p);
float2 b = float2(la,lb);
float f = clamp( (ndot(b,b-2.0*p.xz))/dot(b,b), -1.0, 1.0 );
float2 q = float2(length(p.xz-0.5*b*float2(1.0-f,1.0+f))*sign(p.x*b.y+p.z*b.x-b.x*b.y)-ra, p.y-h);
return min(max(q.x,q.y),0.0) + length(max(q,0.0));
}
float sdOctahedron( float3 p, float s)
{
p = abs(p);
float m = p.x+p.y+p.z-s;
float3 q;
if( 3.0*p.x < m ) q = p.xyz;
else if( 3.0*p.y < m ) q = p.yzx;
else if( 3.0*p.z < m ) q = p.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 sdOctahedronBound( float3 p, float s)
{
p = abs(p);
return (p.x+p.y+p.z-s)*0.57735027;
}
float sdPyramid( float3 p, float h)
{
float m2 = h*h + 0.25;
p.xz = abs(p.xz);
p.xz = (p.z>p.x) ? p.zx : p.xz;
p.xz -= 0.5;
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*p.z)/(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 = min(q.y,-q.x*m2-q.y*0.5) > 0.0 ? 0.0 : min(a,b);
return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));
}
float udTriangle( float3 p, float3 a, float3 b, float3 c )
{
float3 ba = b - a; float3 pa = p - a;
float3 cb = c - b; float3 pb = p - b;
float3 ac = a - c; float3 pc = p - c;
float3 nor = cross( ba, ac );
return sqrt(
(sign(dot(cross(ba,nor),pa)) +
sign(dot(cross(cb,nor),pb)) +
sign(dot(cross(ac,nor),pc))<2.0)
?
min( min(
dot2(ba*clamp(dot(ba,pa)/dot2(ba),0.0,1.0)-pa),
dot2(cb*clamp(dot(cb,pb)/dot2(cb),0.0,1.0)-pb) ),
dot2(ac*clamp(dot(ac,pc)/dot2(ac),0.0,1.0)-pc) )
:
dot(nor,pa)*dot(nor,pa)/dot2(nor) );
}
float udQuad( float3 p, float3 a, float3 b, float3 c, float3 d )
{
float3 ba = b - a; float3 pa = p - a;
float3 cb = c - b; float3 pb = p - b;
float3 dc = d - c; float3 pc = p - c;
float3 ad = a - d; float3 pd = p - d;
float3 nor = cross( ba, ad );
return sqrt(
(sign(dot(cross(ba,nor),pa)) +
sign(dot(cross(cb,nor),pb)) +
sign(dot(cross(dc,nor),pc)) +
sign(dot(cross(ad,nor),pd))<3.0)
?
min( min( min(
dot2(ba*clamp(dot(ba,pa)/dot2(ba),0.0,1.0)-pa),
dot2(cb*clamp(dot(cb,pb)/dot2(cb),0.0,1.0)-pb) ),
dot2(dc*clamp(dot(dc,pc)/dot2(dc),0.0,1.0)-pc) ),
dot2(ad*clamp(dot(ad,pd)/dot2(ad),0.0,1.0)-pd) )
:
dot(nor,pa)*dot(nor,pa)/dot2(nor) );
}
//END OF DISTANCE FUNCTIONS
float opRep( in float3 p, in float3 c )
{
return mod(p+0.5*c,c)-0.5*c;
}
float2 SphereUV(float3 n){
return float2( 0.5+(atan2(n.x,n.z)/(2*PI)) , 0.5-(asin(n.y)/PI) );
}
float CheckerBoard(float2 uv,float scale){
return float( (floor(uv.x*scale)%2==0) ^ ((floor(uv.y*scale)+1)%2==0) );
}
inline float3 GetCameraForward() { return -UNITY_MATRIX_V[2].xyz; }
float3x3 rotationMatrix3(float3 v, float angle)
{
float c = cos(angle);
float s = sin(angle);
return float3x3(c + (1.0 - c) * v.x * v.x, (1.0 - c) * v.x * v.y - s * v.z, (1.0 - c) * v.x * v.z + s * v.y,
(1.0 - c) * v.x * v.y + s * v.z, c + (1.0 - c) * v.y * v.y, (1.0 - c) * v.y * v.z - s * v.x,
(1.0 - c) * v.x * v.z - s * v.y, (1.0 - c) * v.y * v.z + s * v.x, c + (1.0 - c) * v.z * v.z
);
}
float2 rot(float2 uv,float a){
return float2(uv.x*cos(a)-uv.y*sin(a),uv.y*cos(a)+uv.x*sin(a));
}
float LivingKIFS(float3 p) { //based on https://www.shadertoy.com/view/MdS3zm
float3 RotV=float3(0.5,-0.05,-0.5);
float RotAngle=145.;
float Scale=1.27;
float Julia=float3(-2.,-1.5,-.5);
int Iterations = 16;
p=p.zxy;
float3x3 rot = rotationMatrix3(normalize(RotV), RotAngle);
float3 pp=p;
float l;
for (int i=0; i<Iterations; i++) {
p.xy=abs(p.xy);
p=p*Scale+Julia;
p = mul(rot,p);
l=length(p);
}
return l*pow(Scale, -float(Iterations))*.9;
}
float mandelbulb(float3 pos) {
float Power = 7;
float3 z = pos;
float dr = 1.0;
float r = 0.0;
for (int i = 0; i < 16 ; i++) {
r = length(z);
if (r>1.5) break;
float theta = acos(z.z/r);
float phi = atan2(z.y,z.x);
dr = pow( r, Power-1.0)*Power*dr + 1.0;
float zr = pow( r,Power);
theta = theta*Power;
phi = phi*Power;
z = zr*float3(sin(theta)*cos(phi), sin(phi)*sin(theta), cos(theta));
z+=pos;
}
return 0.5*log(r)*r/dr;
}
#define samplerfunction(uv) noise(uv)
float4 boxmap(in float3 p, in float3 n, in float k )
{
float4 x = samplerfunction( p.yz );
float4 y = samplerfunction( p.zx );
float4 z = samplerfunction( p.xy );
float3 w = pow( abs(n), k );
return (x*w.x + y*w.y + z*w.z) / (w.x + w.y + w.z);
}
float4 inverseStereographic(float3 p) {
float k = 2.0/(1.0+dot(p,p));
return float4(k*p,k-1.0);
}
float3 stereographic(float4 p4) {
float k = 1.0/(1.0+p4.w);
return k*p4.xyz;
}
#endif