Taw puisque Ɛandna sphere mlawwna w jawha béhi (kima chofna houni),
nħebbou nzidou ħwéyij okhrin f'el scene. Donc f'el partie hedhi bech netƐalmou kifeh nzidou objects l'el scene.
Généralement el objects f'el 3D houma combination mtaƐ barcha primitives, w kol primitive houwa shape tebda relativement séhla w mafhouma
(kima el sphere, box, pyramide... etc.).
F'el cas mteƐna, el SDF primitives houma functions sehliin bech y'renderiwlna shapes sehliin.
Yq̈oul el q̈ayél, chnouma el primitives el disponibles?... barcha, à l'infini.
Madém kol primitive heyya function, donc ennajmou ntalƐou barcha des functions w nq̈oulou Ɛlihom "primitive".
Nébdéw b'el sèhil!
B'el function hedhi ennajmou n'rendriw box zeda:
float sdBox (vec3 p, vec3 extent)
{
vec3 q = abs (p) - extent;
return length (max (q,0.0)) + min (max (q.x,max (q.y,q.z)),0.0);
}
Jarrab el code ken t'ħeb:
Taw Ɛandna el box ama yodh'hor 2D rectangle, puisque m9abel l'el camera.
F'el cas hedha, yelzem ndawrou el box mteƐna chwaya. Donc nestaƐmlou eli tƐallamnèh f'el SDF basics.
float sceneSDF (vec3 p)
{
vec3 boxExtent = vec3 (1.5, 0.5, 0.5);
// Ndawrou el box Ɛal Y-Axis w'el Z-Axis
p *= rotateY (0.6);
p *= rotateZ (0.4);
return sdBox (p, boxExtent);
}
Hedhiya el SDF mtaƐ l'pyramid.
float sdPyramid (vec3 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;
vec3 q = vec3 ( 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));
}
SoƐbet Ɛlik? Matkhafech, mantekch supposé taƐrafhom el kol, s'ħayyebna Inigo Quilez ħadhrelna el primitives el kol f'hal site.
Taw Ɛandna object f'el scene ama el scene généralement fiha barcha objects w lights, donc bech nħaawlou nzidou objects l'el scene.
Bech nzidou objects l'bƐadh'hom, nestaƐmlou
.
float sceneSDF(vec3 p)
{
vec3 boxP = p;
boxP.x += 2.0; // Nzaħlq̈ou el box Ɛal X-Axis
boxP *= rotateY (0.6); // Ndawrou el box Ɛal Y-Axis
boxP *= rotateZ (0.4); // Ndawrou el box Ɛal Z-Axis
vec3 boxExtent = vec3 (1.5, 0.5, 0.5); // nħadhrou kobr el box
float boxDistance = sdBox (boxP, boxExtent); // NestaƐmlou el SDF bech nħadhrou el distance l'el box
vec3 sphereP = p;
sphereP.x += -2.0; // Nzaħlq̈ou el sphere Ɛal X-Axis
float sphereDistance = sdSphere (sphereP, 1.0); // NestaƐmlou el SDF bech nħadhrou el distance l'el sphere
float finalDistance = min (boxDistance, sphereDistance); // Nzidou objects l'bƐadh'hom
return finalDistance;
}
Ennajmou nzidou chwaya ħweyij m'eli tƐallemnéh, mathalan iTime
bech nħarkou el dhaw w'el positionnét mtaƐ l'objects:
- Nħarkou el box
boxP.y += sin (iTime); // Nzaħlq̈ou el box Ɛal Y-Axis
- Nħarkou el sphere
sphereP.y += cos (iTime); // Nzaħlq̈ou el sphere Ɛal Y-Axis
- Nħarkou el dhaw
vec3 lightDirection = normalize (vec3 (cos (iTime), 1.0, sin (iTime)));
Tnajjem tenzel Ɛal video w tbalbaz waħdek, zid pyramid wela ay primitive.
Ih nƐam, hedhi mochkel. el kol Ɛand'hom nafs el couleur.
F'el implementation mteƐna, neħsbou el distances l'el shapes, w baƐd nlawnou el scene b nafs el couleur.
Bech ennajmou naƐtiw couleurèt moch kifkif, yelzem n'settiw el couleur waq̈t neħsbou el distance.
Donc,
twalli trajƐelna couleur w distance. Ennajmou nħottou el couleur fi 3 values (R, G, B), w'el distance fi 1 value (Alpha).
El total houwa 4 values w hakka, el declaration twalli
.
Bien sûr, lezem nbadlou el ray marching w'el normal estimation function zeda bech y'returniw
.
F'el
, ennajmou n'settiw el scene color b'el
.
vec3 finalColor = vec3 (0.);
finalColor = mix (sphereColor, finalColor, sphereDistance);
finalColor = mix (boxColor, finalColor, boxDistance);
Nbadlou el final color bech testaƐmel el scene color (XYZ m'el result).
vec4 scene = raymarch (cameraPosition, cameraDirection);
// ...
vec3 objectColor = scene.xyz;
Tnajjem tenzel Ɛal code w tjarbou (naħħit el commentérét el zeydin bech ma tetjoujemch)
El primitives waħad'hom mayakfiwech bech naƐmlou complex scenes, donc aƐmel talla Ɛal SDF operators!
Pagèt okhrin: SDF - Operators | Shaders | Home