model/sourcemdl: .vmt materials through codecs/text/KeyValues (cdmaterials search, patch include / insert / replace, $basetexture / $bumpmap as .vtf references), docs/guide scene section

This commit is contained in:
4DBug committed 2026-11-01 18:49:39 -06:00
1 parent d8212c6839
commit 80fd9cf1e8
6 files changed
+280 -7

No files matched your search

+1 -1
View File
@@ -1016,7 +1016,7 @@ local list: { Entry } = {
path = "model/sourcemdl",
extensions = { "mdl" },
mime = { "model/x-source-mdl" },
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Half", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/SourceAnim" },
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Half", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/SourceAnim", "codecs/text/KeyValues" },
caps = { decode = true, encode = false, skins = true, morph = true, animation = true, lods = true },
load = function()
return require("@rosetta/model/sourcemdl")
+163
View File
@@ -0,0 +1,163 @@
--!strict
-- model/sourcemdl/Material: the MDL's texture names through $cdmaterials to materials/<dir><name>.vmt (the engine's
-- search order, then the bare name), parsed with codecs/text/KeyValues; "patch" materials (include + insert /
-- replace) applied. $basetexture / $bumpmap ($normalmap) become .vtf texture references for F.model.loadTextures;
-- $translucent / $alphatest / $nocull / $color map to the PBR fields, every parameter is kept in extras.vmt.
local Types = require("@rosetta/Types")
local Input = require("@rosetta/core/Input")
local KeyValues = require("@rosetta/codecs/text/KeyValues")
local Material = {}
local function dir(s: string): string
local d = string.gsub(s, "\\", "/")
d = string.gsub(d, "^/+", "")
if d ~= "" and string.sub(d, -1) ~= "/" then
d ..= "/"
end
return d
end
local function path(s: string): string
local p = string.gsub(string.gsub(s, "\\", "/"), "^/+", "")
return (string.gsub(p, "%.[vV][mMtT][tTfF]$", ""))
end
local function load(opts: any, uri: string): (string, KeyValues.Block?)
local b = Input.sibling(opts, uri) or Input.sibling(opts, string.lower(uri))
if not b then
return uri, nil
end
local kv = KeyValues.parse(b, { escapes = false, resolve = function(name: string): (buffer | string)?
return Input.sibling(opts, name)
end })
return uri, kv
end
-- { shader, params (lower-case keys) } of a parsed .vmt, patch includes applied (depth 8)
local function flatten(opts: any, kv: KeyValues.Block, depth: number): (string?, { [string]: any })
local first = kv[1]
if not first or type(first.value) ~= "table" then
return nil, {}
end
local shader, body = first.key, first.value :: KeyValues.Block
local params: { [string]: any } = {}
if string.lower(shader) == "patch" and depth < 8 then
local inc = KeyValues.get(body, "include")
local base: KeyValues.Block? = nil
if type(inc) == "string" then
local _, b = load(opts, path(inc) .. ".vmt")
base = b
end
if base then
shader, params = flatten(opts, base, depth + 1)
end
for _, p in body do
local k = string.lower(p.key)
if (k == "insert" or k == "replace") and type(p.value) == "table" then
for _, q in p.value :: KeyValues.Block do
local qk = string.lower(q.key)
if k == "insert" or params[qk] ~= nil then
params[qk] = if q.value == "" then nil else q.value
end
end
end
end
return shader, params
end
for _, p in body do
params[string.lower(p.key)] = p.value
end
return shader, params
end
local function num(v: any): number?
return if type(v) == "number" then v elseif type(v) == "string" then tonumber(v) else nil
end
local function color(v: any): { number }?
if type(v) ~= "string" then
return nil
end
local r, g, b = string.match(v, "^%s*[%[{]%s*([%d.eE+-]+)%s+([%d.eE+-]+)%s+([%d.eE+-]+)")
if not r then
return nil
end
local scale = if string.match(v, "^%s*{") then 1 / 255 else 1
return { (tonumber(r) :: number) * scale, (tonumber(g) :: number) * scale, (tonumber(b) :: number) * scale }
end
function Material.apply(scene: Types.Scene, textures: { { name: string } }, cdmaterials: { string }, opts: any?, warnings: { string })
if type(opts) ~= "table" or (opts.files == nil and opts.resolve == nil) then
return
end
local dirs: { string } = {}
for _, d in cdmaterials do
local nd = dir(d)
if not table.find(dirs, nd) then
table.insert(dirs, nd)
end
end
if not table.find(dirs, "") then
table.insert(dirs, "")
end
local texIndex: { [string]: number } = {}
local function texture(v: any): number?
if type(v) ~= "string" or v == "" then
return nil
end
local uri = "materials/" .. path(v) .. ".vtf"
local i = texIndex[string.lower(uri)]
if not i then
table.insert(scene.textures, { name = path(v), uri = uri, mime = "image/x-vtf" })
i = #scene.textures
texIndex[string.lower(uri)] = i
end
return i
end
for i, t in textures do
local mat = scene.materials[i]
local name = path(t.name)
local found, kv = nil, nil
for _, d in dirs do
local uri, b = load(opts, "materials/" .. d .. name .. ".vmt")
if b then
found, kv = uri, b
break
end
end
if not kv then
table.insert(warnings, "material " .. t.name .. ": no .vmt")
continue
end
local shader, p = flatten(opts, kv, 0)
local ex: { [string]: any } = mat.extras or {}
mat.extras = ex
ex.vmt = { path = found, shader = shader, params = p }
local base = texture(p["$basetexture"])
if base then
mat.baseColorTexture = { texture = base }
end
local bump = texture(p["$bumpmap"] or p["$normalmap"])
if bump then
mat.normalTexture = { texture = bump }
end
local c = color(p["$color2"]) or color(p["$color"])
if c then
mat.baseColor = { c[1], c[2], c[3], num(p["$alpha"]) or 1 }
elseif num(p["$alpha"]) then
mat.baseColor[4] = num(p["$alpha"]) :: number
end
if num(p["$translucent"]) == 1 then
mat.alphaMode = "blend"
elseif num(p["$alphatest"]) == 1 then
mat.alphaMode = "mask"
mat.alphaCutoff = num(p["$alphatestreference"]) or 0.5
end
if num(p["$nocull"]) == 1 then
mat.doubleSided = true
end
end
end
return Material
+8 -5
View File
@@ -1,8 +1,6 @@
--!strict
-- model/sourcemdl: Source engine studio models ("IDST" v44-49) with their .vvd vertices (LOD fixups), .vtx strips
-- (.dx90 / .dx80 / .sw), optional .phy collision and .ani animation blocks, all through opts.resolve / opts.files.
-- Skeleton, procedural bones, skin families, body parts, LODs, flexes, attachments, hitboxes, sequences and
-- animations; layouts from Source SDK 2013 studio.h / optimize.h, cross-checked with srctools and SourceIO (both MIT).
-- model/sourcemdl: Source engine studio models ("IDST" v44-49) with .vvd / .vtx (.dx90 / .dx80 / .sw) and optional .phy /
-- .ani / .vmt through opts.resolve / opts.files; Source SDK 2013 studio.h / optimize.h layouts, checked with srctools.
-- Not supported: v52+ frame animations, IK rules, movement, eyeball shading, $includemodel animations.
-- ------------------------------------------------------------------------------------------------ requires
@@ -15,6 +13,7 @@ local Vvd = require("@rosetta/model/sourcemdl/Vvd")
local Vtx = require("@rosetta/model/sourcemdl/Vtx")
local Phy = require("@rosetta/model/sourcemdl/Phy")
local Build = require("@rosetta/model/sourcemdl/Build")
local Material = require("@rosetta/model/sourcemdl/Material")
-- ------------------------------------------------------------------------------------------------ contract
local SourceMdl = {
@@ -22,7 +21,7 @@ local SourceMdl = {
category = "model",
extensions = { "mdl" },
mime = { "model/x-source-mdl" },
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Half", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/SourceAnim" },
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Half", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/SourceAnim", "codecs/text/KeyValues" },
caps = { decode = true, encode = false, skins = true, morph = true, animation = true, lods = true },
}
@@ -39,6 +38,7 @@ export type DecodeOptions = {
lod: number?, -- 1-based LOD for the primitive indices, default 1
animations: boolean?, -- default true
physics: boolean?, -- default true: read the .phy
materials: boolean?, -- default true: resolve materials/<cdmaterials><texture>.vmt
maxVertices: number?, -- default 2^24
maxFrames: number?, -- per animation, default 65536
}
@@ -158,6 +158,9 @@ function SourceMdl.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?,
if not scene then
return nil, Err.from(berr)
end
if not (opts and opts.materials == false) then
Material.apply(scene, h.textures, h.cdmaterials, opts, warnings)
end
local src = scene.meta.source :: { [string]: any }
src.vtx = vtxName
return scene, nil