dymokomi/luce-usd
Source of release 0.9.1
0.9.1 / src / convert / intrinsics.lucb
## Intrinsic gprims (UsdGeomCube, Sphere, Cylinder, Cone, Capsule, Plane)
## tessellated into polygon arrays shaped like a Mesh prim's, after
## pxr/imaging/geomUtil's mesh generators: they then merge like any mesh.
## Round shapes use 24 segments around and 12 along; the axis attribute
## turns the Z-built shape onto X or Y.
let around: usize = 24
let along: usize = 12
## Polygon arrays being generated.
struct Shape:
var points: Growing[f32]
var counts: Growing[i32]
var indices: Growing[i32]
func init():
self.points = Growing[f32]()
self.counts = Growing[i32]()
self.indices = Growing[i32]()
func close():
self.points.close()
self.counts.close()
self.indices.close()
func point(x: f64, y: f64, z: f64) -> i32!:
try self.points.push((f32)x)
try self.points.push((f32)y)
try self.points.push((f32)z)
return (i32)(self.points.count // 3 - 1)
func face(corners: const i32[]) -> !:
try self.counts.push((i32)corners.length)
for corner in corners: try self.indices.push(corner)
## The arrays of intrinsic gprim `gprim`, as values a MeshSource holds.
func intrinsic_values(stage: Stage*, gprim: const Gprim*) -> (Value, Value, Value)!:
let t = stage.tokens
var shape = Shape()
defer shape.close()
let path = gprim.path
match gprim.kind:
.cube: try cube_shape(&shape, (try stage.number_value(path, t.size, 2.0)) * 0.5)
.sphere: try revolved(&shape, try stage.number_value(path, t.radius, 1.0), 0.0)
.cylinder:
let radius = try stage.number_value(path, t.radius, 1.0)
try tube(&shape, radius, radius, try stage.number_value(path, t.height, 2.0))
.cone: try tube(&shape, try stage.number_value(path, t.radius, 1.0), 0.0, try stage.number_value(path, t.height, 2.0))
.capsule: try revolved(&shape, try stage.number_value(path, t.radius, 0.5), try stage.number_value(path, t.height, 1.0))
_: try plane_shape(&shape, (try stage.number_value(path, t.width, 2.0)) * 0.5, (try stage.number_value(path, t.length, 2.0)) * 0.5)
let axis = try stage.token_value(path, t.axis, t.z_axis)
turn(&shape, axis, stage)
var points = try Column[u8].copy_of(u8[]((u8*)shape.points.items.data, shape.points.count * 4))
errdefer points.release()
var counts = try Column[u8].copy_of(u8[]((u8*)shape.counts.items.data, shape.counts.count * 4))
errdefer counts.release()
let indices = try Column[u8].copy_of(u8[]((u8*)shape.indices.items.data, shape.indices.count * 4))
return (Value.of_array(ValueKind.vec3f, shape.points.count // 3, points), Value.of_array(ValueKind.int, shape.counts.count, counts), Value.of_array(ValueKind.int, shape.indices.count, indices))
## A cube of half size `h`, faces outward.
func cube_shape(shape: Shape*, h: f64) -> !:
for at in 0..<(usize)8: _ = try shape.point(h if at & 1 != 0 else -h, h if at & 2 != 0 else -h, h if at & 4 != 0 else -h)
let faces: i32[24] = [0, 2, 3, 1, 4, 5, 7, 6, 0, 1, 5, 4, 2, 6, 7, 3, 0, 4, 6, 2, 1, 3, 7, 5]
for face in 0..<(usize)6: try shape.face(faces[face * 4..<face * 4 + 4])
## A sphere of `radius` (height 0) or a capsule: hemispheres `height` apart
## along Z, rings of quads, a triangle fan at each pole.
func revolved(shape: Shape*, radius: f64, height: f64) -> !:
let bottom = try shape.point(0.0, 0.0, -radius - height * 0.5)
# Rings from the bottom pole up; a capsule repeats the equator.
var rings: usize = 0
for ring in 1..<along:
let angle = math.pi * ((f64)ring / (f64)along - 0.5)
let z = radius * math.sin(angle)
let r = radius * math.cos(angle)
let halves: usize = 2 if height > 0.0 and ring == along // 2 else 1
for half in 0..<halves:
let lift = height * 0.5 if (z > 0.0 or half == 1) else -height * 0.5
for step in 0..<around:
let theta = 2.0 * math.pi * (f64)step / (f64)around
_ = try shape.point(r * math.cos(theta), r * math.sin(theta), z + lift)
rings += 1
let top = try shape.point(0.0, 0.0, radius + height * 0.5)
let n = (i32)around
for step in 0..<n:
let next = (step + 1) % n
let corners: i32[3] = [bottom, 1 + next, 1 + step]
try shape.face(corners[..<3])
for ring in 0..<(i32)rings - 1:
let base = 1 + ring * n
for step in 0..<n:
let next = (step + 1) % n
let corners: i32[4] = [base + step, base + next, base + n + next, base + n + step]
try shape.face(corners[..<4])
let last = 1 + ((i32)rings - 1) * n
for step in 0..<n:
let next = (step + 1) % n
let corners: i32[3] = [top, last + step, last + next]
try shape.face(corners[..<3])
## A cylinder (equal radii) or cone (top radius 0) of `height` along Z,
## capped.
func tube(shape: Shape*, bottom_radius: f64, top_radius: f64, height: f64) -> !:
let n = (i32)around
for step in 0..<around:
let theta = 2.0 * math.pi * (f64)step / (f64)around
_ = try shape.point(bottom_radius * math.cos(theta), bottom_radius * math.sin(theta), -height * 0.5)
let apex = top_radius == 0.0
if apex: _ = try shape.point(0.0, 0.0, height * 0.5)
else:
for step in 0..<around:
let theta = 2.0 * math.pi * (f64)step / (f64)around
_ = try shape.point(top_radius * math.cos(theta), top_radius * math.sin(theta), height * 0.5)
let bottom = try shape.point(0.0, 0.0, -height * 0.5)
for step in 0..<n:
let next = (step + 1) % n
if apex:
let corners: i32[3] = [step, next, n]
try shape.face(corners[..<3])
else:
let corners: i32[4] = [step, next, n + next, n + step]
try shape.face(corners[..<4])
let cap: i32[3] = [bottom, next, step]
try shape.face(cap[..<3])
if not apex:
let top = try shape.point(0.0, 0.0, height * 0.5)
for step in 0..<n:
let corners: i32[3] = [top, n + step, n + (step + 1) % n]
try shape.face(corners[..<3])
## A plane of half width `w` (X) and half length `l` (Y), facing +Z.
func plane_shape(shape: Shape*, w: f64, l: f64) -> !:
_ = try shape.point(-w, -l, 0.0)
_ = try shape.point(w, -l, 0.0)
_ = try shape.point(w, l, 0.0)
_ = try shape.point(-w, l, 0.0)
let corners: i32[4] = [0, 1, 2, 3]
try shape.face(corners[..<4])
## Turn a Z-built shape onto `axis` (X: Z to X; Y: Z to Y), keeping it
## right-handed.
func turn(shape: Shape*, axis: u32, stage: const Stage*):
if axis == stage.tokens.z_axis: return
let values = shape.points.items[..<shape.points.count]
var at: usize = 0
while at < values.length:
let x = values[at]
let y = values[at + 1]
let z = values[at + 2]
if axis == stage.tokens.x_axis:
values[at] = z
values[at + 1] = x
values[at + 2] = y
else:
values[at] = y
values[at + 1] = z
values[at + 2] = x
at += 3