Libs3D Library Skeletal Animation: Difference between revisions

From GiderosMobile
wip
wip
Line 5: Line 5:


== fbx-conv.exe (json) ==
== fbx-conv.exe (json) ==
So you have your animated model exported to an .fbx file. You need to use '''fbx-conv-win32.exe''' to export 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.


What is '''fbx-conv-win32.exe'''?
What is '''fbx-conv-win32.exe'''?
Line 13: Line 13:
*''Gideros\Tools\fbx-conv-win32.exe''
*''Gideros\Tools\fbx-conv-win32.exe''


Some examples (using Windows PowerShell)
<syntaxhighlight lang="lua">
-- 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"
 
 
This is how I use it to convert my .fbx files into .json files Gideros can manipulate:
*
 
 
 
 
 
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.
 
 
 
 
The steps I did:
- have an fbx model
- copy it in your fbx-conv-win32 folder (optional?)
- open windows powershell in the same directory
- in the command line type: .\fbx-conv-win32.exe -f yourmodel.fbx yourmodel.g3dj
- finally change the extension of yourmodel.g3dj to yourmodel.json
 
you can grab fbx-conv for your system here: https://github.com/libgdx/fbx-conv
 
 
 
 
 
 
.glb
<syntaxhighlight lang="lua">
D3Anim.updateBones()
</syntaxhighlight>
</syntaxhighlight>


 
Then loading your .json animation file is as simple as
== '''.json''' (fbx) format ==
Loading .json animation files 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",
Line 118: Line 89:
</syntaxhighlight>
</syntaxhighlight>


 
== .glb animations ==
 
<syntaxhighlight lang="lua">
'''...TO BE CONTINUED...'''
D3Anim.updateBones()
</syntaxhighlight>


== For the nerd in you ==
== For the nerd in you ==
== fbx-conv.exe (json) ==
The first method using .fbx.
The first method using .fbx.


Line 140: Line 111:


  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.
  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.


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.
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.
The steps I did:
- have an fbx model
- copy it in your fbx-conv-win32 folder (optional?)
- open windows powershell in the same directory
- in the command line type: .\fbx-conv-win32.exe -f yourmodel.fbx yourmodel.g3dj
- finally change the extension of yourmodel.g3dj to yourmodel.json
you can grab fbx-conv for your system here: https://github.com/libgdx/fbx-conv


== I am out 4 ==
== I am out 4 ==

Revision as of 03:39, 13 August 2026

I am ready

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

fbx-conv.exe (json)

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 it in your Gideros installation folder:

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

Some examples (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 is as simple as

local anim = buildGdx("path/to/animated_model.json",
	{ modelpath="path/to/model/textures/", } -- textures path
)
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 animations

D3Anim.updateBones()

For the nerd in you

The first method using .fbx.

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.
So, in theory, all you need is a single file containing your model, your skeleton in its bind pose, and your animations.
My 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 compute matrices used by the shader, but it is the shader that applies transformations to the mesh.
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.

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.

I am out 4

You are strongly encouraged to mess with the code.

Bye for now!


Libs3D