CubismLookParameter.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using Live2D.Cubism.Core;
  8. using UnityEngine;
  9. namespace Live2D.Cubism.Framework.LookAt
  10. {
  11. /// <summary>
  12. /// Look at parameter.
  13. /// </summary>
  14. public sealed class CubismLookParameter : MonoBehaviour
  15. {
  16. /// <summary>
  17. /// Look axis.
  18. /// </summary>
  19. [SerializeField]
  20. public CubismLookAxis Axis;
  21. /// <summary>
  22. /// Factor.
  23. /// </summary>
  24. [SerializeField]
  25. public float Factor;
  26. #region Unity Event Handling
  27. /// <summary>
  28. /// Called by Unity. Guesses best settings.
  29. /// </summary>
  30. private void Reset()
  31. {
  32. var parameter = GetComponent<CubismParameter>();
  33. // Fail silently.
  34. if (parameter == null)
  35. {
  36. return;
  37. }
  38. // Guess axis.
  39. if (parameter.name.EndsWith("Y"))
  40. {
  41. Axis = CubismLookAxis.Y;
  42. }
  43. else if (parameter.name.EndsWith("Z"))
  44. {
  45. Axis = CubismLookAxis.Z;
  46. }
  47. else
  48. {
  49. Axis = CubismLookAxis.X;
  50. }
  51. // Guess factor.
  52. Factor = parameter.MaximumValue;
  53. }
  54. #endregion
  55. #region Interface for Controller
  56. /// <summary>
  57. /// Updates and evaluates the instance.
  58. /// </summary>
  59. /// <param name="targetOffset">Delta to target.</param>
  60. /// <returns>Evaluation result.</returns>
  61. internal float TickAndEvaluate(Vector3 targetOffset)
  62. {
  63. var result = (Axis == CubismLookAxis.X)
  64. ? targetOffset.x
  65. : targetOffset.y;
  66. if (Axis == CubismLookAxis.Z)
  67. {
  68. result = targetOffset.z;
  69. }
  70. return result * Factor;
  71. }
  72. #endregion
  73. }
  74. }