It looks like you're new here. If you want to get involved, click one of these buttons!
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
Comments
It is just a prototype.
Wow nice work!
@Simeon Glad to hear that!
The next I will try to use pure Craft properties and methods to calculate without mesh and matrix.
But before this, I need to learn more about Craft ...