finite repeat operation + drop center point in sdf

derived it all on my own :D

to work correctly, center point as argument to the sdf needs to be removed. Instead, it can be set with by subtracting the position vector from the ray position
master
EmaMaker 2023-01-21 20:38:27 +01:00
parent c4b871ac09
commit c4050461e5
1 changed files with 40 additions and 38 deletions

View File

@ -38,13 +38,13 @@ phongdata phongBox2 = phongdata(vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, 1.0), vec3(0
phongdata phongLightBox = phongdata(lightColor, lightColor, lightColor, 256.0);
// START OF SDFs
float sdfSphere(in vec3 point, in vec3 center, float r)
float sdfSphere(in vec3 point, float r)
{
return length(point - center) - r;
return length(point) - r;
}
float sdfBox(in vec3 point, in vec3 center, in vec3 b){
vec3 q = abs(point - center) - b;
float sdfBox(in vec3 point, in vec3 b){
vec3 q = abs(point) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}
@ -81,47 +81,49 @@ phong opDifference( phong d1, phong d2 ) {
else return d2;
}
//works well if bounding box of object < replength
//reps is the number of times the pattern gets repeated on each axis in each direction (e.g. 1 -> 1 up and 1 down)
vec3 opFiniteRepeat(vec3 pos, vec3 start, vec3 reps, vec3 replength){
vec3 d = round((pos - start) / replength);
vec3 r = clamp(d, -reps, reps);
return start + r * replength;
}
// Repeat exactly reps times across each axis (e.g. 1 means ONLY one repeatition across the given axis, NOT 1 up and 1 down)
vec3 opFiniteRepeat2(in vec3 pos, in vec3 start, in vec3 reps, in vec3 replength){
vec3 m = mod(reps, 2); // 0 if even, 1 if odd
vec3 m1 = vec3(1.0) - m; //1 if even , 0 if odd
vec3 s = vec3(start+0.5*m1*replength);
vec3 d = round((pos-s) / replength);
vec3 r1 = (reps-m)*0.5;
vec3 r = clamp(d, -r1, r1 - m1 ); //m - vec3(1.0) should be the same;
return pos-s-r*replength;
}
vec3 s = vec3(0.0);
vec3 r = vec3(3.0, 5.0, 2.0);
vec3 rl = vec3(2.0, 1.0, 3.0);
vec3 l = r*rl*0.5;
vec3 rl = vec3(4.0, 2.0, 4.0);
phong sdfScene(in vec3 p){
return opUnion(
opUnion(
opUnion(
opUnion(
phong(phongSphere, sdfSphere(p, vec3(0.0), 1)),
phong(phongBox1, sdfBox(p, vec3(2.0, 0.0, 0.0), vec3(0.8)))
),
opUnion(
opDifference(
phong(phongBox1, sdfBox(p, vec3(-3.5, 3.5, 0.0), vec3(0.5))),
phong(phongSphere, sdfSphere(p, vec3(-4.0, 4.0, 0.0), 1))
return opUnion(
opSmoothUnion(
phong(phongSphere, sdfSphere(opFiniteRepeat2(p, vec3(0.0, sin(2*u_time), 0.0), r, rl), 0.4)),
phong(phongBox1,
sdfBox(opFiniteRepeat2(p, s, r, rl),
vec3(0.5, 0.1, 0.5)
)
),
opDifference(
phong(phongSphere, sdfSphere(p, vec3(-4.0, 8.0, 0.0), 0.75+ 0.25 * (1+sin(2*u_time)))),
phong(phongBox1, sdfBox(p, vec3(-4.0, 8.0, 0.0), vec3(0.8)))
)
)
0.4
),
opUnion(
opIntersection(
phong(phongSphere, sdfSphere(p, vec3(0.0, 4.0, 0.0), 1)),
phong(phongBox1, sdfBox(p, vec3(0.0, 4.5, 0.0), vec3(1)))
),
opSmoothUnion(
phong(phongBox2, sdfBox(p, vec3(0.0, 0.0, 6.0), vec3(3.0, 0.2, 3.0)) ),
phong(phongSphere, sdfSphere(p, vec3(0.0, 1.0+sin(u_time), 6.0), 1)),
0.5
)
opDifference(
phong(phongSphere, sdfSphere(p-vec3(0.0, 5.0, 0.0), 0.25 + 0.25* (1+sin(u_time)))),
phong(phongBox2, sdfBox(p-vec3(0.0, 5.0, 0.0), vec3(0.5)))
)
),
opUnion(
phong(phongBox2, sdfBox(p, vec3(5.0, 0.0, 0.0), vec3(2.0, 1.0, 0.5))),
phong(phongLightBox, sdfBox(p, lightPos, vec3(0.1)))
)
);
);
}
// END OF SDFs