Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SphericalHarmonics.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. namespace UnityEngine.Rendering
  3. {
  4. /// <summary>
  5. /// Structure holding Spherical Harmonic L1 coefficient.
  6. /// </summary>
  7. [Serializable]
  8. public struct SphericalHarmonicsL1
  9. {
  10. /// <summary>
  11. /// Red channel of each of the three L1 SH coefficient.
  12. /// </summary>
  13. public Vector4 shAr;
  14. /// <summary>
  15. /// Green channel of each of the three L1 SH coefficient.
  16. /// </summary>
  17. public Vector4 shAg;
  18. /// <summary>
  19. /// Blue channel of each of the three L1 SH coefficient.
  20. /// </summary>
  21. public Vector4 shAb;
  22. /// <summary>
  23. /// A set of L1 coefficients initialized to zero.
  24. /// </summary>
  25. public static readonly SphericalHarmonicsL1 zero = new SphericalHarmonicsL1
  26. {
  27. shAr = Vector4.zero,
  28. shAg = Vector4.zero,
  29. shAb = Vector4.zero
  30. };
  31. // These operators are implemented so that SphericalHarmonicsL1 matches API of SphericalHarmonicsL2.
  32. /// <summary>
  33. /// Sum two SphericalHarmonicsL1.
  34. /// </summary>
  35. /// <param name="lhs">First SphericalHarmonicsL1.</param>
  36. /// <param name="rhs">Second SphericalHarmonicsL1.</param>
  37. /// <returns>The resulting SphericalHarmonicsL1.</returns>
  38. public static SphericalHarmonicsL1 operator +(SphericalHarmonicsL1 lhs, SphericalHarmonicsL1 rhs) => new SphericalHarmonicsL1()
  39. {
  40. shAr = lhs.shAr + rhs.shAr,
  41. shAg = lhs.shAg + rhs.shAg,
  42. shAb = lhs.shAb + rhs.shAb
  43. };
  44. /// <summary>
  45. /// Subtract two SphericalHarmonicsL1.
  46. /// </summary>
  47. /// <param name="lhs">First SphericalHarmonicsL1.</param>
  48. /// <param name="rhs">Second SphericalHarmonicsL1.</param>
  49. /// <returns>The resulting SphericalHarmonicsL1.</returns>
  50. public static SphericalHarmonicsL1 operator -(SphericalHarmonicsL1 lhs, SphericalHarmonicsL1 rhs) => new SphericalHarmonicsL1()
  51. {
  52. shAr = lhs.shAr - rhs.shAr,
  53. shAg = lhs.shAg - rhs.shAg,
  54. shAb = lhs.shAb - rhs.shAb
  55. };
  56. /// <summary>
  57. /// Multiply two SphericalHarmonicsL1.
  58. /// </summary>
  59. /// <param name="lhs">First SphericalHarmonicsL1.</param>
  60. /// <param name="rhs">Second SphericalHarmonicsL1.</param>
  61. /// <returns>The resulting SphericalHarmonicsL1.</returns>
  62. public static SphericalHarmonicsL1 operator *(SphericalHarmonicsL1 lhs, float rhs) => new SphericalHarmonicsL1()
  63. {
  64. shAr = lhs.shAr * rhs,
  65. shAg = lhs.shAg * rhs,
  66. shAb = lhs.shAb * rhs
  67. };
  68. /// <summary>
  69. /// Divide two SphericalHarmonicsL1.
  70. /// </summary>
  71. /// <param name="lhs">First SphericalHarmonicsL1.</param>
  72. /// <param name="rhs">Second SphericalHarmonicsL1.</param>
  73. /// <returns>The resulting SphericalHarmonicsL1.</returns>
  74. public static SphericalHarmonicsL1 operator /(SphericalHarmonicsL1 lhs, float rhs) => new SphericalHarmonicsL1()
  75. {
  76. shAr = lhs.shAr / rhs,
  77. shAg = lhs.shAg / rhs,
  78. shAb = lhs.shAb / rhs
  79. };
  80. /// <summary>
  81. /// Compare two SphericalHarmonicsL1.
  82. /// </summary>
  83. /// <param name="lhs">First SphericalHarmonicsL1.</param>
  84. /// <param name="rhs">Second SphericalHarmonicsL1.</param>
  85. /// <returns>Whether the SphericalHarmonicsL1 match.</returns>
  86. public static bool operator ==(SphericalHarmonicsL1 lhs, SphericalHarmonicsL1 rhs)
  87. {
  88. return lhs.shAr == rhs.shAr
  89. && lhs.shAg == rhs.shAg
  90. && lhs.shAb == rhs.shAb;
  91. }
  92. /// <summary>
  93. /// Check two SphericalHarmonicsL1 inequality.
  94. /// </summary>
  95. /// <param name="lhs">First SphericalHarmonicsL1.</param>
  96. /// <param name="rhs">Second SphericalHarmonicsL1.</param>
  97. /// <returns>Whether the SphericalHarmonicsL1 are different.</returns>
  98. public static bool operator !=(SphericalHarmonicsL1 lhs, SphericalHarmonicsL1 rhs)
  99. {
  100. return !(lhs == rhs);
  101. }
  102. /// <summary>
  103. /// Compare this SphericalHarmonicsL1 with an object.
  104. /// </summary>
  105. /// <param name="other">The object to compare with.</param>
  106. /// <returns>Whether the SphericalHarmonicsL1 is equal to the object passed.</returns>
  107. public override bool Equals(object other)
  108. {
  109. if (!(other is SphericalHarmonicsL1)) return false;
  110. return this == (SphericalHarmonicsL1)other;
  111. }
  112. /// <summary>
  113. /// Produces an hash code of the SphericalHarmonicsL1.
  114. /// </summary>
  115. /// <returns>The hash code for this SphericalHarmonicsL1.</returns>
  116. public override int GetHashCode()
  117. {
  118. return ((17 * 23 + shAr.GetHashCode()) * 23 + shAg.GetHashCode()) * 23 + shAb.GetHashCode();
  119. }
  120. }
  121. /// <summary>
  122. /// A collection of utility functions used to access and set SphericalHarmonicsL2 in a more verbose way.
  123. /// </summary>
  124. public class SphericalHarmonicsL2Utils
  125. {
  126. /// <summary>
  127. /// Returns the L1 coefficients organized in such a way that are swizzled per channel rather than per coefficient.
  128. /// </summary>
  129. /// <param name ="sh"> The SphericalHarmonicsL2 data structure to use to query the information.</param>
  130. /// <param name ="L1_R">The red channel of all coefficient for the L1 band.</param>
  131. /// <param name ="L1_G">The green channel of all coefficient for the L1 band.</param>
  132. /// <param name ="L1_B">The blue channel of all coefficient for the L1 band.</param>
  133. public static void GetL1(SphericalHarmonicsL2 sh, out Vector3 L1_R, out Vector3 L1_G, out Vector3 L1_B)
  134. {
  135. L1_R = new Vector3(sh[0, 1],
  136. sh[0, 2],
  137. sh[0, 3]);
  138. L1_G = new Vector3(sh[1, 1],
  139. sh[1, 2],
  140. sh[1, 3]);
  141. L1_B = new Vector3(sh[2, 1],
  142. sh[2, 2],
  143. sh[2, 3]);
  144. }
  145. /// <summary>
  146. /// Returns all the L2 coefficients.
  147. /// </summary>
  148. /// <param name ="sh"> The SphericalHarmonicsL2 data structure to use to query the information.</param>
  149. /// <param name ="L2_0">The first coefficient for the L2 band.</param>
  150. /// <param name ="L2_1">The second coefficient for the L2 band.</param>
  151. /// <param name ="L2_2">The third coefficient for the L2 band.</param>
  152. /// <param name ="L2_3">The fourth coefficient for the L2 band.</param>
  153. /// <param name ="L2_4">The fifth coefficient for the L2 band.</param>
  154. public static void GetL2(SphericalHarmonicsL2 sh, out Vector3 L2_0, out Vector3 L2_1, out Vector3 L2_2, out Vector3 L2_3, out Vector3 L2_4)
  155. {
  156. L2_0 = new Vector3(sh[0, 4],
  157. sh[1, 4],
  158. sh[2, 4]);
  159. L2_1 = new Vector3(sh[0, 5],
  160. sh[1, 5],
  161. sh[2, 5]);
  162. L2_2 = new Vector3(sh[0, 6],
  163. sh[1, 6],
  164. sh[2, 6]);
  165. L2_3 = new Vector3(sh[0, 7],
  166. sh[1, 7],
  167. sh[2, 7]);
  168. L2_4 = new Vector3(sh[0, 8],
  169. sh[1, 8],
  170. sh[2, 8]);
  171. }
  172. /// <summary>
  173. /// Set L0 coefficient.
  174. /// </summary>
  175. /// <param name ="sh">The SphericalHarmonicsL2 data structure to store information on.</param>
  176. /// <param name ="L0">The L0 coefficient to set.</param>
  177. public static void SetL0(ref SphericalHarmonicsL2 sh, Vector3 L0)
  178. {
  179. sh[0, 0] = L0.x;
  180. sh[1, 0] = L0.y;
  181. sh[2, 0] = L0.z;
  182. }
  183. /// <summary>
  184. /// Set the red channel for each of the L1 coefficients.
  185. /// </summary>
  186. /// <param name ="sh">The SphericalHarmonicsL2 data structure to store information on.</param>
  187. /// <param name ="L1_R">The red channels for each L1 coefficient.</param>
  188. public static void SetL1R(ref SphericalHarmonicsL2 sh, Vector3 L1_R)
  189. {
  190. sh[0, 1] = L1_R.x;
  191. sh[0, 2] = L1_R.y;
  192. sh[0, 3] = L1_R.z;
  193. }
  194. /// <summary>
  195. /// Set the green channel for each of the L1 coefficients.
  196. /// </summary>
  197. /// <param name ="sh">The SphericalHarmonicsL2 data structure to store information on.</param>
  198. /// <param name ="L1_G">The green channels for each L1 coefficient.</param>
  199. public static void SetL1G(ref SphericalHarmonicsL2 sh, Vector3 L1_G)
  200. {
  201. sh[1, 1] = L1_G.x;
  202. sh[1, 2] = L1_G.y;
  203. sh[1, 3] = L1_G.z;
  204. }
  205. /// <summary>
  206. /// Set the blue channel for each of the L1 coefficients.
  207. /// </summary>
  208. /// <param name ="sh">The SphericalHarmonicsL2 data structure to store information on.</param>
  209. /// <param name ="L1_B">The blue channels for each L1 coefficient.</param>
  210. public static void SetL1B(ref SphericalHarmonicsL2 sh, Vector3 L1_B)
  211. {
  212. sh[2, 1] = L1_B.x;
  213. sh[2, 2] = L1_B.y;
  214. sh[2, 3] = L1_B.z;
  215. }
  216. /// <summary>
  217. /// Set all L1 coefficients per channel.
  218. /// </summary>
  219. /// <param name ="sh">The SphericalHarmonicsL2 data structure to store information on.</param>
  220. /// <param name ="L1_R">The red channels for each L1 coefficient.</param>
  221. /// <param name ="L1_G">The green channels for each L1 coefficient.</param>
  222. /// <param name ="L1_B">The blue channels for each L1 coefficient.</param>
  223. public static void SetL1(ref SphericalHarmonicsL2 sh, Vector3 L1_R, Vector3 L1_G, Vector3 L1_B)
  224. {
  225. SetL1R(ref sh, L1_R);
  226. SetL1G(ref sh, L1_G);
  227. SetL1B(ref sh, L1_B);
  228. }
  229. /// <summary>
  230. /// Set a spherical harmonics coefficient.
  231. /// </summary>
  232. /// <param name ="sh">The SphericalHarmonicsL2 data structure to store information on.</param>
  233. /// <param name ="index">The index of the coefficient that is set (must be less than 9).</param>
  234. /// <param name ="coefficient">The values of the coefficient is set.</param>
  235. public static void SetCoefficient(ref SphericalHarmonicsL2 sh, int index, Vector3 coefficient)
  236. {
  237. Debug.Assert(index < 9);
  238. sh[0, index] = coefficient.x;
  239. sh[1, index] = coefficient.y;
  240. sh[2, index] = coefficient.z;
  241. }
  242. /// <summary>
  243. /// Get a spherical harmonics coefficient.
  244. /// </summary>
  245. /// <param name ="sh">The SphericalHarmonicsL2 data structure to get information from.</param>
  246. /// <param name ="index">The index of the coefficient that is requested (must be less than 9).</param>
  247. /// <returns>The value of the requested coefficient.</returns>
  248. public static Vector3 GetCoefficient(SphericalHarmonicsL2 sh, int index)
  249. {
  250. Debug.Assert(index < 9);
  251. return new Vector3(sh[0, index], sh[1, index], sh[2, index]);
  252. }
  253. }
  254. }