ParticleColorParameter.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Runtime.CompilerServices;
  7. using Microsoft.Xna.Framework;
  8. namespace MonoGame.Extended.Particles.Data;
  9. /// <summary>
  10. /// Represents a color parameter for particle properties that can be either a constant color value
  11. /// or a randomly generated color within a specified range.
  12. /// </summary>
  13. /// <remarks>
  14. /// This struct uses <see cref="Vector3"/> to represent color values, the HSL color space.
  15. /// </remarks>
  16. public struct ParticleColorParameter : IEquatable<ParticleColorParameter>
  17. {
  18. /// <summary>
  19. /// The <see cref="ParticleValueKind"/> that determines whether this parameter uses a constant value or a randomly
  20. /// generated value.
  21. /// </summary>
  22. public ParticleValueKind Kind;
  23. /// <summary>
  24. /// The constant color value when <see cref="Kind"/> is set to <see cref="ParticleValueKind.Constant"/>.
  25. /// </summary>
  26. public Vector3 Constant;
  27. /// <summary>
  28. /// The minimum color values of the range when <see cref="Kind"/> is set to <see cref="ParticleValueKind.Random"/>.
  29. /// </summary>
  30. public Vector3 RandomMin;
  31. /// <summary>
  32. /// The maximum color values of the range when <see cref="Kind"/> is set to <see cref="ParticleValueKind.Random"/>.
  33. /// </summary>
  34. public Vector3 RandomMax;
  35. /// <summary>
  36. /// Gets the current color value of this parameter based on its <see cref="Kind"/>.
  37. /// </summary>
  38. /// <remarks>
  39. /// If <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/>, returns <see cref="Constant"/>.
  40. /// If <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>, returns a random color where each component
  41. /// is a random value between the corresponding components of <see cref="RandomMin"/> and <see cref="RandomMax"/>.
  42. /// The vector components represent HSL.
  43. /// </remarks>
  44. public Vector3 Value
  45. {
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. get
  48. {
  49. if (Kind == ParticleValueKind.Constant)
  50. {
  51. return Constant;
  52. }
  53. else
  54. {
  55. Vector3 hsl;
  56. hsl.X = FastRandom.Shared.NextSingle(RandomMin.X, RandomMax.X);
  57. hsl.Y = FastRandom.Shared.NextSingle(RandomMin.Y, RandomMax.Y);
  58. hsl.Z = FastRandom.Shared.NextSingle(RandomMin.Z, RandomMax.Z);
  59. return hsl;
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="ParticleColorParameter"/> struct with a constant color value.
  65. /// </summary>
  66. /// <param name="value">The constant color value for this parameter.</param>
  67. public ParticleColorParameter(Vector3 value)
  68. {
  69. Kind = ParticleValueKind.Constant;
  70. Constant = value;
  71. RandomMin = default;
  72. RandomMax = default;
  73. }
  74. /// <summary>
  75. /// Initializes a new instance of the <see cref="ParticleColorParameter"/> struct with a random color range.
  76. /// </summary>
  77. /// <param name="rangeStart">The minimum color values of the random range.</param>
  78. /// <param name="rangeEnd">The maximum color values of the random range.</param>
  79. public ParticleColorParameter(Vector3 rangeStart, Vector3 rangeEnd)
  80. {
  81. Kind = ParticleValueKind.Random;
  82. Constant = default;
  83. RandomMin = rangeStart;
  84. RandomMax = rangeEnd;
  85. }
  86. /// <summary>
  87. /// Determines whether the specified object is equal to the current parameter.
  88. /// </summary>
  89. /// <param name="obj">The object to compare with the current parameter.</param>
  90. /// <returns>
  91. /// <see langword="true"/> if the specified object is equal tot he current parameter;
  92. /// otherwise, <see langword="false"/>.
  93. /// </returns>
  94. public override readonly bool Equals([NotNullWhen(true)] object obj)
  95. {
  96. return obj is ParticleColorParameter other &&
  97. Equals(other);
  98. }
  99. /// <summary>
  100. /// Determines whether the specified parameter is equal to the current parameter.
  101. /// </summary>
  102. /// <param name="other">The parameter to compare with the current parameter.</param>
  103. /// <returns>
  104. /// <see langword="true"/> if the specified parameter is equal to the current parameter;
  105. /// otherwise, <see langword="false"/>.
  106. /// </returns>
  107. /// <remarks>
  108. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/> only the <see cref="Constant"/> values are
  109. /// compared.
  110. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>, both <see cref="RandomMin"/> and
  111. /// <see cref="RandomMax"/> values are compared.
  112. /// </remarks>
  113. public readonly bool Equals(ParticleColorParameter other)
  114. {
  115. if (Kind == ParticleValueKind.Constant)
  116. {
  117. return Constant.Equals(other.Constant);
  118. }
  119. return RandomMin.Equals(other.RandomMin) && RandomMax.Equals(other.RandomMax);
  120. }
  121. /// <summary>
  122. /// Returns the hash code for this parameter.
  123. /// </summary>
  124. /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
  125. /// <remarks>
  126. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/>, returns the hash of <see cref="Constant"/>.
  127. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/> returns the combined has of
  128. /// <see cref="RandomMin"/> and <see cref="RandomMax"/>.
  129. /// </remarks>
  130. public override readonly int GetHashCode()
  131. {
  132. if (Kind == ParticleValueKind.Constant)
  133. {
  134. return Constant.GetHashCode();
  135. }
  136. return HashCode.Combine(RandomMin, RandomMax);
  137. }
  138. /// <summary>
  139. /// Determines whether two parameters are equal.
  140. /// </summary>
  141. /// <param name="lhs">The first parameter to compare.</param>
  142. /// <param name="rhs">The second parameter to compare.</param>
  143. /// <returns>
  144. /// <see langword="true"/> if the parameters are equal; otherwise, <see langword="false"/>.
  145. /// </returns>
  146. public static bool operator ==(ParticleColorParameter lhs, ParticleColorParameter rhs)
  147. {
  148. return lhs.Equals(rhs);
  149. }
  150. /// <summary>
  151. /// Determines whether two parameters are not equal.
  152. /// </summary>
  153. /// <param name="lhs">The first parameter to compare.</param>
  154. /// <param name="rhs">The second parameter to compare.</param>
  155. /// <returns>
  156. /// <see langword="true"/> if the parameters are not equal; otherwise, <see langword="false"/>.
  157. /// </returns>
  158. public static bool operator !=(ParticleColorParameter lhs, ParticleColorParameter rhs)
  159. {
  160. return !lhs.Equals(rhs);
  161. }
  162. }