Taw Ɛandna primitives snaƐna bihom simple scene,
donc el objective el jéy houwa ano nreddouha complex. El SDF operators ysahloulna el ħkéya hedhi.
El operators houma functions n'appliquew'hom Ɛal SDFs.
(Bech nfasrou quelques SDFs w naƐmlou examples m'el lista hedhi)
"Union" yaƐni 2 SDF objects ywalliw object weħed.
float opUnion( float sdf1, float sdf2 )
{
return min (sdf1,sdf2);
}
Deja estaƐmelneha f'el example hedha:
float boxDistance = sdBox (boxP, boxExtent);
// ...
float sphereDistance = sdSphere (sphereP, 1.0);
// ...
float finalDistance = min (boxDistance, sphereDistance);
Kima tchouf houni nestaƐmlou fi
bech nzidouhom l'nafs el scene.
Bech n'simplifiw chwayya, nħottou just 2 spheres f'el scene.
vec4 sceneSDF (vec3 p){
// Sphere 1
vec3 sphereOneP = vec3 (p.x + 0.5, p.y, p.z);
float sphereOneDistance = sdSphere (sphereOneP , 0.6);
vec3 sphereOneColor = vec3 (0.15, 0.19, 0.7);
// Sphere 2
vec3 sphereTwoP = vec3 (p.x - 0.5, p.y, p.z);
float sphereTwoDistance = sdSphere (sphereTwoP , 0.4);
vec3 sphereTwoColor = vec3 (0.6, 0.1, 0.1);
float finalDistance = min (sphereOneDistance , sphereTwoDistance );
vec3 finalColor = vec3 (0.0);
finalColor = vec3 (0.0);
if (sphereOneDistance < sphereTwoDistance ){
finalColor = sphereOneColor ;
}
else {
finalColor = sphereTwoColor ;
}
return vec4 (finalColor , finalDistance );
}
Taw nħebbou n'ħottouhom yodkhlou fi bƐadh'hom donc nestaƐmlou "Smooth Union" fi Ɛoudh "Union" Ɛadiyya.
Note:
w
houma nafs el ħaja.
float opSmoothUnion (float d1, float d2, float k){
float h = clamp (0.5 + 0.5 * (d2 - d1) / k, 0.0, 1.0);
return mix (d2, d1, h) - k * h * (1.0 - h);
}
Taw s'ħiih el balls yodkhlou fi bƐadh'hom, ama el alwén mq̈asmiin. Fel cas mteƐna n'settiw el color ħasb el distance
if (sphereOneDistance < sphereTwoDistance ){
finalColor = sphereOneColor ;
}
else {
finalColor = sphereTwoColor ;
}
Ennajmou nestaƐmlou zeda el
bech naƐrfou win touq̈ef el value.
finalColor = step (sphereOneDistance , sphereTwoDistance ) * vec3 (sphereOneColor );
finalColor += step (sphereTwoDistance , sphereOneDistance ) * vec3 (sphereTwoColor );
Taw ken nħebbou n'mixiw el coleurét eli binet'ħom, nestaƐmlou
.
float smoothness = abs (sin (iTime ));
float smoothDistance = opSmoothUnion (sphereOneDistance , sphereTwoDistance , smoothness );
// Blend colors ħasb el smoothness
float smoothnessMask = smoothstep (-smoothness , smoothness , sphereOneDistance - sphereTwoDistance );
vec3 finalColor = mix (sphereOneColor , sphereTwoColor , smoothnessMask );
Kima famma Union, famma Substraction (tetsamma zeda "bool/boolean"). Hedhi tkhallina ennaħħiw tarf m'el object ħasb object ekher.
float opSubtraction (float d1, float d2){
return max (-d1, d2);
}
W hedha el code mtaƐ el SDF scene eli yestaƐmel el substraction:
vec4 sceneSDF (vec3 p){
// Sphere 1
vec3 sphereOneP = vec3 (p);
float sphereOneDistance = sdSphere (sphereOneP, .6);
vec3 sphereOneColor = vec3 (0.15, 0.19, 0.7);
// Sphere 2
vec3 sphereTwoP = vec3 (p.x + .5, p.y, p.z - .5);
float sphereTwoDistance = sdSphere (sphereTwoP, .5);
float finalDistance = opSubtraction (sphereTwoDistance, sphereOneDistance);
return vec4 (sphereOneColor, finalDistance);
}
W bien sûr kif kif, Ɛand'ha el smooth version mteƐha!
float opSmoothSubtraction(float d1, float d2, float k)
{
float h = clamp (0.5 - 0.5 * (d2 + d1) / k, 0.0, 1.0);
return mix (d2, -d1, h) + k * h * (1.0 - h);
}
Akthar ħaja tjini interressante f'el SDF, eli heya le fait que tnajjem t'rendri barcha ħweyij bléch!
Ih nƐam, matest'ħaq̈ech GPU aq̈wa bech t'rendri 50000 sphere!
Famma SDF functions yħottouna n'rendriw object wela el scene kemla barcha marrat.
El technique hedhi heya Ɛibara Ɛla tiling l'el coordinates, win nƐawdou el scene barcha marrat mangheir manƐawdou neħsbou kolchay.
Hedha el code el generic implementation:
float opRepetition (vec3 p, vec3 s, sdf3d primitive )
{
vec3 q = p - s * round(p / s);
return primitive (q);
}
Binnesba l'el cas mteƐna, hedhi function l'el sphere eli nħebouha tetƐawéd:
float sdRepeatedSphere (vec3 p, float size, float repitition){
vec3 q = p - repitition * round(p / repitition);
return length (q) - size;
}
Famma zeda limited repitition bech nakhtarou q̈adeh bedhabt nħebbou n'Ɛawdou el object w wiin.
vec3 opLimitedRepetition (vec3 p, float s, vec3 l, sdf3d primitive )
{
vec3 q = p - s * clamp (round (p / s), -l, l);
return primitive (q);
}
Hak taƐraf taw kifeh el background makhdoum! Jarrab ħweyij okhriin m'el lista hedhi.
Pagèt okhrin: SDF - Primitives | SDF - Shading | Home