Libs3D Library Skeletal Animation: Difference between revisions

From GiderosMobile
wip
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:


== I am ready ==
== I am ready ==
Next step is to animate our 3D models, skeletal animations that is ☠️.
Next step is to animate your 3D models, skeletal animations that is ☠️.


== fbx-conv.exe (json) ==
If you are using .fbx files, you would need to convert your files to .json using '''fbx-conv.exe'''.
 
If you are using .glb files this is easier.
 
== '''fbx''' animation ==
So you have your animated model exported to an .fbx file. You need to use '''fbx-conv-win32.exe''' to convert it to a .json file.
So you have your animated model exported to an .fbx file. You need to use '''fbx-conv-win32.exe''' to convert it to a .json file.


Line 13: Line 17:
*''Gideros\Tools\fbx-conv-win32.exe''
*''Gideros\Tools\fbx-conv-win32.exe''


Some examples (using Windows PowerShell)
Some examples of converting an .fbx file to .json (using Windows PowerShell)
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- from within the fbx-conv.exe folder (with your model.fbx)
-- from within the fbx-conv.exe folder (with your model.fbx)
Line 22: Line 26:
</syntaxhighlight>
</syntaxhighlight>


Then loading your .json animation file is as simple as
Then loading your .json animation file in Gideros is as simple as
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local anim = buildGdx("path/to/animated_model.json",
local anim = buildGdx("path/to/animated_model.json",
{ modelpath="path/to/model/textures/", } -- textures path
{ modelpath="path/to/model/textures/", } -- textures path (optional)
)
)
D3Anim.setAnimation(anim, anim.animations[1], "main", true)
D3Anim.setAnimation(anim, anim.animations[1], "main", true)
Line 89: Line 93:
</syntaxhighlight>
</syntaxhighlight>


== .glb animations ==
== '''.glb''' animation ==
So you have your animated model exported to an .glb file. You can animate it this way:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
D3Anim.updateBones()
local glbA = Glb.new("model/path", "file.glb")
local glbB = glbA:getScene()
local glb = G3DFormat.buildG3D(glbB)
-- animations
D3Anim.setAnimation(glb, glb.animations[1], "main", true)
D3Anim.tick() -- animation engine tick
</syntaxhighlight>
</syntaxhighlight>


== For the nerd in you ==
[[File:ss_3DAnims_glb.png]]
The first method using .fbx.
 
Some sample code, pic above (you may have to provide your own assets).
<syntaxhighlight lang="lua">
application:setBackgroundColor(0x2e2e2e)
 
-- we set up a fullscreen 3D viewport (fov: consoles 60, pc 90)
local sw, sh = application:getContentWidth(), application:getContentHeight()
local view = D3.View.new(sw,sh, 90, 0.1,1*1024) -- (screen w,screen h, fov, near plane,far plane)
 
-- we position the view (camera) and set its target
view:lookAt(0.0*8,0.4*8,0.5*8, 0*8,0.25*8,-0.2*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])
 
-- we add the view (camera) to Gideros stage
stage:addChild(view)


Basically buildGdx parse the json file into an intermediate representation (the g3d format), which is then fed into G3DFormat function to generate actual gideros objects.
-- we will add 3D shapes and models to a scene
local scene = view:getScene()


So, in theory, all you need is a single file containing your model, your skeleton in its bind pose, and your animations.
-- loading model
local glbA001 = Glb.new("models/glbs", "_animeCharacters_truebones.glb") -- Glb.new("path", "file")
local glbB001 = glbA001:getScene()
local glb001 = G3DFormat.buildG3D(glbB001)


My loader code will parse the json file and extract several parts:
-- animations
- the skeleton structure and pose (binding pose)
D3Anim.setAnimation(glb001, glb001.animations[4], "main", true)
- the model structure and meshes
-- animation engine tick
- the animations if any
D3Anim.tick()


The shader does the animation. In fact OpenGL, since programmable pipeline was introduced, is more like a driver to the GPU. It is the shader code that dictates what to do with vertices, texture coordinates, animation data and so on.
-- position
glb001:setPosition(0*8, 0*8, 0*8)
-- rotation
glb001:setRotationY(0*15)
-- scale
local scale = 100
glb001:setScale(scale,scale,scale)
-- order
scene:addChild(glb001)


The lua code in 3DAnimation compute matrices used by the shader, but it is the shader that applies transformations to the mesh.
-- light setup
local lsx, lsy, lsz = 1.0*8, 1.0*8, 1.0*8 -- light source position
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
Lighting.setLight(lsx,lsy,lsz, 0.5) -- (x,y,z, ambient)
local pd, fov = 6*8, 7*8 -- 5*8, 6*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
Lighting.computeShadows(scene)


Having animations separated from the model is actually more annoying then having all in the same file, because you may end up with duplicate things. In my fbx files, the skeleton was in each file, but not in the same pose, which was confusing.
-- game loop
stage:addEventListener(Event.ENTER_FRAME, function(e)
D3Anim.tick() -- animation engine tick
Lighting.computeShadows(scene)
end)
</syntaxhighlight>


To try to work out the simplest case, I made a rectangle in Blender, linked to an armature, and added a couple key frames, basically making a block that folds. I exported that to FBX, and used fbx-conv-win32.exe to export it to a JSON file.
== For the nerd in you ==
*in theory, all you need is a single file containing your model, your skeleton in its bind pose, and your animations
*basically ''buildGdx'' parse the json file into an intermediate representation (the g3d format), which is then fed into ''G3DFormat'' function to generate actual gideros objects
*the loader code will parse the json file and extract several parts:
**the skeleton structure and pose (binding pose)
**the model structure and meshes
**the animations if any
*the shader does the animation. In fact OpenGL, since programmable pipeline was introduced, is more like a driver to the GPU. It is the shader code that dictates what to do with vertices, texture coordinates, animation data and so on
*the lua code in ''3DAnimation'' computes matrices used by the shader, but it is the shader that applies transformations to the mesh
*having animations separated from the model is actually cool


== I am out 4 ==
== I am out 4 ==
So here you have your animations playing in Gideros.
You are strongly encouraged to mess with the code.
You are strongly encouraged to mess with the code.



Latest revision as of 16:21, 13 August 2026

I am ready

Next step is to animate your 3D models, skeletal animations that is ☠️.

If you are using .fbx files, you would need to convert your files to .json using fbx-conv.exe.

If you are using .glb files this is easier.

fbx animation

So you have your animated model exported to an .fbx file. You need to use fbx-conv-win32.exe to convert it to a .json file.

What is fbx-conv-win32.exe?

A command line utility using the FBX SDK to convert FBX/Collada/Obj files to a custom text/binary format for static, keyframed and skinned meshes. https://github.com/libgdx/fbx-conv

You can find fbx-conv-win32.exe in your Gideros installation folder:

  • Gideros\Tools\fbx-conv-win32.exe

Some examples of converting an .fbx file to .json (using Windows PowerShell)

-- from within the fbx-conv.exe folder (with your model.fbx)
.\fbx-conv.exe -v -o G3DJ model.fbx model.json

-- from your model.fbx folder
C:\Program Files\Gideros\Tools\fbx-conv.exe -v -o G3DJ "model.fbx" "model.json"

Then loading your .json animation file in Gideros is as simple as

local anim = buildGdx("path/to/animated_model.json",
	{ modelpath="path/to/model/textures/", } -- textures path (optional)
)
D3Anim.setAnimation(anim, anim.animations[1], "main", true)
D3Anim.tick() -- animation engine tick

Some sample code, pic above (you may have to provide your own assets).

application:setBackgroundColor(0x55007f)

-- we set up a fullscreen 3D viewport (fov: consoles 60, pc 90)
local sw, sh = application:getContentWidth(), application:getContentHeight()
local view = D3.View.new(sw,sh, 90, 0.1,1*1024) -- (screen w,screen h, fov, near plane,far plane)

-- we position the view (camera) and set its target
view:lookAt(0*8,1.5*8,3*8, 0*8,1.5*8,0*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])

-- we add the view (camera) to Gideros stage
stage:addChild(view)

-- we will add 3D shapes and models to a scene
local scene = view:getScene()

-- loading models
local anim001=buildGdx("models/fbxs/character_dev.json")
local anim002=buildGdx("models/fbxs/animeCharacter_truebones.json",
	{ modelpath="models/fbxs/textures/", } -- textures path
)

-- set current animations
D3Anim.setAnimation(anim001,anim001.animations[math.random(#anim001.animations)],"main",true)
D3Anim.setAnimation(anim002,anim002.animations[math.random(#anim002.animations)],"main",true)

-- scale
local scale = 0.1
anim001:setScale(scale,scale,scale)
anim002:setScale(scale,scale,scale)
-- position
anim001:setPosition(1*8, 0*8, 0*8)
anim002:setPosition(-1*8, 0*8, 0*8)
-- rotation
anim001:setRotationY(0*15)
anim002:setRotationY(0*15)
-- order
scene:addChild(anim001)
scene:addChild(anim002)

-- light setup
local lsx, lsy, lsz = 5*8, 5*8, 0*8 -- light source position
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
Lighting.setLight(lsx,lsy,lsz, 0.4) -- (x,y,z, ambient)
local pd, fov = 6*8, 5*8 -- 5*8, 6*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)

stage:addEventListener(Event.ENTER_FRAME,function(e)
	-- animation engine tick
	D3Anim.tick()
	-- compute shadows
	Lighting.computeShadows(scene)
end)

.glb animation

So you have your animated model exported to an .glb file. You can animate it this way:

local glbA = Glb.new("model/path", "file.glb")
local glbB = glbA:getScene()
local glb = G3DFormat.buildG3D(glbB)
-- animations
D3Anim.setAnimation(glb, glb.animations[1], "main", true)
D3Anim.tick() -- animation engine tick

Some sample code, pic above (you may have to provide your own assets).

application:setBackgroundColor(0x2e2e2e)

-- we set up a fullscreen 3D viewport (fov: consoles 60, pc 90)
local sw, sh = application:getContentWidth(), application:getContentHeight()
local view = D3.View.new(sw,sh, 90, 0.1,1*1024) -- (screen w,screen h, fov, near plane,far plane)

-- we position the view (camera) and set its target
view:lookAt(0.0*8,0.4*8,0.5*8, 0*8,0.25*8,-0.2*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])

-- we add the view (camera) to Gideros stage
stage:addChild(view)

-- we will add 3D shapes and models to a scene
local scene = view:getScene()

-- loading model
local glbA001 = Glb.new("models/glbs", "_animeCharacters_truebones.glb") -- Glb.new("path", "file")
local glbB001 = glbA001:getScene()
local glb001 = G3DFormat.buildG3D(glbB001)

-- animations
D3Anim.setAnimation(glb001, glb001.animations[4], "main", true)
-- animation engine tick
D3Anim.tick()

-- position
glb001:setPosition(0*8, 0*8, 0*8)
-- rotation
glb001:setRotationY(0*15)
-- scale
local scale = 100
glb001:setScale(scale,scale,scale)
-- order
scene:addChild(glb001)

-- light setup
local lsx, lsy, lsz = 1.0*8, 1.0*8, 1.0*8 -- light source position
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
Lighting.setLight(lsx,lsy,lsz, 0.5) -- (x,y,z, ambient)
local pd, fov = 6*8, 7*8 -- 5*8, 6*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
Lighting.computeShadows(scene)

-- game loop
stage:addEventListener(Event.ENTER_FRAME, function(e)
	D3Anim.tick() -- animation engine tick
	Lighting.computeShadows(scene)
end)

For the nerd in you

  • in theory, all you need is a single file containing your model, your skeleton in its bind pose, and your animations
  • basically buildGdx parse the json file into an intermediate representation (the g3d format), which is then fed into G3DFormat function to generate actual gideros objects
  • the loader code will parse the json file and extract several parts:
    • the skeleton structure and pose (binding pose)
    • the model structure and meshes
    • the animations if any
  • the shader does the animation. In fact OpenGL, since programmable pipeline was introduced, is more like a driver to the GPU. It is the shader code that dictates what to do with vertices, texture coordinates, animation data and so on
  • the lua code in 3DAnimation computes matrices used by the shader, but it is the shader that applies transformations to the mesh
  • having animations separated from the model is actually cool

I am out 4

So here you have your animations playing in Gideros.

You are strongly encouraged to mess with the code.

Bye for now!


Libs3D