-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathLightmapping.cs
More file actions
262 lines (216 loc) · 10.7 KB
/
Copy pathLightmapping.cs
File metadata and controls
262 lines (216 loc) · 10.7 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using UnityEditor;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using UnityEngine.ProBuilder;
using UL = UnityEditor.Lightmapping;
using UnityEditor.SettingsManagement;
namespace UnityEditor.ProBuilder
{
/// <summary>
/// Methods used in manipulating or creating Lightmaps.
/// </summary>
[InitializeOnLoad]
static class Lightmapping
{
const StaticEditorFlags k_ContributeGI = StaticEditorFlags.ContributeGI;
const string k_StaticEditorFlagsProperty = "m_StaticEditorFlags";
[UserSetting("General", "Auto Lightmap UVs", "Automatically build the lightmap UV array when editing ProBuilder meshes. If this feature is disabled, you will need to use the 'Generate UV2' action to build lightmap UVs for meshes prior to baking lightmaps.")]
static Pref<bool> s_AutoUnwrapLightmapUV = new Pref<bool>("lightmapping.autoUnwrapLightmapUV", true);
[UserSetting("General", "Show Missing Lightmap UVs Warning", "Enable or disable a warning log if lightmaps are baked while ProBuilder shapes are missing a valid UV2 channel.")]
static Pref<bool> s_ShowMissingLightmapUVWarning = new Pref<bool>("lightmapping.showMissingLightmapWarning", true, SettingsScope.User);
[UserSetting]
internal static Pref<UnwrapParameters> s_UnwrapParameters = new Pref<UnwrapParameters>("lightmapping.defaultLightmapUnwrapParameters", new UnwrapParameters());
#pragma warning disable 618
static Pref<UL.GIWorkflowMode> s_GiWorkflowMode = new Pref<UL.GIWorkflowMode>("lightmapping.giWorkflowMode", UL.GIWorkflowMode.Iterative, SettingsScope.User);
#pragma warning restore 618
static class Styles
{
public static readonly GUIContent hardAngle = new GUIContent("Hard Angle", "Angle between neighbor triangles that will generate seam.");
public static readonly GUIContent packMargin = new GUIContent("Pack Margin", "Measured in pixels, assuming mesh will cover an entire 1024x1024 lightmap.");
public static readonly GUIContent angleError = new GUIContent("Angle Error", "Measured in percents. Angle error measures deviation of UV angles from geometry angles.");
public static readonly GUIContent areaError = new GUIContent("Area Error", "");
static bool s_Initialized;
public static GUIStyle s_MiniButton;
public static GUIStyle miniButton { get { Init(); return s_MiniButton; } }
public static bool unwrapSettingsFoldout;
public static void Init()
{
if (s_Initialized)
return;
s_Initialized = true;
s_MiniButton = new GUIStyle(GUI.skin.button);
s_MiniButton.stretchHeight = false;
s_MiniButton.stretchWidth = false;
s_MiniButton.padding = new RectOffset(6, 6, 3, 3);
s_MiniButton.margin = new RectOffset(4, 4, 4, 0);
}
#if UNITY_EDITOR
internal static void Reset()
{
s_Initialized = false;
unwrapSettingsFoldout = false;
}
#endif
}
[UserSettingBlock("Mesh Settings")]
static void UnwrapSettingDefaults(string searchContext)
{
Styles.Init();
var isSearching = !string.IsNullOrEmpty(searchContext);
if (!isSearching)
Styles.unwrapSettingsFoldout = EditorGUILayout.Foldout(Styles.unwrapSettingsFoldout, "Lightmap UVs Settings");
if (isSearching || Styles.unwrapSettingsFoldout)
{
EditorGUI.BeginChangeCheck();
var unwrap = (UnwrapParameters)s_UnwrapParameters;
using (new SettingsGUILayout.IndentedGroup())
{
unwrap.hardAngle = SettingsGUILayout.SearchableSlider(Styles.hardAngle, unwrap.hardAngle, 1f, 180f, searchContext);
unwrap.packMargin = SettingsGUILayout.SearchableSlider(Styles.packMargin, unwrap.packMargin, 1f, 64f, searchContext);
unwrap.angleError = SettingsGUILayout.SearchableSlider(Styles.angleError, unwrap.angleError, 1f, 75f, searchContext);
unwrap.areaError = SettingsGUILayout.SearchableSlider(Styles.areaError, unwrap.areaError, 1f, 75f, searchContext);
if (!isSearching)
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Reset", Styles.miniButton))
unwrap.Reset();
GUILayout.EndHorizontal();
}
}
SettingsGUILayout.DoResetContextMenuForLastRect(s_UnwrapParameters);
if (EditorGUI.EndChangeCheck())
s_UnwrapParameters.value = unwrap;
}
}
public static bool autoUnwrapLightmapUV
{
get { return (bool)s_AutoUnwrapLightmapUV; }
set { s_AutoUnwrapLightmapUV.value = value; }
}
static Lightmapping()
{
UL.bakeCompleted += OnLightmappingCompleted;
Undo.postprocessModifications += PostprocessModifications;
}
[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
Styles.Reset();
}
/// <summary>
/// Toggles the LightmapStatic bit of an objects Static flags.
/// </summary>
/// <param name="pb"></param>
/// <param name="isEnabled"></param>
public static void SetLightmapStaticFlagEnabled(ProBuilderMesh pb, bool isEnabled)
{
Entity ent = pb.GetComponent<Entity>();
if (ent != null && ent.entityType == EntityType.Detail)
{
StaticEditorFlags flags = GameObjectUtility.GetStaticEditorFlags(pb.gameObject);
if (isEnabled != (flags & k_ContributeGI) > 0)
{
flags ^= k_ContributeGI;
GameObjectUtility.SetStaticEditorFlags(pb.gameObject, flags);
}
}
}
static UndoPropertyModification[] PostprocessModifications(UndoPropertyModification[] modifications)
{
if (!autoUnwrapLightmapUV)
return modifications;
foreach (var modification in modifications)
{
string property = modification.currentValue == null ? null : modification.currentValue.propertyPath;
if (string.IsNullOrEmpty(property)
|| !property.Equals(k_StaticEditorFlagsProperty)
|| string.IsNullOrEmpty(modification.currentValue.value))
continue;
var staticFlags = uint.Parse(modification.currentValue.value);
var lightmapStatic = (staticFlags & (uint) k_ContributeGI) != 0;
if (lightmapStatic)
{
var gameObject = modification.currentValue.target as GameObject;
if (gameObject != null && gameObject.TryGetComponent<ProBuilderMesh>(out var mesh))
{
mesh.Optimize();
}
}
}
return modifications;
}
static void OnLightmappingCompleted()
{
if (!s_ShowMissingLightmapUVWarning)
return;
var missingUv2 = EditorUtility.FindObjectsByType<ProBuilderMesh>().Where(x => !x.HasArrays(MeshArrays.Lightmap) && x.gameObject.HasStaticFlag(k_ContributeGI));
int count = missingUv2.Count();
if (count > 0)
Log.Warning("{0} ProBuilder {1} included in lightmap bake with missing UV2. " +
"Use \"Tools > ProBuilder > Editors > Open Lightmap UV Editor\" to find and generate missing UVs." +
"\nYou can turn off this warning in Preferences/ProBuilder.",
count, count == 1 ? "mesh" : "meshes");
}
/// <summary>
/// Build Lightmap UVs for each mesh in the selection that is missing the UV2 array.
/// </summary>
/// <param name="selection"></param>
/// <param name="showProgressBar"></param>
public static int RebuildMissingLightmapUVs(IEnumerable<ProBuilderMesh> selection, bool showProgressBar = false)
{
int count = 0;
float total = selection.Count(x => x.gameObject.HasStaticFlag(k_ContributeGI) && !x.HasArrays(MeshArrays.Lightmap));
foreach (var mesh in selection)
{
if (!mesh.gameObject.HasStaticFlag(k_ContributeGI) || mesh.HasArrays(MeshArrays.Texture1))
continue;
if (showProgressBar)
{
if (UnityEditor.EditorUtility.DisplayCancelableProgressBar("Generate Lightmap UVs", "Unwrapping UVs for mesh: " + mesh.name, count / total))
break;
}
count++;
mesh.Optimize(true);
}
UnityEditor.EditorUtility.ClearProgressBar();
return count;
}
/**
* Get the UnwrapParam values from a pb_UnwrapParameters object.
* Not in pb_UnwrapParameters because UnwrapParam is an Editor class.
*/
public static UnwrapParam GetUnwrapParam(UnwrapParameters parameters)
{
UnwrapParam param = new UnwrapParam();
if (parameters != null)
{
param.angleError = Mathf.Clamp(parameters.angleError, 1f, 75f) * .01f;
param.areaError = Mathf.Clamp(parameters.areaError , 1f, 75f) * .01f;
param.hardAngle = Mathf.Clamp(parameters.hardAngle , 0f, 180f);
param.packMargin = Mathf.Clamp(parameters.packMargin, 1f, 64) * .001f;
}
else
{
param.angleError = Mathf.Clamp(UnwrapParameters.k_AngleError, 1f, 75f) * .01f;
param.areaError = Mathf.Clamp(UnwrapParameters.k_AreaError , 1f, 75f) * .01f;
param.hardAngle = Mathf.Clamp(UnwrapParameters.k_HardAngle , 0f, 180f);
param.packMargin = Mathf.Clamp(UnwrapParameters.k_PackMargin, 1f, 64) * .001f;
}
return param;
}
#pragma warning disable 618
internal static void PushGIWorkflowMode()
{
s_GiWorkflowMode.SetValue(UL.giWorkflowMode, true);
if (UL.giWorkflowMode != UL.GIWorkflowMode.Legacy)
UL.giWorkflowMode = UL.GIWorkflowMode.OnDemand;
}
internal static void PopGIWorkflowMode()
{
UL.giWorkflowMode = s_GiWorkflowMode;
}
#pragma warning restore 618
}
}