mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-07 03:17:25 -05:00
update export sample
This commit is contained in:
@@ -17,16 +17,23 @@ namespace VRM.RuntimeExporterSample
|
||||
{
|
||||
if (GUILayout.Button("Load"))
|
||||
{
|
||||
OnLoadClicked();
|
||||
Load();
|
||||
}
|
||||
|
||||
GUI.enabled = m_model != null;
|
||||
|
||||
if (GUILayout.Button("Add custom blend shape"))
|
||||
{
|
||||
AddBlendShapeClip(m_model);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Export"))
|
||||
{
|
||||
OnExportClicked();
|
||||
Export(m_model, UseNormalize);
|
||||
}
|
||||
}
|
||||
|
||||
async void OnLoadClicked()
|
||||
async void Load()
|
||||
{
|
||||
#if UNITY_STANDALONE_WIN
|
||||
var path = FileDialogForWindows.FileDialog("open VRM", ".vrm");
|
||||
@@ -41,24 +48,71 @@ namespace VRM.RuntimeExporterSample
|
||||
}
|
||||
|
||||
var loaded = await VrmUtility.LoadAsync(path);
|
||||
|
||||
loaded.ShowMeshes();
|
||||
loaded.EnableUpdateWhenOffscreen();
|
||||
OnLoaded(loaded.gameObject);
|
||||
}
|
||||
|
||||
void OnLoaded(GameObject go)
|
||||
{
|
||||
if (m_model != null)
|
||||
{
|
||||
GameObject.Destroy(m_model.gameObject);
|
||||
}
|
||||
|
||||
m_model = go;
|
||||
m_model.transform.rotation = Quaternion.Euler(0, 180, 0);
|
||||
m_model = loaded.gameObject;
|
||||
}
|
||||
|
||||
void OnExportClicked()
|
||||
static void AddBlendShapeClip(GameObject go)
|
||||
{
|
||||
// get or create blendshape proxy
|
||||
var proxy = go.GetComponent<VRMBlendShapeProxy>();
|
||||
if (proxy == null)
|
||||
{
|
||||
proxy = go.AddComponent<VRMBlendShapeProxy>();
|
||||
}
|
||||
|
||||
// get or create blendshapeavatar
|
||||
var avatar = proxy.BlendShapeAvatar;
|
||||
if (avatar == null)
|
||||
{
|
||||
avatar = ScriptableObject.CreateInstance<BlendShapeAvatar>();
|
||||
proxy.BlendShapeAvatar = avatar;
|
||||
}
|
||||
|
||||
// add blendshape clip to avatar.Clips
|
||||
var clip = ScriptableObject.CreateInstance<BlendShapeClip>();
|
||||
var name = $"custom#{avatar.Clips.Count}";
|
||||
Debug.Log($"Add {name}");
|
||||
// unity asset name
|
||||
clip.name = name;
|
||||
// vrm export name
|
||||
clip.BlendShapeName = name;
|
||||
clip.Preset = BlendShapePreset.Unknown;
|
||||
|
||||
clip.IsBinary = false;
|
||||
clip.Values = new BlendShapeBinding[]
|
||||
{
|
||||
new BlendShapeBinding
|
||||
{
|
||||
RelativePath = "mesh/face", // target Renderer relative path from root
|
||||
Index = 0, // BlendShapeIndex in SkinnedMeshRenderer
|
||||
Weight = 75f // BlendShape weight, range is [0-100]
|
||||
},
|
||||
};
|
||||
clip.MaterialValues = new MaterialValueBinding[]
|
||||
{
|
||||
new MaterialValueBinding
|
||||
{
|
||||
MaterialName = "Alicia_body", // target_material_name
|
||||
ValueName = "_Color", // target_material_property_name,
|
||||
BaseValue = new Vector4(1, 1, 1, 1), // Target value when the Weight value of BlendShapeClip is 0
|
||||
TargetValue = new Vector4(0, 0, 0, 1), // Target value when the Weight value of BlendShapeClip is 1
|
||||
},
|
||||
};
|
||||
avatar.Clips.Add(clip);
|
||||
|
||||
// done
|
||||
}
|
||||
|
||||
|
||||
static void Export(GameObject model, bool useNormalize)
|
||||
{
|
||||
//#if UNITY_STANDALONE_WIN
|
||||
#if false
|
||||
@@ -71,7 +125,7 @@ namespace VRM.RuntimeExporterSample
|
||||
return;
|
||||
}
|
||||
|
||||
var bytes = UseNormalize ? ExportCustom(m_model) : ExportSimple(m_model);
|
||||
var bytes = useNormalize ? ExportCustom(model) : ExportSimple(model);
|
||||
|
||||
File.WriteAllBytes(path, bytes);
|
||||
Debug.LogFormat("export to {0}", path);
|
||||
|
||||
Reference in New Issue
Block a user