ByNameLipSyncMapper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Collections.Generic;
  2. using TriLibCore.General;
  3. using TriLibCore.Interfaces;
  4. using UnityEngine;
  5. namespace TriLibCore.Mappers
  6. {
  7. /// <summary>Represents a Mapper that search Visemes by searching Blend-Shape Keys names.</summary>
  8. [CreateAssetMenu(menuName = "TriLib/Mappers/LypSync/By Name Lip Sync Mapper", fileName = "ByNameLipSyncMapper")]
  9. public class ByNameLipSyncMapper : LipSyncMapper
  10. {
  11. /// <summary>
  12. /// String comparison mode to use on the mapping.
  13. /// </summary>
  14. [Header("Left = Blend-Shape Key Name, Right = Viseme Name")]
  15. public StringComparisonMode StringComparisonMode;
  16. /// <summary>
  17. /// Is the string comparison case insensitive?
  18. /// </summary>
  19. public bool CaseInsensitive = true;
  20. /// <summary>
  21. /// The viseme candidates.
  22. /// A viseme candidate is a reference between visemes and valid blend-shape names for the viseme.
  23. /// </summary>
  24. public List<VisemeCandidate> VisemeCandidates;
  25. /// <inheritdoc />
  26. protected override int MapViseme(AssetLoaderContext assetLoaderContext, LipSyncViseme viseme, IGeometryGroup geometryGroup)
  27. {
  28. foreach (var visemeCandidate in VisemeCandidates)
  29. {
  30. if (visemeCandidate.Viseme == viseme)
  31. {
  32. foreach (var candidateName in visemeCandidate.CandidateNames)
  33. {
  34. for (var i = 0; i < geometryGroup.BlendShapeGeometryBindings.Count; i++)
  35. {
  36. var blendShapeGeometryBinding = geometryGroup.BlendShapeGeometryBindings[i];
  37. if (Utils.StringComparer.Matches(StringComparisonMode, CaseInsensitive, blendShapeGeometryBinding.Name, candidateName))
  38. {
  39. return i;
  40. }
  41. }
  42. }
  43. }
  44. }
  45. return -1;
  46. }
  47. }
  48. }