oatmeal2009

fds

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