Skip to content

Commit

Permalink
ExpressionAvatar は 独立した ScriptableObject をやめる (#916)
Browse files Browse the repository at this point in the history
* ExpressionAvatar is MonoBehaviour

* fix controller editor
  • Loading branch information
ousttrue authored Apr 27, 2021
1 parent 06559cf commit 22871e9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 79 deletions.
23 changes: 8 additions & 15 deletions Assets/VRM10/Editor/Components/VRM10ControllerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ public KeyValuePair<ExpressionKey, float> Slider()
void OnEnable()
{
m_target = (VRM10Controller)target;
if (m_target.Expression.ExpressionAvatar != null && m_target.Expression.ExpressionAvatar.Clips != null)
{
m_expressionKeyWeights = m_target.Expression.ExpressionAvatar.Clips.ToDictionary(x => ExpressionKey.CreateFromClip(x), x => 0.0f);
m_sliders = m_target.Expression.ExpressionAvatar.Clips
.Where(x => x != null)
.Select(x => new ExpressionSlider(m_expressionKeyWeights, ExpressionKey.CreateFromClip(x)))
.ToList()
;
}

m_expressionKeyWeights = m_target.Expression.Clips.ToDictionary(x => ExpressionKey.CreateFromClip(x), x => 0.0f);
m_sliders = m_target.Expression.Clips
.Where(x => x != null)
.Select(x => new ExpressionSlider(m_expressionKeyWeights, ExpressionKey.CreateFromClip(x)))
.ToList()
;

if (m_target?.Meta.Meta != null)
{
Expand Down Expand Up @@ -150,7 +148,7 @@ public override void OnInspectorGUI()
break;

case Tabs.Expression:
m_expression.RecursiveProperty();
// m_expression.RecursiveProperty();
ExpressionGUI();
break;

Expand All @@ -175,11 +173,6 @@ void ExpressionGUI()
EditorGUILayout.HelpBox("Enable when playing", MessageType.Info);
}

if (m_target.Expression.ExpressionAvatar == null)
{
return;
}

if (m_sliders != null)
{
EditorGUILayout.Space();
Expand Down
3 changes: 0 additions & 3 deletions Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public static void OnGUI(ScriptedImporter importer, GltfParser parser, UniGLTF.E
// meta
importer.DrawRemapGUI<VRM10MetaObject>(new SubAssetKey[] { VRM10MetaObject.SubAssetKey });

// expression avatar
importer.DrawRemapGUI<VRM10ExpressionAvatar>(new SubAssetKey[] { VRM10ExpressionAvatar.SubAssetKey });

// expressions
importer.DrawRemapGUI<VRM10Expression>(vrm.Expressions.Select(x => CreateKey(x).SubAssetKey));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@

namespace UniVRM10
{
[CreateAssetMenu(menuName = "VRM10/ExpressionAvatar")]
public sealed class VRM10ExpressionAvatar : ScriptableObject
public sealed class VRM10ExpressionAvatar : MonoBehaviour
{
public static UniGLTF.SubAssetKey SubAssetKey => new UniGLTF.SubAssetKey(typeof(VRM10ExpressionAvatar), "ExpressionAvatar");

[SerializeField]
public List<VRM10Expression> Clips = new List<VRM10Expression>();

Expand Down
25 changes: 18 additions & 7 deletions Assets/VRM10/Runtime/Components/VRM10ControllerExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ public sealed class VRM10ControllerExpression
{
public static IExpressionValidatorFactory ExpressionValidatorFactory = new DefaultExpressionValidator.Factory();

[SerializeField]
public VRM10ExpressionAvatar ExpressionAvatar;
VRM10ExpressionAvatar m_expressionAvatar;
public IEnumerable<VRM10Expression> Clips
{
get
{
if (m_expressionAvatar == null || m_expressionAvatar.Clips == null)
{
return Enumerable.Empty<VRM10Expression>();
}
return m_expressionAvatar.Clips;
}
}

private List<ExpressionKey> _keys = new List<ExpressionKey>();
private Dictionary<ExpressionKey, float> _inputWeights = new Dictionary<ExpressionKey, float>();
Expand All @@ -35,21 +45,22 @@ public sealed class VRM10ControllerExpression

internal void Setup(Transform transform, ILookAtEyeDirectionProvider eyeDirectionProvider, ILookAtEyeDirectionApplicable eyeDirectionApplicable)
{
if (ExpressionAvatar == null)
m_expressionAvatar = transform.GetComponent<VRM10ExpressionAvatar>();
if (m_expressionAvatar == null)
{
#if VRM_DEVELOP
if (m_debugCount++ == 0)
{
Debug.LogWarning($"{nameof(VRM10ControllerExpression)}.{nameof(ExpressionAvatar)} is null.");
Debug.LogWarning($"{nameof(VRM10ControllerExpression)}.{nameof(m_expressionAvatar)} is null.");
}
#endif
return;
}

Restore();

_merger = new ExpressionMerger(ExpressionAvatar.Clips, transform);
_keys = ExpressionAvatar.Clips.Select(ExpressionKey.CreateFromClip).ToList();
_merger = new ExpressionMerger(m_expressionAvatar.Clips, transform);
_keys = m_expressionAvatar.Clips.Select(ExpressionKey.CreateFromClip).ToList();
var oldInputWeights = _inputWeights;
_inputWeights = _keys.ToDictionary(x => x, x => 0f);
foreach (var key in _keys)
Expand All @@ -58,7 +69,7 @@ internal void Setup(Transform transform, ILookAtEyeDirectionProvider eyeDirectio
if (oldInputWeights.ContainsKey(key)) _inputWeights[key] = oldInputWeights[key];
}
_actualWeights = _keys.ToDictionary(x => x, x => 0f);
_validator = ExpressionValidatorFactory.Create(ExpressionAvatar);
_validator = ExpressionValidatorFactory.Create(m_expressionAvatar);
_eyeDirectionProvider = eyeDirectionProvider;
_eyeDirectionApplicable = eyeDirectionApplicable;
}
Expand Down
5 changes: 3 additions & 2 deletions Assets/VRM10/Runtime/IO/Vrm10Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ UniGLTF.Extensions.VRMC_vrm.TextureTransformBind ExportTextureTransformBinding(M

void ExportExpression(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, VRM10Controller vrmController, Model model, ModelExporter converter)
{
if (vrmController?.Expression?.ExpressionAvatar?.Clips == null)
var expressionAvatar = vrmController.GetComponent<VRM10ExpressionAvatar>();
if (expressionAvatar?.Clips == null)
{
return;
}
Expand All @@ -567,7 +568,7 @@ void ExportExpression(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, VRM10Controller
};

vrm.Expressions = new List<UniGLTF.Extensions.VRMC_vrm.Expression>();
foreach (var e in vrmController.Expression.ExpressionAvatar.Clips)
foreach (var e in expressionAvatar.Clips)
{
var vrmExpression = new UniGLTF.Extensions.VRMC_vrm.Expression
{
Expand Down
88 changes: 40 additions & 48 deletions Assets/VRM10/Runtime/IO/Vrm10Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected override async Task LoadGeometryAsync(IAwaitCaller awaitCaller, Func<s

UnityEngine.Avatar m_humanoid;
VRM10MetaObject m_meta;
VRM10ExpressionAvatar m_exressionAvatar;
List<VRM10Expression> m_expressions = new List<VRM10Expression>();

protected override async Task OnLoadHierarchy(IAwaitCaller awaitCaller, Func<string, IDisposable> MeasureTime)
{
Expand Down Expand Up @@ -291,40 +291,42 @@ async Task LoadVrmAsync(IAwaitCaller awaitCaller, VRM10Controller controller, Un
}

// expression
if (m_externalMap.TryGetValue(VRM10ExpressionAvatar.SubAssetKey, out UnityEngine.Object expressionAvatar))
if (vrm.Expressions != null)
{
controller.Expression.ExpressionAvatar = expressionAvatar as VRM10ExpressionAvatar;
}
else if (vrm.Expressions != null)
{
controller.Expression.ExpressionAvatar = ScriptableObject.CreateInstance<VRM10ExpressionAvatar>();

m_exressionAvatar = controller.Expression.ExpressionAvatar;
m_exressionAvatar.name = VRM10ExpressionAvatar.SubAssetKey.Name;
var expressionAvatar = Root.AddComponent<VRM10ExpressionAvatar>();

foreach (var expression in vrm.Expressions)
{
var clip = ScriptableObject.CreateInstance<UniVRM10.VRM10Expression>();
clip.Preset = expression.Preset;
clip.ExpressionName = expression.Name;
clip.name = Key(expression).SubAssetKey.Name;
clip.IsBinary = expression.IsBinary.GetValueOrDefault();
clip.OverrideBlink = expression.OverrideBlink;
clip.OverrideLookAt = expression.OverrideLookAt;
clip.OverrideMouth = expression.OverrideMouth;

clip.MorphTargetBindings = expression.MorphTargetBinds.Select(x => x.Build10(Root, m_map, m_model))
.ToArray();
clip.MaterialColorBindings = expression.MaterialColorBinds.Select(x => x.Build10(MaterialFactory.Materials))
.Where(x => x.HasValue)
.Select(x => x.Value)
.ToArray();
clip.MaterialUVBindings = expression.TextureTransformBinds.Select(x => x.Build10(MaterialFactory.Materials))
.Where(x => x.HasValue)
.Select(x => x.Value)
.ToArray();

m_exressionAvatar.Clips.Add(clip);
VRM10Expression clip = default;
if (m_externalMap.TryGetValue(Key(expression).SubAssetKey, out UnityEngine.Object expressionObj))
{
clip = expressionObj as VRM10Expression;
}
else
{
clip = ScriptableObject.CreateInstance<UniVRM10.VRM10Expression>();
clip.Preset = expression.Preset;
clip.ExpressionName = expression.Name;
clip.name = Key(expression).SubAssetKey.Name;
clip.IsBinary = expression.IsBinary.GetValueOrDefault();
clip.OverrideBlink = expression.OverrideBlink;
clip.OverrideLookAt = expression.OverrideLookAt;
clip.OverrideMouth = expression.OverrideMouth;

clip.MorphTargetBindings = expression.MorphTargetBinds.Select(x => x.Build10(Root, m_map, m_model))
.ToArray();
clip.MaterialColorBindings = expression.MaterialColorBinds.Select(x => x.Build10(MaterialFactory.Materials))
.Where(x => x.HasValue)
.Select(x => x.Value)
.ToArray();
clip.MaterialUVBindings = expression.TextureTransformBinds.Select(x => x.Build10(MaterialFactory.Materials))
.Where(x => x.HasValue)
.Select(x => x.Value)
.ToArray();
m_expressions.Add(clip);
}

expressionAvatar.Clips.Add(clip);
}
}

Expand Down Expand Up @@ -606,21 +608,14 @@ public override void TransferOwnership(Func<UnityEngine.Object, bool> take)
}
}

if (m_exressionAvatar != null && m_exressionAvatar.Clips != null)
foreach (var x in m_expressions)
{
foreach (var x in m_exressionAvatar.Clips)
if (take(x))
{
if (take(x))
{
// do nothing
}
}

if (take(m_exressionAvatar))
{
m_exressionAvatar = null;
// do nothing
}
}
m_expressions.Clear();

// GLTF のリソース
base.TransferOwnership(take);
Expand All @@ -639,13 +634,10 @@ public override void Dispose()
{
destroy(m_meta);
}
if (m_exressionAvatar != null)

foreach (var clip in m_expressions)
{
foreach (var clip in m_exressionAvatar.Clips)
{
destroy(clip);
}
destroy(m_exressionAvatar);
destroy(clip);
}

base.Dispose();
Expand Down

0 comments on commit 22871e9

Please sign in to comment.