Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement