Skip to content

Commit

Permalink
Merge pull request #11 from refiaa/revert_instance_info_saving
Browse files Browse the repository at this point in the history
Revert instance info saving
  • Loading branch information
refiaa authored Oct 19, 2024
2 parents 952327b + dc6c456 commit 5226f51
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 13 deletions.
102 changes: 89 additions & 13 deletions Editor/DecimaterMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public class DecimaterMain : EditorWindow
private Shader previewShader;

private float decimateLevel = 1.0f;

[MenuItem("Decimater/MeshDecimater")]

private bool isFirstDecimation = true;

[MenuItem("MeshOptimizer/Mesh Optimizer GUI")]
public static void ShowWindow()
{
GetWindow<DecimaterMain>("Decimater for Unity");
GetWindow<DecimaterMain>("Mesh Optimizer GUI");
}

private void OnEnable()
{
LoadShaders();
Expand Down Expand Up @@ -64,25 +66,34 @@ private void OnGUI()
meshPreviewer.PreviewMesh(selectedGameObject, previewRect);

GUILayout.Space(10);
GUILayout.Label("Decimate Level", EditorStyles.boldLabel);
GUILayout.Label("Optimize Level", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
decimateLevel = EditorGUILayout.Slider("Decimate Level", decimateLevel, 0.1f, 1.0f);
decimateLevel = EditorGUILayout.Slider("Optimize Level", decimateLevel, 0.1f, 1.0f);
if (EditorGUI.EndChangeCheck())
{
// TODO:decimateLevelが変更された時の値の保存ロジックを追加したい。
Mesh currentMesh = GetCurrentMesh();
if (currentMesh != null)
{
MeshRevertManager.StoreDecimateLevel(currentMesh, decimateLevel);
}
}

GUILayout.Space(10);
if (GUILayout.Button("Apply Decimation"))
if (GUILayout.Button("Apply Optimization"))
{
ApplyDecimation();
}

if (GUILayout.Button("Revert"))
{
RevertDecimation();
}

if (GUILayout.Button("Revert to Original"))
{
RevertToOriginalMesh();
}

GUILayout.Space(10);
meshInfoDisplay.DisplayMeshInfo(GetCurrentMesh());
}
Expand All @@ -96,6 +107,10 @@ private void ApplyDecimation()

SaveDecimatedMesh();

MeshRevertManager.StoreOriginalMesh(decimatedMesh, originalMesh);

MeshRevertManager.StoreDecimateLevel(decimatedMesh, decimateLevel);

if (selectedGameObject.GetComponent<MeshFilter>() != null)
{
MeshFilter meshFilter = selectedGameObject.GetComponent<MeshFilter>();
Expand All @@ -111,6 +126,14 @@ private void ApplyDecimation()
}

meshPreviewer.UpdatePreviewMesh(selectedGameObject);

// errorめんどいの
if (isFirstDecimation)
{
isFirstDecimation = false;
Debug.LogWarning("First decimation performed. Applying decimation again to prevent mesh data mismatch error.");
ApplyDecimation();
}
}

private void RevertDecimation()
Expand All @@ -130,7 +153,54 @@ private void RevertDecimation()
}

decimateLevel = 1.0f;
MeshRevertManager.StoreDecimateLevel(originalMesh, decimateLevel);

meshPreviewer.UpdatePreviewMesh(selectedGameObject);

isFirstDecimation = true;
}

private void RevertToOriginalMesh()
{
Mesh currentMesh = GetCurrentMesh();
if (currentMesh == null)
{
Debug.LogWarning("No mesh to revert.");
return;
}

Mesh originalMeshFromManager = MeshRevertManager.GetOriginalMesh(currentMesh);
if (originalMeshFromManager != null)
{
if (selectedGameObject.GetComponent<MeshFilter>() != null)
{
MeshFilter meshFilter = selectedGameObject.GetComponent<MeshFilter>();
meshFilter.sharedMesh = originalMeshFromManager;
MeshRenderer meshRenderer = selectedGameObject.GetComponent<MeshRenderer>();
meshRenderer.sharedMaterials = originalMaterials;
}
else if (selectedGameObject.GetComponent<SkinnedMeshRenderer>() != null)
{
SkinnedMeshRenderer skinnedMeshRenderer = selectedGameObject.GetComponent<SkinnedMeshRenderer>();
skinnedMeshRenderer.sharedMesh = originalMeshFromManager;
skinnedMeshRenderer.sharedMaterials = originalMaterials;
}
Debug.Log("Reverted to original mesh.");

decimateLevel = 1.0f;
MeshRevertManager.StoreDecimateLevel(originalMeshFromManager, decimateLevel);
}
else
{
Debug.LogWarning("Original mesh not found.");

decimateLevel = 1.0f;
MeshRevertManager.StoreDecimateLevel(currentMesh, decimateLevel);
}

meshPreviewer.UpdatePreviewMesh(selectedGameObject);

isFirstDecimation = true;
}

private void LoadShaders()
Expand All @@ -142,7 +212,7 @@ private void UpdateSelection(GameObject newSelectedGameObject)
{
if (newSelectedGameObject != selectedGameObject)
{
decimateLevel = DEFAULT_DECIMATE_LEVEL;
isFirstDecimation = true;
}

if (newSelectedGameObject != null)
Expand All @@ -155,9 +225,12 @@ private void UpdateSelection(GameObject newSelectedGameObject)
selectedGameObject = newSelectedGameObject;
originalMesh = meshFilter.sharedMesh;
EnableReadWrite(originalMesh);

decimateLevel = MeshRevertManager.GetDecimateLevel(originalMesh);

decimatedMesh = Instantiate(originalMesh);
meshInfoDisplay.SetOriginalMesh(originalMesh);

Renderer renderer = newSelectedGameObject.GetComponent<Renderer>();
originalMaterials = renderer.sharedMaterials;
originalSubmeshCount = new int[originalMesh.subMeshCount];
Expand All @@ -175,9 +248,12 @@ private void UpdateSelection(GameObject newSelectedGameObject)
selectedGameObject = newSelectedGameObject;
originalMesh = skinnedMeshRenderer.sharedMesh;
EnableReadWrite(originalMesh);

decimateLevel = MeshRevertManager.GetDecimateLevel(originalMesh);

decimatedMesh = Instantiate(originalMesh);
meshInfoDisplay.SetOriginalMesh(originalMesh);

originalMaterials = skinnedMeshRenderer.sharedMaterials;
originalSubmeshCount = new int[originalMesh.subMeshCount];
for (int i = 0; i < originalMesh.subMeshCount; i++)
Expand All @@ -195,7 +271,7 @@ private void UpdateSelection(GameObject newSelectedGameObject)
private void SaveDecimatedMesh()
{
string actualMeshName = GetActualMeshName();

string originalPath = AssetDatabase.GetAssetPath(originalMesh);
string directory = Path.GetDirectoryName(originalPath);
string newFileName = $"{actualMeshName}{MESH_SUFFIX}.asset";
Expand Down
73 changes: 73 additions & 0 deletions Editor/MeshRevertManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public static class MeshRevertManager
{
private static Dictionary<string, Mesh> originalMeshMap = new Dictionary<string, Mesh>();

public static void StoreOriginalMesh(Mesh decimatedMesh, Mesh originalMesh)
{
string key = GetMeshKey(decimatedMesh);
if (!originalMeshMap.ContainsKey(key))
{
originalMeshMap.Add(key, originalMesh);
}
}

public static Mesh GetOriginalMesh(Mesh decimatedMesh)
{
string key = GetMeshKey(decimatedMesh);
if (originalMeshMap.TryGetValue(key, out Mesh originalMesh))
{
return originalMesh;
}
return null;
}

private static string GetMeshKey(Mesh mesh)
{
string assetPath = AssetDatabase.GetAssetPath(mesh);
if (string.IsNullOrEmpty(assetPath))
{
return mesh.name;
}
else
{
return $"{assetPath}:{mesh.name}";
}
}

private static string GetMeshUniqueID(Mesh mesh)
{
string guid;
long localId;
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(mesh, out guid, out localId))
{
return $"{guid}_{localId}";
}
else
{
return mesh.GetInstanceID().ToString();
}
}

public static void StoreDecimateLevel(Mesh mesh, float decimateLevel)
{
string uniqueID = GetMeshUniqueID(mesh);
if (!string.IsNullOrEmpty(uniqueID))
{
EditorPrefs.SetFloat("DecimateLevel_" + uniqueID, decimateLevel);
}
}

public static float GetDecimateLevel(Mesh mesh)
{
string uniqueID = GetMeshUniqueID(mesh);
if (!string.IsNullOrEmpty(uniqueID))
{
return EditorPrefs.GetFloat("DecimateLevel_" + uniqueID, 1.0f);
}
return 1.0f;
}
}
11 changes: 11 additions & 0 deletions Editor/MeshRevertManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions README.jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ v0.0.8:
・decimateしたオブジェクトがVRCにアップロードした時に消える問題を修正しました
> decimateを行ったobjectがAssetに保存されるようになりました。
v0.0.9:

> 名前を"Decimate"から"Optimizer"に変更しました
>
> `Revert to Original`を追加しました
>
> `Optimize(decimate) level`が保存されます
```
work confirmed in
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ v0.0.8:
・Fixed an issue where decimated objects disappeared when uploaded to VRC
> Now the actually decimated object is saved.
v0.0.9:

> Change name "Decimate" to "Optimizer"
>
> `Revert to Original` Added.
>
> `Optimize(decimate) level` is now save globally
```
work confirmed in
Expand Down

0 comments on commit 5226f51

Please sign in to comment.