From ece06be320f92a2b41a4cd9705968d0161c8d311 Mon Sep 17 00:00:00 2001 From: Tore Bergebakken Date: Mon, 23 Dec 2024 17:48:28 +0100 Subject: [PATCH] Avoid rendering the entire scene twice each frame The overview camera has been enabled in the background and caused double rendering load *this entire time*. These changes should fix the problem, but some hiccups will still happen when playing in splitscreen and switching to the overview camera after one player is dead. --- Assets/Scripts/Control&Input/OrbitCamera.cs | 11 ++++++++++- Assets/Scripts/Gamestate/MatchController.cs | 13 ++++++++++++- Assets/Scripts/Gamestate/PlayerManager.cs | 4 ++++ Assets/Scripts/Scoreboard/ScoreboardManager.cs | 5 +++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Control&Input/OrbitCamera.cs b/Assets/Scripts/Control&Input/OrbitCamera.cs index 0a50f1696..f37505a38 100644 --- a/Assets/Scripts/Control&Input/OrbitCamera.cs +++ b/Assets/Scripts/Control&Input/OrbitCamera.cs @@ -95,8 +95,16 @@ private void StartTracking(PlayerManager nextTarget) { if (!cameraTransform || !input || MatchController.Singleton.IsShowingScoreboards) return; - isTracking = true; + camera.enabled = true; + // Improves perf loss when playing splitscreen + var allPlayerCamerasAreDisabled = MatchController.Singleton.Players + .Where(p => p.inputManager != null) + .All(p => p.inputManager.PlayerCamera.enabled); + if (allPlayerCamerasAreDisabled) + MatchController.Singleton.ArenaCamera.enabled = false; + + isTracking = true; target = nextTarget.AiAimSpot; if (nextTarget != player) player.HUDController.DisplaySpectatorScreen(nextTarget.identity); @@ -106,6 +114,7 @@ private void StopTracking() { if (!camera) return; + MatchController.Singleton.ArenaCamera.enabled = true; isTracking = false; camera.enabled = false; ResetCamera(); diff --git a/Assets/Scripts/Gamestate/MatchController.cs b/Assets/Scripts/Gamestate/MatchController.cs index 2e53cef61..c2871ef00 100644 --- a/Assets/Scripts/Gamestate/MatchController.cs +++ b/Assets/Scripts/Gamestate/MatchController.cs @@ -72,6 +72,9 @@ public class MatchController : NetworkBehaviour private bool isShowingScoreboards = false; public bool IsShowingScoreboards => isShowingScoreboards; + private Camera arenaCamera; + public Camera ArenaCamera => arenaCamera; + private void Awake() { @@ -105,6 +108,8 @@ private void Start() currentMapName ??= SceneManager.GetActiveScene().name; + arenaCamera = FindObjectsOfType().FirstOrDefault(x => x.CompareTag("MainCamera")); + var mainLight = GameObject.FindGameObjectsWithTag("MainLight")[0]; RenderSettings.skybox.SetVector("_SunDirection", mainLight.transform.forward); RenderSettings.skybox.SetFloat("_MaxGradientTreshold", 0.25f); @@ -231,6 +236,12 @@ private IEnumerator WaitAndShowResults() { // Delay first so we can see who killed who yield return new WaitForSeconds(delayBeforeRoundResults); + + // Ensure arena camera is the only one that is enabled now! + foreach (var p in players.Where(p => p.inputManager != null)) + p.inputManager.PlayerCamera.enabled = false; + arenaCamera.enabled = true; + isShowingScoreboards = true; // Scoreboard subscribes here onRoundEnd?.Invoke(); @@ -313,7 +324,7 @@ private void DisplayWinScreen() foreach (var loser in loserModels.Zip(loserColors, (model, color) => (model, color))) loser.model.GetComponentInChildren().material.color = loser.color; - Camera.main.GetComponent().PlayVictoryAnimation(winner); + arenaCamera.GetComponent().PlayVictoryAnimation(winner); } public void WaitAndRestartAfterWinScreen() diff --git a/Assets/Scripts/Gamestate/PlayerManager.cs b/Assets/Scripts/Gamestate/PlayerManager.cs index ec567fe31..035bffb19 100644 --- a/Assets/Scripts/Gamestate/PlayerManager.cs +++ b/Assets/Scripts/Gamestate/PlayerManager.cs @@ -258,6 +258,10 @@ public void SetPlayerInput(InputManager playerInput) canvas.worldCamera = inputManager.GetComponentInChildren(); canvas.planeDistance = 0.21f; identity.onChipChange += hudController.OnChipChange; + + // Disable arena camera so we don't render the entire scene twice + if (Camera.main) + Camera.main.enabled = false; } if (playerIK.TryGetComponent(out var biddingPlayer)) diff --git a/Assets/Scripts/Scoreboard/ScoreboardManager.cs b/Assets/Scripts/Scoreboard/ScoreboardManager.cs index 88d29e492..7c64bb0b0 100644 --- a/Assets/Scripts/Scoreboard/ScoreboardManager.cs +++ b/Assets/Scripts/Scoreboard/ScoreboardManager.cs @@ -103,7 +103,8 @@ public void SetupPosters() public IEnumerator ShowMatchResults() { // Animate the after battle scene - Camera.main.GetComponent().PlayScoreboardAnimation(); + var arenaCamera = MatchController.Singleton.ArenaCamera; + arenaCamera.GetComponent().PlayScoreboardAnimation(); for (int i = 0; i < scoreboards.Count; i++) { @@ -113,7 +114,7 @@ public IEnumerator ShowMatchResults() } // Do not start adding crimes before the camera has finished the animation - int delay = Mathf.RoundToInt(Camera.main.GetComponent().runtimeAnimatorController.animationClips[0].length); + int delay = Mathf.RoundToInt(arenaCamera.GetComponent().runtimeAnimatorController.animationClips[0].length); yield return new WaitForSeconds(delay); maxSteps = MaxNumberOfCrimes();