dymokomi/luce-3d
Source of release 0.9.6
0.9.6 / src / core / object.lucb
import interop
from luce_std import math
import memory
import ownership
import thread
import errors
from luce_geocore.geocore import Vector3, Matrix4, Geometry, Mesh
from core.material import Material, Color
pub enum LightKind as u8:
inactive = 0
ambient = 1
directional = 2
pub struct Light:
pub let kind: LightKind
pub let color: Color = Color(1.0, 1.0, 1.0)
pub let intensity: f64 = 0.0
pub let direction: Vector3 = Vector3(0.0, 1.0, 0.0)
## Open scene protocol. node returns one retained reference to stable transform
## and child storage; geometry/material returns transfer optional retained edges.
## polygon returns the mesh a polygon object draws from its GPU arrays, or none.
pub interface Object3D:
func node() -> interop.Reference[ObjectNode]
func geometry() -> interop.Interface[Geometry]?
func polygon() -> interop.Reference[Mesh]?
func material() -> interop.Interface[Material]?
func light() -> Light
struct Child:
let object: interop.Interface[Object3D]
let node: interop.Reference[ObjectNode]
var serial: u64 = 0
var revision: u64 = 0
pub func epoch() -> u64:
return revision
## Shared composition for scene objects, never a tagged bag of mesh/light state.
## Parents own child objects and nodes; children store only their parent's identity.
pub struct ObjectNode:
let identifier: u64
var parent: u64
## The children in `children[..<count]`; the storage grows by doubling
## and keeps its room, so adding a scene's objects one by one copies
## each child a constant number of times on average.
var children: Child[]
var count: usize
var location: Vector3
var angles: Vector3
var scaling: Vector3
var visible: bool
var changing: bool
pub func init() -> !:
if not thread.is_main():
error(interop.wrong_thread, "scene objects require the main thread")
serial = serial +? 1 else error(errors.limit, "scene object identities exhausted")
self.identifier = serial
self.parent = 0
var empty: Child[]
self.children = empty
self.count = 0
self.location = Vector3()
self.angles = Vector3()
self.scaling = Vector3(1.0, 1.0, 1.0)
self.visible = true
self.changing = false
pub func id() -> u64:
return self.identifier
pub func parent_id() -> u64:
return self.parent
pub func position() -> Vector3:
return self.location
pub func rotation() -> Vector3:
return self.angles
pub func scale() -> Vector3:
return self.scaling
## Setting what is already set changes nothing, so renderers keep what
## they prepared.
pub func set_position(position: Vector3) -> !:
try bounded(position)
if position == self.location: return
self.location = position
ObjectNode.invalidate()
pub func set_rotation(rotation: Vector3) -> !:
try bounded(rotation)
if rotation == self.angles: return
self.angles = rotation
ObjectNode.invalidate()
pub func set_scale(scale: Vector3) -> !:
try bounded(scale)
if math.abs(scale.x) < 0.000001 or math.abs(scale.y) < 0.000001 or math.abs(scale.z) < 0.000001:
error(errors.invalid, "scale magnitudes must be at least 0.000001")
if scale == self.scaling: return
self.scaling = scale
ObjectNode.invalidate()
pub func matrix() -> Matrix4:
return Matrix4.compose(self.location, self.angles, self.scaling)
pub func is_visible() -> bool:
return self.visible
pub func set_visible(visible: bool):
if visible == self.visible: return
self.visible = visible
ObjectNode.invalidate()
static func invalidate():
revision = revision +? 1 else trap("scene revision exhausted")
pub func child_count() -> i64:
return (i64)self.count
pub func child(index: i64) -> interop.Interface[Object3D]!:
if index < 0 or (usize)index >= self.count:
error(errors.invalid, "child index is outside this scene object")
return self.children[(usize)index].object.clone()
pub func add(child: interop.Interface[Object3D]) -> !:
if self.changing:
error(errors.busy, "scene membership changes cannot reenter the same parent")
self.changing = true
defer self.finish_change()
let node = try object_node(child)
errdefer node.release()
let state = try node.get()
if state.parent != 0:
error(errors.busy, "remove an object from its current parent before adding it")
if self.count >= 1024 or try contains(state, self.identifier, 0):
error(errors.invalid, "scene membership would exceed its limit or create a cycle")
if self.count == self.children.length:
let replacement = try new Child[8 if self.count == 0 else self.count * 2] --- in memory.heap
for index in 0..<self.count:
replacement[index] = self.children[index]
let previous = self.children
self.children = replacement
if previous.length > 0:
free(previous) in memory.heap
self.children[self.count] = Child(child.clone(), node)
self.count += 1
state.parent = self.identifier
ObjectNode.invalidate()
pub func remove(child: interop.Interface[Object3D]) -> bool!:
if self.changing:
error(errors.busy, "scene membership changes cannot reenter the same parent")
self.changing = true
defer self.finish_change()
var index: usize = 0
while index < self.count:
if self.children[index].object.owner == child.owner:
let removed = self.children[index]
# The later children move down one, keeping their order.
for later in index + 1..<self.count:
self.children[later - 1] = self.children[later]
self.count -= 1
removed.node.value().parent = 0
ObjectNode.invalidate()
removed.object.release()
removed.node.release()
return true
index += 1
return false
pub func clear():
if self.changing:
return
self.changing = true
defer self.finish_change()
let previous = self.children
let count = self.count
var empty: Child[]
self.children = empty
self.count = 0
for child in previous[..<count]:
child.node.value().parent = 0
ObjectNode.invalidate()
for child in previous[..<count]:
child.object.release()
child.node.release()
if previous.length > 0:
free(previous) in memory.heap
func finish_change():
self.changing = false
func dispose():
self.clear()
func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
for child in self.children[..<self.count]:
child.object.trace(visit, context)
child.node.trace(visit, context)
pub let object_node_type: interop.Type[ObjectNode] = interop.Type[ObjectNode]("ObjectNode", ObjectNode.dispose, ObjectNode.trace, main_thread = true)
pub func make_node() -> interop.Reference[ObjectNode]!:
return try interop.Reference[ObjectNode].adopt(new ObjectNode() in memory.heap, object_node_type, memory.heap)
pub func object_node(object: interop.Interface[Object3D]) -> interop.Reference[ObjectNode]!:
try object.enter()
defer object.leave()
return object.value().node()
func contains(node: ObjectNode*, identifier: u64, depth: usize) -> bool!:
if depth >= 64:
error(errors.limit, "a scene supports at most 64 levels")
if node.identifier == identifier:
return true
for child in node.children[..<node.count]:
if try contains(child.node.value(), identifier, depth + 1):
return true
return false
func bounded(vector: Vector3) -> !:
if not vector.is_finite() or math.abs(vector.x) > 1000000.0 or math.abs(vector.y) > 1000000.0 or math.abs(vector.z) > 1000000.0:
error(errors.invalid, "transform components must be finite and at most 1000000 in magnitude")