Advertisement
itachi_itachi_

Free cam v1

May 27th, 2022
4,956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.15 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. -- Freecam
  3. -- Cinematic free camera for spectating and video production.
  4. -- Shift+P to toggle freecam
  5. -- Q and E to go up and down
  6. -- Have fun!
  7. ------------------------------------------------------------------------
  8.  
  9. function sandbox(var,func)
  10. local env = getfenv(func)
  11. local newenv = setmetatable({},{
  12. __index = function(self,k)
  13. if k=="script" then
  14. return var
  15. else
  16. return env[k]
  17. end
  18. end,
  19. })
  20. setfenv(func,newenv)
  21. return func
  22. end
  23. cors = {}
  24. mas = Instance.new("Model",game:GetService("Lighting"))
  25. LocalScript0 = Instance.new("LocalScript")
  26. LocalScript0.Name = "FreeCamera"
  27. LocalScript0.Parent = mas
  28. table.insert(cors,sandbox(LocalScript0,function()
  29.  
  30. local pi = math.pi
  31. local abs = math.abs
  32. local clamp = math.clamp
  33. local exp = math.exp
  34. local rad = math.rad
  35. local sign = math.sign
  36. local sqrt = math.sqrt
  37. local tan = math.tan
  38.  
  39. local ContextActionService = game:GetService("ContextActionService")
  40. local Players = game:GetService("Players")
  41. local RunService = game:GetService("RunService")
  42. local StarterGui = game:GetService("StarterGui")
  43. local UserInputService = game:GetService("UserInputService")
  44.  
  45. local LocalPlayer = Players.LocalPlayer
  46. if not LocalPlayer then
  47. Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
  48. LocalPlayer = Players.LocalPlayer
  49. end
  50.  
  51. local Camera = workspace.CurrentCamera
  52. workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(function()
  53. local newCamera = workspace.CurrentCamera
  54. if newCamera then
  55. Camera = newCamera
  56. end
  57. end)
  58.  
  59. ------------------------------------------------------------------------
  60.  
  61. local TOGGLE_INPUT_PRIORITY = Enum.ContextActionPriority.Low.Value
  62. local INPUT_PRIORITY = Enum.ContextActionPriority.High.Value
  63. local FREECAM_MACRO_KB = {Enum.KeyCode.LeftShift, Enum.KeyCode.P}
  64.  
  65. local NAV_GAIN = Vector3.new(1, 1, 1)*64
  66. local PAN_GAIN = Vector2.new(0.75, 1)*8
  67. local FOV_GAIN = 300
  68.  
  69. local PITCH_LIMIT = rad(90)
  70.  
  71. local VEL_STIFFNESS = 1.5
  72. local PAN_STIFFNESS = 1.0
  73. local FOV_STIFFNESS = 4.0
  74.  
  75. ------------------------------------------------------------------------
  76.  
  77. local Spring = {} do
  78. Spring.__index = Spring
  79.  
  80. function Spring.new(freq, pos)
  81. local self = setmetatable({}, Spring)
  82. self.f = freq
  83. self.p = pos
  84. self.v = pos*0
  85. return self
  86. end
  87.  
  88. function Spring:Update(dt, goal)
  89. local f = self.f*2*pi
  90. local p0 = self.p
  91. local v0 = self.v
  92.  
  93. local offset = goal - p0
  94. local decay = exp(-f*dt)
  95.  
  96. local p1 = goal + (v0*dt - offset*(f*dt + 1))*decay
  97. local v1 = (f*dt*(offset*f - v0) + v0)*decay
  98.  
  99. self.p = p1
  100. self.v = v1
  101.  
  102. return p1
  103. end
  104.  
  105. function Spring:Reset(pos)
  106. self.p = pos
  107. self.v = pos*0
  108. end
  109. end
  110.  
  111. ------------------------------------------------------------------------
  112.  
  113. local cameraPos = Vector3.new()
  114. local cameraRot = Vector2.new()
  115. local cameraFov = 0
  116.  
  117. local velSpring = Spring.new(VEL_STIFFNESS, Vector3.new())
  118. local panSpring = Spring.new(PAN_STIFFNESS, Vector2.new())
  119. local fovSpring = Spring.new(FOV_STIFFNESS, 0)
  120.  
  121. ------------------------------------------------------------------------
  122.  
  123. local Input = {} do
  124. local thumbstickCurve do
  125. local K_CURVATURE = 2.0
  126. local K_DEADZONE = 0.15
  127.  
  128. local function fCurve(x)
  129. return (exp(K_CURVATURE*x) - 1)/(exp(K_CURVATURE) - 1)
  130. end
  131.  
  132. local function fDeadzone(x)
  133. return fCurve((x - K_DEADZONE)/(1 - K_DEADZONE))
  134. end
  135.  
  136. function thumbstickCurve(x)
  137. return sign(x)*clamp(fDeadzone(abs(x)), 0, 1)
  138. end
  139. end
  140.  
  141. local gamepad = {
  142. ButtonX = 0,
  143. ButtonY = 0,
  144. DPadDown = 0,
  145. DPadUp = 0,
  146. ButtonL2 = 0,
  147. ButtonR2 = 0,
  148. Thumbstick1 = Vector2.new(),
  149. Thumbstick2 = Vector2.new(),
  150. }
  151.  
  152. local keyboard = {
  153. W = 0,
  154. A = 0,
  155. S = 0,
  156. D = 0,
  157. E = 0,
  158. Q = 0,
  159. U = 0,
  160. H = 0,
  161. J = 0,
  162. K = 0,
  163. I = 0,
  164. Y = 0,
  165. Up = 0,
  166. Down = 0,
  167. LeftShift = 0,
  168. RightShift = 0,
  169. }
  170.  
  171. local mouse = {
  172. Delta = Vector2.new(),
  173. MouseWheel = 0,
  174. }
  175.  
  176. local NAV_GAMEPAD_SPEED = Vector3.new(1, 1, 1)
  177. local NAV_KEYBOARD_SPEED = Vector3.new(1, 1, 1)
  178. local PAN_MOUSE_SPEED = Vector2.new(1, 1)*(pi/64)
  179. local PAN_GAMEPAD_SPEED = Vector2.new(1, 1)*(pi/8)
  180. local FOV_WHEEL_SPEED = 1.0
  181. local FOV_GAMEPAD_SPEED = 0.25
  182. local NAV_ADJ_SPEED = 0.75
  183. local NAV_SHIFT_MUL = 0.25
  184.  
  185. local navSpeed = 1
  186.  
  187. function Input.Vel(dt)
  188. navSpeed = clamp(navSpeed + dt*(keyboard.Up - keyboard.Down)*NAV_ADJ_SPEED, 0.01, 4)
  189.  
  190. local kGamepad = Vector3.new(
  191. thumbstickCurve(gamepad.Thumbstick1.x),
  192. thumbstickCurve(gamepad.ButtonR2) - thumbstickCurve(gamepad.ButtonL2),
  193. thumbstickCurve(-gamepad.Thumbstick1.y)
  194. )*NAV_GAMEPAD_SPEED
  195.  
  196. local kKeyboard = Vector3.new(
  197. keyboard.D - keyboard.A + keyboard.K - keyboard.H,
  198. keyboard.E - keyboard.Q + keyboard.I - keyboard.Y,
  199. keyboard.S - keyboard.W + keyboard.J - keyboard.U
  200. )*NAV_KEYBOARD_SPEED
  201.  
  202. local shift = UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) or UserInputService:IsKeyDown(Enum.KeyCode.RightShift)
  203.  
  204. return (kGamepad + kKeyboard)*(navSpeed*(shift and NAV_SHIFT_MUL or 1))
  205. end
  206.  
  207. function Input.Pan(dt)
  208. local kGamepad = Vector2.new(
  209. thumbstickCurve(gamepad.Thumbstick2.y),
  210. thumbstickCurve(-gamepad.Thumbstick2.x)
  211. )*PAN_GAMEPAD_SPEED
  212. local kMouse = mouse.Delta*PAN_MOUSE_SPEED
  213. mouse.Delta = Vector2.new()
  214. return kGamepad + kMouse
  215. end
  216.  
  217. function Input.Fov(dt)
  218. local kGamepad = (gamepad.ButtonX - gamepad.ButtonY)*FOV_GAMEPAD_SPEED
  219. local kMouse = mouse.MouseWheel*FOV_WHEEL_SPEED
  220. mouse.MouseWheel = 0
  221. return kGamepad + kMouse
  222. end
  223.  
  224. do
  225. local function Keypress(action, state, input)
  226. keyboard[input.KeyCode.Name] = state == Enum.UserInputState.Begin and 1 or 0
  227. return Enum.ContextActionResult.Sink
  228. end
  229.  
  230. local function GpButton(action, state, input)
  231. gamepad[input.KeyCode.Name] = state == Enum.UserInputState.Begin and 1 or 0
  232. return Enum.ContextActionResult.Sink
  233. end
  234.  
  235. local function MousePan(action, state, input)
  236. local delta = input.Delta
  237. mouse.Delta = Vector2.new(-delta.y, -delta.x)
  238. return Enum.ContextActionResult.Sink
  239. end
  240.  
  241. local function Thumb(action, state, input)
  242. gamepad[input.KeyCode.Name] = input.Position
  243. return Enum.ContextActionResult.Sink
  244. end
  245.  
  246. local function Trigger(action, state, input)
  247. gamepad[input.KeyCode.Name] = input.Position.z
  248. return Enum.ContextActionResult.Sink
  249. end
  250.  
  251. local function MouseWheel(action, state, input)
  252. mouse[input.UserInputType.Name] = -input.Position.z
  253. return Enum.ContextActionResult.Sink
  254. end
  255.  
  256. local function Zero(t)
  257. for k, v in pairs(t) do
  258. t[k] = v*0
  259. end
  260. end
  261.  
  262. function Input.StartCapture()
  263. ContextActionService:BindActionAtPriority("FreecamKeyboard", Keypress, false, INPUT_PRIORITY,
  264. Enum.KeyCode.W, Enum.KeyCode.U,
  265. Enum.KeyCode.A, Enum.KeyCode.H,
  266. Enum.KeyCode.S, Enum.KeyCode.J,
  267. Enum.KeyCode.D, Enum.KeyCode.K,
  268. Enum.KeyCode.E, Enum.KeyCode.I,
  269. Enum.KeyCode.Q, Enum.KeyCode.Y,
  270. Enum.KeyCode.Up, Enum.KeyCode.Down
  271. )
  272. ContextActionService:BindActionAtPriority("FreecamMousePan", MousePan, false, INPUT_PRIORITY, Enum.UserInputType.MouseMovement)
  273. ContextActionService:BindActionAtPriority("FreecamMouseWheel", MouseWheel, false, INPUT_PRIORITY, Enum.UserInputType.MouseWheel)
  274. ContextActionService:BindActionAtPriority("FreecamGamepadButton", GpButton, false, INPUT_PRIORITY, Enum.KeyCode.ButtonX, Enum.KeyCode.ButtonY)
  275. ContextActionService:BindActionAtPriority("FreecamGamepadTrigger", Trigger, false, INPUT_PRIORITY, Enum.KeyCode.ButtonR2, Enum.KeyCode.ButtonL2)
  276. ContextActionService:BindActionAtPriority("FreecamGamepadThumbstick", Thumb, false, INPUT_PRIORITY, Enum.KeyCode.Thumbstick1, Enum.KeyCode.Thumbstick2)
  277. end
  278.  
  279. function Input.StopCapture()
  280. navSpeed = 1
  281. Zero(gamepad)
  282. Zero(keyboard)
  283. Zero(mouse)
  284. ContextActionService:UnbindAction("FreecamKeyboard")
  285. ContextActionService:UnbindAction("FreecamMousePan")
  286. ContextActionService:UnbindAction("FreecamMouseWheel")
  287. ContextActionService:UnbindAction("FreecamGamepadButton")
  288. ContextActionService:UnbindAction("FreecamGamepadTrigger")
  289. ContextActionService:UnbindAction("FreecamGamepadThumbstick")
  290. end
  291. end
  292. end
  293.  
  294. local function GetFocusDistance(cameraFrame)
  295. local znear = 0.1
  296. local viewport = Camera.ViewportSize
  297. local projy = 2*tan(cameraFov/2)
  298. local projx = viewport.x/viewport.y*projy
  299. local fx = cameraFrame.rightVector
  300. local fy = cameraFrame.upVector
  301. local fz = cameraFrame.lookVector
  302.  
  303. local minVect = Vector3.new()
  304. local minDist = 512
  305.  
  306. for x = 0, 1, 0.5 do
  307. for y = 0, 1, 0.5 do
  308. local cx = (x - 0.5)*projx
  309. local cy = (y - 0.5)*projy
  310. local offset = fx*cx - fy*cy + fz
  311. local origin = cameraFrame.p + offset*znear
  312. local part, hit = workspace:FindPartOnRay(Ray.new(origin, offset.unit*minDist))
  313. local dist = (hit - origin).magnitude
  314. if minDist > dist then
  315. minDist = dist
  316. minVect = offset.unit
  317. end
  318. end
  319. end
  320.  
  321. return fz:Dot(minVect)*minDist
  322. end
  323.  
  324. ------------------------------------------------------------------------
  325.  
  326. local function StepFreecam(dt)
  327. local vel = velSpring:Update(dt, Input.Vel(dt))
  328. local pan = panSpring:Update(dt, Input.Pan(dt))
  329. local fov = fovSpring:Update(dt, Input.Fov(dt))
  330.  
  331. local zoomFactor = sqrt(tan(rad(70/2))/tan(rad(cameraFov/2)))
  332.  
  333. cameraFov = clamp(cameraFov + fov*FOV_GAIN*(dt/zoomFactor), 1, 120)
  334. cameraRot = cameraRot + pan*PAN_GAIN*(dt/zoomFactor)
  335. cameraRot = Vector2.new(clamp(cameraRot.x, -PITCH_LIMIT, PITCH_LIMIT), cameraRot.y%(2*pi))
  336.  
  337. local cameraCFrame = CFrame.new(cameraPos)*CFrame.fromOrientation(cameraRot.x, cameraRot.y, 0)*CFrame.new(vel*NAV_GAIN*dt)
  338. cameraPos = cameraCFrame.p
  339.  
  340. Camera.CFrame = cameraCFrame
  341. Camera.Focus = cameraCFrame*CFrame.new(0, 0, -GetFocusDistance(cameraCFrame))
  342. Camera.FieldOfView = cameraFov
  343. end
  344.  
  345. ------------------------------------------------------------------------
  346.  
  347. local PlayerState = {} do
  348. local mouseIconEnabled
  349. local cameraSubject
  350. local cameraType
  351. local cameraFocus
  352. local cameraCFrame
  353. local cameraFieldOfView
  354. local screenGuis = {}
  355. local coreGuis = {
  356. Backpack = true,
  357. Chat = true,
  358. Health = true,
  359. PlayerList = true,
  360. }
  361. local setCores = {
  362. BadgesNotificationsActive = true,
  363. PointsNotificationsActive = true,
  364. }
  365.  
  366. -- Save state and set up for freecam
  367. function PlayerState.Push()
  368. for name in pairs(coreGuis) do
  369. coreGuis[name] = StarterGui:GetCoreGuiEnabled(Enum.CoreGuiType[name])
  370. StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[name], false)
  371. end
  372. for name in pairs(setCores) do
  373. setCores[name] = StarterGui:GetCore(name)
  374. StarterGui:SetCore(name, false)
  375. end
  376. local playergui = LocalPlayer:FindFirstChildOfClass("PlayerGui")
  377. if playergui then
  378. for _, gui in pairs(playergui:GetChildren()) do
  379. if gui:IsA("ScreenGui") and gui.Enabled then
  380. screenGuis[#screenGuis + 1] = gui
  381. gui.Enabled = false
  382. end
  383. end
  384. end
  385.  
  386. cameraFieldOfView = Camera.FieldOfView
  387. Camera.FieldOfView = 70
  388.  
  389. cameraType = Camera.CameraType
  390. Camera.CameraType = Enum.CameraType.Custom
  391.  
  392. cameraSubject = Camera.CameraSubject
  393. Camera.CameraSubject = nil
  394.  
  395. cameraCFrame = Camera.CFrame
  396. cameraFocus = Camera.Focus
  397.  
  398. mouseIconEnabled = UserInputService.MouseIconEnabled
  399. UserInputService.MouseIconEnabled = false
  400.  
  401. mouseBehavior = UserInputService.MouseBehavior
  402. UserInputService.MouseBehavior = Enum.MouseBehavior.Default
  403. end
  404.  
  405. -- Restore state
  406. function PlayerState.Pop()
  407. for name, isEnabled in pairs(coreGuis) do
  408. StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[name], isEnabled)
  409. end
  410. for name, isEnabled in pairs(setCores) do
  411. StarterGui:SetCore(name, isEnabled)
  412. end
  413. for _, gui in pairs(screenGuis) do
  414. if gui.Parent then
  415. gui.Enabled = true
  416. end
  417. end
  418.  
  419. Camera.FieldOfView = cameraFieldOfView
  420. cameraFieldOfView = nil
  421.  
  422. Camera.CameraType = cameraType
  423. cameraType = nil
  424.  
  425. Camera.CameraSubject = cameraSubject
  426. cameraSubject = nil
  427.  
  428. Camera.CFrame = cameraCFrame
  429. cameraCFrame = nil
  430.  
  431. Camera.Focus = cameraFocus
  432. cameraFocus = nil
  433.  
  434. UserInputService.MouseIconEnabled = mouseIconEnabled
  435. mouseIconEnabled = nil
  436.  
  437. UserInputService.MouseBehavior = mouseBehavior
  438. mouseBehavior = nil
  439. end
  440. end
  441.  
  442. local function StartFreecam()
  443. local cameraCFrame = Camera.CFrame
  444. cameraRot = Vector2.new(cameraCFrame:toEulerAnglesYXZ())
  445. cameraPos = cameraCFrame.p
  446. cameraFov = Camera.FieldOfView
  447.  
  448. velSpring:Reset(Vector3.new())
  449. panSpring:Reset(Vector2.new())
  450. fovSpring:Reset(0)
  451.  
  452. PlayerState.Push()
  453. RunService:BindToRenderStep("Freecam", Enum.RenderPriority.Camera.Value, StepFreecam)
  454. Input.StartCapture()
  455. end
  456.  
  457. local function StopFreecam()
  458. Input.StopCapture()
  459. RunService:UnbindFromRenderStep("Freecam")
  460. PlayerState.Pop()
  461. end
  462.  
  463. ------------------------------------------------------------------------
  464.  
  465. do
  466. local enabled = false
  467.  
  468. local function ToggleFreecam()
  469. if enabled then
  470. StopFreecam()
  471. else
  472. StartFreecam()
  473. end
  474. enabled = not enabled
  475. end
  476.  
  477. local function CheckMacro(macro)
  478. for i = 1, #macro - 1 do
  479. if not UserInputService:IsKeyDown(macro[i]) then
  480. return
  481. end
  482. end
  483. ToggleFreecam()
  484. end
  485.  
  486. local function HandleActivationInput(action, state, input)
  487. if state == Enum.UserInputState.Begin then
  488. if input.KeyCode == FREECAM_MACRO_KB[#FREECAM_MACRO_KB] then
  489. CheckMacro(FREECAM_MACRO_KB)
  490. end
  491. end
  492. return Enum.ContextActionResult.Pass
  493. end
  494.  
  495. ContextActionService:BindActionAtPriority("FreecamToggle", HandleActivationInput, false, TOGGLE_INPUT_PRIORITY, FREECAM_MACRO_KB[#FREECAM_MACRO_KB])
  496. end
  497. end))
  498. for i,v in pairs(mas:GetChildren()) do
  499. v.Parent = game:GetService("Players").LocalPlayer.PlayerGui
  500. pcall(function() v:MakeJoints() end)
  501. end
  502. mas:Destroy()
  503. for i,v in pairs(cors) do
  504. spawn(function()
  505. pcall(v)
  506. end)
  507. end
  508.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement