dymokomi/luced-3d
Source of release 0.11.5
0.11.5 / src / scene_nodes.luc
## Camera, Light, Render, Material and Assign Material nodes. Cameras and lights are luce-geocore's
## scene component: they travel in the geometry like everything else, so
## Transform, Merge, Group and Blast work on them. A Camera or Light adds its
## object to its input (or makes just the object when unconnected); Render
## checks the scene it is given and passes it on. Parameter rows and slots
## are in node_catalog (camera_fields, light_fields, render_fields).
from luce_3d.graphics import Vector3
from luce_geocore.geocore import MaterialAssignment
from geometry_data import GeometryData
from luce_ui import ui
from angles import radians
## The engines a Render node offers, in its Engine menu's order.
pub func engines() -> list[str]:
return ["Luce Path"]
## The object's path: the node's Path text, or a default from its kind.
func object_path(text: str, fallback: str) -> str:
let path = text.trim()
return fallback if path == "" else path
func placement(v: list[float]) -> (Vector3, Vector3):
return (Vector3(v[1], v[2], v[3]), Vector3(radians(v[4]), radians(v[5]), radians(v[6])))
## `data` with this Camera node's camera added.
pub func add_camera(id: int, v: list[float], texts: list[str], data: GeometryData) -> GeometryData!:
let (translation, rotation) = placement(v)
let geometry = data.geometry.with_camera(object_path(texts[0], f"camera{id}"), translation, rotation, int(v[0]),
v[7], v[8], v[9], v[10], v[11], v[12], v[13])
return data.with_geometry(geometry)
## `data` with this Light node's light added.
pub func add_light(id: int, v: list[float], texts: list[str], data: GeometryData) -> GeometryData!:
let (translation, rotation) = placement(v)
let temperature = v[13] if v[12] != 0.0 else 0.0
let geometry = data.geometry.with_light(object_path(texts[0], f"light{id}"), translation, rotation, int(v[0]),
Vector3(v[7], v[8], v[9]), v[10], v[11], temperature, v[18] != 0.0,
v[14], v[15], v[16], v[17], v[19] != 0.0, texts[1].trim() if texts.length > 1 else "")
return data.with_geometry(geometry)
## The input passed on once its scene can be rendered: the camera named in
## the Camera text (or a first camera) must exist. A scene without lights
## renders black, which is a warning rather than an error.
pub func check_render(texts: list[str], data: GeometryData) -> GeometryData!:
let scene = data.geometry.scene()
let wanted = texts[0].trim()
var camera = -1
var lights = 0
for index in 0..<scene.count():
if scene.is_camera(index):
if camera < 0 and (wanted == "" or scene.path(index) == wanted):
camera = index
else:
lights += 1
if camera < 0:
error(ui.invalid, "Add a Camera node upstream" if wanted == "" else f"No camera at {wanted}")
if lights == 0:
return data.passed("The scene has no lights; only emissive surfaces will show")
return data
## The OpenPBR parameters node_catalog.material_fields lays out from slot
## `first`, `full` with coat, transmission and opacity: their names, one per
## line, and four numbers each (unused components 0).
pub func material_values(v: list[float], first: int, full: bool) -> (str, list[float]):
let names = ["base_color", "base_metalness", "base_diffuse_roughness", "specular_weight", "specular_color", "specular_roughness",
"specular_ior", "specular_roughness_anisotropy", "emission_luminance", "emission_color"]
let offsets = [0, 3, 4, 5, 6, 9, 10, 11, 12, 13]
let widths = [3, 1, 1, 1, 3, 1, 1, 1, 1, 3]
if full:
names.extend(["coat_weight", "coat_color", "coat_roughness", "coat_ior", "coat_darkening", "transmission_weight", "transmission_color", "geometry_opacity",
"transmission_dispersion_scale", "transmission_dispersion_abbe_number"])
offsets.extend([16, 17, 20, 21, 22, 23, 24, 27, 28, 29])
widths.extend([1, 3, 1, 1, 1, 1, 3, 1, 1, 1])
let values: list[float] = []
for at in 0..<names.length:
for k in 0..<4:
values.append(v[first + offsets[at] + k] if k < widths[at] else 0.0)
return (names.join("\n"), values)
## `data` with a Material node's material added to its library: its OpenPBR
## Surface's values `v` and, when its nodes drive any, their graph
## (Network.resolve_material) as the text parameter shader_graph.
pub func add_material(id: int, v: list[float], texts: list[str], data: GeometryData, graph: str) -> GeometryData!:
let (names, values) = material_values(v, 0, true)
if graph == "":
return data.with_geometry(data.geometry.with_material(object_path(texts[0], f"materials/material{id}"), "openpbr", names, values))
let all_names: list[str] = names.split("\n")
let all_texts: list[str] = []
for _ in 0..<all_names.length:
all_texts.append("")
all_names.append("shader_graph")
all_texts.append(graph)
for _ in 0..<4:
values.append(0.0)
return data.with_geometry(data.geometry.with_material(object_path(texts[0], f"materials/material{id}"), "openpbr", all_names.join("\n"), values, all_texts.join("\n")))
## The geometry with the group's faces bound to a material of the library on
## the second input (by path; empty takes the first), and that library passed on.
pub func assign_material(texts: list[str], data: GeometryData, materials: GeometryData) -> GeometryData!:
let listed = materials.geometry.material_paths()
let paths: list[str] = [] if listed == "" else listed.split("\n")
let wanted = texts[1].trim()
if paths.length == 0:
error(ui.invalid, "The second input has no materials; connect Material nodes")
let path = paths[0] if wanted == "" else wanted
let mesh = data.operand() else return GeometryData(geometry = data.geometry.with_materials_from(materials.geometry)).passed("The input has no polygons to assign to")
let bound = data.with_operand(MaterialAssignment.faces(mesh, texts[0], "material", path))
let result = GeometryData(geometry = bound.geometry.with_materials_from(materials.geometry))
if not (path in paths):
result.warning = f"No material at {path}; those faces render with the default material"
return result