具有多个持续时间的样条曲线

我正在尝试制作具有多个持续时间的样条曲线。基本上,我想创建一个通过用户提供的关键帧生成的样条曲线。可能会有多个具有不同持续时间的关键帧。所以我问,我如何制作一个在不同关键帧之间以不同速度行进的样条曲线。例如,我希望在关键帧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
点赞
用户1847592
用户1847592

使用简单直接的方法:

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
2016-12-10 17:06:13