295 lines
9.2 KiB
Luau
295 lines
9.2 KiB
Luau
--!strict
|
|
-- model/obj/Mtl: Wavefront .mtl material libraries (Paul Bourke's MTL spec + the common PBR extension: Pr, Pm, Ps, Pc,
|
|
-- Pcr, aniso, anisor, Ke, map_Pr, map_Pm, map_Ke, norm). parse(text) -> { name -> { statement -> values } } keeping
|
|
-- every statement; toMaterial maps one onto the Scene Material (glTF PBR, Phong values in extras.obj); write is the
|
|
-- reverse (from extras.obj when present, else from the PBR fields).
|
|
local Types = require("@formats/Types")
|
|
|
|
local Mtl = {}
|
|
|
|
export type Map = { file: string, options: { [string]: { string } } } -- -o / -s / -bm / -clamp / ... with their values
|
|
export type Entry = { name: string, values: { [string]: { number } }, maps: { [string]: Map }, order: { string } }
|
|
|
|
-- the number of arguments of each texture map option (Bourke): -o / -s / -t take 1..3 numbers
|
|
local OPT_ARGS = { blendu = 1, blendv = 1, cc = 1, clamp = 1, mm = 2, bm = 1, boost = 1, texres = 1, imfchan = 1, type = 1 }
|
|
local MAPS = {
|
|
map_Ka = true, map_Kd = true, map_Ks = true, map_Ke = true, map_Ns = true, map_d = true, map_Tr = true,
|
|
map_bump = true, bump = true, map_Bump = true, disp = true, decal = true, refl = true, norm = true, map_Pr = true,
|
|
map_Pm = true, map_Ps = true, map_Pc = true, map_Pcr = true, map_RMA = true, map_ORM = true, map_aniso = true,
|
|
}
|
|
Mtl.MAPS = MAPS
|
|
|
|
local function parseMap(args: { string }): Map
|
|
local opts: { [string]: { string } } = {}
|
|
local i = 1
|
|
while i <= #args and string.sub(args[i], 1, 1) == "-" and tonumber(args[i]) == nil do
|
|
local key = string.sub(args[i], 2)
|
|
local vals = {}
|
|
i += 1
|
|
local n = OPT_ARGS[key]
|
|
if n then
|
|
for _ = 1, n do
|
|
if i <= #args then
|
|
table.insert(vals, args[i])
|
|
i += 1
|
|
end
|
|
end
|
|
else
|
|
-- -o / -s / -t: up to three numbers
|
|
while i <= #args and #vals < 3 and tonumber(args[i]) ~= nil and i < #args do
|
|
table.insert(vals, args[i])
|
|
i += 1
|
|
end
|
|
end
|
|
opts[key] = vals
|
|
end
|
|
-- the file name is the rest (it may contain spaces)
|
|
local file = table.concat(args, " ", i)
|
|
return { file = file, options = opts }
|
|
end
|
|
|
|
-- parse(text) -> { Entry } in file order
|
|
function Mtl.parse(text: string): { Entry }
|
|
local out: { Entry } = {}
|
|
local cur: Entry? = nil
|
|
for line in string.gmatch(text, "[^\r\n]+") do
|
|
local l = string.gsub(line, "#.*$", "")
|
|
local words = {}
|
|
for w in string.gmatch(l, "%S+") do
|
|
table.insert(words, w)
|
|
end
|
|
local key = words[1]
|
|
if key == "newmtl" then
|
|
cur = { name = table.concat(words, " ", 2), values = {}, maps = {}, order = {} }
|
|
table.insert(out, cur :: Entry)
|
|
elseif key and cur then
|
|
local e = cur :: Entry
|
|
local args = table.move(words, 2, #words, 1, {})
|
|
if MAPS[key] then
|
|
e.maps[key] = parseMap(args)
|
|
table.insert(e.order, key)
|
|
else
|
|
local nums = {}
|
|
for _, a in args do
|
|
local v = tonumber(a)
|
|
if v == nil then
|
|
nums = nil :: any
|
|
break
|
|
end
|
|
table.insert(nums, v)
|
|
end
|
|
if nums then
|
|
if e.values[key] == nil then
|
|
table.insert(e.order, key)
|
|
end
|
|
e.values[key] = nums
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
local MIME = { png = "image/png", jpg = "image/jpeg", jpeg = "image/jpeg", webp = "image/webp", gif = "image/gif", bmp = "image/bmp", tga = "image/x-tga", tif = "image/tiff", tiff = "image/tiff", dds = "image/vnd-ms.dds", ktx2 = "image/ktx2", exr = "image/x-exr", hdr = "image/vnd.radiance" }
|
|
function Mtl.mime(file: string): string?
|
|
local ext = string.match(file, "%.([%w]+)$")
|
|
return if ext then MIME[string.lower(ext)] else nil
|
|
end
|
|
|
|
-- toMaterial(entry, texture) -> Material: texture(map) returns the scene texture index for a map
|
|
function Mtl.toMaterial(e: Entry, texture: (Map) -> number): Types.Material
|
|
local m = Types.Scene.material(e.name)
|
|
local v = e.values
|
|
local kd = v.Kd
|
|
if kd and #kd >= 3 then
|
|
m.baseColor = { kd[1], kd[2], kd[3], 1 }
|
|
end
|
|
local d = if v.d then v.d[1] elseif v.Tr then 1 - v.Tr[1] else 1
|
|
m.baseColor[4] = d
|
|
if d < 1 then
|
|
m.alphaMode = "blend"
|
|
end
|
|
local ke = v.Ke
|
|
if ke and #ke >= 3 then
|
|
m.emissive = { ke[1], ke[2], ke[3] }
|
|
end
|
|
m.metallic = if v.Pm then v.Pm[1] else 0
|
|
if v.Pr then
|
|
m.roughness = v.Pr[1]
|
|
elseif v.Ns then
|
|
m.roughness = math.clamp(math.sqrt(2 / (math.max(v.Ns[1], 0) + 2)), 0, 1)
|
|
end
|
|
local function ref(key: string): Types.TextureRef?
|
|
local map = e.maps[key]
|
|
if not map or map.file == "" then
|
|
return nil
|
|
end
|
|
local r: Types.TextureRef = { texture = texture(map) }
|
|
local o, s = map.options.o, map.options.s
|
|
if o or s then
|
|
r.transform = {
|
|
offset = if o then { tonumber(o[1]) or 0, tonumber(o[2]) or 0 } else nil,
|
|
scale = if s then { tonumber(s[1]) or 1, tonumber(s[2]) or 1 } else nil,
|
|
}
|
|
end
|
|
if map.options.bm then
|
|
r.scale = tonumber(map.options.bm[1])
|
|
end
|
|
return r
|
|
end
|
|
m.baseColorTexture = ref("map_Kd")
|
|
m.emissiveTexture = ref("map_Ke")
|
|
m.normalTexture = ref("norm") or ref("map_bump") or ref("bump") or ref("map_Bump")
|
|
m.metallicRoughnessTexture = ref("map_ORM") or ref("map_RMA")
|
|
if m.emissiveTexture and not ke then
|
|
m.emissive = { 1, 1, 1 }
|
|
end
|
|
-- everything else as it was, for the writer and for Phong renderers
|
|
local extra: { [string]: any } = {}
|
|
for k, vals in v do
|
|
extra[k] = vals
|
|
end
|
|
local maps: { [string]: any } = {}
|
|
for k, map in e.maps do
|
|
maps[k] = { file = map.file, options = map.options, texture = texture(map) }
|
|
end
|
|
m.extras = { obj = { values = extra, maps = maps, order = e.order } }
|
|
return m
|
|
end
|
|
|
|
local function num(v: number): string
|
|
if v == math.floor(v) and math.abs(v) < 1e15 then
|
|
return string.format("%d", v)
|
|
end
|
|
for _, f in { "%.6g", "%.9g", "%.17g" } do
|
|
local s = string.format(f, v)
|
|
if tonumber(s) == v then
|
|
return s
|
|
end
|
|
end
|
|
return string.format("%.17g", v)
|
|
end
|
|
Mtl.num = num
|
|
|
|
-- write(materials, name, textureFile) -> MTL text. textureFile(index) -> the file name a map refers to.
|
|
function Mtl.write(materials: { Types.Material }, names: { string }, textureFile: (number) -> string?): string
|
|
local out = { "# written by formats-luau" }
|
|
for i, m in materials do
|
|
table.insert(out, "")
|
|
table.insert(out, "newmtl " .. names[i])
|
|
local saved = m.extras and m.extras.obj
|
|
local values: { [string]: { number } } = {}
|
|
local maps: { [string]: any } = {}
|
|
local order: { string } = {}
|
|
if type(saved) == "table" and type(saved.values) == "table" then
|
|
for k, vals in saved.values do
|
|
values[k] = vals
|
|
end
|
|
for k, mp in saved.maps or {} do
|
|
maps[k] = mp
|
|
end
|
|
order = table.clone(saved.order or {})
|
|
end
|
|
-- the PBR fields win over the saved Phong values they map to
|
|
local bc = m.baseColor or { 1, 1, 1, 1 }
|
|
values.Kd = { bc[1], bc[2], bc[3] }
|
|
if (bc[4] or 1) < 1 or values.d then
|
|
values.d = { bc[4] or 1 }
|
|
values.Tr = nil
|
|
end
|
|
local em = m.emissive or { 0, 0, 0 }
|
|
if em[1] ~= 0 or em[2] ~= 0 or em[3] ~= 0 or values.Ke then
|
|
values.Ke = { em[1], em[2], em[3] }
|
|
end
|
|
if not (saved and saved.values and saved.values.Ns and not saved.values.Pr) then
|
|
values.Pr = { m.roughness }
|
|
end
|
|
if m.metallic ~= 0 or values.Pm then
|
|
values.Pm = { m.metallic }
|
|
end
|
|
local function setMap(key: string, ref: Types.TextureRef?)
|
|
if ref then
|
|
local file = textureFile(ref.texture)
|
|
if file then
|
|
local prev = maps[key]
|
|
local options = if prev and prev.texture == ref.texture then prev.options else {}
|
|
options = table.clone(options or {})
|
|
if ref.transform then
|
|
if ref.transform.offset then
|
|
options.o = { num(ref.transform.offset[1]), num(ref.transform.offset[2]) }
|
|
end
|
|
if ref.transform.scale then
|
|
options.s = { num(ref.transform.scale[1]), num(ref.transform.scale[2]) }
|
|
end
|
|
end
|
|
if ref.scale and key ~= "map_Kd" then
|
|
options.bm = { num(ref.scale) }
|
|
end
|
|
maps[key] = { file = file, options = options, texture = ref.texture }
|
|
end
|
|
end
|
|
end
|
|
setMap("map_Kd", m.baseColorTexture)
|
|
setMap("map_Ke", m.emissiveTexture)
|
|
if m.normalTexture and not maps.map_bump and not maps.bump and not maps.map_Bump then
|
|
setMap("norm", m.normalTexture)
|
|
end
|
|
setMap("map_ORM", m.metallicRoughnessTexture)
|
|
-- the saved order first, then the rest sorted
|
|
local keys = {}
|
|
local seen = {}
|
|
for _, k in order do
|
|
if (values[k] or maps[k]) and not seen[k] then
|
|
table.insert(keys, k)
|
|
seen[k] = true
|
|
end
|
|
end
|
|
local rest = {}
|
|
for k in values do
|
|
if not seen[k] then
|
|
table.insert(rest, k)
|
|
end
|
|
end
|
|
for k in maps do
|
|
if not seen[k] and not values[k] then
|
|
table.insert(rest, k)
|
|
end
|
|
end
|
|
table.sort(rest)
|
|
for _, k in rest do
|
|
seen[k] = true
|
|
table.insert(keys, k)
|
|
end
|
|
for _, k in keys do
|
|
local mp = maps[k]
|
|
if mp and type(mp.file) == "string" then
|
|
local file = if type(mp.texture) == "number" then (textureFile(mp.texture) or mp.file) else mp.file
|
|
local parts = { k }
|
|
local okeys = {}
|
|
for ok in mp.options or {} do
|
|
table.insert(okeys, ok)
|
|
end
|
|
table.sort(okeys)
|
|
for _, ok in okeys do
|
|
table.insert(parts, "-" .. ok)
|
|
for _, ov in mp.options[ok] do
|
|
table.insert(parts, tostring(ov))
|
|
end
|
|
end
|
|
table.insert(parts, file)
|
|
table.insert(out, table.concat(parts, " "))
|
|
elseif values[k] then
|
|
local parts = { k }
|
|
for _, x in values[k] do
|
|
table.insert(parts, num(x))
|
|
end
|
|
table.insert(out, table.concat(parts, " "))
|
|
end
|
|
end
|
|
end
|
|
table.insert(out, "")
|
|
return table.concat(out, "\n")
|
|
end
|
|
|
|
return Mtl
|