@@ -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";
0 commit comments