-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplyAllIn1ShaderToAll.cs
More file actions
283 lines (236 loc) · 9.58 KB
/
ApplyAllIn1ShaderToAll.cs
File metadata and controls
283 lines (236 loc) · 9.58 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
public class ApplyAllIn1ShaderToAll : EditorWindow
{
static string SAVE_FOLDER = "Assets/AllIn1_GeneratedMaterials";
List<GameObject> targetObjects = new List<GameObject>();
Vector2 scrollPos;
[MenuItem("Tools/All In 1 - Apply Shader")]
static void ShowWindow()
{
var window = GetWindow<ApplyAllIn1ShaderToAll>("AllIn1 Shader Applier");
window.minSize = new Vector2(350, 300);
window.Show();
}
void OnGUI()
{
EditorGUILayout.Space(8);
EditorGUILayout.LabelField("Target Objects", EditorStyles.boldLabel);
EditorGUILayout.HelpBox(
"쉐이더를 적용할 오브젝트를 추가하세요.\n각 오브젝트의 모든 자식 오브젝트에도 자동 적용됩니다.",
MessageType.Info);
EditorGUILayout.Space(4);
// --- Drag & Drop Area ---
Rect dropArea = GUILayoutUtility.GetRect(0, 40, GUILayout.ExpandWidth(true));
GUI.Box(dropArea, "Drag & Drop GameObjects Here");
HandleDragAndDrop(dropArea);
EditorGUILayout.Space(4);
// --- Add from Selection button ---
if (GUILayout.Button("Add Selected Objects from Hierarchy"))
{
foreach (GameObject go in Selection.gameObjects)
{
if (!targetObjects.Contains(go))
targetObjects.Add(go);
}
}
EditorGUILayout.Space(8);
// --- Target List ---
EditorGUILayout.LabelField("Targets (" + targetObjects.Count + ")", EditorStyles.boldLabel);
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.MinHeight(100), GUILayout.MaxHeight(300));
for (int i = targetObjects.Count - 1; i >= 0; i--)
{
EditorGUILayout.BeginHorizontal();
targetObjects[i] = (GameObject)EditorGUILayout.ObjectField(
targetObjects[i], typeof(GameObject), true);
if (GUILayout.Button("X", GUILayout.Width(24)))
{
targetObjects.RemoveAt(i);
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndScrollView();
EditorGUILayout.Space(4);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Clear All"))
{
targetObjects.Clear();
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space(12);
// --- Apply Button ---
GUI.enabled = targetObjects.Count > 0;
if (GUILayout.Button("Apply Shader", GUILayout.Height(30)))
{
ApplyToTargets();
}
GUI.enabled = true;
}
void HandleDragAndDrop(Rect dropArea)
{
Event evt = Event.current;
if (!dropArea.Contains(evt.mousePosition)) return;
switch (evt.type)
{
case EventType.DragUpdated:
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
evt.Use();
break;
case EventType.DragPerform:
DragAndDrop.AcceptDrag();
foreach (Object obj in DragAndDrop.objectReferences)
{
GameObject go = obj as GameObject;
if (go != null && !targetObjects.Contains(go))
targetObjects.Add(go);
}
evt.Use();
break;
}
}
void ApplyToTargets()
{
string shaderName = "AllIn13DShader/AllIn13DShader";
Shader allIn1Shader = Shader.Find(shaderName);
if (allIn1Shader == null)
{
EditorUtility.DisplayDialog("Error", shaderName + " not found!", "OK");
return;
}
if (!AssetDatabase.IsValidFolder(SAVE_FOLDER))
{
AssetDatabase.CreateFolder("Assets", "AllIn1_GeneratedMaterials");
}
// Collect all renderers from target objects and their children
List<Renderer> allRenderers = new List<Renderer>();
foreach (GameObject target in targetObjects)
{
if (target == null) continue;
Renderer[] renderers = target.GetComponentsInChildren<Renderer>(true);
foreach (Renderer r in renderers)
{
if (!allRenderers.Contains(r))
allRenderers.Add(r);
}
}
int newCount = 0;
int updatedCount = 0;
foreach (Renderer renderer in allRenderers)
{
Undo.RecordObject(renderer, "Apply AllIn1 Shader");
Material[] materials = renderer.sharedMaterials;
bool changed = false;
for (int i = 0; i < materials.Length; i++)
{
if (materials[i] == null) continue;
string currentShader = materials[i].shader.name;
bool isAlreadyAllIn1 = currentShader.Contains("AllIn13DShader");
if (isAlreadyAllIn1)
{
Undo.RecordObject(materials[i], "Update AllIn1 Settings");
if (materials[i].shader != allIn1Shader)
{
materials[i].shader = allIn1Shader;
changed = true;
}
ApplySettings(materials[i]);
string existingPath = AssetDatabase.GetAssetPath(materials[i]);
if (string.IsNullOrEmpty(existingPath))
{
SaveMaterialAsset(materials[i], renderer.gameObject.name, i);
}
else
{
EditorUtility.SetDirty(materials[i]);
}
updatedCount++;
}
else
{
Material newMat = new Material(allIn1Shader);
newMat.name = "MAT_AllIn1_" + renderer.gameObject.name;
if (materials[i].HasProperty("_BaseMap"))
newMat.SetTexture("_MainTex", materials[i].GetTexture("_BaseMap"));
else if (materials[i].HasProperty("_MainTex"))
newMat.SetTexture("_MainTex", materials[i].GetTexture("_MainTex"));
if (materials[i].HasProperty("_BaseColor"))
newMat.SetColor("_Color", materials[i].GetColor("_BaseColor"));
else if (materials[i].HasProperty("_Color"))
newMat.SetColor("_Color", materials[i].GetColor("_Color"));
ApplySettings(newMat);
SaveMaterialAsset(newMat, renderer.gameObject.name, i);
materials[i] = newMat;
newCount++;
changed = true;
}
}
if (changed)
{
renderer.sharedMaterials = materials;
PrefabUtility.RecordPrefabInstancePropertyModifications(renderer);
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
string msg = "New: " + newCount
+ "\nUpdated: " + updatedCount
+ "\nTotal renderers: " + allRenderers.Count;
Debug.Log("[AllIn1] Done - " + msg);
EditorUtility.DisplayDialog("Result", msg, "OK");
}
static void SaveMaterialAsset(Material mat, string objName, int index)
{
string safeName = objName.Replace("/", "_").Replace("\\", "_").Replace(" ", "_");
string path = SAVE_FOLDER + "/MAT_AllIn1_" + safeName + "_" + index + ".mat";
int counter = 0;
while (AssetDatabase.LoadAssetAtPath<Material>(path) != null)
{
counter++;
path = SAVE_FOLDER + "/MAT_AllIn1_" + safeName + "_" + index + "_" + counter + ".mat";
}
AssetDatabase.CreateAsset(mat, path);
}
static void ApplySettings(Material mat)
{
// 1. Light Model: Toon
mat.SetFloat("_LightModel", 2);
mat.DisableKeyword("_LIGHTMODEL_NONE");
mat.DisableKeyword("_LIGHTMODEL_CLASSIC");
mat.DisableKeyword("_LIGHTMODEL_TOONRAMP");
mat.DisableKeyword("_LIGHTMODEL_HALFLAMBERT");
mat.DisableKeyword("_LIGHTMODEL_FAKEGI");
mat.DisableKeyword("_LIGHTMODEL_FASTLIGHTING");
mat.EnableKeyword("_LIGHTMODEL_TOON");
mat.SetFloat("_ToonCutoff", 0.433f);
mat.SetFloat("_ToonSmoothness", 0f);
// 2. Shading Model: PBR
mat.SetFloat("_ShadingModel", 1);
mat.DisableKeyword("_SHADINGMODEL_BASIC");
mat.EnableKeyword("_SHADINGMODEL_PBR");
mat.SetFloat("_Metallic", 0f);
mat.SetFloat("_Smoothness", 0.7f);
// 3. Specular Model: Toon
mat.SetFloat("_SpecularModel", 2);
mat.DisableKeyword("_SPECULARMODEL_NONE");
mat.DisableKeyword("_SPECULARMODEL_CLASSIC");
mat.DisableKeyword("_SPECULARMODEL_ANISOTROPIC");
mat.DisableKeyword("_SPECULARMODEL_ANISOTROPICTOON");
mat.EnableKeyword("_SPECULARMODEL_TOON");
mat.SetFloat("_SpecularAtten", 0f);
mat.SetFloat("_SpecularToonCutoff", 0f);
mat.SetFloat("_SpecularToonSmoothness", 0f);
// Shadows
mat.SetFloat("_CastShadowsOn", 1f);
mat.EnableKeyword("_CAST_SHADOWS_ON");
mat.SetFloat("_ReceiveShadows", 1f);
mat.EnableKeyword("_RECEIVE_SHADOWS_ON");
// Outline: None
mat.DisableKeyword("_OUTLINETYPE_CONSTANT");
mat.DisableKeyword("_OUTLINETYPE_SIMPLE");
mat.DisableKeyword("_OUTLINETYPE_FADEWITHDISTANCE");
mat.EnableKeyword("_OUTLINETYPE_NONE");
mat.SetFloat("_OutlineType", 0f);
}
}