Advertisement
Sergeant_SethZP

Camera script

Apr 14th, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1. wait()
  2.  
  3. --Insert a model called "Track" in here with the shots you've taken.
  4. --This script simply plays back the track for the respective player.
  5.  
  6. --Start from the original camera position (ie, looking at the player)
  7. from_original_camera_position = true
  8.  
  9. --Tween back to original camera position
  10. to_original_camera_position = true
  11.  
  12. --Don't allow player to walk during the cutscene?
  13. freeze_player = false
  14.  
  15. --Show a GUI? (set to nil if no)
  16. gui = true --script.CutsceneGui
  17.  
  18. --Remove GUI when finished?
  19. remove_gui = true
  20.  
  21. children = function (o) local c = o:GetChildren() local n = #c local i = 0 return function () i = i + 1 if i <= n then return i, c[i] end end end
  22. interpolate = {}
  23.  
  24. function interpolate.linear(a, t, n0)
  25.     local n0 = n0 or 1
  26.     local v0 = t[n0]
  27.     local v1 = t[n0 + 1] or t[n0]
  28.     return v0 * (1 - a) + v1 * a
  29. end
  30.  
  31. function interpolate.cosine(a, t, n0)
  32.     local n0 = n0 or 1
  33.     local v0 = t[n0]
  34.     local v1 = t[n0 + 1] or t[n0]
  35.     local f = (1 - math.cos(a * math.pi)) * .5
  36.     return v0 * (1 - f) + v1 * f
  37. end
  38.  
  39. function interpolate.cubic(a, t, n1)
  40.     local n1 = n1 or 2
  41.     local v0 = t[n1 - 1] or t[n1]
  42.     local v1 = t[n1]
  43.     local v2 = t[n1 + 1] or t[n1]
  44.     local v3 = t[n1 + 2] or t[n1]
  45.     local P = (v3 - v2) - (v0 - v1)
  46.     local Q = (v0 - v1) - P
  47.     local R = v2 - v0
  48.     local S = v1
  49.     return P * math.pow(a, 3) + Q * math.pow(a, 2) + R * a + S
  50. end
  51.  
  52. function thread(func)
  53.     coroutine.resume(coroutine.create(func))
  54. end
  55.  
  56. function interpolateV3(func, a, t, n)
  57.     local xs = {}
  58.     local ys = {}
  59.     local zs = {}
  60.     for i, v in ipairs(t) do
  61.         table.insert(xs, v.x)
  62.         table.insert(ys, v.y)
  63.         table.insert(zs, v.z)
  64.     end
  65.     return Vector3.new(func(a, xs, n), func(a, ys, n), func(a, zs, n))
  66. end
  67.  
  68. function interpolateCF(func, a, t, n)
  69.     local ps = {}
  70.     local lvs = {}
  71.     for i, v in ipairs(t) do
  72.         table.insert(ps, v.p)
  73.         table.insert(lvs, v.lookVector)
  74.     end
  75.     local p = interpolateV3(func, a, ps, n)
  76.     local lv = interpolateV3(func, a, lvs, n)
  77.     return CFrame.new(p, p + lv)
  78. end
  79.  
  80. function getInterpolator(name)
  81.     if interpolate[name] then
  82.         return interpolate[name]
  83.     else
  84.         print("Unknown interpolator " .. name)
  85.         return interpolate.cosine
  86.     end
  87. end
  88.  
  89. function playTrack(camera, track)
  90.     --print("Playing track with " .. tostring(camera) .. " and " .. #track .. " shots")
  91.     --track is a table of shots
  92.     local cf = camera.CoordinateFrame
  93.     local fc = camera.Focus
  94.     local fov = camera.FieldOfView
  95.     local roll = camera:GetRoll()
  96.     local free_mode = false
  97.     local focus_part = camera.CameraSubject
  98.     if camera.CameraSubject then free_mode = false end
  99.     local enabled = true
  100.     thread(function ()
  101.         local lfov = fov
  102.         while enabled do
  103.             camera.CoordinateFrame = cf
  104.             if free_mode then
  105.                 focus_part.CFrame = fc
  106.             else
  107.                 camera.Focus = fc
  108.             end
  109.             camera.FieldOfView = math.max(20, math.min(80, fov))
  110.             camera.CameraType = Enum.CameraType.Scriptable
  111.             camera:SetRoll(roll)
  112.             wait()
  113.         end
  114.     end)
  115.     local cfs = {}
  116.     local fcs = {}
  117.     local fovs = {}
  118.     local rolls = {}
  119.     for i, v in pairs(track) do
  120.         table.insert(cfs, v.CoordinateFrame)
  121.         table.insert(fcs, v.Focus)
  122.         table.insert(fovs, v.FieldOfView)
  123.         table.insert(rolls, v.Roll)
  124.     end
  125.     for n = 1, #track - 1 do
  126.         --print("Shot " .. n .. " to " .. (n + 1))
  127.         local shot_this = track[n]
  128.         --local shot_next = track[n + 1] or track[n]
  129.         --print("Waiting " .. shot_this.Time)
  130.         wait(shot_this.Time)
  131.         local inter = getInterpolator(shot_this.TweenMode)
  132.         local time_start = tick()
  133.         local time = shot_this.TweenTime
  134.         --print("Tweening...")
  135.         while tick() < time_start + time do
  136.             local a = (tick() - time_start) / time
  137.            
  138.             --do the interpolation
  139.             cf = interpolateCF(inter, a, cfs, n)
  140.             fc = interpolateCF(inter, a, fcs, n)
  141.             fov = interpolate.cosine(a, fovs, n)
  142.             roll = interpolate.cosine(a, rolls, n)
  143.            
  144.             wait()
  145.         end
  146.         --print("Done")
  147.     end
  148.     enabled = false
  149. end
  150.  
  151. function newShot(cf, focus, fov, roll, time, tweenmode, tweentime)
  152.     return {
  153.         CoordinateFrame = cf;
  154.         Focus = focus;
  155.         FieldOfView = fov;
  156.         Roll = roll;
  157.         Time = time;
  158.         TweenMode = tweenmode;
  159.         TweenTime = tweentime;
  160.     }
  161. end
  162.  
  163. function getShotFromShotData(camera)
  164.     local time = 0
  165.     local tweentime = 2
  166.     local fov = 60
  167.     local tweenmode = "cosine"
  168.     local cf = CFrame.new(0, 0, 0)
  169.     local fc = CFrame.new(0, 0, 1)
  170.     local roll = 0
  171.    
  172.     if camera:FindFirstChild("STime") then                  time = camera.STime.Value end
  173.     if camera:FindFirstChild("STweenTime") then         tweentime = camera.STweenTime.Value end
  174.     if camera:FindFirstChild("SFOV") then                   fov = camera.SFOV.Value end
  175.     if camera:FindFirstChild("STweenMode") then         tweenmode = camera.STweenMode.Value end
  176.     if camera:FindFirstChild("SCoordinateFrame") then   cf = camera.SCoordinateFrame.Value end
  177.     if camera:FindFirstChild("SFocus") then                 fc = camera.SFocus.Value end
  178.     if camera:FindFirstChild("SRoll") then              roll = camera.SRoll.Value end
  179.    
  180.     return newShot(cf, fc, fov, roll, time, tweenmode, tweentime)
  181. end
  182.  
  183. function getFocusPart()
  184.     local p = Instance.new("Part", workspace)
  185.     p.Anchored = true
  186.     p.FormFactor = Enum.FormFactor.Custom
  187.     p.Size = Vector3.new(0.2, 0.2, 0.2)
  188.     p.Transparency = 1
  189.     return p
  190. end
  191. function getTrack(p)
  192.     if not p then print("Nothing selected") return end
  193.     local shots = {}
  194.     for k, v in children(p) do
  195.         local n = tonumber(v.Name)
  196.         if n then
  197.             local shot = getShotFromShotData(v)
  198.             shots[n] = shot
  199.         end
  200.     end
  201.     local cc = workspace.CurrentCamera
  202.     local current_shot = newShot(cc.CoordinateFrame, cc.Focus, cc.FieldOfView, cc:GetRoll(), 0, "cosine", 2.5)
  203.     if from_original_camera_position then
  204.         table.insert(shots, 1, current_shot)
  205.     end
  206.     if to_original_camera_position then
  207.         table.insert(shots, current_shot)
  208.     end
  209.     local focus_part = getFocusPart()
  210.     focus_part.CFrame = cc.Focus
  211.     --focus_part.Parent = cc
  212.     workspace.CurrentCamera.CameraType = Enum.CameraType.Fixed
  213.     cc.CameraSubject = focus_part
  214.     --[[local nc = Instance.new("Camera", workspace)
  215.     workspace.CurrentCamera = nc
  216.     cc:Destroy()]]
  217.     local player = game.Players.LocalPlayer
  218.     local char = player.Character
  219.     local ws
  220.     if freeze_player then
  221.         ws = char.Humanoid.WalkSpeed
  222.         char.Torso.Anchored = true
  223.         char.Humanoid.WalkSpeed = 0
  224.         char.Humanoid.PlatformStand = true
  225.     end
  226.     local ngui
  227.     if gui then
  228.         ngui = gui:clone()
  229.         ngui.Parent = player.PlayerGui
  230.     end
  231.     playTrack(workspace.CurrentCamera, shots)
  232.     workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
  233.     workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
  234.     focus_part:Destroy()
  235.     if freeze_player then
  236.         char.Humanoid.WalkSpeed = ws
  237.         char.Torso.Anchored = false
  238.         char.Humanoid.PlatformStand = false
  239.     end
  240.     if ngui and remove_gui then
  241.         ngui:Destroy()
  242.     end
  243. end
  244.  
  245. getTrack(script.Track)
  246.  
  247. script:Destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement