Guest User

Untitled

a guest
Dec 4th, 2017
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. function QuaternionFromCFrame(cf)
  2.     local mx,  my,  mz,
  3.           m00, m01, m02,
  4.           m10, m11, m12,
  5.           m20, m21, m22 = cf:components()
  6.     local trace = m00 + m11 + m22
  7.     if trace > 0 then
  8.         local s = math.sqrt(1 + trace)
  9.         local recip = 0.5/s
  10.         return (m21-m12)*recip, (m02-m20)*recip, (m10-m01)*recip, s*0.5
  11.     else
  12.         local i = 0
  13.         if m11 > m00 then i = 1 end
  14.         if m22 > (i == 0 and m00 or m11) then i = 2 end
  15.         if i == 0 then
  16.             local s = math.sqrt(m00-m11-m22+1)
  17.             local recip = 0.5/s
  18.             return 0.5*s, (m10+m01)*recip, (m20+m02)*recip, (m21-m12)*recip
  19.         elseif i == 1 then
  20.             local s = math.sqrt(m11-m22-m00+1)
  21.             local recip = 0.5/s
  22.             return (m01+m10)*recip, 0.5*s, (m21+m12)*recip, (m02-m20)*recip
  23.         elseif i == 2 then
  24.             local s = math.sqrt(m22-m00-m11+1)
  25.             local recip = 0.5/s
  26.             return (m02+m20)*recip, (m12+m21)*recip, 0.5*s, (m10-m01)*recip
  27.         end
  28.     end
  29. end
  30.  
  31. function QuaternionToCFrame(px, py, pz, x, y, z, w)
  32.     local xs, ys, zs = x + x, y + y, z + z
  33.     local wx, wy, wz = w*xs, w*ys, w*zs
  34.     --
  35.     local xx = x*xs
  36.     local xy = x*ys
  37.     local xz = x*zs
  38.     local yy = y*ys
  39.     local yz = y*zs
  40.     local zz = z*zs
  41.     --
  42.     return CFrame.new(px,        py,        pz,
  43.                       1-(yy+zz), xy - wz,   xz + wy,
  44.                       xy + wz,   1-(xx+zz), yz - wx,
  45.                       xz - wy,   yz + wx,   1-(xx+yy))
  46. end
  47.  
  48. function QuaternionSlerp(a, b, t)
  49.     local cosTheta = a[1]*b[1] + a[2]*b[2] + a[3]*b[3] + a[4]*b[4]
  50.     local startInterp, finishInterp;
  51.     if cosTheta >= 0.0001 then
  52.         if (1 - cosTheta) > 0.0001 then
  53.             local theta = math.acos(cosTheta)
  54.             local invSinTheta = 1/math.sin(theta)
  55.             startInterp = math.sin((1-t)*theta)*invSinTheta
  56.             finishInterp = math.sin(t*theta)*invSinTheta
  57.         else
  58.             startInterp = 1-t
  59.             finishInterp = t
  60.         end
  61.     else
  62.         if (1+cosTheta) > 0.0001 then
  63.             local theta = math.acos(-cosTheta)
  64.             local invSinTheta = 1/math.sin(theta)
  65.             startInterp = math.sin((t-1)*theta)*invSinTheta
  66.             finishInterp = math.sin(t*theta)*invSinTheta
  67.         else
  68.             startInterp = t-1
  69.             finishInterp = t
  70.         end
  71.     end
  72.     return a[1]*startInterp + b[1]*finishInterp,
  73.            a[2]*startInterp + b[2]*finishInterp,
  74.            a[3]*startInterp + b[3]*finishInterp,
  75.            a[4]*startInterp + b[4]*finishInterp        
  76. end
  77.  
  78. function HermiteInterpolation(y0, y1,y2, y3,mu,tension,bias)
  79.  
  80.     local m0,m1,mu2,mu3
  81.     local a0,a1,a2,a3
  82.  
  83.     mu2 = mu * mu
  84.     mu3 = mu2 * mu
  85.     m0  = (y1-y0)*(1+bias)*(1-tension)/2
  86.     m0  = m0 + (y2-y1)*(1-bias)*(1-tension)/2
  87.     m1  = (y2-y1)*(1+bias)*(1-tension)/2
  88.     m1  = m1 + (y3-y2)*(1-bias)*(1-tension)/2
  89.     a0  =  2*mu3 - 3*mu2 + 1
  90.     a1  =    mu3 - 2*mu2 + mu
  91.     a2  =    mu3 -   mu2
  92.     a3  = -2*mu3 + 3*mu2
  93.  
  94.    return(a0*y1+a1*m0+a2*m1+a3*y2);
  95. end
  96.  
  97. function interpCFrame(x, a, b, y, t, bias, l)
  98.  local qa = {QuaternionFromCFrame(a)}
  99.  local qb = {QuaternionFromCFrame(b)}
  100.  local qr = {QuaternionSlerp(qa, qb, l*l*l*(l*(l*6-15)+10))}
  101.  local xPos = Vector3.new(x.x, x.y, x.z)
  102.  local aPos = Vector3.new(a.x, a.y, a.z)
  103.  local bPos = Vector3.new(b.x, b.y, b.z)
  104.  local yPos = Vector3.new(y.x, y.y, y.z)
  105.  local rPos = HermiteInterpolation(xPos, aPos, bPos, yPos, l, t, bias)
  106.  return QuaternionToCFrame(rPos.x, rPos.y, rPos.z, qr[1], qr[2], qr[3], qr[4])
  107. end
  108.    
  109. ---------------------------------------------------------------------------------------------  
  110.    
  111. local plr = game.Players.LocalPlayer
  112. local cam = game.Workspace:WaitForChild('Camera')
  113. cam.FieldOfView = 20
  114. local sceneCam = game.Workspace.Cam
  115. sceneCam.Transparency = 1
  116.  
  117. local Sequence = -1
  118. local cF
  119.  
  120. game:GetService("UserInputService").InputBegan:connect(function(input)
  121.     if input.KeyCode == Enum.KeyCode.Return then
  122.         Sequence = 0
  123.     elseif input.KeyCode == Enum.KeyCode.One then
  124.         cF = tick()
  125.         Sequence = 1
  126.     end
  127. end)
  128.  
  129. local c = 0
  130. local animComplete = false
  131. local div = 0.75
  132. local bias = 1
  133. local spline = 0
  134. local cB = {}
  135. for i = 0, 20 do
  136.     local part = game.Workspace:findFirstChild("CamBezier"..i)
  137.     cB[i] = part
  138.     part.Transparency = 1
  139. end
  140. game:GetService("RunService").RenderStepped:connect(function()    
  141.     if Sequence > -1 then
  142.         if not animComplete and Sequence > 0 then
  143.             local lerpTime1 = (tick() - cF)/div
  144.             lerpTime1 = math.min(math.max(lerpTime1, 0), 1)
  145.             sceneCam.CFrame = interpCFrame(cB[c].CFrame, cB[c + 1].CFrame,
  146.                 cB[c + 2].CFrame, cB[c + 3].CFrame, spline, bias, lerpTime1)
  147.             if lerpTime1 == 1 then
  148.                 cF = tick()
  149.                 lerpTime1 = 0
  150.                 c = c + 1
  151.                 if (c+3) > #cB then
  152.                     animComplete = true
  153.                 end
  154.             end
  155.         end
  156.         cam.CoordinateFrame = sceneCam.CFrame
  157.     end
  158. end)
Add Comment
Please, Sign In to add comment