Are we able to animate imported .fbx models in Codea yet?

I’m interested in being able to do 3D animations in the future (e.g. move character limbs). I saw this old post that suggested that we might be able to at some time in the future:

Attila717Attila717
May 2017 Posts: 71
@John Also, can different parts of obj files be treated as separate components of one entity? Or can they be treated as different entities? I guess I’m looking for a way to make characters or other objects move their arms/wheels/etc…

JohnJohn Admin Mod
May 2017 Posts: 713
@Attila717
This is something I’ve been considering (allowing each object to be loaded as children of the parent mesh). One of the issues is that the obj format does not support transformations, so each sub-object mesh will have the middle of the object as its center. Later on I will be adding support for collada and fbx which have proper support for transform hierarchies.

Appreciate any updates.

Thanks!

You can see this:

https://codea.io/talk/discussion/6549/reproducing-blender-animations-in-codea-now-with-code-link-new-

The video:

https://youtu.be/8yTjSnEy8wM

It is based obj, not fbx.

If you’re going to import FBX into Codea, you’ll need to handle the FBX file yourself, but there’s a problem with craft that encapsulates mesh based operations to some extent, the biggest change is that craft doesn’t support the three matrix operations of model, view, and projection.

Most mesh-based animation algorithms are implemented by manipulating the matrix (model, view, projection) , so if you want to animate in craft, there are two ways to do it:

  • first, write your own algorithm based on the principle,;
  • the second is to convert the craft model into mesh and then operate the matrix directly.

At present, the animation algorithms implemented in the forum are all based on mesh, the advantage is that you can operate the matrix, but also need to write their own shader.

Here is another example:

https://youtu.be/8luBCoUp5JY

The last one is a craft prototype:

https://youtu.be/_EJw_iIiwQE

The craft animation prototype:

-- JS3D C06 ?????

function setup()
    -- ????
    scene = craft.scene()
    
    -- ????????????????????
    scene.sky.active = true
    scene.sun.active = true
    scene.sun:get(craft.light).intensity = 0.5
    scene.ambientColor = color(112, 158, 194)
    
    -- ?????????????
    scene.camera.position = vec3(0, 0, -800)
    cam = scene.camera:get(craft.camera)
    cam.fieldOfView = 60
    cam.farPlane = 3000
    
    makeAvatar()
    
    makeTreeAt(200,0)
    makeTreeAt(-200,0)
    makeTreeAt(200,750)
    makeTreeAt(-200,750)
end

-- ????
function makeAvatar()
    -- ??????????3D?????
    marker = scene:entity()
    
    -- ????????
    local model = craft.model.icosphere(30, 1, true)
    local material = craft.material(asset.builtin.Materials.Standard)
    
    body = scene:entity()   
    body:add(craft.renderer, model)
    body.material = material
    body.position = vec3(0,0,0)
    body.parent = marker
    scene.camera.parent = marker
    
    -- Create hands feet,?????
    local model = craft.model.icosphere(15,1,true)
    local material = craft.material(asset.builtin.Materials.Standard)
    
    leftHand = scene:entity()
    leftHand:add(craft.renderer, model)
    leftHand.material = material
    leftHand.position = vec3(45,0,0)
    leftHand.parent = body
    
    rightHand = scene:entity()
    rightHand:add(craft.renderer, model)
    rightHand.material = material
    rightHand.position = vec3(-45,0,0)
    rightHand.parent = body
    
    leftFoot = scene:entity()
    leftFoot:add(craft.renderer, model)
    leftFoot.material = material
    leftFoot.position = vec3(20,-45,0)
    leftFoot.parent = body
    
    rightFoot = scene:entity()
    rightFoot:add(craft.renderer, model)
    rightFoot.material = material
    rightFoot.position = vec3(-20,-45,0) 
    rightFoot.parent = body
    
    -- ???????
    isCartwheeling = false
    isFlipping = false
    isMovingRight = false
    isMovingLeft = false
    isMovingForeward = false
    isMovingBack = false
end

function makeTreeAt(x,z)
    trunk = scene:entity()
    trunk.model = craft.model.cube(vec3(30,90,30))
    trunk.material = craft.material(asset.builtin.Materials.Standard)
    trunk.material.diffuse = color(123, 116, 60)
    trunk.position = vec3(x, -75, z)
    
    top = scene:entity()
    top.model = craft.model.icosphere(60,1,true)
    top.material = craft.material(asset.builtin.Materials.Standard)
    top.material.diffuse = color(89, 198, 35)
    top.y = 90
    
    top.parent = trunk
end

function isWalking()
    if isMovingRight then return true end
    if isMovingLeft then return true end
    if isMovingForeward then return true end
    if isMovingBack then return true end
    return false
end

function walk()
    if (not isWalking()) then return end
    local speed = 10
    local size = 100
    local time = ElapsedTime
    position =math.sin(speed*time)*size
    rightHand.z = position
    leftHand.z = -position
    rightFoot.z = -position
    leftFoot.z = position
end

function acrobatics()
    if (isCartwheeling) then
        local t = ElapsedTime
        local br = body.rotation
        body.rotation = quat.eulerAngles(br.x, br.y, br.z+100*t)  
    end   
    
    if (isFlipping) then
        local speed = 10
        local size = 90
        local time = ElapsedTime
        p =time*size
        body.rotation = quat.eulerAngles(0,0,p)  
    end       
end

function update(dt)
    -- ??????    
    walk()
    acrobatics()
    
    -- ????
    scene:update(dt)    
end

-- Codea ?????????60? 
function draw()   
    update(DeltaTime)
    -- ????
    scene:draw()	
end

-- ??????
function keyboard(key)
    print(key.state)
    local k = key
    if k == "c" then isCartwheeling = not isCartwheeling end
    if k == "f" then isFlipping = not isFlipping end
    if k == "a" then marker.x = marker.x + 15; isMovingLeft = true end
    if k == "d" then marker.x = marker.x - 15; isMovingRight = true end
    if k == "w" then marker.z = marker.z + 15; isMovingForeward = true end
    if k == "s" then marker.z = marker.z - 15; isMovingBack = true end
end

function touched(touch)
    -- ????????
    showKeyboard()    
end

The Bone craft project:

Very neat, @binaryblues, thanks! You put a lot of work into that. I’ll study it :slight_smile:

I posted this a long time ago as an example for using parent to attach different objects to other objects. I guess it could attract arms and legs just as easily.

viewer.mode=STANDARD

function setup()
    parameter.number("Rotate", 0, 360, 208)
    parameter.number("swx",-180,180,45)
    parameter.number("swy",-180,180,0)
    parameter.number("swz",-180,180,0)
    parameter.number("shx",-180,180,180)
    parameter.number("shy",-180,180,-30)
    parameter.number("shz",-180,180,30)
    
    scene = craft.scene()
    scene.sky.active = false
    
    ground = scene:entity()
    ground.model = craft.model.cube(vec3(10,.2,10))
    ground.material = craft.material(asset.builtin.Materials.Specular)
    ground.material.map = readImage(asset.builtin.Blocks.Dirt)
    ground.material.specular = color(0, 0, 0, 255)
    ground.material.offsetRepeat = vec4(0,0,1,1)
    ground.y = -1

    robot = scene:entity()
    robot.model = craft.model(asset.builtin.Blocky_Characters.Robot)
    --robot.material = craft.material("Materials:Specular")
    --robot.material.map = img
    robot.y = -1
    robot.z = 0
    robot.scale = vec3(1,1,1) / 8

    shield = scene:entity()
    shield.model = craft.model(asset.builtin.CastleKit.shieldRed_obj)
    shield.x = -3
    shield.y = 7
    shield.z = 3
    shield.scale = vec3(2.5,2.5,.1)
    
    sword = scene:entity()
    sword.model = craft.model(asset.builtin.CastleKit.sword_obj)
    sword.x = 3.2
    sword.y = 6
    sword.z = 1
    sword.scale = vec3(1,1,1)*1.6

    scene.camera.z = -6
    
    shield.parent=robot
    sword.parent=robot
end

function update(dt)
    robot.eulerAngles = vec3(0, Rotate, 0)
    sword.eulerAngles=vec3(swx,swy,swz)
    shield.eulerAngles=vec3(shx,shy,shz)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()	   
end

Thanks, @dave1707, that will help!