diff --git a/Editor/Scripts/Editor/AssetDatabaseUtils.cs b/Editor/Scripts/Editor/AssetDatabaseUtils.cs index 611258c..39a9336 100644 --- a/Editor/Scripts/Editor/AssetDatabaseUtils.cs +++ b/Editor/Scripts/Editor/AssetDatabaseUtils.cs @@ -43,5 +43,10 @@ namespace TAO.VertexAnimation.Editor } } } + + public static bool HasAsset(string path, System.Type type) + { + return (AssetDatabase.LoadAssetAtPath(path, type) ? true : false); + } } } \ No newline at end of file diff --git a/Editor/Scripts/ModelBaker/AnimationPrefab.cs b/Editor/Scripts/ModelBaker/AnimationPrefab.cs index 5fff3ca..692ceb0 100644 --- a/Editor/Scripts/ModelBaker/AnimationPrefab.cs +++ b/Editor/Scripts/ModelBaker/AnimationPrefab.cs @@ -7,22 +7,65 @@ namespace TAO.VertexAnimation.Editor { public static GameObject Create(string path, string name, Mesh[] meshes, Material material, float[] lodTransitions) { - // Create parent. - GameObject parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimatorComponentAuthoring), typeof(Unity.Entities.ConvertToEntity)); + GameObject parent = null; + if (AssetDatabaseUtils.HasAsset(path, typeof(GameObject))) + { + // Load existing parent. + parent = PrefabUtility.LoadPrefabContents(path); + + // Check setup. + if (!parent.TryGetComponent(out LODGroup _)) + { + parent.AddComponent(); + } + + if (!parent.TryGetComponent(out VA_AnimatorComponentAuthoring _)) + { + parent.AddComponent(); + } + + if (!parent.TryGetComponent(out Unity.Entities.ConvertToEntity _)) + { + parent.AddComponent(); + } + } + else + { + // Create parent. + parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimatorComponentAuthoring), typeof(Unity.Entities.ConvertToEntity)); + } // Create all LODs. LOD[] lods = new LOD[meshes.Length]; for (int i = 0; i < meshes.Length; i++) { - GameObject lod = new GameObject(string.Format("{0}_LOD{1}", name, i), typeof(MeshFilter), typeof(MeshRenderer)); - - var mf = lod.GetComponent(); - mf.sharedMesh = meshes[i]; - var mr = lod.GetComponent(); - mr.sharedMaterial = material; + string childName = string.Format("{0}_LOD{1}", name, i); - lod.transform.SetParent(parent.transform); + GameObject child; + { + Transform t = parent.transform.Find(childName); + if (t) + { + child = t.gameObject; + } + else + { + child = new GameObject(childName, typeof(MeshFilter), typeof(MeshRenderer)); + } + } + + if (child.TryGetComponent(out MeshFilter mf)) + { + mf.sharedMesh = meshes[i]; + } + + if (child.TryGetComponent(out MeshRenderer mr)) + { + mr.sharedMaterial = material; + } + + child.transform.SetParent(parent.transform); lods[i] = new LOD(lodTransitions[i], new Renderer[1] { mr }); } diff --git a/Editor/Scripts/ModelBaker/Editor/VA_ModelBakerEditor.cs b/Editor/Scripts/ModelBaker/Editor/VA_ModelBakerEditor.cs index 7d30377..e92faf7 100644 --- a/Editor/Scripts/ModelBaker/Editor/VA_ModelBakerEditor.cs +++ b/Editor/Scripts/ModelBaker/Editor/VA_ModelBakerEditor.cs @@ -39,31 +39,19 @@ namespace TAO.VertexAnimation.Editor private void BakeGUI() { - EditorGUILayout.PropertyField(serializedObject.FindProperty("saveBakedDataToAsset")); + EditorGUILayout.PropertyField(serializedObject.FindProperty("lodSettings").FindPropertyRelative("lodSettings")); + EditorGUILayout.PropertyField(serializedObject.FindProperty("generateAnimationBook")); - int il = EditorGUI.indentLevel; - if (modelBaker.saveBakedDataToAsset) + using (new EditorGUILayout.HorizontalScope()) { - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(serializedObject.FindProperty("generateAnimationBook")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("lodSettings")); - - using (new EditorGUILayout.HorizontalScope()) - { - EditorGUILayout.PropertyField(serializedObject.FindProperty("generatePrefab")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("materialShader"), new GUIContent("")); - } + EditorGUILayout.PropertyField(serializedObject.FindProperty("generatePrefab")); + EditorGUILayout.PropertyField(serializedObject.FindProperty("materialShader"), new GUIContent("")); } - EditorGUI.indentLevel = il; if (GUILayout.Button("Bake", GUILayout.Height(32))) { modelBaker.Bake(); - - if (modelBaker.saveBakedDataToAsset) - { - modelBaker.SaveAssets(); - } + modelBaker.SaveAssets(); } if (GUILayout.Button("Delete", EditorStyles.miniButtonRight)) diff --git a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs index 0887c20..d480c72 100644 --- a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs +++ b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs @@ -16,7 +16,6 @@ namespace TAO.VertexAnimation.Editor public int textureWidth = 512; public LODSettings lodSettings = new LODSettings(); - public bool saveBakedDataToAsset = true; public bool generateAnimationBook = true; public bool generatePrefab = true; public Shader materialShader = null; @@ -34,7 +33,6 @@ namespace TAO.VertexAnimation.Editor [System.Serializable] public class LODSettings { - public bool generate = true; public LODSetting[] lodSettings = new LODSetting[3] { new LODSetting(1, .4f), new LODSetting(.6f, .15f), new LODSetting(.3f, .01f) }; public float[] GetQualitySettings() @@ -91,15 +89,7 @@ namespace TAO.VertexAnimation.Editor bakedData = target.Bake(animationClips, fps, textureWidth); positionMap = VA_Texture2DArrayUtils.CreateTextureArray(bakedData.positionMaps.ToArray(), false, true, TextureWrapMode.Repeat, FilterMode.Point, 1, string.Format("{0}_PositionMap", name), true); - - if (lodSettings.generate) - { - meshes = bakedData.mesh.GenerateLOD(lodSettings.LODCount(), lodSettings.GetQualitySettings()); - } - else - { - meshes = new Mesh[1] { bakedData.mesh }; - } + meshes = bakedData.mesh.GenerateLOD(lodSettings.LODCount(), lodSettings.GetQualitySettings()); DestroyImmediate(target); } @@ -114,10 +104,6 @@ namespace TAO.VertexAnimation.Editor } AssetDatabase.AddObjectToAsset(positionMap, this); - //foreach (var pm in bakedData.positionMaps) - //{ - // AssetDatabase.AddObjectToAsset(pm, this); - //} AssetDatabase.SaveAssets(); @@ -161,7 +147,7 @@ namespace TAO.VertexAnimation.Editor AssetDatabase.Refresh(); } - public void GeneratePrefab() + private void GeneratePrefab() { string path = AssetDatabase.GetAssetPath(this); int start = path.LastIndexOf('/'); @@ -186,7 +172,7 @@ namespace TAO.VertexAnimation.Editor prefab = AnimationPrefab.Create(path, name, meshes, material, lodSettings.GetTransitionSettings()); } - public void GenerateBook() + private void GenerateBook() { if (!book) {