dymokomi/luced-3d
Source of release 0.11.6
0.11.6 / tests / examples / main.luc
# Example scenes for the Code node, after classic Houdini wrangle
# tutorials: each is built as a user would, every Code node must cook
# without an error, and the scene is saved to examples/wrangles/ for
# opening in the editor. Gaussian splat wrangles, on a synthetic capture
# the scene makes, go to examples/splats/.
from luce_std import files, paths, process
from workspace import Workspace
import node_commands
import node_catalog
import code_node
import code_cook
import project_file
import node_paths
from luce_ui import ui
from scripts import script_examples
from showing import show_chain
## A scene in progress: its workspace and where its next node goes.
class Scene:
let w: Workspace
let name: str
var y: float = 0.0
func init(self, name: str) -> unit!:
self.name = name
self.w = Workspace()
node_commands.delete_selected(self.w)
## A `kind` node after the selected one, its values `values` by slot.
func add(self, kind: str, values: list[(int, float)] = []) -> int!:
# A saved handle can select a node in a previously visited network.
if self.w.network.selected >= 0:
node_commands.reveal_node(self.w, self.w.network.selected)
if kind in node_catalog.scene_kinds():
while self.w.network.scope >= 0: node_commands.leave_group(self.w)
elif not node_catalog.allowed(kind, self.w.network.context()):
var container = "Geo"
if kind.starts_with("SDF ") or kind == "Mesh to SDF": container = "SDF"
elif node_catalog.category(kind) == "Volumes": container = "Volumes"
elif node_catalog.category(kind) == "GSplats": container = "GSplats"
let source = self.w.network.selected
node_commands.add_node(self.w, container, 0.0, self.y)
let boundary = self.w.network.selected
if source >= 0 and node_catalog.input_count(kind) > 0:
node_commands.connect(self.w, source, boundary, 0)
node_commands.enter_group(self.w, boundary)
if source >= 0 and node_catalog.input_count(kind) > 0:
node_commands.add_node(self.w, "Input", 0.0, 0.0)
node_commands.add_node(self.w, kind, 0.0, self.y)
assert((self.w.selected() else trap(kind)).kind == kind, self.w.notice)
self.y += 90.0
for (slot, value) in values:
node_commands.set_parameter(self.w, slot, value)
return self.w.network.selected
## A Code node running over `run_over` (Houdini's menu order: 0 detail,
## 1 points, 2 vertices, 3 primitives, 4 numbers) with `body`.
func code(self, run_over: int, body: str, header: str = "") -> int!:
let id = self.add("Code", [(code_cook.run_over_slot, float(run_over))])
if header != "":
code_node.set_code(self.w, id, code_cook.header_slot, header)
code_node.set_code(self.w, id, code_cook.body_slot, body)
let node = self.w.network.require(id)
if node.failure != "":
error(ui.invalid, f"{self.name}: {node.failure}")
return id
## Connect node `source` to input `port` of node `target`.
func wire(self, source: int, target: int, port: int) -> unit!:
# A nested source exposes its container at the target's level.
let graph = self.w.network
let parent = graph.require(target).parent
var exposed = source
while graph.require(exposed).parent != parent and graph.require(exposed).parent >= 0:
exposed = graph.require(exposed).parent
# Or the target's container takes it from its own level, through an
# Input node.
let outside = graph.require(parent).parent if parent >= 0 else -1
var around = source
while graph.require(around).parent != outside and graph.require(around).parent >= 0:
around = graph.require(around).parent
let saved = graph.scope
graph.scope = parent
if graph.require(exposed).parent == parent:
graph.connect(exposed, target, port)
elif graph.has_inputs(parent) and graph.require(around).parent == outside:
graph.connect(around, parent, port)
let input = graph.add("Input", 180.0, self.y)
input.values[0] = float(port)
graph.connect(input.id, target, port)
else:
# Otherwise (an object has no inputs) it reads the source through
# an Import node.
let imported = graph.add("Import", 180.0, self.y)
imported.texts[0] = node_paths.absolute(graph.nodes, graph.require(source))
graph.connect(imported.id, target, port)
graph.scope = saved
self.w.network.invalidate(target)
self.w.rebuild()
let node = self.w.network.require(target)
if node.failure != "":
error(ui.invalid, f"{self.name}: {node.failure}")
## Display the last node, check every node cooked, and save.
func save(self, folder: str) -> unit!:
# The last node is the scene's output, displayed and framed.
show_chain(self.w, self.w.network.selected)
self.w.view.yaw = 0.7
self.w.view.pitch = 0.55
self.w.frame_cube()
for node in self.w.network.nodes:
if node.failure != "":
error(ui.invalid, f"{self.name}: {node.name}: {node.failure}")
project_file.save(paths.join(folder, self.name + ".prisma"), self.w, [1.0, 0.0, 0.0])
pub func main(arguments: list[str]) -> int!:
if let here = process.variable("LUC_TEST_DIR"):
process.change_directory(here)
let folder = paths.join(paths.directory(paths.directory(process.current_directory())), "examples/wrangles")
files.ensure_directory(folder)
var count = 0
for build in [sine_wave, ripple, noise_sphere, color_by_height, twist, random_prim_colors, jitter, attractor, checker, terrain, million, spiral, holes, smoothing, distance_color, projection, ramps, matrix_twist, cells, curl_flow, inside_outside, surface_distance, pc_smooth, shrink_wrap, console, names, median, neighbor_lists, gyroid, metaballs, noise_cloud, fire, two_smokes, eroded_points]:
build(folder)
count += 1
print(f"PASS {count} wrangle example scenes cooked and saved to examples/wrangles")
let splat_folder = paths.join(paths.directory(folder), "splats")
files.ensure_directory(splat_folder)
var splat_count = 0
for build in [fade_by_height, recolor, squash, from_polygons, splats_to_mesh, splat_normals, splat_relight, splat_shadows]:
build(splat_folder)
splat_count += 1
print(f"PASS {splat_count} splat example scenes cooked and saved to examples/splats")
let scripts = script_examples(paths.join(paths.directory(folder), "scripts"))
print(f"PASS {scripts} Script example scenes built, cooked and saved to examples/scripts")
return 0
## A grid rolling as a sine wave along X, moving with time.
func sine_wave(folder: str) -> unit!:
let s = Scene("sine_wave")
_ = s.add("Grid", [(0, 10.0), (1, 10.0), (2, 64.0), (3, 64.0)])
_ = s.code(1, "# A wave along X that travels with time.\nlet frequency = k.f32(\"frequency\", 1.5)\nlet speed = k.f32(\"speed\", 2.0)\nlet amplitude = k.f32(\"amplitude\", 0.4)\np.P[1] = math32.sin(p.P[0] * frequency + (f32)k.time * speed) * amplitude\n")
s.save(folder)
## Rings spreading out from the center, fading with distance.
func ripple(folder: str) -> unit!:
let s = Scene("ripple")
_ = s.add("Grid", [(0, 12.0), (1, 12.0), (2, 64.0), (3, 64.0)])
_ = s.code(1, "# Distance from the center in the ground plane.\nlet r = math32.sqrt(p.P[0] * p.P[0] + p.P[2] * p.P[2])\nlet wave = math32.sin(r * k.f32(\"frequency\", 4.0) - (f32)k.time * k.f32(\"speed\", 4.0))\np.P[1] = wave * k.f32(\"height\", 0.5) / (1.0 + r)\n")
s.save(folder)
## A sphere pushed in and out along its normals by animated noise.
func noise_sphere(folder: str) -> unit!:
let s = Scene("noise_sphere")
_ = s.add("Sphere", [(0, 1.0), (1, 64.0), (2, 32.0)])
_ = s.code(1, "# Out from the center by noise that drifts with time.\nlet scale = k.f32(\"scale\", 2.5)\nlet n = snoise(p.P * scale + [0.0, 0.0, (f32)k.time * 0.5, 0.0])\np.P = p.P + normalized(p.P) * (n * k.f32(\"amount\", 0.25))\n")
s.save(folder)
## Noise hills colored from blue valleys to white peaks.
func color_by_height(folder: str) -> unit!:
let s = Scene("color_by_height")
_ = s.add("Grid", [(0, 10.0), (1, 10.0), (2, 64.0), (3, 64.0)])
_ = s.code(1, "p.P[1] = noise(p.P * 0.6) * 2.0 - 1.0\n")
_ = s.code(1, "# 0 at the lowest valley, 1 at the highest peak.\nlet t = fit(p.P[1], -0.5, 0.5, 0.0, 1.0)\np.Cd = [lerp(0.1, 1.0, t), lerp(0.25, 1.0, t), lerp(0.8, 1.0, t), 0.0]\n")
s.save(folder)
## A grid wrapped into a tube by one Code node (columns round a circle,
## rows up its height), then twisted about Y, more with height, by another.
func twist(folder: str) -> unit!:
let s = Scene("twist")
_ = s.add("Grid", [(0, 6.0), (1, 6.0), (2, 64.0), (3, 64.0)])
_ = s.code(1, "# x goes round, z goes up.\nlet around = (p.P[0] / 6.0 + 0.5) * 2.0 * math32.pi\nlet radius = k.f32(\"radius\", 0.8)\np.P = [math32.cos(around) * radius, p.P[2] + 3.0, math32.sin(around) * radius, 0.0]\n")
_ = s.code(1, "# Turn about Y by an angle that grows with height.\nlet angle = p.P[1] * k.f32(\"twist\", 0.6)\nlet c = math32.cos(angle)\nlet sn = math32.sin(angle)\np.P = [p.P[0] * c - p.P[2] * sn, p.P[1], p.P[0] * sn + p.P[2] * c, 0.0]\np.P[0] *= 1.0 + 0.3 * math32.sin(p.P[1] * 2.0)\n")
s.save(folder)
## Every primitive its own random color, as @Cd = rand(@primnum).
func random_prim_colors(folder: str) -> unit!:
let s = Scene("random_prim_colors")
_ = s.add("Grid", [(0, 6.0), (1, 6.0), (2, 24.0), (3, 24.0)])
_ = s.code(3, "f.Cd = rand3((i64)f.primnum + (i64)k.i32(\"seed\", 7) * 100000)\n")
s.save(folder)
## Points jittered by a random offset each, seeded by their number.
func jitter(folder: str) -> unit!:
let s = Scene("jitter")
_ = s.add("Grid", [(0, 6.0), (1, 6.0), (2, 48.0), (3, 48.0)])
_ = s.code(1, "let offset = rand3((i64)p.ptnum) - [0.5, 0.5, 0.5, 0.0]\np.P = p.P + offset * k.f32(\"amount\", 0.08)\n")
s.save(folder)
## Points pulled toward an attractor, the pull fading with distance.
func attractor(folder: str) -> unit!:
let s = Scene("attractor")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 64.0), (3, 64.0)])
_ = s.code(1, "let target = k.vec3(\"target\", [0.0, 1.5, 0.0, 0.0])\nlet reach = k.f32(\"radius\", 2.0)\n# The falloff by distance in the ground plane: a peak under the target.\nlet d = distance(p.P * [1.0, 0.0, 1.0, 0.0], target * [1.0, 0.0, 1.0, 0.0])\nlet pull = 1.0 - smooth(0.0, reach, d)\np.P = p.P + (target - p.P) * (pull * k.f32(\"strength\", 0.9))\np.Cd = [pull, 0.3, 1.0 - pull, 0.0]\n")
s.save(folder)
## A checkerboard from each primitive's center.
func checker(folder: str) -> unit!:
let s = Scene("checker")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 32.0), (3, 32.0)])
_ = s.code(3, "let c = f.center()\nlet size = k.f32(\"size\", 1.0)\nlet cell = (i64)math32.floor(c[0] / size) + (i64)math32.floor(c[2] / size)\nf.Cd = [0.9, 0.9, 0.9, 0.0] if cell % 2 == 0 else [0.1, 0.1, 0.1, 0.0]\n")
s.save(folder)
## Fractal terrain: octaves of noise summed in a loop, the header holding
## the octave sum, and a detail attribute recording the octaves.
func terrain(folder: str) -> unit!:
let s = Scene("terrain")
_ = s.add("Grid", [(0, 20.0), (1, 20.0), (2, 64.0), (3, 64.0)])
let header = "## Noise octaves: each twice as fine and half as strong.\nfunc fbm(at: f32[4], octaves: i32) -> f32:\n var sum: f32 = 0.0\n var scale: f32 = 1.0\n var weight: f32 = 0.5\n for i in 0..<octaves:\n sum += snoise(at * scale) * weight\n scale *= 2.0\n weight *= 0.5\n return sum\n"
_ = s.code(1, "p.P[1] = fbm(p.P * k.f32(\"scale\", 0.15), k.i32(\"octaves\", 5)) * k.f32(\"height\", 4.0)\n", header)
_ = s.code(1, "let t = clamp(fit(p.P[1], -1.5, 2.0, 0.0, 1.0), 0.0, 1.0)\np.Cd = [lerp(0.2, 0.95, t), lerp(0.45, 0.95, t), lerp(0.2, 0.95, t), 0.0]\n")
_ = s.code(0, "k.set_detail_i32(\"octaves\", k.i32(\"octaves\", 5))\n")
s.save(folder)
## A million points (a 1000 × 1000 grid) displaced by animated noise and
## colored by height: scrub the timeline to feel the speed.
func million(folder: str) -> unit!:
let s = Scene("million_points")
_ = s.add("Grid", [(0, 20.0), (1, 20.0), (2, 1000.0), (3, 1000.0)])
_ = s.code(1, "let n = snoise(p.P * k.f32(\"scale\", 0.3) + [0.0, (f32)k.time * 0.3, 0.0, 0.0])\np.P[1] = n * k.f32(\"height\", 1.5)\nlet t = fit(n, -1.0, 1.0, 0.0, 1.0)\np.Cd = [t, 0.4 + 0.4 * t, 1.0 - t, 0.0]\n")
s.save(folder)
## A spiral ribbon made from nothing: Numbers mode, each n adds two points
## and the quad joining them to the previous pair (k.add_point, k.add_prim;
## a new point's number is n · 1024 + its order within the run).
func spiral(folder: str) -> unit!:
let s = Scene("spiral")
let id = s.code(4, "let t = (f32)n * k.f32(\"turn\", 0.15)\nlet r = 0.5 + (f32)n * 0.012\nlet up = (f32)n * k.f32(\"rise\", 0.01)\nlet across = [math32.cos(t), 0.0, math32.sin(t), 0.0]\nlet inner = k.add_point(across * r + [0.0, up, 0.0, 0.0])\nlet outer = k.add_point(across * (r + 0.4) + [0.0, up, 0.0, 0.0])\nif n > 0:\n let before = (i32)(n - 1) * 1024\n var quad: i32[16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n quad[0] = before\n quad[1] = before + 1\n quad[2] = outer\n quad[3] = inner\n _ = k.add_prim(quad, 4)\n")
s.w.network.selected = id
node_commands.set_parameter(s.w, code_cook.count_slot, 300.0)
s.save(folder)
## Faces deleted at random, a fraction of them (k.remove_prim): Houdini's
## "delete by condition".
func holes(folder: str) -> unit!:
let s = Scene("holes")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 48.0), (3, 48.0)])
_ = s.code(3, "if rand((i64)f.primnum) < k.f32(\"fraction\", 0.35):\n k.remove_prim(f.primnum)\n")
s.save(folder)
## A rough noise blob smoothed by averaging each point with its nearest
## neighbors in the input (nearpoints, point_P): Houdini's point-cloud blur.
## Points the average collapses together leave zero-area faces, as Houdini
## allows.
func smoothing(folder: str) -> unit!:
let s = Scene("smoothing")
_ = s.add("Sphere", [(0, 1.0), (1, 64.0), (2, 32.0)])
_ = s.code(1, "let n = snoise(p.P * 6.0) + rand((i64)p.ptnum) * 0.5\np.P = p.P + normalized(p.P) * (n * 0.12)\n")
# Three passes, each averaging the one before.
for _ in 0..<3:
_ = s.code(1, "let g = k.input(0)\nlet near = g.nearpoints(p.P, k.f32(\"radius\", 0.15), k.i32(\"points\", 12))\nvar sum: f32[4] = [0.0, 0.0, 0.0, 0.0]\nfor i in 0..<near.count:\n sum = sum + g.point_P(near.points[i])\nif near.count > 0:\n p.P = sum * (1.0 / (f32)near.count)\n")
s.save(folder)
## A grid colored by its distance to a sphere in the second input
## (xyzdist): near is warm, far is cool.
func distance_color(folder: str) -> unit!:
let s = Scene("distance_color")
let grid = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 64.0), (3, 64.0)])
let ball = s.add("Sphere", [(0, 1.5), (1, 32.0), (2, 16.0)])
s.w.network.selected = grid
let id = s.code(1, "let d = k.input(1).xyzdist(p.P)\nlet t = clamp(d / k.f32(\"range\", 2.0), 0.0, 1.0)\np.Cd = [1.0 - t, 0.3 + 0.3 * t, t, 0.0]\np.P[1] = -0.2 * t\n")
s.wire(ball, id, 1)
s.save(folder)
## A flat grid dropped onto noise terrain in the second input by casting a
## ray down from each point (intersect): Houdini's Ray SOP in a wrangle.
func projection(folder: str) -> unit!:
let s = Scene("projection")
_ = s.add("Grid", [(0, 10.0), (1, 10.0), (2, 64.0), (3, 64.0)])
_ = s.code(1, "p.P[1] = snoise(p.P * 0.4) * 1.2\n")
let ground = s.w.network.selected
let sheet = s.add("Grid", [(0, 6.0), (1, 6.0), (2, 40.0), (3, 40.0)])
s.w.network.selected = sheet
let id = s.code(1, "let start = p.P + [0.0, 5.0, 0.0, 0.0]\nlet hit = k.input(1).intersect(start, [0.0, -1.0, 0.0, 0.0])\nif hit.prim >= 0:\n p.P = hit.position + [0.0, k.f32(\"lift\", 0.05), 0.0, 0.0]\n p.Cd = [1.0, 0.6, 0.2, 0.0]\n")
s.wire(ground, id, 1)
s.save(folder)
## Ramps (Houdini's chramp): a float ramp shapes a mound's profile by
## distance from the center, a color ramp paints it by height.
func ramps(folder: str) -> unit!:
let s = Scene("ramps")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 96.0), (3, 96.0)])
let id = s.code(1, "let r = clamp(math32.sqrt(p.P[0] * p.P[0] + p.P[2] * p.P[2]) / k.f32(\"radius\", 3.5), 0.0, 1.0)\np.P[1] = k.ramp(\"profile\", r) * k.f32(\"height\", 2.0)\np.Cd = k.ramp_color(\"colors\", p.P[1] / 2.0)\n")
# A volcano: up to a rim, a crater inside, smooth.
code_node.set_ramp(s.w, id, 0, [1.0, 1.0, 4.0, 0.0, 0.35, 2.0, 0.25, 1.0, 2.0, 0.6, 0.15, 2.0, 1.0, 0.0, 2.0])
# Deep blue, sand, green, white.
code_node.set_ramp(s.w, id, 1, [1.0, 3.0, 4.0, 0.0, 0.1, 0.2, 0.6, 1.0, 0.25, 0.85, 0.75, 0.5, 1.0, 0.55, 0.3, 0.6, 0.25, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
s.save(folder)
## A tube twisted with a rotation matrix per point (Mat3.rotation), the
## angle growing with height: transforms as VEX does them.
func matrix_twist(folder: str) -> unit!:
let s = Scene("matrix_twist")
_ = s.add("Grid", [(0, 6.0), (1, 6.0), (2, 64.0), (3, 64.0)])
_ = s.code(1, "let around = (p.P[0] / 6.0 + 0.5) * 2.0 * math32.pi\np.P = [math32.cos(around) * 0.5, p.P[2] + 3.0, math32.sin(around) * 0.5, 0.0]\n")
_ = s.code(1, "# Square it off, then turn it about Y with a matrix.\nlet square = [clamp(p.P[0] * 1.6, -0.5, 0.5), p.P[1], clamp(p.P[2] * 1.6, -0.5, 0.5), 0.0]\nlet turn = Mat3.rotation(p.P[1] * k.f32(\"twist\", 0.5), [0.0, 1.0, 0.0, 0.0])\np.P = turn.transform(square)\n")
s.save(folder)
## Worley cells: each face colored by its cell and darkened toward the
## cell borders (wnoise's f1 and f2).
func cells(folder: str) -> unit!:
let s = Scene("cells")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 160.0), (3, 160.0)])
_ = s.code(1, "let w = wnoise(p.P * k.f32(\"scale\", 1.2))\nlet edge = clamp((w.f2 - w.f1) * 4.0, 0.0, 1.0)\np.Cd = rand3((i64)w.seed) * edge\np.P[1] = edge * 0.15\n")
s.save(folder)
## Points swirled along curl noise, a divergence-free flow: a grid
## advected in a few steps, colored by how far each point traveled.
func curl_flow(folder: str) -> unit!:
let s = Scene("curl_flow")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 128.0), (3, 128.0)])
_ = s.code(1, "let start = p.P\nvar at = p.P\nfor i in 0..<k.i32(\"steps\", 12):\n at = at + curlnoise(at * k.f32(\"scale\", 0.4)) * k.f32(\"step\", 0.08)\np.P = at\nlet moved = clamp(distance(at, start), 0.0, 1.5) / 1.5\np.Cd = [moved, 0.4, 1.0 - moved, 0.0]\n")
s.save(folder)
## A grid colored by whether each point is inside a sphere in the second
## input (windingnumber): inside is lifted and warm.
func inside_outside(folder: str) -> unit!:
let s = Scene("inside_outside")
let grid = s.add("Grid", [(0, 6.0), (1, 6.0), (2, 96.0), (3, 96.0)])
let ball = s.add("Sphere", [(0, 1.5), (1, 48.0), (2, 24.0)])
s.w.network.selected = grid
let id = s.code(1, "let shifted = p.P + [0.0, 1.5, 0.0, 0.0]\nlet inside = k.input(1).windingnumber(shifted) > 0.5\np.Cd = [1.0, 0.5, 0.1, 0.0] if inside else [0.2, 0.3, 0.6, 0.0]\np.P[1] = 0.3 if inside else 0.0\n")
s.wire(ball, id, 1)
s.save(folder)
## Distance along the surface from a group of points (surfacedist): rings
## of color around the grid's center points, walking its edges.
func surface_distance(folder: str) -> unit!:
let s = Scene("surface_distance")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 64.0), (3, 64.0)])
_ = s.code(1, "p.P[1] = snoise(p.P * 0.35) * 1.5\np.set_group(\"seed\", p.ptnum == 2080 or p.ptnum == 600)\n")
_ = s.code(1, "let d = k.input(0).surfacedist(\"seed\", p.ptnum)\nlet band = math32.sin(d * k.f32(\"rings\", 6.0)) * 0.5 + 0.5\np.Cd = [band, 0.3 + 0.4 * band, 1.0 - band, 0.0]\n")
s.save(folder)
## Point-cloud smoothing in one call (pcopen + filter_vec3, VEX's
## pcfilter): a rough blob relaxed twice.
func pc_smooth(folder: str) -> unit!:
let s = Scene("pc_smooth")
_ = s.add("Sphere", [(0, 1.0), (1, 64.0), (2, 32.0)])
_ = s.code(1, "let n = snoise(p.P * 5.0) + rand((i64)p.ptnum) * 0.6\np.P = p.P + normalized(p.P) * (n * 0.12)\n")
for _ in 0..<2:
_ = s.code(1, "p.P = k.input(0).pcopen(p.P, k.f32(\"radius\", 0.15), k.i32(\"points\", 20)).filter_vec3(\"P\")\n")
s.save(folder)
## A grid shrink-wrapped onto a signed distance field in the second input:
## each point steps back along the field's gradient by its distance
## (volume_sample, volume_gradient).
func shrink_wrap(folder: str) -> unit!:
let s = Scene("shrink_wrap")
let grid = s.add("Grid", [(0, 3.0), (1, 3.0), (2, 64.0), (3, 64.0)])
let field = s.add("SDF Sphere", [(0, 1.2)])
s.w.network.selected = grid
let id = s.code(1, "let g = k.input(1)\nvar q = p.P + [0.0, 1.6, 0.0, 0.0]\nfor i in 0..<k.i32(\"steps\", 3):\n q = q - normalized(g.volume_gradient(\"\", q)) * g.volume_sample(\"\", q)\np.P = q\np.Cd = [0.9, 0.5 + 0.4 * (q[1] / 1.2), 0.3, 0.0]\n")
s.wire(field, id, 1)
s.save(folder)
## printf and warnings: a Detail run reporting the input's size and
## bounds to the node's console (select the node to read it).
func console(folder: str) -> unit!:
let s = Scene("console")
_ = s.add("Torus", [(0, 1.0), (1, 0.3), (2, 32.0), (3, 32.0)])
_ = s.code(0, "let g = k.input(0)\nk.printf(\"points %d, primitives %d\\n\", (f32)g.point_count(), (f32)g.prim_count())\nlet size = g.bounds().size\nk.printf(\"size %.2f x %.2f x %.2f\\n\", size[0], size[1], size[2])\nif g.point_count() > 500:\n k.warning(\"more than 500 points\")\n")
s.save(folder)
## Texts: each face named by its quadrant (k.sprintf, f.set_text), colored
## by the number read back out of its name, and one quadrant deleted by
## name downstream (a Blast of @name=quad_2), as names drive groups.
func names(folder: str) -> unit!:
let s = Scene("names")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 32.0), (3, 32.0)])
_ = s.code(3, "let c = f.center()\nlet quadrant = (0 if c[0] < 0.0 else 1) + (0 if c[2] < 0.0 else 2)\nf.set_text(\"name\", k.sprintf(\"quad_%d\", (f32)quadrant))\n")
_ = s.code(3, "let name = f.text(\"name\")\nlet number = name.slice(5, name.length()).to_i32()\nf.Cd = rand3((i64)number + 11)\n")
_ = s.add("Blast")
node_commands.set_text(s.w, 0, "@name=quad_2")
s.save(folder)
## A median filter with arrays: spiky terrain cleaned by taking, at each
## point, the median height of its neighborhood across the grid's edges
## (neighbor, k.floats, append, sort), which drops spikes a mean would only
## smear.
func median(folder: str) -> unit!:
let s = Scene("median")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 80.0), (3, 80.0)])
_ = s.code(1, "p.P[1] = snoise(p.P * 0.4) * 0.8\nif rand((i64)p.ptnum) < 0.05:\n p.P[1] += 1.5\n")
_ = s.code(1, "# The point and its neighbors across the grid's edges (and theirs).\nlet g = k.input(0)\nvar heights = k.floats()\nheights = heights.append(p.P[1])\nfor i in 0..<g.neighbor_count(p.ptnum):\n let n = g.neighbor(p.ptnum, i)\n heights = heights.append(g.point_P(n)[1])\n for j in 0..<g.neighbor_count(n):\n heights = heights.append(g.point_P(g.neighbor(n, j))[1])\nheights = heights.sort()\np.P[1] = heights.at(heights.len() // 2)\n")
s.save(folder)
## Houdini's neighbor-list idiom with array attributes: one Code node
## stores each point's neighbors as an i32 array (i[]@nbrs), a later one
## reads the list back and averages their heights, three passes, smoothing
## a noisy grid. The lists show in the Geometry Spreadsheet as [1, 2, 3].
func neighbor_lists(folder: str) -> unit!:
let s = Scene("neighbor_lists")
_ = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 80.0), (3, 80.0)])
_ = s.code(1, "p.P[1] = snoise(p.P * 0.5) * 0.6 + rand((i64)p.ptnum) * 0.3\nvar nbrs = k.ints()\nlet g = k.input(0)\nfor i in 0..<g.neighbor_count(p.ptnum):\n nbrs = nbrs.append(g.neighbor(p.ptnum, i))\np.set_i32_array(\"nbrs\", nbrs)\n")
for _ in 0..<3:
_ = s.code(1, "let g = k.input(0)\nlet nbrs = p.i32_array(\"nbrs\")\nvar sum = p.P[1]\nfor i in 0..<nbrs.len():\n sum += g.point_P(nbrs.at(i))[1]\np.P[1] = sum / (f32)(nbrs.len() + 1)\n")
s.save(folder)
# -- Volume Wrangles: Code running over voxels (Run Over 5). The Volume
# node's slots: 0 class (0 fog, 1 level set), 1-3 center, 4-6 size,
# 7 voxel size, 8 background, 9 initial value, 10 dense.
## A gyroid lattice cut to a ball: two distances in one voxel run, the
## larger kept (an intersection), then meshed.
func gyroid(folder: str) -> unit!:
let s = Scene("gyroid")
_ = s.add("Volume", [(0, 1.0), (4, 3.0), (5, 3.0), (6, 3.0), (7, 0.03), (9, 1.0)])
_ = s.code(5, "let q = x.P * k.f32(\"frequency\", 6.0)\nlet g = math32.sin(q[0]) * math32.cos(q[1]) + math32.sin(q[1]) * math32.cos(q[2]) + math32.sin(q[2]) * math32.cos(q[0])\nlet shell = math32.abs(g) / k.f32(\"frequency\", 6.0) - k.f32(\"thickness\", 0.03)\nlet ball = length(x.P) - 1.3\nx.value = math32.max(ball, shell)\n")
_ = s.add("Convert to Mesh")
s.save(folder)
## Metaballs: five spheres drifting with time, joined by a smooth minimum
## (the header's smin), then meshed. Scrub the timeline.
func metaballs(folder: str) -> unit!:
let s = Scene("metaballs")
_ = s.add("Volume", [(0, 1.0), (4, 4.0), (5, 4.0), (6, 4.0), (7, 0.04), (9, 1.0)])
let header = "## A smooth minimum: blends two distances over `r`.\nfunc smin(a: f32, b: f32, r: f32) -> f32:\n let h = clamp(0.5 + 0.5 * (b - a) / r, 0.0, 1.0)\n return lerp(b, a, h) - r * h * (1.0 - h)\n"
_ = s.code(5, "let t = (f32)k.time\nvar d: f32 = 100.0\nfor i in 0..<5:\n let a = (f32)i * 1.2566 + t * 0.7\n let c: f32[4] = [math32.cos(a) * 0.9, math32.sin(a * 1.7 + t) * 0.6, math32.sin(a) * 0.9, 0.0]\n d = smin(d, length(x.P - c) - 0.5, k.f32(\"blend\", 0.6))\nx.value = d\n", header)
_ = s.add("Convert to Mesh")
s.save(folder)
## A cloud: fog density from noise octaves, fading out toward the box's
## edge (the classic Volume Wrangle cloud). The viewport draws fog volumes
## as smoke.
func noise_cloud(folder: str) -> unit!:
let s = Scene("noise_cloud")
_ = s.add("Volume", [(0, 0.0), (4, 4.0), (5, 2.5), (6, 4.0), (7, 0.04)])
let header = "## Noise octaves: each twice as fine and half as strong.\nfunc fbm(at: f32[4]) -> f32:\n var sum: f32 = 0.0\n var scale: f32 = 1.0\n var weight: f32 = 0.5\n for i in 0..<5:\n sum += snoise(at * scale) * weight\n scale *= 2.0\n weight *= 0.5\n return sum\n"
_ = s.code(5, "let shape = 1.0 - length(x.P * [0.6, 1.0, 0.6, 0.0])\nlet n = fbm(x.P * k.f32(\"scale\", 1.2) + [0.0, 0.0, (f32)k.time * 0.2, 0.0])\nx.value = clamp((shape + n) * k.f32(\"density\", 2.0), 0.0, 1.0)\n", header)
# How the viewport draws it: denser smoke, softer self-shadowing.
_ = s.add("Volume Visualization", [(0, 2.0), (4, 0.6)])
s.save(folder)
## Fire: one Voxels run writes a flame's density (smoke) and its heat, hot
## at the bottom and cooling as it rises; Volume Visualization makes heat the
## emission field over thin dark smoke, colored by its emission color ramp
## (Houdini's): dark red where the heat is low, through orange and yellow to
## near white where it is highest.
func fire(folder: str) -> unit!:
let s = Scene("fire")
_ = s.add("Volume", [(0, 0.0), (2, 1.2), (4, 2.0), (5, 3.0), (6, 2.0), (7, 0.025)])
let header = "## Noise octaves: each twice as fine and half as strong.\nfunc fbm(at: f32[4]) -> f32:\n var sum: f32 = 0.0\n var scale: f32 = 1.0\n var weight: f32 = 0.5\n for i in 0..<5:\n sum += snoise(at * scale) * weight\n scale *= 2.0\n weight *= 0.5\n return sum\n"
_ = s.code(5, "# A flame: a column narrowing as it rises, torn by noise drifting up.\nlet height = clamp(x.P[1] / 2.4, 0.0, 1.0)\nlet radius = length(x.P * [1.0, 0.0, 1.0, 0.0])\nlet n = fbm(x.P * 2.2 - [0.0, (f32)k.time * 1.5, 0.0, 0.0])\nlet flame = clamp((0.55 * (1.0 - height) - radius) * 4.0 + n * 1.2, 0.0, 1.0)\nx.value = flame * (0.3 + height * 0.7)\nx.set_f32(\"heat\", flame * (1.0 - height) * (1.0 - height))\n", header)
# Slots: 0 density scale, 1-3 smoke color, 5 emission scale, 9 the
# emission ramp on, 10-11 the heat its ends stand for; text 2 the
# emission field, text 3 the ramp (dark red, deep orange, orange, pale
# yellow).
let look = s.add("Volume Visualization", [(0, 3.0), (1, 0.25), (2, 0.25), (3, 0.25), (5, 4.0), (9, 1.0), (10, 0.0), (11, 1.4)])
node_commands.set_text(s.w, 2, "heat")
node_commands.set_ramp_text(s.w, look, 3, [1.0, 3.0, 4.0, 0.0, 0.2, 0.01, 0.0, 1.0, 0.35, 1.0, 0.18, 0.01, 1.0, 0.7, 1.0, 0.55, 0.08, 1.0, 1.0, 1.0, 0.85, 0.4, 1.0])
s.save(folder)
## Two smokes in one set, a look each (Houdini's per-primitive volvis
## attributes): two fog grids, "left" and "right", each a noisy ball filled
## by its own Voxels run; one Volume Visualization tints the left grid
## warm, another the right one cool, each by its Group.
func two_smokes(folder: str) -> unit!:
let s = Scene("two_smokes")
for (name, x) in [("left", -1.3), ("right", 1.3)]:
_ = s.add("Volume", [(0, 0.0), (1, x), (4, 2.6), (5, 2.6), (6, 2.6), (7, 0.04)])
node_commands.set_text(s.w, 0, name)
_ = s.code(5, f"let center: f32[4] = [{x}, 0.0, 0.0, 0.0]\nlet n = snoise(x.P * 1.8) * 0.35 + snoise(x.P * 4.0) * 0.12\nx.value = clamp((1.0 - length(x.P - center) / 1.1 + n) * 1.5, 0.0, 1.0)\n")
node_commands.set_text(s.w, 0, name)
_ = s.add("Volume Visualization", [(0, 3.0), (1, 1.0), (2, 0.45), (3, 0.2), (4, 0.7)])
node_commands.set_text(s.w, 0, "left")
_ = s.add("Volume Visualization", [(0, 3.0), (1, 0.25), (2, 0.55), (3, 1.0), (4, 0.7)])
node_commands.set_text(s.w, 0, "right")
s.save(folder)
## Points to a level set, its surface eaten by noise, meshed: a sphere's
## points become a rough rock.
func eroded_points(folder: str) -> unit!:
let s = Scene("eroded_points")
_ = s.add("Sphere", [(0, 1.0), (1, 24.0), (2, 12.0)])
# Spheres wide enough to merge, and a band wide enough for the noise.
_ = s.add("Volume from Points", [(0, 0.3), (1, 0.03), (2, 6.0)])
_ = s.code(5, "let n = snoise(x.P * k.f32(\"scale\", 2.5)) + 0.4 * snoise(x.P * 6.0)\nx.value = x.value + n * k.f32(\"amount\", 0.08)\n")
_ = s.add("Convert to Mesh")
s.save(folder)
# -- Gaussian splats: the scenes start from a synthetic capture.
## A synthetic capture: a sphere's points jittered and colored warm to cool,
## baked into splats (Bake GSplats, Source Points), given SH degree 1 and a
## view-dependent sheen in its bands, so the wrangles that follow have every
## splat attribute to work on. No file is read.
func capture(s: Scene) -> unit!:
_ = s.add("Sphere", [(0, 1.0), (1, 64.0), (2, 32.0)])
_ = s.code(1, "# A fuzzy ball, warm at the bottom and cool on top.\nlet n = snoise(p.P * 3.0)\np.P = p.P * (1.0 + n * 0.08)\nlet t = fit(p.P[1], -1.0, 1.0, 0.0, 1.0)\np.Cd = [lerp(0.9, 0.2, t), lerp(0.4, 0.6, t), lerp(0.2, 0.9, t), 0.0]\np.set_f32(\"pscale\", 0.035)\n")
_ = s.add("Bake GSplats", [(0, 1.0), (3, 0.85)])
_ = s.add("GSplats SH Degree", [(0, 1.0)])
_ = s.code(1, "# A sheen in the first band: brighter where the surface faces the viewer.\nlet n = normalized(p.P)\nlet w = -k.f32(\"sheen\", 0.35)\np.set_sh(0, [-n[1] * w, -n[1] * w, -n[1] * w, 0.0])\np.set_sh(1, [n[2] * w, n[2] * w, n[2] * w, 0.0])\np.set_sh(2, [-n[0] * w, -n[0] * w, -n[0] * w, 0.0])\n")
## Splats fading out above a height: opacity scaled down between two
## heights (a smooth ramp from the Start to the End parameter).
func fade_by_height(folder: str) -> unit!:
let s = Scene("fade_by_height")
capture(s)
_ = s.code(1, "# Full opacity below Start, none above End.\nlet t = clamp(fit(p.P[1], k.f32(\"start\", -0.2), k.f32(\"end\", 0.8), 0.0, 1.0), 0.0, 1.0)\np.opacity *= 1.0 - t * t * (3.0 - 2.0 * t)\n")
s.save(folder)
## Recoloring: the view-independent color (Cd, the DC term) desaturated,
## while the SH bands, the view-dependent part, stay as they were.
func recolor(folder: str) -> unit!:
let s = Scene("recolor")
capture(s)
_ = s.code(1, "# Gray by the mean of the channels; Amount blends toward it.\nlet g = (p.Cd[0] + p.Cd[1] + p.Cd[2]) / 3.0\nlet a = k.f32(\"amount\", 1.0)\np.Cd = [lerp(p.Cd[0], g, a), lerp(p.Cd[1], g, a), lerp(p.Cd[2], g, a), 0.0]\n")
s.save(folder)
## Every splat squashed along world Y through its covariance (gsplat
## helpers): Σ' = S Σ S with S = diag(1, squash, 1), decomposed back into
## orient and scale, so each splat flattens into a disc lying in XZ.
func squash(folder: str) -> unit!:
let s = Scene("squash")
capture(s)
_ = s.code(1, "# Scale each splat's covariance along world Y.\nlet m = Mat3.scaling([1.0, k.f32(\"squash\", 0.2), 1.0, 0.0])\ngsplat.set_covariance(p, m.multiply(gsplat.covariance(p)).multiply(m))\n")
s.save(folder)
# -- Gaussian splat conversions: a bumpy ball made into splats, and back.
## A sphere bumped by noise along its normals and colored warm to cool by
## height: the surface the conversion scenes cover with splats.
func bumpy_ball(s: Scene) -> unit!:
_ = s.add("Sphere", [(0, 1.0), (1, 96.0), (2, 48.0)])
_ = s.code(1, "# Bumps along the normal (the sphere stands on the ground, its center at\n# height 1), warm at the bottom and cool on top.\nlet out = normalized(p.P - [0.0, 1.0, 0.0, 0.0])\np.P = p.P + out * (snoise(p.P * 2.5) * 0.12)\nlet t = fit(p.P[1], 0.0, 2.0, 0.0, 1.0)\np.Cd = [lerp(0.9, 0.15, t), lerp(0.45, 0.55, t), lerp(0.15, 0.85, t), 0.0]\n")
## Splats with no training: GSplats from Polygons covers the ball with flat
## discs, 3000 per unit of area, each taking the mesh's Cd where it lands.
func from_polygons(folder: str) -> unit!:
let s = Scene("from_polygons")
bumpy_ball(s)
_ = s.add("GSplats from Polygons", [(0, 3000.0)])
s.save(folder)
## Splats to a mesh: the ball's splats (discs half as thick as wide) into
## a fog volume by GSplats to Volume, then surfaced by Convert to Mesh where
## the density is 0.3.
func splats_to_mesh(folder: str) -> unit!:
let s = Scene("splats_to_mesh")
bumpy_ball(s)
_ = s.add("GSplats from Polygons", [(0, 3000.0), (2, 0.5)])
_ = s.add("GSplats to Volume", [(0, 0.02)])
_ = s.add("Convert to Mesh", [(1, 0.3)])
s.save(folder)
## Normals from GSplats on the ball's splats, shown as colors (Visualize
## Normals): each splat's shortest axis turned outward, then refined.
func splat_normals(folder: str) -> unit!:
let s = Scene("splat_normals")
bumpy_ball(s)
_ = s.add("GSplats from Polygons", [(0, 3000.0)])
_ = s.add("Normals from GSplats", [(4, 1.0)])
s.save(folder)
# -- Gaussian splat lighting and rendering.
## Relighting: the ball's splats (their Cd standing for a capture's lit
## colors) get normals, have their light taken out by Delight GSplats into
## `albedo`, and are lit again by a low sun from the side by Relight GSplats:
## diffuse and a highlight into Cd and the SH, shadowed through the splats.
func splat_relight(folder: str) -> unit!:
let s = Scene("splat_relight")
bumpy_ball(s)
_ = s.add("GSplats from Polygons", [(0, 3000.0)])
_ = s.add("Normals from GSplats")
let delit = s.add("Delight GSplats")
let light = s.add("Light", [(0, 3.0), (4, -35.0), (5, 60.0), (10, 3.0)])
# Objects are not wired: the splats read the light through an Import.
s.w.network.selected = delit
let lit = s.add("Merge")
s.wire(light, lit, 1)
_ = s.add("Relight GSplats")
s.save(folder)
## Splats and meshes in one render: the ball's splats (a stand-in capture)
## on a floor beside a sphere, under an area light, through a camera, into a
## Render node. In luce-render the splats emit their colors and absorb what
## passes through them: they shadow the floor and the sphere, the sphere
## shadows the floor, and the floor and sphere show through the splats' thin
## edges. Press Render on the Render node.
func splat_shadows(folder: str) -> unit!:
let s = Scene("splat_shadows")
bumpy_ball(s)
let splats = s.add("GSplats from Polygons", [(0, 3000.0)])
let floor = s.add("Grid", [(0, 8.0), (1, 8.0), (2, 8.0), (3, 8.0)])
s.w.network.selected = splats
let with_floor = s.add("Merge")
s.wire(floor, with_floor, 1)
let ball = s.add("Sphere", [(0, 0.5), (1, 48.0), (2, 24.0)])
let moved = s.add("Transform", [(1, 1.8), (2, 0.5), (3, 0.6)])
_ = ball
s.w.network.selected = with_floor
let scene = s.add("Merge")
s.wire(moved, scene, 1)
_ = scene
# The light and the camera are objects of their own at the root, beside
# the splats' container; Render renders every displayed object there.
_ = s.add("Light", [(1, -2.0), (2, 4.0), (3, 1.0), (4, -70.0), (5, -30.0), (10, 30.0), (14, 2.0), (15, 2.0)])
_ = s.add("Camera", [(1, 3.5), (2, 2.5), (3, 6.5), (4, -16.0), (5, 25.0)])
_ = s.add("Render", [(3, 64.0)])
s.save(folder)