暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BatchRendererGroupGlobals.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace UnityEngine.Rendering
  4. {
  5. /// <summary>
  6. /// Contains spherical harmonic coefficients used for lighting representation in the format
  7. /// expected by <c>DOTS_INSTANCING_ON</c> shaders.
  8. ///
  9. /// The size of the struct is padded to a power of two so arrays of such structs can be efficiently
  10. /// indexed in shaders.
  11. /// </summary>
  12. /// <seealso cref="SphericalHarmonicsL2"/>
  13. [StructLayout(LayoutKind.Sequential)]
  14. [Serializable]
  15. public struct SHCoefficients : IEquatable<SHCoefficients>
  16. {
  17. /// <summary>
  18. /// Contains the SH coefficients that correspond to the <c>unity_SHAr</c> shader property.
  19. /// </summary>
  20. public Vector4 SHAr;
  21. /// <summary>
  22. /// Contains the SH coefficients that correspond to the <c>unity_SHAg</c> shader property.
  23. /// </summary>
  24. public Vector4 SHAg;
  25. /// <summary>
  26. /// Contains the SH coefficients that correspond to the <c>unity_SHAb</c> shader property.
  27. /// </summary>
  28. public Vector4 SHAb;
  29. /// <summary>
  30. /// Contains the SH coefficients that correspond to the <c>unity_SHBr</c> shader property.
  31. /// </summary>
  32. public Vector4 SHBr;
  33. /// <summary>
  34. /// Contains the SH coefficients that correspond to the <c>unity_SHBg</c> shader property.
  35. /// </summary>
  36. public Vector4 SHBg;
  37. /// <summary>
  38. /// Contains the SH coefficients that correspond to the <c>unity_SHBb</c> shader property.
  39. /// </summary>
  40. public Vector4 SHBb;
  41. /// <summary>
  42. /// Contains the SH coefficients that correspond to the <c>unity_SHC</c> shader property.
  43. /// </summary>
  44. public Vector4 SHC;
  45. /// <summary>
  46. /// Contains the baked shadowing data that corresponds to the <c>unity_ProbesOcclusion</c> shader property.
  47. /// </summary>
  48. public Vector4 ProbesOcclusion;
  49. /// <summary>
  50. /// Construct an instance of <c>SHCoefficients</c> that represents the same spherical
  51. /// harmonic coefficients as the parameter.
  52. /// </summary>
  53. /// <param name="sh">The spherical harmonic coefficients to initialize with.</param>
  54. public SHCoefficients(SphericalHarmonicsL2 sh)
  55. {
  56. SHAr = GetSHA(sh, 0);
  57. SHAg = GetSHA(sh, 1);
  58. SHAb = GetSHA(sh, 2);
  59. SHBr = GetSHB(sh, 0);
  60. SHBg = GetSHB(sh, 1);
  61. SHBb = GetSHB(sh, 2);
  62. SHC = GetSHC(sh);
  63. ProbesOcclusion = Vector4.one;
  64. }
  65. /// <summary>
  66. /// Construct an instance of <c>SHCoefficients</c> that represents the same spherical
  67. /// harmonic coefficients as the parameter.
  68. /// </summary>
  69. /// <param name="sh">The spherical harmonic coefficients to initialize with.</param>
  70. /// <param name="probesOcclusion">The baked shadowing data to include with this set of spherical harmonic coefficients.</param>
  71. public SHCoefficients(SphericalHarmonicsL2 sh, Vector4 probesOcclusion)
  72. : this(sh)
  73. {
  74. ProbesOcclusion = probesOcclusion;
  75. }
  76. static Vector4 GetSHA(SphericalHarmonicsL2 sh, int i)
  77. {
  78. return new Vector4(sh[i, 3], sh[i, 1], sh[i, 2], sh[i, 0] - sh[i, 6]);
  79. }
  80. static Vector4 GetSHB(SphericalHarmonicsL2 sh, int i)
  81. {
  82. return new Vector4(sh[i, 4], sh[i, 5], sh[i, 6] * 3f, sh[i, 7]);
  83. }
  84. static Vector4 GetSHC(SphericalHarmonicsL2 sh)
  85. {
  86. return new Vector4(sh[0, 8], sh[1, 8], sh[2, 8], 1);
  87. }
  88. /// <summary>
  89. /// Equals implementation.
  90. /// </summary>
  91. /// <param name="other">Other SHCoefficients instance to comapre this against.</param>
  92. /// <returns>True if contents are equal, False otherwise.</returns>
  93. public bool Equals(SHCoefficients other)
  94. {
  95. return SHAr.Equals(other.SHAr) && SHAg.Equals(other.SHAg) && SHAb.Equals(other.SHAb) && SHBr.Equals(other.SHBr) && SHBg.Equals(other.SHBg) && SHBb.Equals(other.SHBb) && SHC.Equals(other.SHC) && ProbesOcclusion.Equals(other.ProbesOcclusion);
  96. }
  97. /// <summary>
  98. /// Equals implementation.
  99. /// </summary>
  100. /// <param name="obj">Other object to compare this object against</param>
  101. /// <returns>True if contents are equal, False otherwise.</returns>
  102. public override bool Equals(object obj)
  103. {
  104. return obj is SHCoefficients other && Equals(other);
  105. }
  106. /// <summary>
  107. /// GetHashCode implementation.
  108. /// </summary>
  109. /// <returns>Returns a hashcode based on SHA coefficients.</returns>
  110. public override int GetHashCode()
  111. {
  112. return HashCode.Combine(SHAr, SHAg, SHAb, SHBr, SHBg, SHBb, SHC, ProbesOcclusion);
  113. }
  114. /// <summary>
  115. /// Equality operator implementation.
  116. /// </summary>
  117. /// <param name="left">Left operand of comparison</param>
  118. /// <param name="right">Right operand of comparison</param>
  119. /// <returns>True if contents are equal, False otherwise.</returns>
  120. public static bool operator ==(SHCoefficients left, SHCoefficients right)
  121. {
  122. return left.Equals(right);
  123. }
  124. /// <summary>
  125. /// Not equals operator implementation.
  126. /// </summary>
  127. /// <param name="left">Left operand of comparison</param>
  128. /// <param name="right">Right operand of comparison</param>
  129. /// <returns>True if contents are not equal, False otherwise.</returns>
  130. public static bool operator !=(SHCoefficients left, SHCoefficients right)
  131. {
  132. return !left.Equals(right);
  133. }
  134. }
  135. /// <summary>
  136. /// Contains default values for built-in properties that the user is expected to manually
  137. /// provide for <c>DOTS_INSTANCING_ON</c> shaders. The struct layout matches the
  138. /// <c>unity_DOTSInstanceGlobalValues</c> constant buffer the shader expects the default
  139. /// values in.
  140. /// </summary>
  141. [Obsolete("BatchRendererGroupGlobals and associated cbuffer are now set automatically by Unity. Setting it manually is no longer necessary or supported.")]
  142. [StructLayout(LayoutKind.Sequential)]
  143. [Serializable]
  144. public struct BatchRendererGroupGlobals : IEquatable<BatchRendererGroupGlobals>
  145. {
  146. /// <summary>
  147. /// The string name of the constant buffer <c>DOTS_INSTANCING_ON</c> shaders use
  148. /// to read default values for the built-in properties contained in this struct.
  149. /// </summary>
  150. public const string kGlobalsPropertyName = "unity_DOTSInstanceGlobalValues";
  151. /// <summary>
  152. /// The unique identifier for <see cref="kGlobalsPropertyName"/>, retrieved using
  153. /// <see cref="Shader.PropertyToID"/>.
  154. /// </summary>
  155. /// <seealso cref="Shader.PropertyToID"/>
  156. public static readonly int kGlobalsPropertyId = Shader.PropertyToID(kGlobalsPropertyName);
  157. /// <summary>
  158. /// The default value to use for the <c>unity_ProbesOcclusion</c> built-in shader property.
  159. /// </summary>
  160. public Vector4 ProbesOcclusion;
  161. /// <summary>
  162. /// The default value to use for the <c>unity_SpecCube0_HDR</c> built-in shader property.
  163. /// </summary>
  164. public Vector4 SpecCube0_HDR;
  165. /// <summary>
  166. /// The default value to use for the <c>unity_SpecCube1_HDR</c> built-in shader property.
  167. /// </summary>
  168. public Vector4 SpecCube1_HDR;
  169. /// <summary>
  170. /// The default values to use for the built-in spherical harmonics shader properties.
  171. /// </summary>
  172. /// <seealso cref="SHCoefficients"/>
  173. public SHCoefficients SHCoefficients;
  174. /// <summary>
  175. /// Construct a struct with default values based on the currently active reflection probe
  176. /// and ambient lighting settings.
  177. /// </summary>
  178. public static BatchRendererGroupGlobals Default
  179. {
  180. get
  181. {
  182. var globals = new BatchRendererGroupGlobals();
  183. globals.ProbesOcclusion = Vector4.one;
  184. globals.SpecCube0_HDR = ReflectionProbe.defaultTextureHDRDecodeValues;
  185. globals.SpecCube1_HDR = globals.SpecCube0_HDR;
  186. globals.SHCoefficients = new SHCoefficients(RenderSettings.ambientProbe);
  187. return globals;
  188. }
  189. }
  190. /// <summary>
  191. /// Equals implementation.
  192. /// </summary>
  193. /// <param name="other">Other BatchRendererGroupGlobals instance to comapre this against.</param>
  194. /// <returns>True if contents are equal, False otherwise.</returns>
  195. public bool Equals(BatchRendererGroupGlobals other)
  196. {
  197. return ProbesOcclusion.Equals(other.ProbesOcclusion) && SpecCube0_HDR.Equals(other.SpecCube0_HDR) && SpecCube1_HDR.Equals(other.SpecCube1_HDR) && SHCoefficients.Equals(other.SHCoefficients);
  198. }
  199. /// <summary>
  200. /// Equals implementation.
  201. /// </summary>
  202. /// <param name="obj">Other object to comapre this against.</param>
  203. /// <returns>True if contents are equal, False otherwise.</returns>
  204. public override bool Equals(object obj)
  205. {
  206. return obj is BatchRendererGroupGlobals other && Equals(other);
  207. }
  208. /// <summary>
  209. /// GetHashCode implementation.
  210. /// </summary>
  211. /// <returns>Returns a hashcode based on ProbesOcclusion, SpecCube and SH coefficients parameters</returns>
  212. public override int GetHashCode()
  213. {
  214. return HashCode.Combine(ProbesOcclusion, SpecCube0_HDR, SpecCube1_HDR, SHCoefficients);
  215. }
  216. /// <summary>
  217. /// Equality operator implementation.
  218. /// </summary>
  219. /// <param name="left">Left operand of comparison</param>
  220. /// <param name="right">Right operand of comparison</param>
  221. /// <returns>True if contents are equal, False otherwise.</returns>
  222. public static bool operator ==(BatchRendererGroupGlobals left, BatchRendererGroupGlobals right)
  223. {
  224. return left.Equals(right);
  225. }
  226. /// <summary>
  227. /// Not equals operator implementation.
  228. /// </summary>
  229. /// <param name="left">Left operand of comparison</param>
  230. /// <param name="right">Right operand of comparison</param>
  231. /// <returns>True if contents are not equal, False otherwise.</returns>
  232. public static bool operator !=(BatchRendererGroupGlobals left, BatchRendererGroupGlobals right)
  233. {
  234. return !left.Equals(right);
  235. }
  236. }
  237. }