Repository navigation
Expand file tree
/
Copy pathVisualEnvironmentController.cs
More file actions
131 lines (118 loc) · 6.11 KB
/
Copy pathVisualEnvironmentController.cs
File metadata and controls
131 lines (118 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using BeautifyEffect;
using UnityEngine;
namespace CrazePath
{
/// <summary>Owns shared rendering and coordinates prepared, active and released local presentation.</summary>
[DefaultExecutionOrder(-80)]
[RequireComponent(typeof(CameraController), typeof(EnvironmentReflections), typeof(PlanarReflections))]
public sealed class VisualEnvironmentController : Singleton<VisualEnvironmentController>
{
public static event Action OnReleasing;
public static VisualEnvironmentContext Current { get; private set; }
[Header("Windows rendering limits")]
[SerializeField] [Min(1)] [Tooltip("Shared limit across window, lamp, accent and emissive lights.")] private int maximumLocalLights = 128;
[SerializeField] [Min(0)] private int maximumShadowLights = 64;
private CameraController cameraController;
private EnvironmentReflections environmentReflections;
private PlanarReflections planarReflections;
private ContactSoftShadows directionalShadows;
private WorldImageEffects worldEffects;
private Beautify beautify;
private Color previousBackground;
private CameraClearFlags previousClearFlags;
private float previousFarClip;
private DepthTextureMode previousDepthMode;
private bool previousHdr;
private bool previousMsaa;
protected override void Awake()
{
cameraController = GetComponent<CameraController>();
environmentReflections = GetComponent<EnvironmentReflections>();
planarReflections = GetComponent<PlanarReflections>();
directionalShadows = GetComponent<ContactSoftShadows>();
worldEffects = cameraController.Camera.GetComponent<WorldImageEffects>();
beautify = cameraController.Camera.GetComponent<Beautify>();
previousBackground = cameraController.Camera.backgroundColor;
previousClearFlags = cameraController.Camera.clearFlags;
previousFarClip = cameraController.Camera.farClipPlane;
previousDepthMode = cameraController.Camera.depthTextureMode;
previousHdr = cameraController.Camera.allowHDR;
previousMsaa = cameraController.Camera.allowMSAA;
base.Awake();
Release();
}
private void OnDisable()
{
Release();
}
public void Prepare(VisualEnvironmentProfile profile, Transform target, Transform lightAnchor, PathFollower pathFollower = null, Transform reflectionExclusion = null)
{
if (profile == null || profile.beautify == null)
throw new InvalidOperationException("The environment must have a complete visual profile and a Beautify profile.");
Release();
Current = new VisualEnvironmentContext(cameraController.Camera, target, reflectionExclusion, profile, maximumLocalLights, maximumShadowLights);
LightController.Instance.ApplySettings(profile.lighting);
if (pathFollower != null)
LightController.Instance.Follow(pathFollower);
else
LightController.Instance.Follow(lightAnchor);
cameraController.Camera.allowHDR = true;
cameraController.Camera.allowMSAA = false;
cameraController.Camera.clearFlags = CameraClearFlags.SolidColor;
cameraController.Camera.backgroundColor = profile.cameraBackground;
cameraController.Camera.farClipPlane = profile.cameraFarClip;
cameraController.Camera.depthTextureMode = DepthTextureMode.Depth;
worldEffects.Configure(profile.worldEffects);
beautify.profile = profile.beautify;
beautify.enabled = true;
environmentReflections.Configure(profile.environmentReflections);
planarReflections.Configure(profile.planarReflections);
directionalShadows.Configure(profile.directionalShadows);
foreach (VisualEnvironmentModule module in VisualEnvironmentModule.Modules.ToArray())
if (module != null)
module.Prepare(Current);
}
public void Activate()
{
if (Current == null)
throw new InvalidOperationException("Prepare the visual environment before activation.");
Current.IsActive = true;
}
public void Release()
{
if (Current != null)
{
Current.IsActive = false;
Current.IsReleased = true;
OnReleasing?.Invoke();
foreach (VisualEnvironmentModule module in VisualEnvironmentModule.Modules.ToArray())
if (module != null)
module.Release();
Current = null;
}
if (environmentReflections != null) environmentReflections.enabled = false;
if (planarReflections != null) planarReflections.enabled = false;
if (directionalShadows != null) directionalShadows.enabled = false;
if (worldEffects != null) worldEffects.Clear();
if (beautify != null) beautify.enabled = false;
if (cameraController != null)
{
cameraController.Camera.backgroundColor = previousBackground;
cameraController.Camera.clearFlags = previousClearFlags;
cameraController.Camera.farClipPlane = previousFarClip;
cameraController.Camera.depthTextureMode = previousDepthMode;
cameraController.Camera.allowHDR = previousHdr;
cameraController.Camera.allowMSAA = previousMsaa;
}
if (LightController.InstanceExists) LightController.Instance.Clear();
Shader.SetGlobalFloat("_CrazePathReflectionAttenuation", 0f);
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetEnvironment()
{
Current = null;
OnReleasing = null;
}
}
}