mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-24 23:18:04 -05:00
replace implementation of MorphTargetBindingMerger
This commit is contained in:
parent
adc22ed416
commit
53416516de
|
|
@ -1,8 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using VrmLib;
|
||||
|
||||
|
||||
namespace UniVRM10
|
||||
{
|
||||
|
|
@ -63,7 +61,7 @@ namespace UniVRM10
|
|||
return;
|
||||
}
|
||||
|
||||
m_morphTargetBindingMerger.AccumulateValue(clip, value);
|
||||
m_morphTargetBindingMerger.AccumulateValue(key, value);
|
||||
m_materialValueBindingMerger.AccumulateValue(clip, value);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,115 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniVRM10
|
||||
{
|
||||
///
|
||||
/// A.Value * A.Weight + B.Value * B.Weight ...
|
||||
///
|
||||
internal sealed class MorphTargetBindingMerger
|
||||
{
|
||||
class DictionaryKeyMorphTargetBindingComparer : IEqualityComparer<MorphTargetBinding>
|
||||
{
|
||||
public bool Equals(MorphTargetBinding x, MorphTargetBinding y)
|
||||
{
|
||||
return x.RelativePath == y.RelativePath
|
||||
&& x.Index == y.Index;
|
||||
}
|
||||
|
||||
public int GetHashCode(MorphTargetBinding obj)
|
||||
{
|
||||
return obj.RelativePath.GetHashCode() + obj.Index;
|
||||
}
|
||||
}
|
||||
|
||||
private static DictionaryKeyMorphTargetBindingComparer comparer = new DictionaryKeyMorphTargetBindingComparer();
|
||||
|
||||
/// <summary>
|
||||
/// MorphTargetBinding の適用値を蓄積する
|
||||
/// </summary>
|
||||
/// <typeparam name="MorphTargetBinding"></typeparam>
|
||||
/// <typeparam name="float"></typeparam>
|
||||
/// <returns></returns>
|
||||
Dictionary<MorphTargetBinding, float> m_morphTargetValueMap = new Dictionary<MorphTargetBinding, float>(comparer);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Dictionary<MorphTargetBinding, Action<float>> m_morphTargetSetterMap = new Dictionary<MorphTargetBinding, Action<float>>(comparer);
|
||||
|
||||
public MorphTargetBindingMerger(Dictionary<ExpressionKey, VRM10Expression> clipMap, Transform root)
|
||||
{
|
||||
foreach (var kv in clipMap)
|
||||
{
|
||||
foreach (var binding in kv.Value.MorphTargetBindings)
|
||||
{
|
||||
if (!m_morphTargetSetterMap.ContainsKey(binding))
|
||||
{
|
||||
var _target = root.Find(binding.RelativePath);
|
||||
SkinnedMeshRenderer target = null;
|
||||
if (_target != null)
|
||||
{
|
||||
target = _target.GetComponent<SkinnedMeshRenderer>();
|
||||
}
|
||||
if (target != null)
|
||||
{
|
||||
if (binding.Index >= 0 && binding.Index < target.sharedMesh.blendShapeCount)
|
||||
{
|
||||
m_morphTargetSetterMap.Add(binding, x =>
|
||||
{
|
||||
if (target == null)
|
||||
{
|
||||
// recompile in editor ?
|
||||
return;
|
||||
}
|
||||
// VRM-1.0 weight is 0-1
|
||||
target.SetBlendShapeWeight(binding.Index, x * MorphTargetBinding.VRM_TO_UNITY);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarningFormat("Invalid morphTarget binding: {0}: {1}", target.name, binding.Index);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarningFormat("SkinnedMeshRenderer: {0} not found", binding.RelativePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AccumulateValue(VRM10Expression clip, float value)
|
||||
{
|
||||
foreach (var binding in clip.MorphTargetBindings)
|
||||
{
|
||||
float acc;
|
||||
if (m_morphTargetValueMap.TryGetValue(binding, out acc))
|
||||
{
|
||||
m_morphTargetValueMap[binding] = acc + binding.Weight * value;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_morphTargetValueMap[binding] = binding.Weight * value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Apply()
|
||||
{
|
||||
foreach (var kv in m_morphTargetValueMap)
|
||||
{
|
||||
Action<float> setter;
|
||||
if (m_morphTargetSetterMap.TryGetValue(kv.Key, out setter))
|
||||
{
|
||||
setter(kv.Value);
|
||||
}
|
||||
}
|
||||
m_morphTargetValueMap.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b955b3371ff699842a355d0e1348a859
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -11,7 +11,7 @@ namespace UniVRM10
|
|||
/// Dictionary を使用すると、その GetEnumerator()(foreach) や get/set を 100,1000 オーダーで呼び出すことになる。
|
||||
/// するとモバイル環境ではかなりの定常負荷になってしまうため、その使用を避けて実装する。
|
||||
/// </summary>
|
||||
internal sealed class MorphTargetBindingMerger2
|
||||
internal sealed class MorphTargetBindingMerger
|
||||
{
|
||||
private readonly ExpressionKey[] _keyOrder;
|
||||
private readonly Dictionary<ExpressionKey, int> _keyIndexReverseMap = new(ExpressionKey.Comparer);
|
||||
|
|
@ -27,7 +27,7 @@ namespace UniVRM10
|
|||
/// </summary>
|
||||
private readonly float?[] _accumulatedWeights;
|
||||
|
||||
public MorphTargetBindingMerger2(Dictionary<ExpressionKey, VRM10Expression> expressions, Transform modelRoot)
|
||||
public MorphTargetBindingMerger(Dictionary<ExpressionKey, VRM10Expression> expressions, Transform modelRoot)
|
||||
{
|
||||
_keyOrder = expressions.Keys.ToArray();
|
||||
for (var keyIdx = 0; keyIdx < _keyOrder.Length; ++keyIdx)
|
||||
Loading…
Reference in New Issue
Block a user