›View the shape's GLSL: this is the whole model
float sdDragonBody(vec3 p) {
// Main body as a series of capsules
float body = sdCapsule(p, vec3(-0.6, -0.2, 0.0), vec3(0.6, -0.2, 0.0), 0.15);
float neck = sdCapsule(p, vec3(-0.6, -0.2, 0.0), vec3(-0.8, 0.3, 0.0), 0.1);
float tail = sdCapsule(p, vec3(0.6, -0.2, 0.0), vec3(0.9, -0.4, 0.0), 0.08);
return opSmoothUnion(opSmoothUnion(body, neck, 0.1), tail, 0.1);
}
float sdDragonHead(vec3 p) {
// Head as a rounded box
vec3 headPos = vec3(-0.85, 0.35, 0.0);
float head = sdRoundBox(p - headPos, vec3(0.15, 0.1, 0.1), 0.05);
// Eyes as small spheres
float eye1 = sdSphere(p - (headPos + vec3(0.05, 0.05, 0.1)), 0.03);
float eye2 = sdSphere(p - (headPos + vec3(0.05, 0.05, -0.1)), 0.03);
return opSmoothUnion(opSmoothUnion(head, eye1, 0.05), eye2, 0.05);
}
float sdDragonWings(vec3 p) {
// Wings as larger rotated cones
vec3 wingBase = vec3(-0.2, 0.0, 0.0);
float wing1 = sdRoundCone(rotZ(p - wingBase, -0.5), 0.0, 0.3, 0.7);
float wing2 = sdRoundCone(rotZ(p - wingBase, 0.5), 0.0, 0.3, 0.7);
return opSmoothUnion(wing1, wing2, 0.1);
}
float sdDragonLegs(vec3 p) {
// Legs as capsules
float leg1 = sdCapsule(p, vec3(-0.3, -0.8, 0.1), vec3(-0.3, -0.5, 0.1), 0.05);
float leg2 = sdCapsule(p, vec3(0.3, -0.8, 0.1), vec3(0.3, -0.5, 0.1), 0.05);
return opSmoothUnion(leg1, leg2, 0.1);
}
float sdf(vec3 p) {
// Combine all parts of the dragon
float body = sdDragonBody(p);
float head = sdDragonHead(p);
float wings = sdDragonWings(p);
float legs = sdDragonLegs(p);
return opSmoothUnion(opSmoothUnion(opSmoothUnion(body, head, 0.1), wings, 0.1), legs, 0.1);
}
vec3 sdf_color(vec3 p) {
// Color the dragon with a gradient from green to red
float height = p.y + 1.0;
return mix(vec3(0.0, 1.0, 0.0), vec3(1.0, 0.0, 0.0), height / 2.0);
}
src/CAD_Harness/sdf/examples/dragon_gpt4o.glsl · 1,851 bytes · 47 lines// A snowman: three smoothly blended snow spheres, coal eyes and buttons,
// a carrot nose, stick arms and a top hat. Faces +Z, rests on y = -1.
float snow(vec3 p) {
float base = sdSphere(p - vec3(0.0, -0.55, 0.0), 0.45);
float torso = sdSphere(p - vec3(0.0, 0.15, 0.0), 0.33);
float head = sdSphere(p - vec3(0.0, 0.62, 0.0), 0.23);
return opSmoothUnion(opSmoothUnion(base, torso, 0.08), head, 0.06);
}
float coal(vec3 p) {
vec3 q = vec3(abs(p.x), p.y, p.z); // mirror: one eye models both
float eyes = sdSphere(q - vec3(0.08, 0.68, 0.195), 0.03);
float buttons = min(sdSphere(p - vec3(0.0, 0.25, 0.305), 0.035),
min(sdSphere(p - vec3(0.0, 0.12, 0.318), 0.035),
sdSphere(p - vec3(0.0, -0.01, 0.28), 0.035)));
return min(eyes, buttons);
}
float carrot(vec3 p) {
// the cone's axis is +Y; rotate it to point along +Z
return sdRoundCone(rotX(p - vec3(0.0, 0.62, 0.2), -1.5708), 0.04, 0.008, 0.2);
}
float sticks(vec3 p) {
vec3 q = vec3(abs(p.x), p.y, p.z);
float arm = sdCapsule(q, vec3(0.28, 0.2, 0.0), vec3(0.62, 0.42, 0.0), 0.02);
float twig = sdCapsule(q, vec3(0.52, 0.36, 0.0), vec3(0.6, 0.5, 0.02), 0.012);
return min(arm, twig);
}
float hat(vec3 p) {
float crown = sdCylinder(p - vec3(0.0, 0.92, 0.0), 0.14, 0.09);
float brim = sdCylinder(p - vec3(0.0, 0.83, 0.0), 0.24, 0.012);
return min(crown, brim);
}
float sdf(vec3 p) {
return min(min(snow(p), coal(p)), min(min(carrot(p), sticks(p)), hat(p)));
}
vec3 sdf_color(vec3 p) {
float d = sdf(p) + 1e-3;
if (coal(p) <= d) return vec3(0.04);
if (carrot(p) <= d) return vec3(0.95, 0.4, 0.08);
if (sticks(p) <= d) return vec3(0.35, 0.2, 0.1);
if (hat(p) <= d) return vec3(0.08, 0.08, 0.1);
return vec3(0.95, 0.97, 1.0);
}
src/CAD_Harness/sdf/examples/snowman.glsl · 1,824 bytes · 49 lines// A rubber duck: an organic shape from ellipsoids and spheres joined with
// smooth unions. Faces +Z, rests on y = -1.
float body(vec3 p) {
float torso = sdEllipsoid(p - vec3(0.0, -0.6, -0.05), vec3(0.48, 0.36, 0.62));
float tail = sdEllipsoid(rotX(p - vec3(0.0, -0.4, -0.58), 0.6), vec3(0.16, 0.08, 0.16));
float neck = sdSphere(p - vec3(0.0, -0.25, 0.25), 0.2);
float head = sdSphere(p - vec3(0.0, 0.05, 0.3), 0.3);
vec3 q = vec3(abs(p.x), p.y, p.z); // mirror: one wing models both
float wing = sdEllipsoid(rotZ(q - vec3(0.42, -0.52, -0.1), -0.25), vec3(0.08, 0.18, 0.32));
float d = opSmoothUnion(torso, tail, 0.12);
d = opSmoothUnion(d, wing, 0.06);
d = opSmoothUnion(d, neck, 0.15);
return opSmoothUnion(d, head, 0.12);
}
float beak(vec3 p) {
float upper = sdEllipsoid(p - vec3(0.0, 0.02, 0.6), vec3(0.14, 0.05, 0.13));
float lower = sdEllipsoid(p - vec3(0.0, -0.03, 0.57), vec3(0.11, 0.035, 0.1));
return opSmoothUnion(upper, lower, 0.02);
}
float eyes(vec3 p) {
vec3 q = vec3(abs(p.x), p.y, p.z);
return sdSphere(q - vec3(0.12, 0.13, 0.55), 0.045);
}
float sdf(vec3 p) {
return min(min(body(p), beak(p)), eyes(p));
}
vec3 sdf_color(vec3 p) {
float d = sdf(p) + 1e-3;
if (eyes(p) <= d) return vec3(0.03);
if (beak(p) <= d) return vec3(1.0, 0.45, 0.05);
return vec3(1.0, 0.82, 0.1);
}
src/CAD_Harness/sdf/examples/duck.glsl · 1,379 bytes · 38 lines