In Codea 4, mesh can load GLTF animation, here is a demo

I found that the mesh increase in Codea 4 supports a lot of file types, so I tried gltf. However, in this routine there is only a simple load and no animation, because I know very little about gltf. hopefully @John will provide some sample code, such as to get the roles moving.

These gltf are from the app: Anima Toon, it is very easy to control the character`s actions.

The code is very short, However, I suggest you download the project file, because it includes the GLTF file.

function setup()

    -- 使用基本场景的默认镜头
    scn = scene.default3d()
    scn.sky = nil
    -- scn.camera.farPlane =5000
    
    cam = scn.camera:get(camera)
    cam.farPlane = 50000
    print("cam: ", cam.farPlane)
    
    -- create a sun entity and add a directional light to it
    sunEntity = scn:entity("sun")
    sun = sunEntity:add(light.directional(vec3(-1,-1,-1)))
    sun.color = color(233, 150, 80)
    -- sun.clearColor = color(233, 150, 80)
    sun.intensity = 0.05   
    -- sun.direction =vec3(-100,-100,-100)
    sun.castShadows = false
    
    rig = scn.camera:add(camera.rigs.orbit)
    rig.distance = 50
    rig.angles.x = 25    
    rig.angles.y = 75
    
    -- 用于地面:新建 material.lit() 会自动打开 阴影,如何关闭? 
    checkers = material.lit()
    checkers.map = image.read(asset.builtin.Blocks.Brick_Grey)
    checkers.color = color(243, 243, 242)
    checkers.scaleOffset = vec4(0.125, 0.125, 0.0, 0.0)
    checkers.roughness = 0.5
    
    -- 地面
    ground = scn:entity("ground")
    ground:add(mesh.box(50, 0.1, 50))
    ground.y = -0.05
    ground.material = checkers

        
    robot = scn:entity("robot")    
    robot:add(mesh.read(asset.builtin.Blocky_Characters.Robot))
    robot.scale = vec3(.5,.5,.5)
    robot.position = vec3(10,0,-10)
    robot.material = checkers
    
    -- load Run.gltf (1 action)
    myChar = scn:entity("myChar")    
    myChar:add(mesh.read(asset.run))
    -- myChar.material = checkers
    myChar.position = vec3(0,0.5,0)    
    
    for k,v in pairs(myChar.mesh.animations) do 
        print(k,v)
    end
    
    -- load DogWalk.gltf (1 action)
    myPet = scn:entity("myPet")    
    myPet:add(mesh.read(asset.DogWalk))
    -- myPet.material = checkers
    myPet.position = vec3(0,0.5,-10)
    
    -- load DogWalk.gltf (3 actions)
    myPet1 = scn:entity("myPet1")    
    myPet1:add(mesh.read(asset.ExportDog))
    -- myPet.material = checkers
    myPet1.position = vec3(-5,0.6,-12)
    
    scene.main = scn
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    rig.angles.y = time.elapsed*30
end

C4AnimationGltf.zip (404.7 KB)

The demo video:

Codea 4 scene

The original animation:

The walking man

A dog include 3 actions

In Build 349 the new feature “performance stats display” is very easy to use.

The frame rate is very low after loading gltf, and it seems that the bottleneck of processing is still on the CPU side.

1 Like

Looking at your stats, 330 draw calls seems a little high for the number of models on screen. If you post the example project I can try to see what’s causing the performance issues

Thank you @John , Here is the project:

https://talk.codea.io/uploads/short-url/8BkgJgmYqrDtPivq69xgXJiKL1L.zip

I’m also confused because I’m only showing a static model, not making it move

I think that the mesh rendering needs optimisation but I also think those models are horribly optimised. Each dog has 58 submeshes, which are also skinned and also need to be rendered several extra times for shadows mapping. They could be one submesh each. It will be a good benchmark to stress test this kind of stuff though.

These gltf animations are all exported from this app: Anima Toon, which is a voxel animation editor. The export menu does not provide optimization options and may be exported according to the default configuration, I’ll see if I can optimize the models in blender.

2 Likes

Looking at the models in Blender, there’s an enormous number of empty objects (2844) which are slowing down mesh rendering by simply existing. I wonder what they are for?

I ran some additional post processing options in the mesh loader (removing redundant nodes, etc…) as well as some other fixes (cache coherency, memory alignment, mesh draw code fixes) and got your example running at 120fps with shadows :smiley:

The robot texture seems to be missing so there’s still some bugs to be fixed but it’s a lot better than 5fps!

1 Like

Also got the animation working:

image

In setup(): anim = myChar.mesh.animations[1]
In draw(): anim:apply(myChar, time.elapsed % anim.duration)

1 Like

@John I didn’t expect Anima Toon to export a model like this. It looks like there’s a lot of potential for performance optimization when exporting models. I’m going to submit your findings to the developers and hope they can fix them.

You can do 120 frames! Yes! The Codea 4 character animation features in the performance is no problem, will give us a better 3D game development with Codea 4 experience! And it looks like this animation class encapsulates very well, use is also super simple and clear! Thank you again for your hard work!

1 Like