86 lines
4.2 KiB
Luau
86 lines
4.2 KiB
Luau
--!strict
|
|
-- model/x3d: X3D (ISO/IEC 19775-1) in its XML encoding (.x3d, ISO/IEC 19776-1) and its classic VRML encoding (.x3dv,
|
|
-- 19776-2), plain or gzipped (.x3dz / .x3dvz), a thin format over codecs/model/Vrml (the engine model/vrml shares).
|
|
-- New code.
|
|
-- Reads: everything model/vrml reads, plus the X3D nodes: IndexedTriangleSet, TriangleSet, IndexedTriangleFanSet,
|
|
-- TriangleFanSet, IndexedTriangleStripSet, TriangleStripSet, IndexedQuadSet, QuadSet, LineSet, ColorRGBA,
|
|
-- CoordinateDouble, MultiTexture / MultiTextureCoordinate (the first), PhysicalMaterial / UnlitMaterial /
|
|
-- TwoSidedMaterial, OrthoViewpoint, CAD groups; XML ProtoDeclare / ProtoInstance, ROUTE elements, containerField;
|
|
-- head meta, components and UNIT length (-> meta.unit).
|
|
-- Writes: X3D 3.3 XML (default) or the classic encoding (opts.encoding = "x3dv"; the api passes the extension), the
|
|
-- same scene graph as model/vrml writes (ColorRGBA for colours with alpha, OrthoViewpoint for orthographic cameras).
|
|
-- Not supported: the binary encodings (.x3db Fast Infoset / EXI: UNSUPPORTED), 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 }?, -- textures / Inline files by uri or base name (buffer or string)
|
|
resolve: ((uri: string) -> any)?, -- or: reads them (the api sets it for paths)
|
|
triangulate: boolean?, -- fan-triangulate polygons (default true); false keeps "polygons" primitives
|
|
flipV: boolean?, -- texture t -> 1 - t (default true: glTF's convention)
|
|
segments: number?, -- Sphere / Cone / Cylinder segments around (default 32)
|
|
maxVertices: number?, -- per geometry (default 2^24)
|
|
}
|
|
export type EncodeOptions = {
|
|
encoding: ("xml" | "x3dv")?, -- default "xml"
|
|
format: string?, -- the api passes the extension: "x3dv" / "x3dvz" select the classic encoding
|
|
files: { [string]: any }?, -- receives the texture images (else url = texture.uri)
|
|
name: string?, -- the destination's file name (names the image files)
|
|
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
|