rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,84 @@
|
||||
--!strict
|
||||
-- model/mdl: Quake 1 ("IDPO" version 6: model/mdl/Quake) and Half-Life 1 / GoldSrc ("IDST" version 10:
|
||||
-- model/mdl/HalfLife) models, read into a Scene. See the two submodules for what each maps. Quake skins need the
|
||||
-- game's 768-byte palette (gfx/palette.lmp, not redistributable: never built in): opts.palette (a buffer / string),
|
||||
-- else opts.resolve("gfx/palette.lmp") / opts.files["gfx/palette.lmp"], else a grey ramp (meta.source.palette =
|
||||
-- false). Half-Life textures carry their own palettes. Source engine MDL ("IDST" version 44+, with .vvd / .vtx
|
||||
-- files) is UNSUPPORTED.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Input = require("@rosetta/core/Input")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Quake = require("@rosetta/model/mdl/Quake")
|
||||
local HalfLife = require("@rosetta/model/mdl/HalfLife")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Mdl = {
|
||||
name = "mdl",
|
||||
category = "model",
|
||||
extensions = { "mdl" },
|
||||
mime = { "model/x-mdl" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/model/Mat4", "codecs/model/Rig" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "halflife" }, morph = true, skins = true, animation = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
palette: (buffer | string)?, -- Quake: the 768-byte palette (default: resolved gfx/palette.lmp, else grey)
|
||||
resolve: ((uri: string) -> (buffer | string)?)?,
|
||||
files: { [string]: buffer | string }?,
|
||||
frame: number?, -- Quake: the frame used as the base POSITION (default 1)
|
||||
morph: boolean?, -- Quake: frames as morph targets + animations (default true)
|
||||
fps: number?, -- Quake: animation frame rate (default 10)
|
||||
bodies: { number }?, -- Half-Life: the model picked for each body part (1-based, default 1)
|
||||
textures: (buffer | string)?, -- Half-Life: the "<name>t.mdl" texture file of a model without textures
|
||||
maxPixels: number?, -- default 2^24
|
||||
maxVertices: number?, -- default 2^24
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Mdl.sniff(r: Bytes.Reader): number
|
||||
local m = r:peek(4)
|
||||
local v = r:peekU8(4)
|
||||
if (m == "IDPO" and v == 6) or (m == "IDST" and v == 10) then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Mdl.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local size = r.limit - r.base
|
||||
if size < 8 then
|
||||
return nil, Err.new(Err.TRUNCATED, "header", 0)
|
||||
end
|
||||
local magic, version = r:peek(4), buffer.readi32(r.buf, r.base + 4)
|
||||
local scene: Types.Scene?, msg: string?
|
||||
if magic == "IDPO" then
|
||||
if version ~= 6 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "Quake MDL version " .. version, 4)
|
||||
end
|
||||
local pal = if opts and opts.palette then Bytes.toBuffer(opts.palette) else Input.sibling(opts, "gfx/palette.lmp")
|
||||
if pal and buffer.len(pal) < 768 then
|
||||
pal = nil
|
||||
end
|
||||
scene, msg = Quake.decode(r.buf, r.base, size, pal, opts :: any)
|
||||
elseif magic == "IDST" then
|
||||
if version ~= 10 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "studio model version " .. version .. " (Source engine models need .vvd / .vtx)", 4)
|
||||
end
|
||||
local o: any = opts and table.clone(opts :: any) or {}
|
||||
o.textures = if opts and opts.textures then Bytes.toBuffer(opts.textures) else nil
|
||||
scene, msg = HalfLife.decode(r.buf, r.base, size, o)
|
||||
else
|
||||
return nil, Err.new(Err.CORRUPT, "bad magic", 0)
|
||||
end
|
||||
if not scene then
|
||||
return nil, Err.from(msg)
|
||||
end
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Mdl
|
||||
Reference in new issue
Block a user