dymokomi/luced-3d
Source of release 0.11.1
## Edit SDF: the Edit framework's kind for signed distance fields. Its one
## level, Object, picks whole primitives of the SDF program on their proxies
## (each primitive's own shape, placed: luce-geocore SdfEdits), so moves keep
## the selection and subtracted shapes stay pickable. Primitives (sphere,
## box, torus, capsule, cylinder) are recipe steps added to the program;
## the tools set a primitive's boolean (Add, Subtract, Intersect), its blend
## radius (Blend, by the tool amount), or delete it; the gizmo moves,
## rotates and scales primitives. Other components pass through.
from luce_geocore.geocore import Mesh, Groups, Domain, GroupType, SdfEdits, Vector3
from geometry_data import GeometryData
from edit_operations import EditStep
from edit_types import EditKind, EditLevel, EditTool, fixed_numbers, own_data
from luce_ui import ui
pub let kind_name = "Edit SDF"
pub func primitive_names() -> list[str]:
return ["Sphere", "Box", "Torus", "Capsule", "Cylinder"]
pub func kind() -> EditKind:
return EditKind(kind_name, levels(), tool_list(), primitive_names(), own_data, pick, run, create, see_through = true)
func levels() -> list[EditLevel]:
return [EditLevel("Object", "sphere", Domain.face, GroupType.faces, piece)]
## Add, Subtract and Intersect set the boolean combining a primitive with
## the shapes before it; Blend rounds that join by the tool amount (0 is
## sharp); Delete removes primitives.
func tool_list() -> list[EditTool]:
return [EditTool("Add", "SDF Mode", [], fixed_numbers([0.0])), EditTool("Subtract", "SDF Mode", [], fixed_numbers([1.0])),
EditTool("Intersect", "SDF Mode", [], fixed_numbers([2.0])), EditTool("Blend", "SDF Blend", [], blend_numbers),
EditTool("Delete", "SDF Delete", [], fixed_numbers([]))]
func blend_numbers(amount: float) -> list[float]:
return [amount if amount > 0.0 else 0.0]
## The primitive under a picked proxy face.
func piece(mesh: Mesh, face: int) -> str!:
return f"@sdf_prim={int(mesh.attribute_value(mesh.find_attribute("sdf_prim", 2), face))}"
## The proxies of the SDF's primitives (made once per result), with what
## the last step created as `.selection`. Steps read their groups on this
## same mesh (a selection may be written as `.selection`).
func pick(data: GeometryData, level: int) -> Mesh?!:
return picked(data)
func picked(data: GeometryData) -> Mesh!:
let shown = proxies(data)
if data.created != "":
return Groups.with_group(shown, ".selection", 2, data.created)
return shown
func proxies(data: GeometryData) -> Mesh!:
if let cached = data.pick_cache:
return cached
let made = SdfEdits.proxies(data.geometry)
data.pick_cache = made
return made
func run(step: EditStep, data: GeometryData) -> GeometryData!:
if step.verb.starts_with("Create "):
return created(step, data)
if SdfEdits.count(data.geometry) == 0:
error(ui.invalid, "Edit SDF has no primitives to edit yet: create one, or connect an SDF")
let shown = picked(data)
if step.topology != 0 and step.topology != shown.topology_hash():
error(ui.invalid, "The SDF input changed. Undo the upstream change or clear this node's edits.")
if step.verb == "Transform Components":
return GeometryData(geometry = SdfEdits.transformed(data.geometry, shown, step.group, step.numbers))
if step.verb == "SDF Mode":
return GeometryData(geometry = SdfEdits.with_boolean(data.geometry, shown, step.group, int(step.numbers[0]), -1.0))
if step.verb == "SDF Blend":
return GeometryData(geometry = SdfEdits.with_boolean(data.geometry, shown, step.group, -1, step.numbers[0]))
if step.verb == "SDF Delete":
return GeometryData(geometry = SdfEdits.without(data.geometry, shown, step.group))
error(ui.invalid, f"{step.verb} does not apply to SDF primitives")
## The step adding primitive `name` at the origin, combined by Add. Its
## numbers: three sizes (sphere radius; box width, height, depth; torus ring
## and tube radii; capsule and cylinder radius and height), the position,
## the boolean (0 add, 1 subtract, 2 intersect) and the blend radius.
func create(name: str) -> EditStep?!:
let shape = shape_of(name)
if shape < 0:
return none
let sizes = [[1.0, 0.0, 0.0], [2.0, 2.0, 2.0], [1.0, 0.3, 0.0], [0.5, 2.0, 0.0], [1.0, 2.0, 0.0]][shape]
return EditStep("Create " + name, "", 0, sizes + [0.0, 0.0, 0.0, 0.0, 0.0], 0)
## Primitive `name`'s shape number (SdfEdits.with_primitive), or -1.
func shape_of(name: str) -> int:
return primitive_names().index_of(name) else -1
## `data` with the primitive of `step` added, selected as what it created.
func created(step: EditStep, data: GeometryData) -> GeometryData!:
let v = step.numbers
if v.length < 8:
error(ui.invalid, f"{step.verb} needs eight numbers")
let shape = shape_of(step.verb[7..])
if shape < 0:
error(ui.invalid, f"Edit SDF cannot create {step.verb[7..]}")
let index = SdfEdits.count(data.geometry)
let result = GeometryData(geometry = SdfEdits.with_primitive(data.geometry, shape, v[..<3], Vector3(v[3], v[4], v[5]), int(v[6]), v[7]))
result.created = f"@sdf_prim={index}"
return result