65 lines
2.7 KiB
Luau
65 lines
2.7 KiB
Luau
--!strict
|
|
-- model/vrml: VRML97 (ISO/IEC 14772-1, `#VRML V2.0 utf8`) .wrl / .wrz over codecs/model/Vrml; new code. Supports:
|
|
-- the scene graph, DEF / USE, PROTO / EXTERNPROTO, the geometry nodes, Viewpoints, lights, interpolator ROUTEs as
|
|
-- animations. Not supported: VRML 1.0, Text, Script, sensors, sound, NURBS; morph targets and skins on write.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Types = require("@rosetta/Types")
|
|
local Vrml = require("@rosetta/codecs/model/Vrml")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Wrl = {
|
|
name = "vrml",
|
|
category = "model",
|
|
extensions = { "wrl", "wrz", "vrml" },
|
|
mime = { "model/vrml", "x-world/x-vrml" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Vrml" },
|
|
caps = { decode = true, encode = true, materials = true, polygons = true, animation = true, cameras = true, lights = true, lines = true, points = true },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type DecodeOptions = {
|
|
files: { [string]: any }?, -- by uri or base name
|
|
resolve: ((uri: string) -> any)?,
|
|
triangulate: boolean?, -- default true
|
|
flipV: boolean?, -- default true
|
|
segments: number?, -- default 32
|
|
maxVertices: number?, -- per geometry (default 2^24)
|
|
}
|
|
export type EncodeOptions = {
|
|
files: { [string]: any }?, -- else url = texture.uri
|
|
name: string?,
|
|
flipV: boolean?, -- default true (the inverse of decode)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Wrl.sniff(r: Bytes.Reader): number
|
|
local head = r:peek(16)
|
|
if string.sub(head, 1, 3) == "\239\187\191" then
|
|
head = string.sub(head, 4)
|
|
end
|
|
return if string.sub(head, 1, 10) == "#VRML V2.0" then 1 elseif string.sub(head, 1, 6) == "#VRML " then 0.6 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function Wrl.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
|
local o: any = table.clone(opts or {})
|
|
o.format = "vrml"
|
|
return Vrml.decode(buffer.readstring(r.buf, r.base, r.limit - r.base), o)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Wrl.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
local o: any = table.clone(opts or {})
|
|
o.encoding = "vrml97"
|
|
local text, err = Vrml.encode(scene, o)
|
|
if not text then
|
|
return nil, err
|
|
end
|
|
return buffer.fromstring(text), nil
|
|
end
|
|
|
|
return Wrl
|