Skip to content

Commit

Permalink
Avoid rendering the entire scene twice each frame
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
toberge committed Dec 23, 2024
1 parent d23c53c commit ece06be
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
11 changes: 10 additions & 1 deletion Assets/Scripts/Control&Input/OrbitCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -106,6 +114,7 @@ private void StopTracking()
{
if (!camera)
return;
MatchController.Singleton.ArenaCamera.enabled = true;
isTracking = false;
camera.enabled = false;
ResetCamera();
Expand Down
13 changes: 12 additions & 1 deletion Assets/Scripts/Gamestate/MatchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -105,6 +108,8 @@ private void Start()

currentMapName ??= SceneManager.GetActiveScene().name;

arenaCamera = FindObjectsOfType<Camera>().FirstOrDefault(x => x.CompareTag("MainCamera"));

var mainLight = GameObject.FindGameObjectsWithTag("MainLight")[0];
RenderSettings.skybox.SetVector("_SunDirection", mainLight.transform.forward);
RenderSettings.skybox.SetFloat("_MaxGradientTreshold", 0.25f);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -313,7 +324,7 @@ private void DisplayWinScreen()
foreach (var loser in loserModels.Zip(loserColors, (model, color) => (model, color)))
loser.model.GetComponentInChildren<SkinnedMeshRenderer>().material.color = loser.color;

Camera.main.GetComponent<ArenaCamera>().PlayVictoryAnimation(winner);
arenaCamera.GetComponent<ArenaCamera>().PlayVictoryAnimation(winner);
}

public void WaitAndRestartAfterWinScreen()
Expand Down
4 changes: 4 additions & 0 deletions Assets/Scripts/Gamestate/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ public void SetPlayerInput(InputManager playerInput)
canvas.worldCamera = inputManager.GetComponentInChildren<Camera>();
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<BiddingPlayer>(out var biddingPlayer))
Expand Down
5 changes: 3 additions & 2 deletions Assets/Scripts/Scoreboard/ScoreboardManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public void SetupPosters()
public IEnumerator ShowMatchResults()
{
// Animate the after battle scene
Camera.main.GetComponent<ArenaCamera>().PlayScoreboardAnimation();
var arenaCamera = MatchController.Singleton.ArenaCamera;
arenaCamera.GetComponent<ArenaCamera>().PlayScoreboardAnimation();

for (int i = 0; i < scoreboards.Count; i++)
{
Expand All @@ -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<Animator>().runtimeAnimatorController.animationClips[0].length);
int delay = Mathf.RoundToInt(arenaCamera.GetComponent<Animator>().runtimeAnimatorController.animationClips[0].length);
yield return new WaitForSeconds(delay);

maxSteps = MaxNumberOfCrimes();
Expand Down

0 comments on commit ece06be

Please sign in to comment.