77 lines
3.1 KiB
Luau
77 lines
3.1 KiB
Luau
--!strict
|
|
-- model/x3d: X3D (ISO/IEC 19775-1) XML (.x3d, 19776-1) and classic VRML (.x3dv, 19776-2) encodings, plain or gzipped,
|
|
-- over codecs/model/Vrml; new code. Supports: the model/vrml set plus the X3D geometry and material nodes.
|
|
-- Not supported: binary encodings (.x3db Fast Infoset / EXI), X3D JSON, Text, NURBS, shaders, H-Anim skinning, scripts.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ 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 X3d = {
|
|
name = "x3d",
|
|
category = "model",
|
|
extensions = { "x3d", "x3dv", "x3dz", "x3dvz", "x3db" },
|
|
mime = { "model/x3d+xml", "model/x3d-vrml", "model/x3d+fastinfoset" },
|
|
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, xml = true, classic = 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 = {
|
|
encoding: ("xml" | "x3dv")?, -- default "xml"
|
|
format: string?, -- "x3dv" / "x3dvz" select the classic encoding
|
|
files: { [string]: any }?, -- else url = texture.uri
|
|
name: string?,
|
|
flipV: boolean?, -- default true (the inverse of decode)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function X3d.sniff(r: Bytes.Reader): number
|
|
local head = r:peek(1024)
|
|
if string.sub(head, 1, 3) == "\239\187\191" then
|
|
head = string.sub(head, 4)
|
|
end
|
|
if string.sub(head, 1, 5) == "#X3D " then
|
|
return 1
|
|
end
|
|
if string.sub(head, 1, 4) == "\224\0\0\1" and string.find(head, "x3d", 1, true) then
|
|
return 0.9
|
|
end
|
|
if string.find(head, "<X3D[%s>]") or string.find(head, "<!DOCTYPE X3D", 1, true) then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function X3d.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
|
local o: any = table.clone(opts or {})
|
|
o.format = "x3d"
|
|
return Vrml.decode(buffer.readstring(r.buf, r.base, r.limit - r.base), o)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function X3d.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
local o: any = table.clone(opts or {})
|
|
local classic = o.encoding == "x3dv" or o.format == "x3dv" or o.format == "x3dvz"
|
|
o.encoding = if classic then "x3dv" else "xml"
|
|
local text, err = Vrml.encode(scene, o)
|
|
if not text then
|
|
return nil, err
|
|
end
|
|
return buffer.fromstring(text), nil
|
|
end
|
|
|
|
return X3d
|