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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
// Cristian Pop - https://boxophobic.com/
using UnityEngine;
using Boxophobic;
using Boxophobic.StyledGUI;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace AtmosphericHeightFog
{
[ExecuteInEditMode]
[DisallowMultipleComponent]
[HelpURL("https://docs.google.com/document/d/1pIzIHIZ-cSh2ykODSZCbAPtScJ4Jpuu7lS3rNEHCLbc/edit#heading=h.pzat2b29j9a0")]
public class HeightFogPerObject : StyledMonoBehaviour
{
[StyledBanner(0.474f, 0.709f, 0.901f, "Height Fog Per Object", "", "https://docs.google.com/document/d/1pIzIHIZ-cSh2ykODSZCbAPtScJ4Jpuu7lS3rNEHCLbc/edit#heading=h.pzat2b29j9a0")]
public bool styledBanner;
[StyledMessage("Info", "The Object does not have a Mesh Renderer!", 5, 5)]
public bool messageNoRenderer = false;
[StyledMessage("Info", "Objects using multiple materials are not supported!", 5, 5)]
public bool messageMultiMaterials = false;
[StyledMessage("Info", "The Object does not have a Material assigned!", 5, 5)]
public bool messageNoMaterial = false;
[StyledMessage("Info", "Please note that the Height Fog Per Object option will not work for all transparent objects. Available in Play mode only. Please read the documentation for more!", 0, 0)]
public bool messageTransparencySupport = true;
[StyledCategory("Settings")]
public bool categoryMaterial;
public Material customFogMaterial = null;
[StyledMessage("Info", "The is not a valid Height Fog material! Please assign the correct shader first!", 5, 0)]
public bool messageInvalidFogMaterial = false;
[StyledSpace(5)]
public bool styledSpace0;
int transparencyRenderQueue = 3002;
Material originalMaterial;
Material instanceMaterial;
Material transparencyMaterial;
GameObject transparencyGO;
void Awake()
{
if (GameObjectIsInvalid())
{
return;
}
#if UNITY_EDITOR
if (Application.isPlaying == false)
{
GameObjectDisableBathingFlag();
return;
}
#endif
transparencyGO = new GameObject(gameObject.name + " (Height Fog Object)");
transparencyGO.transform.parent = gameObject.transform;
transparencyGO.transform.localPosition = Vector3.zero;
transparencyGO.transform.localRotation = Quaternion.identity;
transparencyGO.transform.localScale = Vector3.one;
transparencyGO.AddComponent<MeshFilter>();
transparencyGO.AddComponent<MeshRenderer>();
transparencyGO.GetComponent<MeshFilter>().sharedMesh = gameObject.GetComponent<MeshFilter>().sharedMesh;
Material originalMaterial = gameObject.GetComponent<MeshRenderer>().sharedMaterial;
instanceMaterial = new Material(originalMaterial);
instanceMaterial.name = originalMaterial.name + " (Instance)";
//instanceMaterial.SetOverrideTag("DisableBatching", "True");
if (customFogMaterial == null)
{
transparencyMaterial = new Material(instanceMaterial);
transparencyMaterial.shader = Shader.Find("BOXOPHOBIC/Atmospherics/Height Fog Per Object");
transparencyMaterial.name = originalMaterial.name + " (Generic Fog)";
}
else if (customFogMaterial != null)
{
if (customFogMaterial.HasProperty("_IsHeightFogShader"))
{
transparencyMaterial = customFogMaterial;
transparencyMaterial.name = originalMaterial.name + " (Custom Fog)";
}
else
{
transparencyMaterial = new Material(instanceMaterial);
transparencyMaterial.shader = Shader.Find("BOXOPHOBIC/Atmospherics/Height Fog Per Object");
transparencyMaterial.name = originalMaterial.name + " (Generic Fog)";
}
}
if (transparencyMaterial.HasProperty("_IsStandardPipeline"))
{
transparencyRenderQueue = 3002;
}
else
{
transparencyRenderQueue = 3102;
}
instanceMaterial.renderQueue = transparencyRenderQueue;
transparencyMaterial.renderQueue = transparencyRenderQueue + 1;
gameObject.GetComponent<MeshRenderer>().material = instanceMaterial;
transparencyGO.GetComponent<MeshRenderer>().material = transparencyMaterial;
}
#if UNITY_EDITOR
void Update()
{
if (Application.isPlaying == true)
{
return;
}
if (gameObject.isStatic)
{
GameObjectDisableBathingFlag();
}
if (customFogMaterial == null)
{
messageInvalidFogMaterial = false;
}
else if (customFogMaterial != null)
{
if (customFogMaterial.HasProperty("_IsHeightFogShader") == false)
{
messageInvalidFogMaterial = true;
}
else
{
messageInvalidFogMaterial = false;
}
}
}
#endif
bool GameObjectIsInvalid()
{
bool invalid = false;
if (gameObject.GetComponent<MeshRenderer>() == null)
{
messageNoRenderer = true;
invalid = true;
}
else if (gameObject.GetComponent<MeshRenderer>().sharedMaterials.Length > 1)
{
messageMultiMaterials = true;
invalid = true;
}
else if (gameObject.GetComponent<MeshRenderer>().sharedMaterial == null)
{
messageNoMaterial = true;
invalid = true;
}
return invalid;
}
#if UNITY_EDITOR
void GameObjectDisableBathingFlag()
{
StaticEditorFlags flags = GameObjectUtility.GetStaticEditorFlags(gameObject);
flags = flags & ~(StaticEditorFlags.BatchingStatic);
GameObjectUtility.SetStaticEditorFlags(gameObject, flags);
}
#endif
}
}
|