An bone animation
I used mesh, not craft model, the reason is that with model matrix, it is easy to understand and calculate the positions, angles of bone animation.
Download the object files and save to your local folder — Documents
Model obj files:
https://GitHub.com/FreeBlues/bone-animation
Source code:
--# Main
-- BoneMesh
-- ????? Bones ? MeshTest ?? BoneTest ?????????? mesh ??????????
-- 2020.02.22 ?????????????????
function setup()
-- craft scene ???
craftSceneInit()
objectsInit()
-- ?? lovntArray ?????????????? robot
robot = Robot(objectArray)
-- ???????
dat = DoActionThread(robot)
-- ?? OrbitViewer ??????????????????????
scene.camera:add(OrbitViewer, vec3(0,0,0),5, 10, 5)
end
-- ? ???(angle,x,y,z)??????(w,x,y,z)
function axis2quat(a,x,y,z)
local w = math.cos(math.rad(a)/2)
local x = math.sin(math.rad(a)/2)*x
local y = math.sin(math.rad(a)/2)*y
local z = math.sin(math.rad(a)/2)*z
return quat(w,x,y,z)
end
function update(dt)
dat:run()
scene:update(dt)
end
function draw()
update(DeltaTime)
-- ?? craft ??
scene:draw()
-- ?????
scene.debug:line(vec3(0,0,0),vec3(1,0,0),color(255,0,0,255))
scene.debug:line(vec3(0,0,0),vec3(0,1,0),color(0,255,0,255))
scene.debug:line(vec3(0,0,0),vec3(0,0,1),color(255,255,0,255))
-- ?? mesh ???????????
perspective()
-- background()
-- ?? craft ???????? mesh ?????
local scp = scene.camera.position
local x,y,z = scp.x, scp.y, scp.z
camera(x,y,z,0,0,0, 0,1,0)
-- ?? Robot ???? drawSelf ??
robot:drawSelf()
end
function objectsInit()
-- run only once ????????14??????Model1.txt ~ Model14.txt
for k=1,14 do
saveText(asset.."Model"..k..".txt", "")
end
objectArray = {}
objectArray[1] = LoadObject("Documents:body", 1)
objectArray[2] = LoadObject("Documents:body", 2)
objectArray[3] = LoadObject("Documents:head", 3)
objectArray[4] = LoadObject("Documents:left_top",4)
objectArray[5] = LoadObject("Documents:left_bottom",5)
objectArray[6] = LoadObject("Documents:right_top",6)
objectArray[7] = LoadObject("Documents:right_bottom",7)
objectArray[8] = LoadObject("Documents:right_leg_top",8)
objectArray[9] = LoadObject("Documents:right_leg_bottom",9)
objectArray[10] = LoadObject("Documents:left_leg_top",10)
objectArray[11] = LoadObject("Documents:left_leg_bottom",11)
objectArray[12] = LoadObject("Documents:left_foot", 12)
objectArray[13] = LoadObject("Documents:right_foot", 13)
floor = LoadObject("Documents:floor", 14)
end
function craftSceneInit()
-- Create a new craft scene
scene = craft.scene()
-- ???????
local sunny = readText(asset.builtin.Environments.Night)
local env = craft.cubeTexture(json.decode(sunny))
scene.sky.material.envMap = env
scene.sky.eulerAngles = vec3(0,180,0)
scene.camera.z = -10
scene.camera.eulerAngles = vec3(0,0,0)
scene.camera.position = vec3(0,0,0)
-- ???????????
-- scene.sun.active = false
-- scene.sky.active = false
-- Move the main camera
scene.camera.position = vec3(0, -10, -10)
-- ??????
scene.sun:get(craft.light).intensity = 0.8
scene.sun.position = vec3(0, -10, 0)
scene.sun.rotation = quat.eulerAngles(45,0,45)
scene.ambientColor = color(88, 107, 222, 255)
end