2016-12-03 3 views
0

私は複数の持続時間を持つスプラインを作成しようとしています。基本的には、ユーザーが入力したキーフレームを使って生成されたスプラインを作成したいと考えています。異なる継続時間を有する複数のキーフレームが存在してもよい。ですから、どのようにして異なるキーフレームで異なるスピードで移動するのか、私は尋ねます。キーフレームAとBの間に1秒、BとCの間に1秒としたいとしましょう。Source FilmmakerやAutodesk Mayaのようなアニメーションエディタと同様にします。誰かがスプラインを作るのを見るたびに、常に1つの一定のスピードがあります。常に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 

     -- Main spline equation 
     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 
+0

をあなたは手の込んだてもらえますか? – TheRoomDiedWithIt

+0

(本当に都合の良い時間ではなく、遅れて申し訳ありません)私はLuaを使用していますが、この質問はその言語特有のものではありません。私はちょうどそれを行う方法を知りたい。同様に、私はスピードを変えるのですか?スピードを解く数式はありますか? – TheRoomDiedWithIt

+0

(遅い応答、動きが苦しい)まあ、私はあなたが何を意味するのか理解するのが苦労しています、あなたはたぶんコメントではなく答えとしてあなたの応答を投稿し、それをより詳細にすることができますか? – TheRoomDiedWithIt

答えて

0

簡単な方法を用いた:

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, "Non-increasing time sequence") 
     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 
    -- Prepare formulas for each segment of spline 
    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, "Non-increasing time sequence") 
     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 
    -- Calculate spline points 
    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 "timestamp in seconds" 
local checkpoint_A = {11, 12, 13, time = 0} 
local checkpoint_B = {21, 15, 18, time = 1} -- 1 second between A and B 
local checkpoint_C = {13, 24, 20, time = 6} -- 5 seconds between B and C 

local checkpoints = {checkpoint_A, checkpoint_B, checkpoint_C} 

-- total duration is 6 seconds, 10 frames per second, 61 points returned 
local array_of_61_points = smooth(checkpoints, 10) 

for _, point in ipairs(array_of_61_points) do 
    print(string.format("time = %.1f, x = %.3f, y = %.3f, z = %.3f", 
     point.time, point[1], point[2], point[3])) 
end 
関連する問題