Skip to content

Commit 5226f51

Browse files
authored
Merge pull request #11 from refiaa/revert_instance_info_saving
Revert instance info saving
2 parents 952327b + dc6c456 commit 5226f51

5 files changed

Lines changed: 189 additions & 13 deletions

File tree

Editor/DecimaterMain.cs

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ public class DecimaterMain : EditorWindow
2121
private Shader previewShader;
2222

2323
private float decimateLevel = 1.0f;
24-
25-
[MenuItem("Decimater/MeshDecimater")]
24+
25+
private bool isFirstDecimation = true;
26+
27+
[MenuItem("MeshOptimizer/Mesh Optimizer GUI")]
2628
public static void ShowWindow()
2729
{
28-
GetWindow<DecimaterMain>("Decimater for Unity");
30+
GetWindow<DecimaterMain>("Mesh Optimizer GUI");
2931
}
30-
32+
3133
private void OnEnable()
3234
{
3335
LoadShaders();
@@ -64,25 +66,34 @@ private void OnGUI()
6466
meshPreviewer.PreviewMesh(selectedGameObject, previewRect);
6567

6668
GUILayout.Space(10);
67-
GUILayout.Label("Decimate Level", EditorStyles.boldLabel);
69+
GUILayout.Label("Optimize Level", EditorStyles.boldLabel);
6870
EditorGUI.BeginChangeCheck();
69-
decimateLevel = EditorGUILayout.Slider("Decimate Level", decimateLevel, 0.1f, 1.0f);
71+
decimateLevel = EditorGUILayout.Slider("Optimize Level", decimateLevel, 0.1f, 1.0f);
7072
if (EditorGUI.EndChangeCheck())
7173
{
72-
// TODO:decimateLevelが変更された時の値の保存ロジックを追加したい。
74+
Mesh currentMesh = GetCurrentMesh();
75+
if (currentMesh != null)
76+
{
77+
MeshRevertManager.StoreDecimateLevel(currentMesh, decimateLevel);
78+
}
7379
}
7480

7581
GUILayout.Space(10);
76-
if (GUILayout.Button("Apply Decimation"))
82+
if (GUILayout.Button("Apply Optimization"))
7783
{
7884
ApplyDecimation();
7985
}
80-
86+
8187
if (GUILayout.Button("Revert"))
8288
{
8389
RevertDecimation();
8490
}
8591

92+
if (GUILayout.Button("Revert to Original"))
93+
{
94+
RevertToOriginalMesh();
95+
}
96+
8697
GUILayout.Space(10);
8798
meshInfoDisplay.DisplayMeshInfo(GetCurrentMesh());
8899
}
@@ -96,6 +107,10 @@ private void ApplyDecimation()
96107

97108
SaveDecimatedMesh();
98109

110+
MeshRevertManager.StoreOriginalMesh(decimatedMesh, originalMesh);
111+
112+
MeshRevertManager.StoreDecimateLevel(decimatedMesh, decimateLevel);
113+
99114
if (selectedGameObject.GetComponent<MeshFilter>() != null)
100115
{
101116
MeshFilter meshFilter = selectedGameObject.GetComponent<MeshFilter>();
@@ -111,6 +126,14 @@ private void ApplyDecimation()
111126
}
112127

113128
meshPreviewer.UpdatePreviewMesh(selectedGameObject);
129+
130+
// errorめんどいの
131+
if (isFirstDecimation)
132+
{
133+
isFirstDecimation = false;
134+
Debug.LogWarning("First decimation performed. Applying decimation again to prevent mesh data mismatch error.");
135+
ApplyDecimation();
136+
}
114137
}
115138

116139
private void RevertDecimation()
@@ -130,7 +153,54 @@ private void RevertDecimation()
130153
}
131154

132155
decimateLevel = 1.0f;
156+
MeshRevertManager.StoreDecimateLevel(originalMesh, decimateLevel);
157+
133158
meshPreviewer.UpdatePreviewMesh(selectedGameObject);
159+
160+
isFirstDecimation = true;
161+
}
162+
163+
private void RevertToOriginalMesh()
164+
{
165+
Mesh currentMesh = GetCurrentMesh();
166+
if (currentMesh == null)
167+
{
168+
Debug.LogWarning("No mesh to revert.");
169+
return;
170+
}
171+
172+
Mesh originalMeshFromManager = MeshRevertManager.GetOriginalMesh(currentMesh);
173+
if (originalMeshFromManager != null)
174+
{
175+
if (selectedGameObject.GetComponent<MeshFilter>() != null)
176+
{
177+
MeshFilter meshFilter = selectedGameObject.GetComponent<MeshFilter>();
178+
meshFilter.sharedMesh = originalMeshFromManager;
179+
MeshRenderer meshRenderer = selectedGameObject.GetComponent<MeshRenderer>();
180+
meshRenderer.sharedMaterials = originalMaterials;
181+
}
182+
else if (selectedGameObject.GetComponent<SkinnedMeshRenderer>() != null)
183+
{
184+
SkinnedMeshRenderer skinnedMeshRenderer = selectedGameObject.GetComponent<SkinnedMeshRenderer>();
185+
skinnedMeshRenderer.sharedMesh = originalMeshFromManager;
186+
skinnedMeshRenderer.sharedMaterials = originalMaterials;
187+
}
188+
Debug.Log("Reverted to original mesh.");
189+
190+
decimateLevel = 1.0f;
191+
MeshRevertManager.StoreDecimateLevel(originalMeshFromManager, decimateLevel);
192+
}
193+
else
194+
{
195+
Debug.LogWarning("Original mesh not found.");
196+
197+
decimateLevel = 1.0f;
198+
MeshRevertManager.StoreDecimateLevel(currentMesh, decimateLevel);
199+
}
200+
201+
meshPreviewer.UpdatePreviewMesh(selectedGameObject);
202+
203+
isFirstDecimation = true;
134204
}
135205

136206
private void LoadShaders()
@@ -142,7 +212,7 @@ private void UpdateSelection(GameObject newSelectedGameObject)
142212
{
143213
if (newSelectedGameObject != selectedGameObject)
144214
{
145-
decimateLevel = DEFAULT_DECIMATE_LEVEL;
215+
isFirstDecimation = true;
146216
}
147217

148218
if (newSelectedGameObject != null)
@@ -155,9 +225,12 @@ private void UpdateSelection(GameObject newSelectedGameObject)
155225
selectedGameObject = newSelectedGameObject;
156226
originalMesh = meshFilter.sharedMesh;
157227
EnableReadWrite(originalMesh);
228+
229+
decimateLevel = MeshRevertManager.GetDecimateLevel(originalMesh);
230+
158231
decimatedMesh = Instantiate(originalMesh);
159232
meshInfoDisplay.SetOriginalMesh(originalMesh);
160-
233+
161234
Renderer renderer = newSelectedGameObject.GetComponent<Renderer>();
162235
originalMaterials = renderer.sharedMaterials;
163236
originalSubmeshCount = new int[originalMesh.subMeshCount];
@@ -175,9 +248,12 @@ private void UpdateSelection(GameObject newSelectedGameObject)
175248
selectedGameObject = newSelectedGameObject;
176249
originalMesh = skinnedMeshRenderer.sharedMesh;
177250
EnableReadWrite(originalMesh);
251+
252+
decimateLevel = MeshRevertManager.GetDecimateLevel(originalMesh);
253+
178254
decimatedMesh = Instantiate(originalMesh);
179255
meshInfoDisplay.SetOriginalMesh(originalMesh);
180-
256+
181257
originalMaterials = skinnedMeshRenderer.sharedMaterials;
182258
originalSubmeshCount = new int[originalMesh.subMeshCount];
183259
for (int i = 0; i < originalMesh.subMeshCount; i++)
@@ -195,7 +271,7 @@ private void UpdateSelection(GameObject newSelectedGameObject)
195271
private void SaveDecimatedMesh()
196272
{
197273
string actualMeshName = GetActualMeshName();
198-
274+
199275
string originalPath = AssetDatabase.GetAssetPath(originalMesh);
200276
string directory = Path.GetDirectoryName(originalPath);
201277
string newFileName = $"{actualMeshName}{MESH_SUFFIX}.asset";

Editor/MeshRevertManager.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
using UnityEditor;
4+
5+
public static class MeshRevertManager
6+
{
7+
private static Dictionary<string, Mesh> originalMeshMap = new Dictionary<string, Mesh>();
8+
9+
public static void StoreOriginalMesh(Mesh decimatedMesh, Mesh originalMesh)
10+
{
11+
string key = GetMeshKey(decimatedMesh);
12+
if (!originalMeshMap.ContainsKey(key))
13+
{
14+
originalMeshMap.Add(key, originalMesh);
15+
}
16+
}
17+
18+
public static Mesh GetOriginalMesh(Mesh decimatedMesh)
19+
{
20+
string key = GetMeshKey(decimatedMesh);
21+
if (originalMeshMap.TryGetValue(key, out Mesh originalMesh))
22+
{
23+
return originalMesh;
24+
}
25+
return null;
26+
}
27+
28+
private static string GetMeshKey(Mesh mesh)
29+
{
30+
string assetPath = AssetDatabase.GetAssetPath(mesh);
31+
if (string.IsNullOrEmpty(assetPath))
32+
{
33+
return mesh.name;
34+
}
35+
else
36+
{
37+
return $"{assetPath}:{mesh.name}";
38+
}
39+
}
40+
41+
private static string GetMeshUniqueID(Mesh mesh)
42+
{
43+
string guid;
44+
long localId;
45+
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(mesh, out guid, out localId))
46+
{
47+
return $"{guid}_{localId}";
48+
}
49+
else
50+
{
51+
return mesh.GetInstanceID().ToString();
52+
}
53+
}
54+
55+
public static void StoreDecimateLevel(Mesh mesh, float decimateLevel)
56+
{
57+
string uniqueID = GetMeshUniqueID(mesh);
58+
if (!string.IsNullOrEmpty(uniqueID))
59+
{
60+
EditorPrefs.SetFloat("DecimateLevel_" + uniqueID, decimateLevel);
61+
}
62+
}
63+
64+
public static float GetDecimateLevel(Mesh mesh)
65+
{
66+
string uniqueID = GetMeshUniqueID(mesh);
67+
if (!string.IsNullOrEmpty(uniqueID))
68+
{
69+
return EditorPrefs.GetFloat("DecimateLevel_" + uniqueID, 1.0f);
70+
}
71+
return 1.0f;
72+
}
73+
}

Editor/MeshRevertManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.jp.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ v0.0.8:
122122
・decimateしたオブジェクトがVRCにアップロードした時に消える問題を修正しました
123123
> decimateを行ったobjectがAssetに保存されるようになりました。
124124
125+
v0.0.9:
126+
127+
> 名前を"Decimate"から"Optimizer"に変更しました
128+
>
129+
> `Revert to Original`を追加しました
130+
>
131+
> `Optimize(decimate) level`が保存されます
132+
125133
```
126134
work confirmed in
127135

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ v0.0.8:
123123
・Fixed an issue where decimated objects disappeared when uploaded to VRC
124124
> Now the actually decimated object is saved.
125125
126+
v0.0.9:
127+
128+
> Change name "Decimate" to "Optimizer"
129+
>
130+
> `Revert to Original` Added.
131+
>
132+
> `Optimize(decimate) level` is now save globally
133+
126134
```
127135
work confirmed in
128136

0 commit comments

Comments
 (0)