model/blend: material inputs traced through reroutes, mix, colour adjustments, colour ramps and node groups; unresolved links flagged with neutral values; roughness / metallic image textures with their channel; vertex colours; bump; UDIM images with the <UDIM> token and their tiles
This commit is contained in:
1 parent
8fd5a4c3f2
commit
3a56a66ac1
13 files changed
+1781
-531
No files matched your search
+261
-38
@@ -165952,10 +165952,13 @@ local __compat = __compat local buffer, bit32, type, __iter = __compat.buffer, _
|
||||
-- model/blend/Material: Blender materials to Scene PBR materials. With nodes: the Principled BSDF (the one feeding the
|
||||
-- material output, else the first): Base Color, Metallic, Roughness, Alpha, Emission (Color) x Emission Strength
|
||||
-- (KHR_materials_emissive_strength above 1, as Blender's glTF exporter writes it), images linked directly to Base
|
||||
-- Color / Emission and through a Normal Map node to Normal (its Strength as the scale), a Separate RGB / Color node to
|
||||
-- Roughness (G) and Metallic (B), UV Map nodes for the texCoord. Without nodes (or before 2.8 without a
|
||||
-- Principled node): Material.r / g / b / alpha, metallic, roughness. Images: packed files as encoded bytes, external
|
||||
-- ones as uris (with the bytes when opts.files / opts.resolve has them).
|
||||
-- Color / Emission / Roughness / Metallic and through a Normal Map node to Normal (its Strength as the scale), a
|
||||
-- Separate RGB / Color node to Roughness (G) and Metallic (B), UV Map nodes for the texCoord; links are followed
|
||||
-- through reroutes, Mix / MixRGB (A then B), Hue-Saturation, Bright-Contrast, Gamma, Invert, RGB Curves, Color Ramp
|
||||
-- and node groups; a linked input that reaches no image takes a neutral value (base colour 0.8 grey, Blender's
|
||||
-- default; metallic 0, roughness 0.5) and is flagged. Without nodes (or before 2.8 without a Principled node):
|
||||
-- Material.r / g / b / alpha, metallic, roughness. Images: packed files as encoded bytes, external ones as uris (with
|
||||
-- the bytes when opts.files / opts.resolve has them); tiled (UDIM) images keep the <UDIM> token in the uri.
|
||||
local Blend = __include("containers/Blend")
|
||||
local Types = __include("Types")
|
||||
|
||||
@@ -166021,6 +166024,34 @@ local f = ctx.f
|
||||
end
|
||||
end
|
||||
local tex= { name = name, sampler = sampler }
|
||||
local tiles= nil
|
||||
if (img:num("source") or 0) == 6 then -- IMA_SRC_TILED
|
||||
tiles = {}
|
||||
for _, t in __iter(img:list("tiles", "ImageTile")) do
|
||||
local label = t:str("label")
|
||||
table.insert(tiles , { number = t:num("tile_number") or 1001, label = (label and label ~= "" and{label }or{nil })[1]})
|
||||
end
|
||||
if #(tiles ) == 0 then
|
||||
table.insert(tiles , { number = 1001 })
|
||||
end
|
||||
if path and not string.find(path, "<UDIM>", 1, true) and not string.find(path, "<UVTILE>", 1, true) then
|
||||
-- the tile number in the file name becomes the token, as Blender's versioning does for older files
|
||||
local first = tostring((tiles )[1].number)
|
||||
local dir, file = string.match(path, "^(.-)([^/\\]*)$")
|
||||
local a, b = nil, nil
|
||||
local from = 1
|
||||
while true do
|
||||
local x, y = string.find(file or "", first, from, true)
|
||||
if not x then
|
||||
break
|
||||
end
|
||||
a, b, from = x, y, x + 1
|
||||
end
|
||||
if a and b and file then
|
||||
path = (dir or "") .. string.sub(file, 1, a - 1) .. "<UDIM>" .. string.sub(file, b + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
if pf then
|
||||
local blk = pf:target("data")
|
||||
local size = pf:num("size") or 0
|
||||
@@ -166037,7 +166068,8 @@ local f = ctx.f
|
||||
uri = string.gsub(uri, "\\", "/")
|
||||
if not tex.data then
|
||||
tex.uri = uri
|
||||
local data = fetch(ctx, uri)
|
||||
local first = (tiles and{(tostring(tiles[1].number) )}or{nil
|
||||
})[1] local data = fetch(ctx, (first and{(string.gsub(string.gsub(uri, "<UDIM>", first), "<UVTILE>", "u1_v1")) }or{uri})[1])
|
||||
if data then
|
||||
tex.data = data
|
||||
tex.mime = sniffMime(data, 0, buffer.len(data), uri)
|
||||
@@ -166045,7 +166077,12 @@ local f = ctx.f
|
||||
tex.mime = sniffMime(buffer.create(0), 0, 0, uri)
|
||||
end
|
||||
end
|
||||
tex.extras = { blend = { filepath = path } }
|
||||
tex.extras = { blend = { filepath = path, tiles = tiles } }
|
||||
if tiles then
|
||||
for _, t in __iter(tiles) do
|
||||
t.filename = (string.gsub(uri, "<UDIM>", tostring(t.number)))
|
||||
end
|
||||
end
|
||||
end
|
||||
if not tex.data and not tex.uri then
|
||||
tex.extras = { blend = { generated = true } }
|
||||
@@ -166061,14 +166098,19 @@ end
|
||||
|
||||
|
||||
|
||||
|
||||
local function graph(tree)
|
||||
local g= { nodes = {}, links = {} }
|
||||
for _, n in __iter(tree:list("nodes", "bNode")) do
|
||||
local node= { view = n, idname = n:str("idname") or "", inputs = {}, outputs = {} }
|
||||
local node= { view = n, idname = n:str("idname") or "", inputs = {}, byId = {}, outputs = {}, outIds = {} }
|
||||
for _, s in __iter(n:list("inputs", "bNodeSocket")) do
|
||||
local id = s:str("identifier") or s:str("name") or ""
|
||||
local nm = s:str("name") or id
|
||||
node.inputs[nm] = s
|
||||
node.first = node.first or s
|
||||
node.byId[id] = s
|
||||
if node.inputs[nm] == nil then
|
||||
node.inputs[nm] = s
|
||||
end
|
||||
if node.inputs[id] == nil then
|
||||
node.inputs[id] = s
|
||||
end
|
||||
@@ -166076,6 +166118,7 @@ local g= { nodes = {}, links = {} }
|
||||
for _, s in __iter(n:list("outputs", "bNodeSocket")) do
|
||||
if s.block then
|
||||
node.outputs[s.block.addr] = s:str("name") or ""
|
||||
node.outIds[s.block.addr] = s:str("identifier") or s:str("name") or ""
|
||||
end
|
||||
end
|
||||
if n.block then
|
||||
@@ -166086,7 +166129,7 @@ local g= { nodes = {}, links = {} }
|
||||
local from, fs, to = l:ptr("fromnode"), l:ptr("fromsock"), l:ptr("tosock")
|
||||
local fn = from and g.nodes[from]
|
||||
if fn and to ~= nil and bit32.band(l:num("flag") or 0, 0x10) == 0 then
|
||||
g.links[to] = { node = fn, socket = (fs and fn.outputs[fs]) or "" }
|
||||
g.links[to] = { node = fn, socket = (fs and fn.outputs[fs]) or "", ident = (fs and fn.outIds[fs]) or "" }
|
||||
end
|
||||
end
|
||||
return g
|
||||
@@ -166121,23 +166164,87 @@ if not s then
|
||||
return v and v:num("value")
|
||||
end
|
||||
|
||||
local function imageRef(ctx, g, uvNames, s, through)
|
||||
-- pass-through nodes: the inputs (by identifier) that may lead to an image, in the order tried
|
||||
local PASS= {
|
||||
NodeReroute = { "Input" }, ShaderNodeMix = { "A_Color", "B_Color" }, ShaderNodeMixRGB = { "Color1", "Color2" },
|
||||
ShaderNodeHueSaturation = { "Color" }, ShaderNodeBrightContrast = { "Color" }, ShaderNodeGamma = { "Color" },
|
||||
ShaderNodeInvert = { "Color" }, ShaderNodeRGBCurve = { "Color" }, ShaderNodeValToRGB = { "Fac" },
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
local function groupGraph(n)
|
||||
local tree = n.view:deref("id", 0, "bNodeTree")
|
||||
return (tree and{(graph(tree) )}or{nil
|
||||
})[1]end
|
||||
|
||||
-- the first image texture node a socket reaches through pass-through nodes and node groups (via: the nodes passed),
|
||||
-- else the node it is linked to (reroutes skipped)
|
||||
local function trace(g, s, via, stack, depth)
|
||||
local l = link(g, s)
|
||||
if not l then
|
||||
if not l or depth > 64 then
|
||||
return nil, nil
|
||||
end
|
||||
local n = l.node
|
||||
if through and n.idname == through then
|
||||
local inner = input(n, "Color", "Image")
|
||||
local ref, tn = imageRef(ctx, g, uvNames, inner, nil)
|
||||
return ref, (ref and{n }or{tn
|
||||
})[1] end
|
||||
if n.idname ~= "ShaderNodeTexImage" then
|
||||
if n.idname == "ShaderNodeTexImage" then
|
||||
return { node = n, g = g, output = l.socket }, n
|
||||
elseif n.idname == "ShaderNodeGroup" then
|
||||
local inner = groupGraph(n)
|
||||
local out= nil
|
||||
for _, x in __iter((inner and{inner.nodes }or{{} })[1])do
|
||||
if x.idname == "NodeGroupOutput" and (out == nil or bit32.band(x.view:num("flag") or 0, 2) ~= 0) then
|
||||
out = x
|
||||
end
|
||||
end
|
||||
local sock = out and out.byId[l.ident]
|
||||
if inner and sock then
|
||||
local stack2 = table.clone(stack)
|
||||
table.insert(stack2, { node = n, g = g })
|
||||
local sub = {}
|
||||
local hit = trace(inner, sock, sub, stack2, depth + 1)
|
||||
if hit then
|
||||
table.insert(via, n.idname)
|
||||
table.move(sub, 1, #sub, #via + 1, via)
|
||||
return hit, n
|
||||
end
|
||||
end
|
||||
return nil, n
|
||||
elseif n.idname == "NodeGroupInput" and #stack > 0 then
|
||||
local top = stack[#stack]
|
||||
local outer = top.node.byId[l.ident]
|
||||
local stack2 = table.clone(stack)
|
||||
table.remove(stack2)
|
||||
if outer then
|
||||
return trace(top.g, outer, via, stack2, depth + 1)
|
||||
end
|
||||
return nil, n
|
||||
end
|
||||
local ids = PASS[n.idname]
|
||||
if ids then
|
||||
for _, id in __iter(ids) do
|
||||
local sock = n.byId[id] or ((n.idname == "NodeReroute" and{n.first }or{nil})[1])
|
||||
local sub = {}
|
||||
local hit, last = trace(g, sock, sub, stack, depth + 1)
|
||||
if hit then
|
||||
table.insert(via, n.idname)
|
||||
table.move(sub, 1, #sub, #via + 1, via)
|
||||
return hit, n
|
||||
end
|
||||
if n.idname == "NodeReroute" then
|
||||
return nil, last or n
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil, n
|
||||
end
|
||||
|
||||
-- the TextureRef of an image texture node (its sampler, its UV Map node's texCoord)
|
||||
local function refOf(ctx, uvNames, hit)
|
||||
local n, g = hit.node, hit.g
|
||||
local img = n.view:deref("id", 0, "Image")
|
||||
if not img then
|
||||
return nil, n
|
||||
return nil
|
||||
end
|
||||
local sampler= {}
|
||||
local custom = false
|
||||
@@ -166157,7 +166264,7 @@ local l = link(g, s)
|
||||
end
|
||||
local t = texture(ctx, img, (custom and{sampler }or{nil})[1])
|
||||
if not t then
|
||||
return nil, n
|
||||
return nil
|
||||
end
|
||||
local ref= { texture = t }
|
||||
local vec = link(g, input(n, "Vector"))
|
||||
@@ -166168,7 +166275,31 @@ local l = link(g, s)
|
||||
ref.texCoord = uvNames[name]
|
||||
end
|
||||
end
|
||||
return ref, n
|
||||
return ref
|
||||
end
|
||||
|
||||
local function imageRef(ctx, g, uvNames, s)
|
||||
local via = {}
|
||||
local hit, last = trace(g, s, via, {}, 0)
|
||||
if not hit then
|
||||
return nil, last, via, nil
|
||||
end
|
||||
return refOf(ctx, uvNames, hit), last, via, hit
|
||||
end
|
||||
|
||||
-- the vertex colour layer a Vertex Color / Attribute node reads
|
||||
local function vertexColor(n)
|
||||
if not n then
|
||||
return nil
|
||||
end
|
||||
if n.idname == "ShaderNodeVertexColor" then
|
||||
local st = n.view:deref("storage", 0, "NodeShaderVertexColor")
|
||||
return st and st:str("layer_name") or ""
|
||||
elseif n.idname == "ShaderNodeAttribute" then
|
||||
local st = n.view:deref("storage", 0, "NodeShaderAttribute")
|
||||
return st and st:str("name") or ""
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function principled(g)
|
||||
@@ -166232,47 +166363,139 @@ local key = mv.block and mv.block.addr
|
||||
end
|
||||
m.metallic = value(input(bsdf, "Metallic"), "float") or m.metallic
|
||||
m.roughness = value(input(bsdf, "Roughness"), "float") or m.roughness
|
||||
local bref = imageRef(ctx, g, uvNames, input(bsdf, "Base Color"), nil)
|
||||
local linked= {}
|
||||
local via= {}
|
||||
-- node names as Blender 3.4+ shows them (its versioning turns shader MixRGB nodes into Mix nodes)
|
||||
local function api(idname)
|
||||
return idname == "ShaderNodeMixRGB" and f.version < 304 and"ShaderNodeMix" or idname
|
||||
end
|
||||
local function resolve(key, sock)
|
||||
if not link(g , sock) then
|
||||
return nil, nil, nil
|
||||
end
|
||||
local ref, last, passed, hit = imageRef(ctx, g , uvNames, sock)
|
||||
if ref then
|
||||
if #passed > 0 then
|
||||
for i, x in __iter(passed) do
|
||||
passed[i] = api(x)
|
||||
end
|
||||
via[key] = passed
|
||||
end
|
||||
end
|
||||
return ref, last, hit
|
||||
end
|
||||
local function unresolved(key, sock)
|
||||
local l = link(g , sock)
|
||||
if l then
|
||||
local _, last = trace(g , sock, {}, {}, 0) -- reroutes skipped
|
||||
linked[key] = api((last or l.node).idname)
|
||||
end
|
||||
end
|
||||
local baseSock = input(bsdf, "Base Color")
|
||||
local bref, blast = resolve("baseColor", baseSock)
|
||||
if bref then
|
||||
m.baseColorTexture = bref
|
||||
m.baseColor = { 1, 1, 1, alpha }
|
||||
elseif link(g, baseSock) then
|
||||
local layer = vertexColor(blast)
|
||||
if layer then
|
||||
extras.vertexColor = layer
|
||||
m.baseColor = { 1, 1, 1, alpha }
|
||||
else
|
||||
m.baseColor = { 0.8, 0.8, 0.8, alpha } -- Blender's default base colour, never the linked socket's value
|
||||
unresolved("baseColor", baseSock)
|
||||
end
|
||||
end
|
||||
local aref = imageRef(ctx, g, uvNames, input(bsdf, "Alpha"), nil)
|
||||
local alphaSock = input(bsdf, "Alpha")
|
||||
local aref = resolve("alpha", alphaSock)
|
||||
if aref and not m.baseColorTexture then
|
||||
m.baseColorTexture = aref
|
||||
elseif not aref and link(g, alphaSock) then
|
||||
m.baseColor[4] = 1
|
||||
unresolved("alpha", alphaSock)
|
||||
end
|
||||
local nref, nnode = imageRef(ctx, g, uvNames, input(bsdf, "Normal"), "ShaderNodeNormalMap")
|
||||
if nref then
|
||||
nref.scale = (nnode and{value(nnode.inputs.Strength, "float") or 1 }or{1
|
||||
})[1] m.normalTexture = nref
|
||||
local nl = link(g, input(bsdf, "Normal"))
|
||||
if nl and nl.node.idname == "ShaderNodeNormalMap" then
|
||||
local nref = resolve("normal", input(nl.node, "Color"))
|
||||
if nref then
|
||||
nref.scale = value(nl.node.inputs.Strength, "float") or 1
|
||||
m.normalTexture = nref
|
||||
else
|
||||
unresolved("normal", input(bsdf, "Normal"))
|
||||
end
|
||||
elseif nl and nl.node.idname == "ShaderNodeBump" then
|
||||
local href = resolve("bump", input(nl.node, "Height"))
|
||||
if href then
|
||||
extras.bumpTexture = href
|
||||
extras.bumpStrength = value(input(nl.node, "Strength"), "float") or 1
|
||||
extras.bumpDistance = value(input(nl.node, "Distance"), "float") or 1
|
||||
else
|
||||
unresolved("normal", input(bsdf, "Normal"))
|
||||
end
|
||||
elseif nl then
|
||||
unresolved("normal", input(bsdf, "Normal"))
|
||||
end
|
||||
local rl = link(g, input(bsdf, "Roughness"))
|
||||
local ml = link(g, input(bsdf, "Metallic"))
|
||||
local sep = (rl and rl.node) or (ml and ml.node)
|
||||
if sep and (sep.idname == "ShaderNodeSeparateRGB" or sep.idname == "ShaderNodeSeparateColor") then
|
||||
local mref = imageRef(ctx, g, uvNames, input(sep, "Image", "Color"), nil)
|
||||
if mref then
|
||||
m.metallicRoughnessTexture = mref
|
||||
if rl then
|
||||
m.roughness = 1
|
||||
local channelOf = { Red = "R", Green = "G", Blue = "B", R = "R", G = "G", B = "B", Alpha = "A", Color = "R", Fac = "R" }
|
||||
local function scalar(key, sock)
|
||||
local l = link(g , sock)
|
||||
if not l then
|
||||
return nil
|
||||
end
|
||||
local sep = l.node
|
||||
if sep.idname == "ShaderNodeSeparateRGB" or sep.idname == "ShaderNodeSeparateColor" then
|
||||
local ref = resolve(key, input(sep, "Image", "Color"))
|
||||
if ref then
|
||||
m.metallicRoughnessTexture = m.metallicRoughnessTexture or table.clone(ref)
|
||||
local r = (table.clone(ref) )
|
||||
r.channel = channelOf[l.socket] or l.socket
|
||||
extras[key .. "Texture"] = r
|
||||
return true
|
||||
end
|
||||
if ml then
|
||||
m.metallic = 1
|
||||
else
|
||||
local ref, _, hit = resolve(key, sock)
|
||||
if ref and hit then
|
||||
local r = ref
|
||||
r.channel = channelOf[hit.output] or hit.output
|
||||
extras[key .. "Texture"] = r
|
||||
return true
|
||||
end
|
||||
end
|
||||
unresolved(key, sock)
|
||||
return false
|
||||
end
|
||||
local rs = scalar("roughness", input(bsdf, "Roughness"))
|
||||
if rs == true then
|
||||
m.roughness = 1
|
||||
elseif rs == false then
|
||||
m.roughness = 0.5
|
||||
end
|
||||
local ms = scalar("metallic", input(bsdf, "Metallic"))
|
||||
if ms == true then
|
||||
m.metallic = 1
|
||||
elseif ms == false then
|
||||
m.metallic = 0
|
||||
end
|
||||
local es = input(bsdf, "Emission Color", "Emission")
|
||||
local ecol = value(es, "rgba") or { 0, 0, 0, 1 }
|
||||
local strength = value(input(bsdf, "Emission Strength"), "float") or 1
|
||||
local eref = imageRef(ctx, g, uvNames, es, nil)
|
||||
local eref = resolve("emissive", es)
|
||||
if eref then
|
||||
m.emissiveTexture = eref
|
||||
ecol = { 1, 1, 1, 1 }
|
||||
elseif link(g, es) then
|
||||
ecol = { 0, 0, 0, 1 }
|
||||
unresolved("emissive", es)
|
||||
end
|
||||
emissive(m, ecol, strength)
|
||||
if not eref and m.emissive[1] == 0 and m.emissive[2] == 0 and m.emissive[3] == 0 then
|
||||
m.emissive = { 0, 0, 0 }
|
||||
end
|
||||
if next(linked) ~= nil then
|
||||
extras.linked = linked
|
||||
end
|
||||
if next(via) ~= nil then
|
||||
extras.via = via
|
||||
end
|
||||
local alphaLinked = link(g, input(bsdf, "Alpha")) ~= nil
|
||||
if alpha < 1 or alphaLinked then
|
||||
local bm = mv:num("blend_method") or 0
|
||||
|
||||
Reference in new issue
Block a user