Skip to content

Instantly share code, notes, and snippets.

@nomnomab
Last active May 27, 2024 23:53
Show Gist options
  • Save nomnomab/b68f82a075f008cf1fb791a83837b602 to your computer and use it in GitHub Desktop.
Save nomnomab/b68f82a075f008cf1fb791a83837b602 to your computer and use it in GitHub Desktop.
Lets you view a scene with even lighting.
using UnityEditor;
using UnityEngine;
namespace Nomnom.Editor {
public sealed class FullBrightSceneCamera {
private const string KEY = "key_FullBrightSceneCamera";
private static Transform? _rig = null;
[InitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void OnLoad() {
HandleState();
SceneView.onSceneGUIDelegate -= OnSceneGUI;
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
// togglable editor menu item
[MenuItem("Tools/Nomnom/Toggle Full Bright Scene Camera")]
private static void Toggle() {
var value = !EditorPrefs.GetBool(KEY);
if (value) {
Debug.Log("Enabled Full Bright Scene Camera");
} else {
Debug.Log("Disabled Full Bright Scene Camera");
}
EditorPrefs.SetBool(KEY, value);
HandleState();
SceneView.onSceneGUIDelegate -= OnSceneGUI;
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
private static void OnSceneGUI(SceneView sceneview) {
HandleState();
}
private static bool GetRigState() {
var enabled = EditorPrefs.GetBool(KEY) && !EditorApplication.isPlaying;
var sceneView = GetSceneView();
if (sceneView is not null && !sceneView.sceneLighting) {
enabled = false;
}
return enabled;
}
private static void HandleState() {
var found = GameObject.FindWithTag("EDITOR_RIG");
if (!found && GetRigState()) {
CreateRig();
}
if (!_rig && found) {
_rig = found.transform;
}
HandleRigState();
}
private static void HandleRigState() {
if (!_rig) return;
if (!GetRigState()) {
GameObject.DestroyImmediate(_rig!.gameObject);
Debug.Log("Destroyed UnlitRig");
}
}
private static SceneView? GetSceneView() {
var sceneViews = SceneView.sceneViews;
var lastActiveSceneView = SceneView.lastActiveSceneView;
if (lastActiveSceneView is null) {
if (sceneViews.Count == 0) return null;
lastActiveSceneView = sceneViews[0] as SceneView;
}
if (lastActiveSceneView is null) return null;
return lastActiveSceneView;
}
private static void CreateRig() {
CreateRig(GetSceneView()?.camera!);
}
private static void CreateRig(Camera camera) {
if (!camera) return;
var rig = new GameObject("UnlitRig");
rig.tag = "EDITOR_RIG";
rig.transform.SetParent(camera.transform.parent);
rig.hideFlags = HideFlags.HideInHierarchy | HideFlags.DontSave;
var directions = new[] {
new Vector3(0, 0, 1),
new Vector3(0, 0, -1),
new Vector3(1, 0, 0),
new Vector3(-1, 0, 0),
new Vector3(0, 1, 0),
new Vector3(0, -1, 0),
};
for (int i = 0; i < 6; i++) {
var light = new GameObject($"Light{i}");
light.transform.SetParent(rig.transform);
light.transform.rotation = Quaternion.LookRotation(directions[i]);
var lightComponent = light.AddComponent<Light>();
lightComponent.type = LightType.Directional;
lightComponent.intensity = 0.3f;
}
_rig = rig.transform;
Debug.Log("Created UnlitRig");
HandleRigState();
}
}
}
@nomnomab
Copy link
Author

Will disable automatically when entering play mode, and when scene lighting is turned off.

Toggle the effect via Tools > Nomnom > Toggle Full Bright Scene Camera

Unity_lMcy9G3FOJ.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment