optimize performance

This commit is contained in:
1605296300@qq.com
2022-08-22 01:25:59 +08:00
parent 544226fde9
commit cdb31634c7
17 changed files with 847 additions and 263 deletions
-264
View File
@@ -1,264 +0,0 @@
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using Newtonsoft.Json.Linq;
class SelectAllOfTag : ScriptableWizard
{
//此函数可将无法读写的Tex克隆成一份可读写的Tex,用于提取图片至png
Texture2D duplicateTexture(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
[MenuItem("Model/Get Select Model")]
static void CreateWindow()
{
ScriptableWizard.DisplayWizard(
"Get Select Model",
typeof(SelectAllOfTag),
"Start!");
}
void OnWizardCreate()
{
Debug.Log(Selection.objects[0]);
GameObject gameobj = (GameObject)Selection.objects[0];
if (!AssetDatabase.IsValidFolder("Assets/Model"))
{
AssetDatabase.CreateFolder("Assets", "Model");
}
string folder_name = "Assets/Model/" + gameobj.name;
if (!AssetDatabase.IsValidFolder(folder_name))
{
AssetDatabase.CreateFolder("Assets/Model", gameobj.name);
}
foreach (SkinnedMeshRenderer skin_mesh in gameobj.transform.GetComponentsInChildren<SkinnedMeshRenderer>())
{
//提取所有Mesh至文件夹
Mesh m = skin_mesh.sharedMesh;
string m_parent = folder_name + "/Mesh";
string m_path = folder_name + "/Mesh/" + m.name + ".mesh";
if (!AssetDatabase.IsValidFolder(m_parent))
{
AssetDatabase.CreateFolder(folder_name, "Mesh");
}
Debug.Log(m.name);
Debug.Log(AssetDatabase.IsValidFolder(m_parent));
AssetDatabase.RemoveObjectFromAsset(m);
AssetDatabase.CreateAsset(m, folder_name + "/Mesh/" + m.name + ".mesh");
//创建Texture2D文件夹
string tex_parent = folder_name + "/Texture2D";
if (!AssetDatabase.IsValidFolder(tex_parent))
{
AssetDatabase.CreateFolder(folder_name, "Texture2D");
}
//创建Material文件夹
string mat_parent = folder_name + "/Material";
if (!AssetDatabase.IsValidFolder(mat_parent))
{
AssetDatabase.CreateFolder(folder_name, "Material");
}
foreach (Material mat in skin_mesh.sharedMaterials)
{
JArray p_name_list = new JArray();
JArray tex_name_list = new JArray();
//获得材质的所有属性名,如果改属性名对应的是贴图,提取该贴图
Shader mat_shader = mat.shader;
int p_num = mat_shader.GetPropertyCount();
Debug.Log(p_num);
for (int i = 0; i < p_num; i++)
{
//Debug.Log(mat_shader.GetPropertyName(i));
//Debug.Log(mat_shader.GetPropertyType(i));
string p_name = mat_shader.GetPropertyName(i);
if (mat_shader.GetPropertyType(i) == UnityEngine.Rendering.ShaderPropertyType.Texture)
{
//Debug.Log("Hello!");
Debug.Log(p_name);
Texture2D mat_texture = (Texture2D)mat.GetTexture(p_name);
if (mat_texture)
{
//Debug.Log("There is a Name!");
string tex_name = mat_texture.name;
string tex_path = folder_name + "/Texture2D/" + tex_name + ".png";
if (!File.Exists(tex_path))
{
mat_texture = duplicateTexture(mat_texture);
byte[] bytes = mat_texture.EncodeToPNG();
File.WriteAllBytes(tex_path, bytes);
}
//记忆图片与属性名之间的关联
p_name_list.Add(new JValue(p_name));
tex_name_list.Add(new JValue(tex_path));
}
}
}
//提取材质
AssetDatabase.RemoveObjectFromAsset(mat);
AssetDatabase.CreateAsset(mat, folder_name + "/Material/" + mat.name + ".mat");
//刷新资源,将所有提取出来的图片载入Assets
AssetDatabase.Refresh();
//将图片附加进材质
for (int i = 0; i < p_name_list.Count; i++)
{
Debug.Log(p_name_list[i]);
Debug.Log(tex_name_list[i]);
mat.SetTexture(p_name_list[i].ToString(), (Texture2D)AssetDatabase.LoadAssetAtPath(tex_name_list[i].ToString(), typeof(Texture2D)));
};
}
}
foreach (MeshFilter mf in gameobj.transform.GetComponentsInChildren<MeshFilter>())
{
//提取所有Mesh至文件夹
Mesh m = mf.sharedMesh;
string m_parent = folder_name + "/Mesh";
string m_path = folder_name + "/Mesh/" + m.name + ".mesh";
if (!AssetDatabase.IsValidFolder(m_parent))
{
AssetDatabase.CreateFolder(folder_name, "Mesh");
}
Debug.Log(m.name);
Debug.Log(AssetDatabase.IsValidFolder(m_parent));
AssetDatabase.RemoveObjectFromAsset(m);
AssetDatabase.CreateAsset(m, folder_name + "/Mesh/" + m.name + ".mesh");
}
foreach (MeshRenderer mr in gameobj.transform.GetComponentsInChildren<MeshRenderer>())
{
//创建Texture2D文件夹
string tex_parent = folder_name + "/Texture2D";
if (!AssetDatabase.IsValidFolder(tex_parent))
{
AssetDatabase.CreateFolder(folder_name, "Texture2D");
}
//创建Material文件夹
string mat_parent = folder_name + "/Material";
if (!AssetDatabase.IsValidFolder(mat_parent))
{
AssetDatabase.CreateFolder(folder_name, "Material");
}
foreach (Material mat in mr.sharedMaterials)
{
JArray p_name_list = new JArray();
JArray tex_name_list = new JArray();
//获得材质的所有属性名,如果改属性名对应的是贴图,提取该贴图
Shader mat_shader = mat.shader;
int p_num = mat_shader.GetPropertyCount();
Debug.Log(p_num);
for (int i = 0; i < p_num; i++)
{
//Debug.Log(mat_shader.GetPropertyName(i));
//Debug.Log(mat_shader.GetPropertyType(i));
string p_name = mat_shader.GetPropertyName(i);
if (mat_shader.GetPropertyType(i) == UnityEngine.Rendering.ShaderPropertyType.Texture)
{
//Debug.Log("Hello!");
Debug.Log(p_name);
Texture2D mat_texture = (Texture2D)mat.GetTexture(p_name);
if (mat_texture)
{
//Debug.Log("There is a Name!");
string tex_name = mat_texture.name;
string tex_path = folder_name + "/Texture2D/" + tex_name + ".png";
if (!File.Exists(tex_path))
{
mat_texture = duplicateTexture(mat_texture);
byte[] bytes = mat_texture.EncodeToPNG();
File.WriteAllBytes(tex_path, bytes);
}
//记忆图片与属性名之间的关联
p_name_list.Add(new JValue(p_name));
tex_name_list.Add(new JValue(tex_path));
}
}
}
//提取材质
AssetDatabase.RemoveObjectFromAsset(mat);
AssetDatabase.CreateAsset(mat, folder_name + "/Material/" + mat.name + ".mat");
//刷新资源,将所有提取出来的图片载入Assets
AssetDatabase.Refresh();
//将图片附加进材质
for (int i = 0; i < p_name_list.Count; i++)
{
Debug.Log(p_name_list[i]);
Debug.Log(tex_name_list[i]);
mat.SetTexture(p_name_list[i].ToString(), (Texture2D)AssetDatabase.LoadAssetAtPath(tex_name_list[i].ToString(), typeof(Texture2D)));
};
}
}
foreach (Animator anim in gameobj.transform.GetComponentsInChildren<Animator>())
{
//提取所有Avatar至文件夹
Avatar av = anim.avatar;
string av_parent = folder_name + "/Avatar";
string av_path = folder_name + "/Avatar/" + av.name + ".asset";
if (!AssetDatabase.IsValidFolder(av_parent))
{
AssetDatabase.CreateFolder(folder_name, "Avatar");
}
Debug.Log(av.name);
Debug.Log(AssetDatabase.IsValidFolder(av_parent));
AssetDatabase.RemoveObjectFromAsset(av);
AssetDatabase.CreateAsset(av, folder_name + "/Avatar/" + av.name + ".asset");
}
//提取预制体
PrefabUtility.SaveAsPrefabAsset(gameobj, folder_name + "/" + gameobj.name + ".prefab");
}
}
+1 -1
View File
@@ -7,6 +7,6 @@ public class UmaUIContainer : MonoBehaviour
{
public TMPro.TextMeshProUGUI Name;
public Button Button;
public Slider Slider;
}
+2 -1
View File
@@ -226,6 +226,7 @@ public class UmaViewerBuilder : MonoBehaviour
var firsehead = CurrentContainer.Heads[0];
var FaceDriven = firsehead.GetComponent<AssetHolder>()._assetTable.list.Find(a => { return a.Key == "facial_target"; }).Value as FaceDrivenKeyTarget;
var newFaceDriven = firsehead.AddComponent<FaceDrivenKeyTarget>();
newFaceDriven.callBack = UmaViewerUI.Instance;
newFaceDriven._eyebrowTarget = FaceDriven._eyebrowTarget;
newFaceDriven._eyeTarget = FaceDriven._eyeTarget;
newFaceDriven._mouthTarget = FaceDriven._mouthTarget;
@@ -391,7 +392,7 @@ public class UmaViewerBuilder : MonoBehaviour
object asset = bundle.LoadAsset(name);
if (asset == null) { continue; }
Debug.Log("##"+bundle.name + "/" + name + $" ({asset.GetType()})");
Debug.Log("Bundle:"+bundle.name + "/" + name + $" ({asset.GetType()})");
Type aType = asset.GetType();
if (aType == typeof(AnimationClip))
{
+45 -2
View File
@@ -1,3 +1,4 @@
using Gallop;
using System.Collections;
using System.Collections.Generic;
using System.IO;
@@ -5,7 +6,7 @@ using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class UmaViewerUI : MonoBehaviour
public class UmaViewerUI : MonoBehaviour,FaceLoadCallBack
{
public static UmaViewerUI Instance;
private UmaViewerMain Main => UmaViewerMain.Instance;
@@ -25,13 +26,15 @@ public class UmaViewerUI : MonoBehaviour
public ScrollRect PropList;
public ScrollRect SceneList;
public ScrollRect FacialList;
//settings
public TMPro.TMP_InputField SSWidth, SSHeight;
public List<GameObject> TogglablePanels = new List<GameObject>();
public GameObject UmaContainerPrefab;
public GameObject UmaContainerSliderPrefab;
private int LoadedAssetCount = 0;
[SerializeField] private RectTransform LoadedAssetsPanel;
[SerializeField] private TMPro.TextMeshProUGUI LoadedAssetsText;
@@ -100,6 +103,41 @@ public class UmaViewerUI : MonoBehaviour
}
}
public void LoadFacialPanels(FaceDrivenKeyTarget target)
{
foreach (UmaUIContainer ui in FacialList.content.GetComponentsInChildren<UmaUIContainer>())
{
Destroy(ui.gameObject);
}
foreach (FacialMorph morph in target.EyeBrowMorphs)
{
var container = Instantiate(UmaContainerSliderPrefab, FacialList.content).GetComponent<UmaUIContainer>();
container.Name.text = morph.name;
container.Slider.value = morph.Weight;
container.Slider.maxValue = 1;
container.Slider.minValue = 0;
container.Slider.onValueChanged.AddListener((a) => { morph.Weight = a;});
}
foreach (FacialMorph morph in target.EyeMorphs)
{
var container = Instantiate(UmaContainerSliderPrefab, FacialList.content).GetComponent<UmaUIContainer>();
container.Name.text = morph.name;
container.Slider.value = morph.Weight;
container.Slider.maxValue = 1;
container.Slider.minValue = 0;
container.Slider.onValueChanged.AddListener((a) => { morph.Weight = a; });
}
foreach (FacialMorph morph in target.MouthMorphs)
{
var container = Instantiate(UmaContainerSliderPrefab, FacialList.content).GetComponent<UmaUIContainer>();
container.Name.text = morph.name;
container.Slider.value = morph.Weight;
container.Slider.maxValue = 1;
container.Slider.minValue = 0;
container.Slider.onValueChanged.AddListener((a) => { morph.Weight = a; });
}
}
public void LoadMiniModelPanels()
{
var container = Instantiate(UmaContainerPrefab, MiniAnimationSetList.content).GetComponent<UmaUIContainer>();
@@ -308,4 +346,9 @@ public class UmaViewerUI : MonoBehaviour
{
Camera.main.backgroundColor = color;
}
public void CallBack(FaceDrivenKeyTarget target)
{
LoadFacialPanels(target);
}
}
+77 -50
View File
@@ -5,7 +5,7 @@ using UnityEngine;
namespace Gallop
{
public class FaceDrivenKeyTarget : MonoBehaviour
public class FaceDrivenKeyTarget : MonoBehaviour
{
public List<EyeTarget> _eyeTarget;
public List<EyebrowTarget> _eyebrowTarget;
@@ -19,9 +19,11 @@ namespace Gallop
public List<FacialMorph> EyeBrowMorphs = new List<FacialMorph>();
public List<FacialMorph> EyeMorphs = new List<FacialMorph>();
public List<FacialMorph> MouthMorphs = new List<FacialMorph>();
public FaceLoadCallBack callBack;
private void Start()
{
Objs.AddRange(GetComponentsInChildren<Transform>());
foreach (Transform g in Objs)
{
@@ -38,6 +40,10 @@ namespace Gallop
morph.SetFace(this);
morph.name = "EyeBrow_" + i + "_" + (j > 0 ? "R" : "L");
morph.trsArray = _eyebrowTarget[i]._faceGroupInfo[j]._trsArray;
foreach(TrsArray trs in morph.trsArray)
{
trs.transform = Objs.Find(ani => ani.name.Equals(trs._path));
}
EyeBrowMorphs.Add(morph);
}
}
@@ -50,6 +56,10 @@ namespace Gallop
morph.SetFace(this);
morph.name = "Eye_" + i + "_" + (j > 0 ? "R" : "L");
morph.trsArray = _eyeTarget[i]._faceGroupInfo[j]._trsArray;
foreach (TrsArray trs in morph.trsArray)
{
trs.transform = Objs.Find(ani => ani.name.Equals(trs._path));
}
EyeMorphs.Add(morph);
}
}
@@ -62,11 +72,16 @@ namespace Gallop
morph.SetFace(this);
morph.name = "Mouth_" + i + "_" + j;
morph.trsArray = _mouthTarget[i]._faceGroupInfo[j]._trsArray;
foreach (TrsArray trs in morph.trsArray)
{
trs.transform = Objs.Find(ani => ani.name.Equals(trs._path));
}
MouthMorphs.Add(morph);
}
}
MouthMorphs[4].Weight = 1;
if (callBack != null) { callBack.CallBack(this); }
}
@@ -78,7 +93,6 @@ namespace Gallop
if(EyeBrowMorphs[i].LastWeight!= EyeBrowMorphs[i].Weight)
{
changed = true;
EyeBrowMorphs[i].LastWeight = EyeBrowMorphs[i].Weight;
}
}
for (int i = 0; i < EyeMorphs.Count; i++)
@@ -86,7 +100,6 @@ namespace Gallop
if (EyeMorphs[i].LastWeight != EyeMorphs[i].Weight)
{
changed = true;
EyeMorphs[i].LastWeight = EyeMorphs[i].Weight;
}
}
for (int i = 0; i < MouthMorphs.Count; i++)
@@ -94,7 +107,6 @@ namespace Gallop
if (MouthMorphs[i].LastWeight != MouthMorphs[i].Weight)
{
changed = true;
MouthMorphs[i].LastWeight = MouthMorphs[i].Weight;
}
}
@@ -106,35 +118,37 @@ namespace Gallop
FacialReset();
foreach (FacialMorph morph in EyeBrowMorphs)
{
if(morph.LastWeight!=morph.Weight || morph.Weight > 0)
foreach(TrsArray trs in morph.trsArray)
{
var bone = Objs.Find(ani => ani.name.Equals(trs._path));
if (bone)
{
if (trs.transform)
{
if (!trs.IsOverrideTarget)
{
var tmp = bone.localRotation.eulerAngles;
var tmp = trs.transform.localRotation.eulerAngles;
tmp += (trs._rotation * morph.Weight);
bone.localRotation = Quaternion.Euler(bone.localRotation.eulerAngles + (trs._rotation * morph.Weight));
bone.localPosition += trs._position * morph.Weight;
bone.localScale += trs._scale * morph.Weight;
trs.transform.localRotation = Quaternion.Euler(trs.transform.localRotation.eulerAngles + (trs._rotation * morph.Weight));
trs.transform.localPosition += trs._position * morph.Weight;
trs.transform.localScale += trs._scale * morph.Weight;
}
};
};
}
}
foreach (FacialMorph morph in EyeMorphs)
{
if(morph.LastWeight!=morph.Weight || morph.Weight > 0)
foreach (TrsArray trs in morph.trsArray)
{
var bone = Objs.Find(ani => ani.name.Equals(trs._path));
if (bone)
if (trs.transform)
{
if (!trs.IsOverrideTarget)
{
bone.localRotation = Quaternion.Euler(bone.localRotation.eulerAngles + (trs._rotation * morph.Weight));
bone.localPosition += trs._position * morph.Weight;
bone.localScale += trs._scale * morph.Weight;
var tmp = trs.transform.localRotation.eulerAngles;
tmp += (trs._rotation * morph.Weight);
trs.transform.localRotation = Quaternion.Euler(trs.transform.localRotation.eulerAngles + (trs._rotation * morph.Weight));
trs.transform.localPosition += trs._position * morph.Weight;
trs.transform.localScale += trs._scale * morph.Weight;
}
};
}
@@ -142,16 +156,18 @@ namespace Gallop
foreach (FacialMorph morph in MouthMorphs)
{
if(morph.LastWeight!=morph.Weight || morph.Weight > 0)
foreach (TrsArray trs in morph.trsArray)
{
var bone = Objs.Find(ani => ani.name.Equals(trs._path));
if (bone)
if (trs.transform)
{
if (!trs.IsOverrideTarget)
{
bone.localRotation = Quaternion.Euler(bone.localRotation.eulerAngles + (trs._rotation * morph.Weight));
bone.localPosition += trs._position * morph.Weight;
bone.localScale += trs._scale * morph.Weight;
var tmp = trs.transform.localRotation.eulerAngles;
tmp += (trs._rotation * morph.Weight);
trs.transform.localRotation = Quaternion.Euler(trs.transform.localRotation.eulerAngles + (trs._rotation * morph.Weight));
trs.transform.localPosition += trs._position * morph.Weight;
trs.transform.localScale += trs._scale * morph.Weight;
}
};
}
@@ -160,52 +176,61 @@ namespace Gallop
foreach (FacialMorph morph in EyeBrowMorphs)//´¦Àí¸²¸Ç±íÇé
{
foreach (TrsArray trs in morph.trsArray)
if (morph.LastWeight != morph.Weight || morph.Weight > 0)
{
var bone = Objs.Find(ani => ani.name.Equals(trs._path));
if (bone)
foreach (TrsArray trs in morph.trsArray)
{
if (trs.IsOverrideTarget)
if (trs.transform)
{
bone.localRotation = Quaternion.Euler(Vector3.Lerp(bone.localRotation.eulerAngles, trs._rotation, morph.Weight));
bone.localPosition = Vector3.Lerp(bone.localPosition, trs._position, morph.Weight);
bone.localScale = Vector3.Lerp(bone.localScale, trs._scale, morph.Weight);
}
};
if (trs.IsOverrideTarget)
{
trs.transform.localRotation = Quaternion.Euler(Vector3.Lerp(trs.transform.localRotation.eulerAngles, trs._rotation, morph.Weight));
trs.transform.localPosition = Vector3.Lerp(trs.transform.localPosition, trs._position, morph.Weight);
trs.transform.localScale = Vector3.Lerp(trs.transform.localScale, trs._scale, morph.Weight);
}
};
}
morph.LastWeight = morph.Weight;
}
}
foreach (FacialMorph morph in EyeMorphs)
{
foreach (TrsArray trs in morph.trsArray)
if (morph.LastWeight != morph.Weight || morph.Weight > 0)
{
var bone = Objs.Find(ani => ani.name.Equals(trs._path));
if (bone)
foreach (TrsArray trs in morph.trsArray)
{
if (trs.IsOverrideTarget)
if (trs.transform)
{
bone.localRotation = Quaternion.Euler(Vector3.Lerp(bone.localRotation.eulerAngles, trs._rotation, morph.Weight));
bone.localPosition = Vector3.Lerp(bone.localPosition, trs._position, morph.Weight);
bone.localScale = Vector3.Lerp(bone.localScale, trs._scale, morph.Weight);
}
};
if (trs.IsOverrideTarget)
{
trs.transform.localRotation = Quaternion.Euler(Vector3.Lerp(trs.transform.localRotation.eulerAngles, trs._rotation, morph.Weight));
trs.transform.localPosition = Vector3.Lerp(trs.transform.localPosition, trs._position, morph.Weight);
trs.transform.localScale = Vector3.Lerp(trs.transform.localScale, trs._scale, morph.Weight);
}
};
}
morph.LastWeight = morph.Weight;
}
}
foreach (FacialMorph morph in MouthMorphs)
{
foreach (TrsArray trs in morph.trsArray)
if (morph.LastWeight != morph.Weight || morph.Weight > 0)
{
var bone = Objs.Find(ani => ani.name.Equals(trs._path));
if (bone)
foreach (TrsArray trs in morph.trsArray)
{
if (trs.IsOverrideTarget)
if (trs.transform)
{
bone.localRotation = Quaternion.Euler(Vector3.Lerp(bone.localRotation.eulerAngles, trs._rotation, morph.Weight));
bone.localPosition = Vector3.Lerp(bone.localPosition, trs._position, morph.Weight);
bone.localScale = Vector3.Lerp(bone.localScale, trs._scale, morph.Weight);
}
};
if (trs.IsOverrideTarget)
{
trs.transform.localRotation = Quaternion.Euler(Vector3.Lerp(trs.transform.localRotation.eulerAngles, trs._rotation, morph.Weight));
trs.transform.localPosition = Vector3.Lerp(trs.transform.localPosition, trs._position, morph.Weight);
trs.transform.localScale = Vector3.Lerp(trs.transform.localScale, trs._scale, morph.Weight);
}
};
}
morph.LastWeight = morph.Weight;
}
}
}
@@ -219,6 +244,8 @@ namespace Gallop
Objs[i].localScale = OriScal[i];
}
}
}
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Gallop
{
public interface FaceLoadCallBack
{
public abstract void CallBack(FaceDrivenKeyTarget target);
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: f45bcbff6c596104888b02292eba909a
guid: d7b0dd94109f6b445bd06165e34aea69
MonoImporter:
externalObjects: {}
serializedVersion: 2
+1
View File
@@ -16,6 +16,7 @@ namespace Gallop
public float Weight;
[HideInInspector]public float LastWeight;
[HideInInspector] public List<TrsArray> trsArray;
public void SetFace(FaceDrivenKeyTarget faceTarget)
{
face = faceTarget;
+1
View File
@@ -34,6 +34,7 @@ public class StringValuePair
public class TrsArray
{
public string _path;
public Transform transform;
public bool _isValidScaleTransform;
public Vector3 _position;
public Vector3 _scale;