Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to 3d library and 3d-anim sample #528

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Library/3dbase/3DAnimation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function D3Anim.updateBones()
for n,bd in ipairs(k.animBones) do
local b=bd.bone
-- local name=b.name

local m=Matrix.new()
m:setMatrix(b.poseIMat:getMatrix())
while true do
Expand Down Expand Up @@ -101,6 +100,7 @@ function D3Anim.tick()
end

function D3Anim.setBonesPose(m,poses)
-- local m=m.bonesTop
m=m.bonesTop
local a=D3Anim._animatedModel[m]
for bone,srtl in pairs(poses) do
Expand Down Expand Up @@ -131,7 +131,11 @@ function D3Anim.setAnimation(model,anim,track,loop,transitionTime,speed)
oldStart=tm
oldLen=transitionTime
end
an.animations[track]={anim=anim,tm=tm,loop=loop,oldAnim=oldAnim,oldStart=oldStart,oldLen=oldLen,speed=speed}
an.animations[track]={
anim=anim,tm=tm,loop=loop,
oldAnim=oldAnim,oldStart=oldStart,oldLen=oldLen,
speed=speed
}
end

function D3Anim._addMesh(m)
Expand Down
46 changes: 31 additions & 15 deletions Library/3dbase/3DGdxLoader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "json"
local function buildPart(gdx,mparts,bones)
local mp=mparts[gdx.meshpartid]
assert(mp,"No such part id:"..gdx.meshpartid)
--print("NodeMesh:",gdx.meshpartid)
-- print("NodeMesh:",gdx.meshpartid)
if gdx.bones then
for _,b in ipairs(gdx.bones) do
bones[b.node]=true
Expand All @@ -13,11 +13,17 @@ local function buildPart(gdx,mparts,bones)
b.translation=nil
end
end
local m={ vertices=mp.v, texcoords=mp.t, normals=mp.n, indices=mp.i, animdata=mp.bw and mp.bi and { bw=mp.bw, bi=mp.bi}, type="mesh",
material=gdx.materialid,bones=gdx.bones,name=gdx.meshpartid }
--[[treeDesc.material=mtl
treeDesc.color=m.color
treeDesc.material=m.material]]
local m={
vertices=mp.v,
texcoords=mp.t,
normals=mp.n,
indices=mp.i,
animdata=mp.bw and mp.bi and { bw=mp.bw, bi=mp.bi},
type="mesh",
material=gdx.materialid,
bones=gdx.bones,
name=gdx.meshpartid
}
return m
end

Expand Down Expand Up @@ -47,7 +53,7 @@ function loadGdx(file,imtls)
local js=fd:read("*all")
fd:close()
local gdx=json.decode(js)

local mtls=imtls or {}

local root={}
Expand All @@ -60,7 +66,7 @@ function loadGdx(file,imtls)
local attrs={}
local ds=0
local as, an = 0, ""
for _,a in ipairs(gm.attributes) do
for _,a in ipairs(gm.attributes) do
if a=="POSITION" then as=3 an="v"
elseif a=="NORMAL" then as=3 an="n"
elseif a=="COLOR" then as=4 an="c"
Expand All @@ -75,7 +81,9 @@ function loadGdx(file,imtls)
-- Parse attributes data
for _,a in pairs(attrs) do
for i=0,dc-1 do
for j=1,a.al do a.d[i*a.al+j]=gm.vertices[i*ds+a.ab+j] end
for j=1,a.al do
a.d[i*a.al+j]=gm.vertices[i*ds+a.ab+j]
end
end
end
-- Reformulate anim (bw)
Expand Down Expand Up @@ -115,7 +123,14 @@ function loadGdx(file,imtls)
local ia=part.indices
local ian=#part.indices
for i=1,ian do ia[i]+=1 end
mparts[part.id]={ v=attrs.v.d, t=attrs.t and attrs.t.d, n=attrs.n and attrs.n.d, i=part.indices, bw=attrs.bw and attrs.bw.d, bi=attrs.bi and attrs.bi.d }
mparts[part.id]={
v=attrs.v.d,
t=attrs.t and attrs.t.d,
n=attrs.n and attrs.n.d,
i=part.indices,
bw=attrs.bw and attrs.bw.d,
bi=attrs.bi and attrs.bi.d
}
end
end
local bones,all={},{}
Expand All @@ -126,21 +141,22 @@ function loadGdx(file,imtls)
if #bones==0 then bones=all end
root.bones=bones
root.animations=gdx.animations

-- materials (texture id, tex path, color, ...)
for _,mat in ipairs(gdx.materials or {}) do
local md=mtls[mat.id] or {}
md.kd=mat.diffuse
-- the image textures
md.modelpath = mtls.modelpath
for _, tex in ipairs(mat.textures or {}) do
if tex.type == "DIFFUSE" then md.diffusetexfile = tex.filename
elseif tex.type == "NORMAL" then md.normaltexfile = tex.filename
-- print("md model path", md.modelpath)
for _, tex in ipairs(mat.textures or {}) do -- XXX
if tex.type == "DIFFUSE" then md.textureFile = tex.filename
elseif tex.type == "NORMAL" then md.normalMapFile = tex.filename
end
end
mtls[mat.id]=md
end
G3DFormat.computeG3DSizes(root)

G3DFormat.computeG3DSizes(root)
return root,mtls
end

Expand Down
41 changes: 21 additions & 20 deletions Library/3dbase/3DLighting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local slang=Shader.getShaderLanguage()

if debug then print(glversion) end
if debug then print(Shader.getProperties().version) end

Shader.extensions={}
if slang=="glsl" then
for ex in Shader.getProperties().extensions:gmatch("%S+") do Shader.extensions[ex]=true end
Expand Down Expand Up @@ -42,29 +43,28 @@ local LightingShaderConstants={
{name="g_Texture",type=Shader.CTEXTURE,mult=1,vertex=false},
{name="g_NormalMap",type=Shader.CTEXTURE,mult=1,vertex=false,code="n"},
{name="g_ShadowMap",type=Shader.CTEXTURE,subtype="shadow",mult=1,vertex=false,code="s"},
--[[
{name="bones",type=Shader.CMATRIX,mult=32,vertex=true,code="a"},
{name="InstanceMatrix",type=Shader.CMATRIX,mult=1,vertex=true,code="i"},
}
--[[
LightingShaderConstants[#LightingShaderConstants+1]=
{name="g_InstanceMap",type=Shader.CTEXTURE,mult=1,vertex=true}
LightingShaderConstants[#LightingShaderConstants+1]=
{name="InstanceMapWidth",type=Shader.CFLOAT,mult=1,vertex=true}
]]
{name="bones",type=Shader.CMATRIX,mult=32,vertex=true,code="a"},
{name="InstanceMatrix",type=Shader.CMATRIX,mult=1,vertex=true,code="i"},
}
]]

local LightingShaderVarying={
{name="position",type=Shader.CFLOAT3},
{name="texCoord",type=Shader.CFLOAT2,code="t"},
{name="normalCoord",type=Shader.CFLOAT3},
{name="lightSpace",type=Shader.CFLOAT4,code="s"},
{name="position",type=Shader.CFLOAT3},
{name="texCoord",type=Shader.CFLOAT2,code="t"},
{name="normalCoord",type=Shader.CFLOAT3},
{name="lightSpace",type=Shader.CFLOAT4,code="s"},
}

-- Shaders defs
local function ShaderFilter(t,code)
local o={}
for _,l in ipairs(t) do
if l.code==nil or code:find(l.code) then
o[#o+1]=l
end
if l.code==nil or code:find(l.code) then o[#o+1]=l end
end
return o
end
Expand All @@ -73,14 +73,14 @@ Lighting._shaders={}
Lighting.getShader=function(code)
local cmap={
{"t","TEXTURED",true},
{"s","SHADOWS",isES3Level and ((slang~="glsl") or
{"s","SHADOWS",isES3Level and ((slang~="glsl") or
-- isES3 or
Shader.extensions.GL_EXT_shadow_samplers or
Shader.extensions.GL_EXT_shadow_samplers or
Shader.extensions.GL_EXT_shadow_funcs)},
{"n","NORMMAP",true},
{"i","INSTANCED",true},
{"a","ANIMATED",true},
}
}
local lcode,ccode,acode="","",""
local lconst={}
for _,k in ipairs(cmap) do
Expand All @@ -99,7 +99,7 @@ Lighting.getShader=function(code)
if D3._V_Shader then
if not Lighting._shaders[lcode] then
for _,a in ipairs(LightingShaderAttrs) do
if not a.code or code:find(a.code) then a.mult=a.amult else a.mult=0 end
if not a.code or code:find(a.code) then a.mult=a.amult else a.mult=0 end
end
--[[
v=Shader.new(
Expand Down Expand Up @@ -193,9 +193,7 @@ function Lighting.setSpriteMode(sprite,mode)
end
Lighting._shadowed[sprite]=sprite
end
if sc:find("a") then
D3Anim._addMesh(sprite)
end
if sc:find("a") then D3Anim._addMesh(sprite) end
end
end

Expand Down Expand Up @@ -232,7 +230,10 @@ function Lighting.computeShadows(scene)
local view=Lighting.shadowview
view:setContent(scene)
view:setProjection(p)
view:lookAt(Lighting.light[1],Lighting.light[2],Lighting.light[3],Lighting.lightTarget[1],Lighting.lightTarget[2],Lighting.lightTarget[3],0,1,0)
view:lookAt(
Lighting.light[1],Lighting.light[2],Lighting.light[3],
Lighting.lightTarget[1],Lighting.lightTarget[2],Lighting.lightTarget[3],
0,1,0)
p:multiply(view:getTransform())
for k,v in pairs(Lighting._shaders) do
v:setConstant("g_LMatrix",Shader.CMATRIX,1,p:getMatrix())
Expand Down
26 changes: 10 additions & 16 deletions Library/3dbase/3DObjLoader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ Returned sprite has a few specific attributes:
]]

local function Split(str, delim, maxNb)
-- Eliminate bad cases...
if string.find(str, delim) == nil then return { str } end
-- Eliminate bad cases...
if string.find(str, delim) == nil then return { str } end
if maxNb == nil or maxNb < 1 then
maxNb = 0 -- No limit
maxNb = 0 -- No limit
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local nb = 0
local lastPos
for part, pos in string.gfind(str, pat) do
-- for part, pos in string.gfind(str, pat) do -- doesn't work for me!
for part, pos in string.gmatch(str, pat) do
if #part>0 then
nb = nb + 1
result[nb] = part
Expand All @@ -33,23 +34,20 @@ end
local function parsemtl(mtls,path,file,prefix,textureFolder,textureMap)
if not io.open(path.."/"..file) then
print("Material file not found:"..path.."/"..file)
return
return
end
local mtl={ texturew=0, textureh=0 }
for line in io.lines(path.."/"..file) do
line=line:gsub(" ", " ") -- fix for new blender 3.1 inserting 2 spaces instead of 1
fld=Split(line," ",10)
for i=1,#fld,1 do fld[i]=string.gsub(fld[i], "\r", "") end
if (fld[2]~=nil) then fld[2]=string.gsub(fld[2], "\r", "") end
if fld[1]=="newmtl" then
--print("DM",fld[2])
mtl={}
mtls[prefix..fld[2]]=mtl
elseif fld[1]=="Kd" then
mtl.kd={fld[2],fld[3],fld[4],1.0}
if fld[1]=="newmtl" then mtl={} mtls[prefix..fld[2]]=mtl
elseif fld[1]=="Kd" then mtl.kd={fld[2],fld[3],fld[4],1.0}
elseif fld[1]=="d" then mtl.kd[4]=fld[2] -- alpha XXX
elseif fld[1]=="map_Kd" then
table.remove(fld,1)
local f=table.concat(fld," ")
--print("Texture:.. ["..path.."/"..f.."]")
if textureMap then f=textureMap[f] or f end
mtl.textureFile=(textureFolder or path).."/"..f
elseif fld[1]=="map_Bump" then
Expand All @@ -59,7 +57,6 @@ local function parsemtl(mtls,path,file,prefix,textureFolder,textureMap)
table.remove(fld,1)
end
local f=table.concat(fld," ")
--print("Texture:.. ["..path.."/"..f.."]")
if textureMap then f=textureMap[f] or f end
mtl.normalMapFile=(textureFolder or path).."/"..f
end
Expand All @@ -83,7 +80,6 @@ function importObj(path,file,imtls,matpfx,textureFolder,textureMap)
local oname=nil
local function buildObject()
if (imap~=nil) then
--print(#v/3,#vt/2,#vn/3)
local treeDesc=G3DFormat.mapCoords(v,vt,vn,imap)
treeDesc.type="mesh"
treeDesc.material=mtl
Expand Down Expand Up @@ -122,7 +118,6 @@ function importObj(path,file,imtls,matpfx,textureFolder,textureMap)
table.insert(imap,itab)
elseif fld[1]=="o" or fld[1]=="g" then
buildObject()
--print(line)
oname=fld[2]
elseif fld[1]=="mtllib" then
table.remove(fld,1)
Expand All @@ -132,7 +127,6 @@ function importObj(path,file,imtls,matpfx,textureFolder,textureMap)
mtl=matpfx..fld[2]
end
end
--spr:setColorTransform(1.0,0,0,1.0)
buildObject() --If any in progress
G3DFormat.computeG3DSizes(root)
return root,mtls
Expand Down
Loading