ws-docs: guide pages cad, font, layered, music, playlist, subtitles, tilemap, video; runnable examples per category; README converting note
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
66cf0c4a6b
commit
fd9684b903
23 files changed
+2503
-5
No files matched your search
@@ -0,0 +1,81 @@
|
||||
-- examples/scene.luau: builds a 3D scene from scratch (a cube with a material, plus a voxel grid), edits it with
|
||||
-- F.ops.mesh and saves it as GLB, OBJ + MTL and STL. Run from the repository root: luau examples/scene.luau
|
||||
-- (docs/guide/scene.md explains the Scene type).
|
||||
local F = require("@formats")
|
||||
local S = F.Types.Scene
|
||||
|
||||
-- a unit cube: 8 corners, 12 triangles (indices are 0-based vertex numbers, as in glTF)
|
||||
local corners = {
|
||||
{ -0.5, -0.5, -0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, 0.5, -0.5 }, { -0.5, 0.5, -0.5 },
|
||||
{ -0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { -0.5, 0.5, 0.5 },
|
||||
}
|
||||
local tris = {
|
||||
0, 2, 1, 0, 3, 2, -- back (-z)
|
||||
4, 5, 6, 4, 6, 7, -- front (+z)
|
||||
0, 1, 5, 0, 5, 4, -- bottom
|
||||
3, 7, 6, 3, 6, 2, -- top
|
||||
0, 4, 7, 0, 7, 3, -- left
|
||||
1, 2, 6, 1, 6, 5, -- right
|
||||
}
|
||||
local pos = S.accessor(nil, "f32", 3, #corners) -- tightly packed: x, y, z per vertex
|
||||
for i, c in corners do
|
||||
for k = 1, 3 do
|
||||
buffer.writef32(pos.data, ((i - 1) * 3 + k - 1) * 4, c[k])
|
||||
end
|
||||
end
|
||||
local idx = S.accessor(nil, "u32", 1, #tris)
|
||||
for i, v in tris do
|
||||
buffer.writeu32(idx.data, (i - 1) * 4, v)
|
||||
end
|
||||
|
||||
local scene = S.fromPrimitive({ attributes = { POSITION = pos }, indices = idx, mode = "triangles", material = 1 }, "cube")
|
||||
local red = S.material("red")
|
||||
red.baseColor = { 0.8, 0.05, 0.05, 1 } -- linear RGBA
|
||||
red.metallic, red.roughness = 0, 0.6
|
||||
scene.materials[1] = red
|
||||
scene.nodes[1].translation = { 0, 0.5, 0 } -- sit on the ground (y up)
|
||||
scene.meta.up = "y"
|
||||
|
||||
-- flat normals: split the corners so each face has its own normal
|
||||
local prim = scene.meshes[1].primitives[1]
|
||||
prim = assert(F.ops.mesh.computeNormals(prim, { flat = true }))
|
||||
scene.meshes[1].primitives[1] = prim
|
||||
assert(prim.attributes.NORMAL.count == 36)
|
||||
|
||||
local lo, hi = F.ops.mesh.bounds(scene) -- world space: the node's translation applies
|
||||
print(string.format("cube bounds: (%g, %g, %g) .. (%g, %g, %g)", lo[1], lo[2], lo[3], hi[1], hi[2], hi[3]))
|
||||
assert(lo[2] == 0 and hi[2] == 1)
|
||||
|
||||
-- save: GLB (one buffer), OBJ (+ the .mtl in opts.files), STL (geometry only)
|
||||
local glb = assert(F.save(scene, nil, { format = "glb" }))
|
||||
local files = {}
|
||||
local obj = assert(F.save(scene, nil, { format = "obj", name = "cube.obj", files = files }))
|
||||
local stl = assert(F.save(scene, nil, { format = "stl" }))
|
||||
for name, data in files do
|
||||
print("obj writes " .. name .. " too (" .. (if type(data) == "string" then #data else buffer.len(data)) .. " bytes)")
|
||||
end
|
||||
print(string.format("glb %d bytes, obj %d bytes, stl %d bytes", buffer.len(glb), buffer.len(obj), buffer.len(stl)))
|
||||
|
||||
local back = assert(F.model.load(glb))
|
||||
assert(#back.meshes == 1 and back.materials[1].name == "red")
|
||||
|
||||
-- voxels: a 4 x 4 x 4 grid with a staircase, greedy-meshed into triangles
|
||||
local V = F.Types.Voxels
|
||||
local grid = V.new(4, 4, 4)
|
||||
grid.palette[1] = { 90, 160, 60, 255 }
|
||||
local cells = 0
|
||||
for x = 0, 3 do
|
||||
for y = 0, 3 do
|
||||
for z = 0, x do
|
||||
V.set(grid, x, y, z, 1) -- cell (x, y, z) = palette entry 1 (stored at x + 4 * (y + 4 * z))
|
||||
cells += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
local vprim = assert(F.ops.mesh.voxelsToPrimitive(grid))
|
||||
print(string.format("voxels: %d cells set, greedy mesh with %d triangles", cells, vprim.indices.count // 3))
|
||||
local vox = S.fromPrimitive(vprim, "stairs")
|
||||
vox.materials[1] = S.material("voxels")
|
||||
vprim.material = 1
|
||||
assert(F.save(vox, nil, { format = "glb" }))
|
||||
print("scene example: ok")
|
||||
Reference in new issue
Block a user