Skip to content

Usability improvements #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
**/.DS_Store/
**/.vs/
**/.idea/
**/obj/
**/bin/
**/Jetbrains*

**/Library/
**/Temp/
**/Build/
**/Logs/

*.sln
*.user
*.csproj
*.userprefs
52 changes: 0 additions & 52 deletions DrawCallState/DrawCallState.shader

This file was deleted.

Binary file removed OverdrawMonitor/.vs/OverdrawMonitor/v15/.suo
Binary file not shown.
Binary file added OverdrawMonitor/Assets/Demo.unity
Binary file not shown.
7 changes: 7 additions & 0 deletions OverdrawMonitor/Assets/Demo.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "OverdrawMonitor.Editor",
"references": [
"GUID:7594097e076d14122ac321968bbd88c0"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": []
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

182 changes: 182 additions & 0 deletions OverdrawMonitor/Assets/OverdrawMonitor/Editor/OverdrawMonitorWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class OverdrawMonitorWindow : EditorWindow
{
bool isEnabled => _monitorsGo != null && Application.isPlaying;
GameObject _monitorsGo;
Dictionary<CameraOverdrawMonitor, CameraOverdrawStats> _stats;

[MenuItem("Tools/Overdraw Monitor")]
static void ShowWindow()
{
GetWindow<OverdrawMonitorWindow>().Show();
}

void Init()
{
if (_monitorsGo != null)
throw new Exception("Attempt to start overdraw monitor twice");

_monitorsGo = new GameObject("OverdrawMonitor");
_monitorsGo.hideFlags = HideFlags.HideAndDontSave;
_stats = new Dictionary<CameraOverdrawMonitor, CameraOverdrawStats>();
}

void TryShutdown()
{
if (_monitorsGo == null)
return;

DestroyImmediate(_monitorsGo);
_stats = null;
}

void Update()
{
// Check shutdown if needed
if (!isEnabled)
{
TryShutdown();
return;
}

Camera[] activeCameras = Camera.allCameras;

// Remove monitors for non-active cameras
var monitors = GetAllMonitors();
foreach (var monitor in monitors)
if (!Array.Exists(activeCameras, c => monitor.targetCamera == c))
DestroyImmediate(monitor);

// Add new monitors
monitors = GetAllMonitors();
foreach (Camera activeCamera in activeCameras)
{
if (!Array.Exists(monitors,m => m.targetCamera == activeCamera))
{
var monitor = _monitorsGo.AddComponent<CameraOverdrawMonitor>();
monitor.SetTargetCamera(activeCamera);
}
}
}

CameraOverdrawMonitor[] GetAllMonitors()
{
return _monitorsGo.GetComponentsInChildren<CameraOverdrawMonitor>(true);
}

void OnGUI()
{
if (Application.isPlaying)
{
int startButtonHeight = 25;
if (GUILayout.Button(isEnabled ? "Stop" : "Start", GUILayout.MaxWidth(100), GUILayout.MaxHeight(startButtonHeight)))
{
if (!isEnabled)
Init();
else
TryShutdown();
}

if (!isEnabled)
return;

CameraOverdrawMonitor[] monitors = GetAllMonitors();

GUILayout.Space(-startButtonHeight);
using (new GUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();

if (GUILayout.Button("Reset Stats", GUILayout.Width(100), GUILayout.Height(20)))
ResetStats();
}

GUILayout.Space(5);

Vector2Int gameViewResolution = GetGameViewResolution();
GUILayout.Label($"Screen {gameViewResolution.x}x{gameViewResolution.y}");

GUILayout.Space(5);

foreach (CameraOverdrawMonitor monitor in _stats.Keys.ToArray())
if (!Array.Exists(monitors, m => monitor))
_stats.Remove(monitor);

long gameViewArea = gameViewResolution.x * gameViewResolution.y;
float totalGlobalOverdrawRatio = 0f;
foreach (CameraOverdrawMonitor monitor in monitors)
{
using (new GUILayout.HorizontalScope())
{
Camera cam = monitor.targetCamera;
GUILayout.Label($"{cam.name} {cam.pixelWidth}x{cam.pixelHeight}");

GUILayout.FlexibleSpace();

float localOverdrawRatio = monitor.overdrawRatio;
float globalOverdrawRatio = monitor.fragmentsCount / (float)gameViewArea;
totalGlobalOverdrawRatio += globalOverdrawRatio;

if (!_stats.TryGetValue(monitor, out CameraOverdrawStats monitorStats))
{
monitorStats = new CameraOverdrawStats();
_stats.Add(monitor, monitorStats);
}
monitorStats.maxLocalOverdrawRatio = Math.Max(localOverdrawRatio, monitorStats.maxLocalOverdrawRatio);
monitorStats.maxGlobalOverdrawRatio = Math.Max(globalOverdrawRatio, monitorStats.maxGlobalOverdrawRatio);

GUILayout.Label(FormatResult("Local: {0} / {1} \t Global: {2} / {3}",
localOverdrawRatio, monitorStats.maxLocalOverdrawRatio, globalOverdrawRatio, monitorStats.maxGlobalOverdrawRatio));
}
}

GUILayout.Space(5);

float maxTotalGlobalOverdrawRatio = 0f;
foreach (CameraOverdrawStats stat in _stats.Values)
maxTotalGlobalOverdrawRatio += stat.maxGlobalOverdrawRatio;
using (new GUILayout.HorizontalScope())
{
GUILayout.Label("TOTAL");
GUILayout.FlexibleSpace();
GUILayout.Label(FormatResult("Global: {0} / {1}", totalGlobalOverdrawRatio, maxTotalGlobalOverdrawRatio));
}
}
else
{
GUILayout.Label("Available only in Play mode");
}

Repaint();
}

void ResetStats()
{
_stats.Clear();
}

string FormatResult(string format, params float[] args)
{
var stringArgs = new List<string>();
foreach (float arg in args)
stringArgs.Add($"{arg:N3}");
return string.Format(format, stringArgs.ToArray());
}

static Vector2Int GetGameViewResolution()
{
var resString = UnityStats.screenRes.Split('x');
return new Vector2Int(int.Parse(resString[0]), int.Parse(resString[1]));
}
}

class CameraOverdrawStats
{
public float maxLocalOverdrawRatio;
public float maxGlobalOverdrawRatio;
}

This file was deleted.

This file was deleted.

Loading