From ab4f6231b1d0444cfd28cbdbca92b390ea5837c9 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 18 May 2022 18:24:34 +0900 Subject: [PATCH] update export sample --- .../VRMRuntimeExporter.cs | 78 ++++++++++++++++--- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/Assets/VRM_Samples/RuntimeExporterSample/VRMRuntimeExporter.cs b/Assets/VRM_Samples/RuntimeExporterSample/VRMRuntimeExporter.cs index 29789ab14..3d396b37b 100644 --- a/Assets/VRM_Samples/RuntimeExporterSample/VRMRuntimeExporter.cs +++ b/Assets/VRM_Samples/RuntimeExporterSample/VRMRuntimeExporter.cs @@ -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(); + if (proxy == null) + { + proxy = go.AddComponent(); + } + + // get or create blendshapeavatar + var avatar = proxy.BlendShapeAvatar; + if (avatar == null) + { + avatar = ScriptableObject.CreateInstance(); + proxy.BlendShapeAvatar = avatar; + } + + // add blendshape clip to avatar.Clips + var clip = ScriptableObject.CreateInstance(); + 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);