modular-avatar/Runtime/ModularAvatarBlendshapeSync.cs

140 lines
4.4 KiB
C#
Raw Normal View History

2022-10-20 10:42:33 +08:00
using System;
using System.Collections.Generic;
using UnityEngine;
2022-11-11 12:39:58 +08:00
namespace nadena.dev.modular_avatar.core
2022-10-20 10:42:33 +08:00
{
[Serializable]
public struct BlendshapeBinding
{
public AvatarObjectReference ReferenceMesh;
public string Blendshape;
public string LocalBlendshape;
2022-10-20 10:42:33 +08:00
public bool Equals(BlendshapeBinding other)
{
return Equals(ReferenceMesh, other.ReferenceMesh) && Blendshape == other.Blendshape;
}
public override bool Equals(object obj)
{
return obj is BlendshapeBinding other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
return ((ReferenceMesh != null ? ReferenceMesh.GetHashCode() : 0) * 397) ^
(Blendshape != null ? Blendshape.GetHashCode() : 0);
}
}
}
[RequireComponent(typeof(SkinnedMeshRenderer))]
[DisallowMultipleComponent]
[ExecuteAlways]
2022-11-10 09:49:00 +08:00
[AddComponentMenu("Modular Avatar/MA Blendshape Sync")]
[HelpURL("https://modular-avatar.nadena.dev/docs/reference/blendshape-sync?lang=auto")]
public class ModularAvatarBlendshapeSync : AvatarTagComponent, IHaveObjReferences
2022-10-20 10:42:33 +08:00
{
public List<BlendshapeBinding> Bindings = new List<BlendshapeBinding>();
struct EditorBlendshapeBinding
{
public SkinnedMeshRenderer TargetMesh;
public int RemoteBlendshapeIndex;
public int LocalBlendshapeIndex;
}
private List<EditorBlendshapeBinding> _editorBindings;
2023-01-19 20:32:44 +08:00
protected override void OnValidate()
2022-10-20 10:42:33 +08:00
{
2023-01-19 20:32:44 +08:00
base.OnValidate();
2022-10-20 11:44:46 +08:00
if (RuntimeUtil.isPlaying) return;
2022-10-20 10:42:33 +08:00
RuntimeUtil.delayCall(Rebind);
RuntimeUtil.OnHierarchyChanged -= Rebind;
RuntimeUtil.OnHierarchyChanged += Rebind;
}
2023-07-30 00:15:16 +08:00
protected override void OnDestroy()
2022-10-20 10:42:33 +08:00
{
2023-07-30 00:15:16 +08:00
base.OnDestroy();
2022-10-20 10:42:33 +08:00
RuntimeUtil.OnHierarchyChanged -= Rebind;
}
public override void ResolveReferences()
2023-08-05 14:47:03 +08:00
{
// no-op
}
2022-10-20 10:42:33 +08:00
private void Rebind()
{
#if UNITY_EDITOR
2022-10-20 11:44:46 +08:00
if (this == null) return;
if (UnityEditor.PrefabUtility.IsPartOfPrefabAsset(this)) return;
2022-10-20 11:44:46 +08:00
2022-10-20 10:42:33 +08:00
_editorBindings = new List<EditorBlendshapeBinding>();
var localRenderer = GetComponent<SkinnedMeshRenderer>();
var localMesh = localRenderer.sharedMesh;
if (localMesh == null)
return;
foreach (var binding in Bindings)
{
var obj = binding.ReferenceMesh.Get(this);
if (obj == null)
continue;
var smr = obj.GetComponent<SkinnedMeshRenderer>();
if (smr == null)
continue;
var mesh = smr.sharedMesh;
if (mesh == null)
continue;
var localShape = string.IsNullOrWhiteSpace(binding.LocalBlendshape)
? binding.Blendshape
: binding.LocalBlendshape;
var localIndex = localMesh.GetBlendShapeIndex(localShape);
2022-10-20 10:42:33 +08:00
var refIndex = mesh.GetBlendShapeIndex(binding.Blendshape);
if (localIndex == -1 || refIndex == -1)
continue;
_editorBindings.Add(new EditorBlendshapeBinding()
{
TargetMesh = smr,
RemoteBlendshapeIndex = refIndex,
LocalBlendshapeIndex = localIndex
});
}
Update();
#endif
2022-10-20 10:42:33 +08:00
}
private void Update()
{
if (RuntimeUtil.isPlaying) return;
if (_editorBindings == null) return;
var localRenderer = GetComponent<SkinnedMeshRenderer>();
if (localRenderer == null) return;
foreach (var binding in _editorBindings)
{
if (binding.TargetMesh == null) return;
var weight = binding.TargetMesh.GetBlendShapeWeight(binding.RemoteBlendshapeIndex);
localRenderer.SetBlendShapeWeight(binding.LocalBlendshapeIndex, weight);
}
}
public IEnumerable<AvatarObjectReference> GetObjectReferences()
{
foreach (var binding in Bindings)
if (binding.ReferenceMesh != null)
yield return binding.ReferenceMesh;
}
2022-10-20 10:42:33 +08:00
}
}