Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. function smooth( points, steps ) --points is an array, steps is how many frames inbetween spline points
  2.  
  3. if #points < 3 then
  4. return points
  5. end
  6.  
  7. local steps = steps or 5
  8.  
  9. local spline = {}
  10. local count = #points - 1
  11. local p0, p1, p2, p3, x, y, z
  12.  
  13. for i = 1, count do
  14.  
  15. if i == 1 then
  16. p0, p1, p2, p3 = points[i], points[i], points[i + 1], points[i + 2]
  17. elseif i == count then
  18. p0, p1, p2, p3 = points[#points - 2], points[#points - 1], points[#points], points[#points]
  19. else
  20. p0, p1, p2, p3 = points[i - 1], points[i], points[i + 1], points[i + 2]
  21. end
  22.  
  23. for t = 0, 1, 1 / steps do
  24.  
  25. -- Main spline equation
  26. 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 )
  27. 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 )
  28. 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 )
  29. if not(#spline > 0 and spline[#spline].x == x and spline[#spline].y == y and spline[#spline].z == z) then
  30. table.insert( spline , { x = x , y = y, z = z } )
  31. end
  32.  
  33. end
  34.  
  35. end
  36.  
  37. return spline
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement