Advertisement
synapse_tutorials

Free cam / scene view script

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