具有多个持续时间的样条曲线
2016-12-10 17:6:49
收藏:0
阅读:92
评论:1
我正在尝试制作具有多个持续时间的样条曲线。基本上,我想创建一个通过用户提供的关键帧生成的样条曲线。可能会有多个具有不同持续时间的关键帧。所以我问,我如何制作一个在不同关键帧之间以不同速度行进的样条曲线。例如,我希望在关键帧A和B之间间隔1秒,但在B和C之间间隔5秒。类似于动画编辑器(如Source Filmmaker或Autodesk Maya)的方式。每次我看到有人制作一条样条曲线,它总是具有一个恒定的速度。关键帧之间总是_X_秒,但这不是动画编辑器的工作方式,它们具有不同的速度,这就是我想要的。
请注意,我已经尝试过。一旦到达不同的关键帧,我就改变了持续时间,但这只是立即减慢了它,就像某些慢动作电影效果一样,这不是我要找的。我该如何逐渐转换速度来适应下一个关键帧?只有一些方程吗?
function smooth( points, steps ) --points is an array, steps is how many frames inbetween spline points
if #points < 3 then
return points
end
local steps = steps or 5
local spline = {}
local count = #points - 1
local p0, p1, p2, p3, x, y, z
for i = 1, count do
if i == 1 then
p0, p1, p2, p3 = points[i], points[i], points[i + 1], points[i + 2]
elseif i == count then
p0, p1, p2, p3 = points[#points - 2], points[#points - 1], points[#points], points[#points]
else
p0, p1, p2, p3 = points[i - 1], points[i], points[i + 1], points[i + 2]
end
for t = 0, 1, 1 / steps do
-- 主要的样条曲线方程
x = 1 * ( ( 2 * p1.x ) + ( p2.x - p0.x ) * t + ( 2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x ) * t * t + ( 3 * p1.x - p0.x - 3 * p2.x + p3.x ) * t * t * t )
y = 1 * ( ( 2 * p1.y ) + ( p2.y - p0.y ) * t + ( 2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y ) * t * t + ( 3 * p1.y - p0.y - 3 * p2.y + p3.y ) * t * t * t )
z = 1 * ( ( 2 * p1.z ) + ( p2.z - p0.z ) * t + ( 2 * p0.z - 5 * p1.z + 4 * p2.z - p3.z ) * t * t + ( 3 * p1.z - p0.z - 3 * p2.z + p3.z ) * t * t * t )
if not(#spline > 0 and spline[#spline].x == x and spline[#spline].y == y and spline[#spline].z == z) then
table.insert( spline , { x = x , y = y, z = z } )
end
end
end
return spline
end
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

使用简单直接的方法:
local zero_vector = {0, 0, 0} local function get_slope(is_edge, left, right) if is_edge then return zero_vector else local t = right.time - left.time assert(t > 0, "时间序列不递增") return {(right[1] - left[1])/t, (right[2] - left[2])/t, (right[3] - left[3])/t} end end function smooth(checkpoints, frames_per_second) frames_per_second = frames_per_second or 5 if #checkpoints < 2 then return checkpoints end -- 为样条线的每个段准备公式 local formulas = {} for segment = 1, #checkpoints - 1 do local left = checkpoints[segment] local right = checkpoints[segment + 1] local t = right.time - left.time assert(t > 0, "时间序列不递增") local left_deriv = get_slope(segment == 1, checkpoints[segment - 1], right) local right_deriv = get_slope(segment == #checkpoints - 1, left, checkpoints[segment + 2]) formulas[segment] = {} for j = 1, 3 do local d = left[j] local c = left_deriv[j] local a = (right[j] - d - c*t) / (t*t) local b = 3*a + (c - right_deriv[j])/t formulas[segment][j] = {(a - b)/t, b, c, d} end end -- 计算样条线上的点 local total_seconds = checkpoints[#checkpoints].time - checkpoints[1].time local segment = 1 local spline = {} for frame_no = 0, total_seconds * frames_per_second do local t = checkpoints[1].time + frame_no / frames_per_second local point = {time = t} while segment < #formulas and t > checkpoints[segment + 1].time do segment = segment + 1 end t = t - checkpoints[segment].time for j = 1, 3 do local c = formulas[segment][j] point[j] = ((c[1]*t + c[2])*t + c[3])*t + c[4] end table.insert(spline, point) end return spline end用法示例:
-- x y z "时间戳(以秒为单位)" local checkpoint_A = {11, 12, 13, time = 0} local checkpoint_B = {21, 15, 18, time = 1} -- A 和 B 之间间隔 1 秒 local checkpoint_C = {13, 24, 20, time = 6} -- B 和 C 之间间隔 5 秒 local checkpoints = {checkpoint_A, checkpoint_B, checkpoint_C} -- 总持续时间为 6 秒,每秒 10 帧,返回 61 个点 local array_of_61_points = smooth(checkpoints, 10) for _, point in ipairs(array_of_61_points) do print(string.format("时间 = %.1f, x = %.3f, y = %.3f, z = %.3f", point.time, point[1], point[2], point[3])) end