dymokomi/luced-3d
Source of release 0.11.5
0.11.5 / tests / editor / fog_display_tests.luc
## Fog volumes in the viewport: a Volume node (fog) filled by a Code node over
## its voxels shows no surface; the viewport ray-marches it, without the
## worker and with it, and its atlas goes to the GPU once per cook result,
## however often the view redraws or orbits. A Volume Visualization node's
## look (density, emission from a heat grid, its color ramp) changes the
## pixels and keeps the atlas.
from app import Luce3D
from workspace import Workspace
from offscreen_pixels import OffscreenPixels
import node_commands
import node_catalog
import code_node
import code_cook
import time
## A fog ball: a 2-unit Fog box of 0.05 voxels, its densities falling from 1
## at the center to 0 at a radius of 0.9, and the same values as "heat";
## then a Volume Visualization naming heat its emission field. Returns the
## Code node and the Visualization node.
func fog_ball(w: Workspace) -> (int, int)!:
node_commands.delete_selected(w)
node_commands.add_node(w, "Volume", 0.0, 0.0)
node_commands.add_node(w, "Code", 0.0, 100.0)
let code = w.network.selected
node_commands.set_parameter(w, code_cook.run_over_slot, 5.0)
node_commands.set_text(w, 0, "density")
code_node.set_code(w, code, code_cook.body_slot, "x.value = math32.max(0.0, 1.0 - length(x.P) / 0.9)\nx.set_f32(\"heat\", x.value)\n")
assert(w.network.require(code).failure == "")
node_commands.add_node(w, "Volume Visualization", 0.0, 200.0)
let look = w.network.selected
# Text 2: the emission field (0 is the Group, 1 the density field).
node_commands.set_text(w, 2, "heat")
node_commands.display_selected(w)
return (code, look)
## Poll the worker until its result is in.
func settle(w: Workspace) -> unit!:
let session = w.compute else trap("compute")
let deadline = time.now() + 20000000000
while session.busy and time.now() < deadline:
_ = w.poll_compute()
assert(not session.busy)
## Draw the editor's viewport over the whole texture; the luminance of its
## middle.
func middle(editor: Luce3D, view: OffscreenPixels) -> int!:
let target = view.begin()
editor.panels.view.draw(target)
view.end()
return view.luminance(400, 300)
pub func fog_display_tests() -> unit!:
let editor = Luce3D()
let w = editor.workspace
let (code, look) = fog_ball(w)
let data = w.network.evaluate_data(code)
assert(data.description().starts_with("2 volume grids") and data.field_preview() == none)
assert(w.batches.volumes.length == 1)
let volume = w.batches.volumes[0]
assert(volume.peak() > 0.9 and volume.upload_count() == 0)
let view = OffscreenPixels(800, 600)
let fogged = middle(editor, view)
assert(volume.upload_count() == 1)
# Orbiting redraws without uploading.
w.view.orbit(40.0, 10.0)
_ = middle(editor, view)
w.view.orbit(-40.0, -10.0)
assert(volume.upload_count() == 1 and middle(editor, view) == fogged)
# Without the fog the middle is the backdrop.
w.batches.volumes = []
let bare = middle(editor, view)
assert(fogged > bare + 20)
# Thicker smoke from the Visualization node: the same volume (its grids
# are unchanged), other pixels, no upload.
w.network.selected = look
node_commands.set_parameter(w, 0, 4.0)
assert(w.batches.volumes.length == 1 and w.batches.volumes[0] is volume and volume.look().density == 4.0)
let thick = middle(editor, view)
assert(thick != fogged and volume.upload_count() == 1)
# Orange emission from the heat grid: red rises over blue.
node_commands.set_parameter(w, 5, 2.0)
node_commands.set_parameter(w, 7, 0.4)
node_commands.set_parameter(w, 8, 0.0)
assert(w.batches.volumes[0] is volume)
let before_red = view.channel(400, 300, 0) - view.channel(400, 300, 2)
_ = middle(editor, view)
assert(view.channel(400, 300, 0) - view.channel(400, 300, 2) > before_red + 30 and volume.upload_count() == 1)
# The emission color field: a text row with the fields, in text slot 4.
# The emission color ramp: a row after the numbers, in text slot 3,
# showing the verb's flame ramp until edited. On, it colors the glow
# (one new strip, the same atlas); a ramp from blue to blue turns it blue.
let rows = node_catalog.fields("Volume Visualization")
let last = rows[rows.length - 1]
assert(rows[0].name == "Group" and rows[2].name == "Emission field" and rows[2].slot == 2 and last.kind == "color_ramp" and last.slot == 3)
assert(rows[3].name == "Emission color field" and rows[3].slot == 4 and w.network.require(look).texts.length == 5)
assert(node_catalog.verb_ramp("Volume Visualization", 3, "").length == 23)
let strips = volume.strip_upload_count()
node_commands.set_parameter(w, 9, 1.0)
_ = middle(editor, view)
assert(w.batches.volumes[0] is volume and volume.ramped() and volume.upload_count() == 1 and volume.strip_upload_count() == strips + 1)
w.begin("Ramp")
node_commands.set_ramp_text(w, look, 3, [1.0, 3.0, 2.0, 0.0, 0.1, 0.3, 1.0, 1.0, 1.0, 0.1, 0.3, 1.0, 1.0])
w.commit()
assert(w.network.require(look).texts[3] != "" and node_catalog.verb_ramp("Volume Visualization", 3, w.network.require(look).texts[3]).length == 13)
_ = middle(editor, view)
# Blue over white smoke: blue leads red by about 30 (GPUs round apart).
assert(view.channel(400, 300, 2) > view.channel(400, 300, 0) + 15 and volume.strip_upload_count() == strips + 2 and volume.upload_count() == 1)
node_commands.set_parameter(w, 9, 0.0)
# With the worker: the volume is made there and adopted once; a new
# result that keeps the node's stamp keeps the same volume.
w.enable_compute()
let session = w.compute else trap("compute")
var deadline = time.now() + 20000000000
while session.busy and time.now() < deadline:
_ = w.poll_compute()
assert(not session.busy and w.notice == "" and w.batches.volumes.length == 1)
let adopted = w.batches.volumes[0]
assert(middle(editor, view) > bare + 20 and adopted.upload_count() == 1)
w.rebuild()
deadline = time.now() + 20000000000
while session.busy and time.now() < deadline:
_ = w.poll_compute()
assert(w.batches.volumes.length == 1 and w.batches.volumes[0] is adopted)
let glowing = middle(editor, view)
assert(adopted.upload_count() == 1)
# A new look through the worker: the held volume takes it, no upload.
w.network.selected = look
node_commands.set_parameter(w, 5, 0.0)
deadline = time.now() + 20000000000
while session.busy and time.now() < deadline:
_ = w.poll_compute()
assert(w.batches.volumes.length == 1 and w.batches.volumes[0] is adopted and adopted.look().emission == 0.0)
assert(middle(editor, view) != glowing and adopted.upload_count() == 1)
editor.close()
print("PASS fog display: no surface preview, ray-marched with and without the worker, one upload per result, looks from Volume Visualization")
## The worker's first result replaces the local cook's scene even when it
## holds no mesh: the editor starts with the Cube cooked on the UI thread,
## then a project of fog only is opened before the worker publishes anything
## (as Luce3D.run does with a project on the command line). The Cube must
## leave the screen.
pub func fog_only_first_result_tests() -> unit!:
let editor = Luce3D()
let w = editor.workspace
let view = OffscreenPixels(800, 600)
let cube = middle(editor, view)
w.enable_compute()
_ = fog_ball(w)
settle(w)
assert(w.notice == "" and w.batches.volumes.length == 1 and w.publisher.meshes.length == 0)
# Without the fog the middle is the backdrop, not the Cube.
w.batches.volumes = []
let bare = middle(editor, view)
assert(cube > bare + 40)
editor.close()
print("PASS fog only first result: the worker's first result clears the local cook's meshes")