Advertisement
Guest User

Code For Video

a guest
Dec 12th, 2020
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.47 KB | None | 0 0
  1. --[[
  2.     CameraModule - This ModuleScript implements a singleton class to manage the
  3.     selection, activation, and deactivation of the current camera controller,
  4.     character occlusion controller, and transparency controller. This script binds to
  5.     RenderStepped at Camera priority and calls the Update() methods on the active
  6.     controller instances.
  7.  
  8.     The camera controller ModuleScripts implement classes which are instantiated and
  9.     activated as-needed, they are no longer all instantiated up front as they were in
  10.     the previous generation of PlayerScripts.
  11.  
  12.     2018 PlayerScripts Update - AllYourBlox
  13. --]]
  14.  
  15. local CameraModule = {}
  16. CameraModule.__index = CameraModule
  17.  
  18. local FFlagUserCameraToggle do
  19.     local success, result = pcall(function()
  20.         return UserSettings():IsUserFeatureEnabled("UserCameraToggle")
  21.     end)
  22.     FFlagUserCameraToggle = success and result
  23. end
  24.  
  25. local FFlagUserRemoveTheCameraApi do
  26.     local success, result = pcall(function()
  27.         return UserSettings():IsUserFeatureEnabled("UserRemoveTheCameraApi")
  28.     end)
  29.     FFlagUserRemoveTheCameraApi = success and result
  30. end
  31.  
  32. local FFlagUserCameraInputRefactor do
  33.     local success, result = pcall(function()
  34.         return UserSettings():IsUserFeatureEnabled("UserCameraInputRefactor3")
  35.     end)
  36.     FFlagUserCameraInputRefactor = success and result
  37. end
  38.  
  39. local FFlagUserCarCam do
  40.     local success, result = pcall(function()
  41.         return UserSettings():IsUserFeatureEnabled("UserCarCam")
  42.     end)
  43.     FFlagUserCarCam = success and result
  44. end
  45.  
  46. -- NOTICE: Player property names do not all match their StarterPlayer equivalents,
  47. -- with the differences noted in the comments on the right
  48. local PLAYER_CAMERA_PROPERTIES =
  49. {
  50.     "CameraMinZoomDistance",
  51.     "CameraMaxZoomDistance",
  52.     "CameraMode",
  53.     "DevCameraOcclusionMode",
  54.     "DevComputerCameraMode",            -- Corresponds to StarterPlayer.DevComputerCameraMovementMode
  55.     "DevTouchCameraMode",               -- Corresponds to StarterPlayer.DevTouchCameraMovementMode
  56.  
  57.     -- Character movement mode
  58.     "DevComputerMovementMode",
  59.     "DevTouchMovementMode",
  60.     "DevEnableMouseLock",               -- Corresponds to StarterPlayer.EnableMouseLockOption
  61. }
  62.  
  63. local USER_GAME_SETTINGS_PROPERTIES =
  64. {
  65.     "ComputerCameraMovementMode",
  66.     "ComputerMovementMode",
  67.     "ControlMode",
  68.     "GamepadCameraSensitivity",
  69.     "MouseSensitivity",
  70.     "RotationType",
  71.     "TouchCameraMovementMode",
  72.     "TouchMovementMode",
  73. }
  74.  
  75. --[[ Roblox Services ]]--
  76. local Players = game:GetService("Players")
  77. local RunService = game:GetService("RunService")
  78. local UserInputService = game:GetService("UserInputService")
  79. local UserGameSettings = UserSettings():GetService("UserGameSettings")
  80.  
  81. -- Static camera utils
  82. local CameraUtils = require(script:WaitForChild("CameraUtils"))
  83. local CameraInput = require(script:WaitForChild("CameraInput"))
  84.  
  85. -- Load Roblox Camera Controller Modules
  86. local ClassicCamera = require(script:WaitForChild("ClassicCamera"))
  87. local OrbitalCamera = require(script:WaitForChild("OrbitalCamera"))
  88. local LegacyCamera = require(script:WaitForChild("LegacyCamera"))
  89. local VehicleCamera = require(script:WaitForChild("VehicleCamera"))
  90.  
  91. -- Load Roblox Occlusion Modules
  92. local Invisicam = require(script:WaitForChild("Invisicam"))
  93. local Poppercam = require(script:WaitForChild("Poppercam"))
  94.  
  95. -- Load the near-field character transparency controller and the mouse lock "shift lock" controller
  96. local TransparencyController = require(script:WaitForChild("TransparencyController"))
  97. local MouseLockController = require(script:WaitForChild("MouseLockController"))
  98.  
  99. -- Table of camera controllers that have been instantiated. They are instantiated as they are used.
  100. local instantiatedCameraControllers = {}
  101. local instantiatedOcclusionModules = {}
  102.  
  103. -- Management of which options appear on the Roblox User Settings screen
  104. do
  105.     local PlayerScripts = Players.LocalPlayer:WaitForChild("PlayerScripts")
  106.  
  107.     PlayerScripts:RegisterTouchCameraMovementMode(Enum.TouchCameraMovementMode.Default)
  108.     PlayerScripts:RegisterTouchCameraMovementMode(Enum.TouchCameraMovementMode.Follow)
  109.     PlayerScripts:RegisterTouchCameraMovementMode(Enum.TouchCameraMovementMode.Classic)
  110.  
  111.     PlayerScripts:RegisterComputerCameraMovementMode(Enum.ComputerCameraMovementMode.Default)
  112.     PlayerScripts:RegisterComputerCameraMovementMode(Enum.ComputerCameraMovementMode.Follow)
  113.     PlayerScripts:RegisterComputerCameraMovementMode(Enum.ComputerCameraMovementMode.Classic)
  114.     if FFlagUserCameraToggle then
  115.         PlayerScripts:RegisterComputerCameraMovementMode(Enum.ComputerCameraMovementMode.CameraToggle)
  116.     end
  117. end
  118.  
  119.  
  120. function CameraModule.new()
  121.     local self = setmetatable({},CameraModule)
  122.  
  123.     -- Current active controller instances
  124.     self.activeCameraController = nil
  125.     self.activeOcclusionModule = nil
  126.     self.activeTransparencyController = nil
  127.     self.activeMouseLockController = nil
  128.  
  129.     self.currentComputerCameraMovementMode = nil
  130.  
  131.     -- Connections to events
  132.     self.cameraSubjectChangedConn = nil
  133.     self.cameraTypeChangedConn = nil
  134.  
  135.     -- Adds CharacterAdded and CharacterRemoving event handlers for all current players
  136.     for _,player in pairs(Players:GetPlayers()) do
  137.         self:OnPlayerAdded(player)
  138.     end
  139.  
  140.     -- Adds CharacterAdded and CharacterRemoving event handlers for all players who join in the future
  141.     Players.PlayerAdded:Connect(function(player)
  142.         self:OnPlayerAdded(player)
  143.     end)
  144.  
  145.     self.activeTransparencyController = TransparencyController.new()
  146.     self.activeTransparencyController:Enable(true)
  147.  
  148.     if not UserInputService.TouchEnabled then
  149.         self.activeMouseLockController = MouseLockController.new()
  150.         local toggleEvent = self.activeMouseLockController:GetBindableToggleEvent()
  151.         if toggleEvent then
  152.             toggleEvent:Connect(function()
  153.                 self:OnMouseLockToggled()
  154.             end)
  155.         end
  156.     end
  157.  
  158.     self:ActivateCameraController(self:GetCameraControlChoice())
  159.     self:ActivateOcclusionModule(Players.LocalPlayer.DevCameraOcclusionMode)
  160.     self:OnCurrentCameraChanged() -- Does initializations and makes first camera controller
  161.     RunService:BindToRenderStep("cameraRenderUpdate", Enum.RenderPriority.Camera.Value, function(dt) self:Update(dt) end)
  162.  
  163.     -- Connect listeners to camera-related properties
  164.     for _, propertyName in pairs(PLAYER_CAMERA_PROPERTIES) do
  165.         Players.LocalPlayer:GetPropertyChangedSignal(propertyName):Connect(function()
  166.             self:OnLocalPlayerCameraPropertyChanged(propertyName)
  167.         end)
  168.     end
  169.  
  170.     for _, propertyName in pairs(USER_GAME_SETTINGS_PROPERTIES) do
  171.         UserGameSettings:GetPropertyChangedSignal(propertyName):Connect(function()
  172.             self:OnUserGameSettingsPropertyChanged(propertyName)
  173.         end)
  174.     end
  175.     game.Workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(function()
  176.         self:OnCurrentCameraChanged()
  177.     end)
  178.  
  179.     self.lastInputType = UserInputService:GetLastInputType()
  180.     UserInputService.LastInputTypeChanged:Connect(function(newLastInputType)
  181.         self.lastInputType = newLastInputType
  182.     end)
  183.  
  184.     return self
  185. end
  186.  
  187. function CameraModule:GetCameraMovementModeFromSettings()
  188.     local cameraMode = Players.LocalPlayer.CameraMode
  189.  
  190.     -- Lock First Person trumps all other settings and forces ClassicCamera
  191.     if cameraMode == Enum.CameraMode.LockFirstPerson then
  192.         return CameraUtils.ConvertCameraModeEnumToStandard(Enum.ComputerCameraMovementMode.Classic)
  193.     end
  194.  
  195.     local devMode, userMode
  196.     if UserInputService.TouchEnabled then
  197.         devMode = CameraUtils.ConvertCameraModeEnumToStandard(Players.LocalPlayer.DevTouchCameraMode)
  198.         userMode = CameraUtils.ConvertCameraModeEnumToStandard(UserGameSettings.TouchCameraMovementMode)
  199.     else
  200.         devMode = CameraUtils.ConvertCameraModeEnumToStandard(Players.LocalPlayer.DevComputerCameraMode)
  201.         userMode = CameraUtils.ConvertCameraModeEnumToStandard(UserGameSettings.ComputerCameraMovementMode)
  202.     end
  203.  
  204.     if devMode == Enum.DevComputerCameraMovementMode.UserChoice then
  205.         -- Developer is allowing user choice, so user setting is respected
  206.         return userMode
  207.     end
  208.  
  209.     return devMode
  210. end
  211.  
  212. function CameraModule:ActivateOcclusionModule( occlusionMode )
  213.     local newModuleCreator
  214.     if occlusionMode == Enum.DevCameraOcclusionMode.Zoom then
  215.         newModuleCreator = Poppercam
  216.     elseif occlusionMode == Enum.DevCameraOcclusionMode.Invisicam then
  217.         newModuleCreator = Invisicam
  218.     else
  219.         warn("CameraScript ActivateOcclusionModule called with unsupported mode")
  220.         return
  221.     end
  222.  
  223.     if FFlagUserCarCam then
  224.         self.occlusionMode = occlusionMode
  225.     end
  226.  
  227.     -- First check to see if there is actually a change. If the module being requested is already
  228.     -- the currently-active solution then just make sure it's enabled and exit early
  229.     if self.activeOcclusionModule and self.activeOcclusionModule:GetOcclusionMode() == occlusionMode then
  230.         if not self.activeOcclusionModule:GetEnabled() then
  231.             self.activeOcclusionModule:Enable(true)
  232.         end
  233.         return
  234.     end
  235.  
  236.     -- Save a reference to the current active module (may be nil) so that we can disable it if
  237.     -- we are successful in activating its replacement
  238.     local prevOcclusionModule = self.activeOcclusionModule
  239.  
  240.     -- If there is no active module, see if the one we need has already been instantiated
  241.     self.activeOcclusionModule = instantiatedOcclusionModules[newModuleCreator]
  242.  
  243.     -- If the module was not already instantiated and selected above, instantiate it
  244.     if not self.activeOcclusionModule then
  245.         self.activeOcclusionModule = newModuleCreator.new()
  246.         if self.activeOcclusionModule then
  247.             instantiatedOcclusionModules[newModuleCreator] = self.activeOcclusionModule
  248.         end
  249.     end
  250.  
  251.     -- If we were successful in either selecting or instantiating the module,
  252.     -- enable it if it's not already the currently-active enabled module
  253.     if self.activeOcclusionModule then
  254.         local newModuleOcclusionMode = self.activeOcclusionModule:GetOcclusionMode()
  255.         -- Sanity check that the module we selected or instantiated actually supports the desired occlusionMode
  256.         if newModuleOcclusionMode ~= occlusionMode then
  257.             warn("CameraScript ActivateOcclusionModule mismatch: ",self.activeOcclusionModule:GetOcclusionMode(),"~=",occlusionMode)
  258.         end
  259.  
  260.         -- Deactivate current module if there is one
  261.         if prevOcclusionModule then
  262.             -- Sanity check that current module is not being replaced by itself (that should have been handled above)
  263.             if prevOcclusionModule ~= self.activeOcclusionModule then
  264.                 prevOcclusionModule:Enable(false)
  265.             else
  266.                 warn("CameraScript ActivateOcclusionModule failure to detect already running correct module")
  267.             end
  268.         end
  269.  
  270.         -- Occlusion modules need to be initialized with information about characters and cameraSubject
  271.         -- Invisicam needs the LocalPlayer's character
  272.         -- Poppercam needs all player characters and the camera subject
  273.         if occlusionMode == Enum.DevCameraOcclusionMode.Invisicam then
  274.             -- Optimization to only send Invisicam what we know it needs
  275.             if Players.LocalPlayer.Character then
  276.                 self.activeOcclusionModule:CharacterAdded(Players.LocalPlayer.Character, Players.LocalPlayer )
  277.             end
  278.         else
  279.             -- When Poppercam is enabled, we send it all existing player characters for its raycast ignore list
  280.             for _, player in pairs(Players:GetPlayers()) do
  281.                 if player and player.Character then
  282.                     self.activeOcclusionModule:CharacterAdded(player.Character, player)
  283.                 end
  284.             end
  285.             self.activeOcclusionModule:OnCameraSubjectChanged(game.Workspace.CurrentCamera.CameraSubject)
  286.         end
  287.  
  288.         -- Activate new choice
  289.         self.activeOcclusionModule:Enable(true)
  290.     end
  291. end
  292.  
  293. function CameraModule:ShouldUseVehicleCamera()
  294.     assert(FFlagUserCarCam)
  295.    
  296.     local camera = workspace.CurrentCamera
  297.     if not camera then
  298.         return false
  299.     end
  300.    
  301.     local cameraType = camera.CameraType
  302.     local cameraSubject = camera.CameraSubject
  303.    
  304.     local isEligibleType = cameraType == Enum.CameraType.Custom or cameraType == Enum.CameraType.Follow
  305.     local isEligibleSubject = cameraSubject and cameraSubject:IsA("VehicleSeat") or false
  306.     local isEligibleOcclusionMode = self.occlusionMode ~= Enum.DevCameraOcclusionMode.Invisicam
  307.  
  308.     return isEligibleSubject and isEligibleType and isEligibleOcclusionMode
  309. end
  310.  
  311. -- When supplied, legacyCameraType is used and cameraMovementMode is ignored (should be nil anyways)
  312. -- Next, if userCameraCreator is passed in, that is used as the cameraCreator
  313. function CameraModule:ActivateCameraController(cameraMovementMode, legacyCameraType)
  314.     local newCameraCreator = nil
  315.  
  316.     if legacyCameraType~=nil then
  317.         --[[
  318.             This function has been passed a CameraType enum value. Some of these map to the use of
  319.             the LegacyCamera module, the value "Custom" will be translated to a movementMode enum
  320.             value based on Dev and User settings, and "Scriptable" will disable the camera controller.
  321.         --]]
  322.  
  323.         if legacyCameraType == Enum.CameraType.Scriptable then
  324.             if self.activeCameraController then
  325.                 self.activeCameraController:Enable(false)
  326.                 self.activeCameraController = nil
  327.                 return
  328.             end
  329.         elseif legacyCameraType == Enum.CameraType.Custom then
  330.             cameraMovementMode = self:GetCameraMovementModeFromSettings()
  331.  
  332.         elseif legacyCameraType == Enum.CameraType.Track then
  333.             -- Note: The TrackCamera module was basically an older, less fully-featured
  334.             -- version of ClassicCamera, no longer actively maintained, but it is re-implemented in
  335.             -- case a game was dependent on its lack of ClassicCamera's extra functionality.
  336.             cameraMovementMode = Enum.ComputerCameraMovementMode.Classic
  337.  
  338.         elseif legacyCameraType == Enum.CameraType.Follow then
  339.             cameraMovementMode = Enum.ComputerCameraMovementMode.Follow
  340.  
  341.         elseif legacyCameraType == Enum.CameraType.Orbital then
  342.             cameraMovementMode = Enum.ComputerCameraMovementMode.Orbital
  343.  
  344.         elseif legacyCameraType == Enum.CameraType.Attach or
  345.                legacyCameraType == Enum.CameraType.Watch or
  346.                legacyCameraType == Enum.CameraType.Fixed then
  347.             newCameraCreator = LegacyCamera
  348.         else
  349.             warn("CameraScript encountered an unhandled Camera.CameraType value: ",legacyCameraType)
  350.         end
  351.     end
  352.  
  353.     if not newCameraCreator then
  354.         if cameraMovementMode == Enum.ComputerCameraMovementMode.Classic or
  355.             cameraMovementMode == Enum.ComputerCameraMovementMode.Follow or
  356.             cameraMovementMode == Enum.ComputerCameraMovementMode.Default or
  357.             (FFlagUserCameraToggle and cameraMovementMode == Enum.ComputerCameraMovementMode.CameraToggle) then
  358.             newCameraCreator = ClassicCamera
  359.         elseif cameraMovementMode == Enum.ComputerCameraMovementMode.Orbital then
  360.             newCameraCreator = OrbitalCamera
  361.         else
  362.             warn("ActivateCameraController did not select a module.")
  363.             return
  364.         end
  365.     end
  366.  
  367.     local isVehicleCamera = FFlagUserCarCam and self:ShouldUseVehicleCamera()
  368.     if isVehicleCamera then
  369.         newCameraCreator = VehicleCamera
  370.     end
  371.  
  372.     -- Create the camera control module we need if it does not already exist in instantiatedCameraControllers
  373.     local newCameraController
  374.     if not instantiatedCameraControllers[newCameraCreator] then
  375.         newCameraController = newCameraCreator.new()
  376.         instantiatedCameraControllers[newCameraCreator] = newCameraController
  377.     else
  378.         newCameraController = instantiatedCameraControllers[newCameraCreator]
  379.         if FFlagUserCarCam and newCameraController.Reset then
  380.             newCameraController:Reset()
  381.         end
  382.     end
  383.    
  384.     if self.activeCameraController then
  385.         -- deactivate the old controller and activate the new one
  386.         if self.activeCameraController ~= newCameraController then
  387.             self.activeCameraController:Enable(false)
  388.             self.activeCameraController = newCameraController
  389.             self.activeCameraController:Enable(true)
  390.         elseif not self.activeCameraController:GetEnabled() then
  391.             self.activeCameraController:Enable(true)
  392.         end
  393.     elseif newCameraController ~= nil then
  394.         -- only activate the new controller
  395.         self.activeCameraController = newCameraController
  396.         self.activeCameraController:Enable(true)
  397.     end
  398.  
  399.     if self.activeCameraController then
  400.         if cameraMovementMode~=nil then
  401.             self.activeCameraController:SetCameraMovementMode(cameraMovementMode)
  402.         elseif legacyCameraType~=nil then
  403.             -- Note that this is only called when legacyCameraType is not a type that
  404.             -- was convertible to a ComputerCameraMovementMode value, i.e. really only applies to LegacyCamera
  405.             self.activeCameraController:SetCameraType(legacyCameraType)
  406.         end
  407.     end
  408. end
  409.  
  410. -- Note: The active transparency controller could be made to listen for this event itself.
  411. function CameraModule:OnCameraSubjectChanged()
  412.     local camera = workspace.CurrentCamera
  413.     local cameraSubject = camera and camera.CameraSubject
  414.  
  415.     if self.activeTransparencyController then
  416.         self.activeTransparencyController:SetSubject(cameraSubject)
  417.     end
  418.  
  419.     if self.activeOcclusionModule then
  420.         self.activeOcclusionModule:OnCameraSubjectChanged(cameraSubject)
  421.     end
  422.  
  423.     if FFlagUserCarCam then
  424.         self:ActivateCameraController(nil, camera.CameraType)
  425.     end
  426. end
  427.  
  428. function CameraModule:OnCameraTypeChanged(newCameraType)
  429.     if newCameraType == Enum.CameraType.Scriptable then
  430.         if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then
  431.             UserInputService.MouseBehavior = Enum.MouseBehavior.Default
  432.         end
  433.     end
  434.  
  435.     -- Forward the change to ActivateCameraController to handle
  436.     self:ActivateCameraController(nil, newCameraType)
  437. end
  438.  
  439. -- Note: Called whenever workspace.CurrentCamera changes, but also on initialization of this script
  440. function CameraModule:OnCurrentCameraChanged()
  441.     local currentCamera = game.Workspace.CurrentCamera
  442.     if not currentCamera then return end
  443.  
  444.     if self.cameraSubjectChangedConn then
  445.         self.cameraSubjectChangedConn:Disconnect()
  446.     end
  447.  
  448.     if self.cameraTypeChangedConn then
  449.         self.cameraTypeChangedConn:Disconnect()
  450.     end
  451.  
  452.     self.cameraSubjectChangedConn = currentCamera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
  453.         self:OnCameraSubjectChanged(currentCamera.CameraSubject)
  454.     end)
  455.  
  456.     self.cameraTypeChangedConn = currentCamera:GetPropertyChangedSignal("CameraType"):Connect(function()
  457.         self:OnCameraTypeChanged(currentCamera.CameraType)
  458.     end)
  459.  
  460.     self:OnCameraSubjectChanged(currentCamera.CameraSubject)
  461.     self:OnCameraTypeChanged(currentCamera.CameraType)
  462. end
  463.  
  464. function CameraModule:OnLocalPlayerCameraPropertyChanged(propertyName)
  465.     if propertyName == "CameraMode" then
  466.         -- CameraMode is only used to turn on/off forcing the player into first person view. The
  467.         -- Note: The case "Classic" is used for all other views and does not correspond only to the ClassicCamera module
  468.         if Players.LocalPlayer.CameraMode == Enum.CameraMode.LockFirstPerson then
  469.             -- Locked in first person, use ClassicCamera which supports this
  470.             if not self.activeCameraController or self.activeCameraController:GetModuleName() ~= "ClassicCamera" then
  471.                 self:ActivateCameraController(CameraUtils.ConvertCameraModeEnumToStandard(Enum.DevComputerCameraMovementMode.Classic))
  472.             end
  473.  
  474.             if self.activeCameraController then
  475.                 self.activeCameraController:UpdateForDistancePropertyChange()
  476.             end
  477.         elseif Players.LocalPlayer.CameraMode == Enum.CameraMode.Classic then
  478.             -- Not locked in first person view
  479.             local cameraMovementMode = self:GetCameraMovementModeFromSettings()
  480.             self:ActivateCameraController(CameraUtils.ConvertCameraModeEnumToStandard(cameraMovementMode))
  481.         else
  482.             warn("Unhandled value for property player.CameraMode: ",Players.LocalPlayer.CameraMode)
  483.         end
  484.  
  485.     elseif propertyName == "DevComputerCameraMode" or
  486.            propertyName == "DevTouchCameraMode" then
  487.         local cameraMovementMode = self:GetCameraMovementModeFromSettings()
  488.         self:ActivateCameraController(CameraUtils.ConvertCameraModeEnumToStandard(cameraMovementMode))
  489.  
  490.     elseif propertyName == "DevCameraOcclusionMode" then
  491.         self:ActivateOcclusionModule(Players.LocalPlayer.DevCameraOcclusionMode)
  492.  
  493.     elseif propertyName == "CameraMinZoomDistance" or propertyName == "CameraMaxZoomDistance" then
  494.         if self.activeCameraController then
  495.             self.activeCameraController:UpdateForDistancePropertyChange()
  496.         end
  497.     elseif propertyName == "DevTouchMovementMode" then
  498.     elseif propertyName == "DevComputerMovementMode" then
  499.     elseif propertyName == "DevEnableMouseLock" then
  500.         -- This is the enabling/disabling of "Shift Lock" mode, not LockFirstPerson (which is a CameraMode)
  501.         -- Note: Enabling and disabling of MouseLock mode is normally only a publish-time choice made via
  502.         -- the corresponding EnableMouseLockOption checkbox of StarterPlayer, and this script does not have
  503.         -- support for changing the availability of MouseLock at runtime (this would require listening to
  504.         -- Player.DevEnableMouseLock changes)
  505.     end
  506. end
  507.  
  508. function CameraModule:OnUserGameSettingsPropertyChanged(propertyName)
  509.     if propertyName == "ComputerCameraMovementMode" then
  510.         local cameraMovementMode = self:GetCameraMovementModeFromSettings()
  511.         self:ActivateCameraController(CameraUtils.ConvertCameraModeEnumToStandard(cameraMovementMode))
  512.     end
  513. end
  514.  
  515. --[[
  516.     Main RenderStep Update. The camera controller and occlusion module both have opportunities
  517.     to set and modify (respectively) the CFrame and Focus before it is set once on CurrentCamera.
  518.     The camera and occlusion modules should only return CFrames, not set the CFrame property of
  519.     CurrentCamera directly.
  520. --]]
  521. function CameraModule:Update(dt)
  522.     if self.activeCameraController then
  523.         local localPlayer = game.Players.LocalPlayer;
  524.         local character = localPlayer.Character;
  525.         if character:FindFirstChildOfClass("Tool") then
  526.             self.activeCameraController:SetIsMouseLocked(true)
  527.             character.Humanoid.CameraOffset = Vector3.new(2.1, 0, 0)
  528.             -- Write the next lines only if you'd want the mouse to be disabled while the tool is equipped
  529.             game:GetService("UserInputService").MouseIconEnabled = false
  530.         else
  531.             self.activeCameraController:SetIsMouseLocked(false)
  532.             character.Humanoid.CameraOffset = Vector3.new(0, 0, 0)
  533.             game:GetService("UserInputService").MouseIconEnabled = true
  534.         end
  535.        
  536.         if FFlagUserCameraToggle then
  537.             self.activeCameraController:UpdateMouseBehavior()
  538.         end
  539.  
  540.         local newCameraCFrame, newCameraFocus = self.activeCameraController:Update(dt)
  541.         self.activeCameraController:ApplyVRTransform()
  542.         if self.activeOcclusionModule then
  543.             newCameraCFrame, newCameraFocus = self.activeOcclusionModule:Update(dt, newCameraCFrame, newCameraFocus)
  544.         end
  545.  
  546.         -- Here is where the new CFrame and Focus are set for this render frame
  547.         game.Workspace.CurrentCamera.CFrame = newCameraCFrame
  548.         game.Workspace.CurrentCamera.Focus = newCameraFocus
  549.  
  550.         -- Update to character local transparency as needed based on camera-to-subject distance
  551.         if self.activeTransparencyController then
  552.             self.activeTransparencyController:Update()
  553.         end
  554.  
  555.         if FFlagUserCameraInputRefactor and CameraInput.getInputEnabled() then
  556.             CameraInput.resetInputForFrameEnd()
  557.         end
  558.     end
  559. end
  560.  
  561. -- Formerly getCurrentCameraMode, this function resolves developer and user camera control settings to
  562. -- decide which camera control module should be instantiated. The old method of converting redundant enum types
  563. function CameraModule:GetCameraControlChoice()
  564.     local player = Players.LocalPlayer
  565.  
  566.     if player then
  567.         if self.lastInputType == Enum.UserInputType.Touch or UserInputService.TouchEnabled then
  568.             -- Touch
  569.             if player.DevTouchCameraMode == Enum.DevTouchCameraMovementMode.UserChoice then
  570.                 return CameraUtils.ConvertCameraModeEnumToStandard( UserGameSettings.TouchCameraMovementMode )
  571.             else
  572.                 return CameraUtils.ConvertCameraModeEnumToStandard( player.DevTouchCameraMode )
  573.             end
  574.         else
  575.             -- Computer
  576.             if player.DevComputerCameraMode == Enum.DevComputerCameraMovementMode.UserChoice then
  577.                 local computerMovementMode = CameraUtils.ConvertCameraModeEnumToStandard(UserGameSettings.ComputerCameraMovementMode)
  578.                 return CameraUtils.ConvertCameraModeEnumToStandard(computerMovementMode)
  579.             else
  580.                 return CameraUtils.ConvertCameraModeEnumToStandard(player.DevComputerCameraMode)
  581.             end
  582.         end
  583.     end
  584. end
  585.  
  586. function CameraModule:OnCharacterAdded(char, player)
  587.     if self.activeOcclusionModule then
  588.         self.activeOcclusionModule:CharacterAdded(char, player)
  589.     end
  590. end
  591.  
  592. function CameraModule:OnCharacterRemoving(char, player)
  593.     if self.activeOcclusionModule then
  594.         self.activeOcclusionModule:CharacterRemoving(char, player)
  595.     end
  596. end
  597.  
  598. function CameraModule:OnPlayerAdded(player)
  599.     player.CharacterAdded:Connect(function(char)
  600.         self:OnCharacterAdded(char, player)
  601.     end)
  602.     player.CharacterRemoving:Connect(function(char)
  603.         self:OnCharacterRemoving(char, player)
  604.     end)
  605. end
  606.  
  607. function CameraModule:OnMouseLockToggled()
  608.     if self.activeMouseLockController then
  609.         local mouseLocked = self.activeMouseLockController:GetIsMouseLocked()
  610.         local mouseLockOffset = self.activeMouseLockController:GetMouseLockOffset()
  611.         if self.activeCameraController then
  612.             self.activeCameraController:SetIsMouseLocked(mouseLocked)
  613.             self.activeCameraController:SetMouseLockOffset(mouseLockOffset)
  614.         end
  615.     end
  616. end
  617.  
  618. local cameraModuleObject = CameraModule.new()
  619. local cameraApi = {}
  620.  
  621. if FFlagUserRemoveTheCameraApi then
  622.     return cameraApi
  623. else
  624.     return cameraModuleObject
  625. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement