ByNameLipSyncMapper.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. for (var i = 0; i < VisemeCandidates.Count; i++)
  29. {
  30. var visemeCandidate = VisemeCandidates[i];
  31. if (visemeCandidate.Viseme == viseme)
  32. {
  33. for (var k = 0; k < visemeCandidate.CandidateNames.Count; k++)
  34. {
  35. var candidateName = visemeCandidate.CandidateNames[k];
  36. for (var j = 0; j < geometryGroup.BlendShapeKeys.Count; j++)
  37. {
  38. var blendShapeGeometryBinding = geometryGroup.BlendShapeKeys[j];
  39. if (Utils.StringComparer.Matches(StringComparisonMode, CaseInsensitive, blendShapeGeometryBinding.Name, candidateName))
  40. {
  41. return j;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return -1;
  48. }
  49. }
  50. }