CubismMaskTransform.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 UnityEngine;
  8. namespace Live2D.Cubism.Rendering.Masking
  9. {
  10. /// <summary>
  11. /// Holds info used for masking.
  12. /// </summary>
  13. public struct CubismMaskTransform
  14. {
  15. #region Conversion
  16. /// <summary>
  17. /// <see cref="UniqueId"/> backing field.
  18. /// </summary>
  19. private static int _uniqueId;
  20. /// <summary>
  21. /// HACK Prevents dynamic batching of <see cref="CubismRenderer"/>s that are masked.
  22. /// </summary>
  23. /// <remarks>
  24. /// As Unity transforms vertex positions into world space on dynamic batching, and masking relies on vertex positions to be in local space,
  25. /// masking isn't compatible with dynamic batching.
  26. ///
  27. /// Unity exposes a shader tag for disabling dynamic batching ("DynamicBatching"), but this would make it necessary for creating separate shaders...
  28. /// </remarks>
  29. private static int UniqueId
  30. {
  31. get
  32. {
  33. // We just have to make sure consecutive drawables with the same mask aren't batched; having more than 1024 cases in a row seems pretty rare, so...
  34. if (_uniqueId > 1024)
  35. {
  36. _uniqueId = 0;
  37. }
  38. return (++_uniqueId);
  39. }
  40. }
  41. /// <summary>
  42. /// Converts a <see cref="CubismMaskTile"/> to a <see cref="Vector4"/>.
  43. /// </summary>
  44. /// <param name="value">Value to convert.</param>
  45. public static implicit operator Vector4(CubismMaskTransform value)
  46. {
  47. return new Vector4
  48. {
  49. x = value.Offset.x,
  50. y = value.Offset.y,
  51. z = value.Scale,
  52. w = UniqueId
  53. };
  54. }
  55. #endregion
  56. /// <summary>
  57. /// Offset in model space.
  58. /// </summary>
  59. public Vector2 Offset;
  60. /// <summary>
  61. /// Scale in model space.
  62. /// </summary>
  63. public float Scale;
  64. }
  65. }