Нема описа
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.

LensFlareDataSRP.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. using NUnit.Framework;
  2. using UnityEngine.Serialization;
  3. namespace UnityEngine.Rendering
  4. {
  5. /// <summary>
  6. /// SRPLensFlareBlendMode defined the available blend mode for each LensFlareElement
  7. /// </summary>
  8. [System.Serializable]
  9. public enum SRPLensFlareBlendMode
  10. {
  11. /// <summary>
  12. /// Additive: Blend One One
  13. /// </summary>
  14. Additive,
  15. /// <summary>
  16. /// Screen:
  17. /// Blend One OneMinusSrcColor
  18. /// </summary>
  19. Screen,
  20. /// <summary>
  21. /// Premultiply:
  22. /// Blend One OneMinusSrcAlpha
  23. /// ColorMask RGB
  24. /// </summary>
  25. Premultiply,
  26. /// <summary>
  27. /// Lerp: Blend SrcAlpha OneMinusSrcAlpha
  28. /// </summary>
  29. Lerp
  30. }
  31. /// <summary>
  32. /// SRPLensFlareDistribution defined how we spread the flare element when count > 1
  33. /// </summary>
  34. [System.Serializable]
  35. public enum SRPLensFlareDistribution
  36. {
  37. /// <summary>
  38. /// Uniformly spread
  39. /// </summary>
  40. Uniform,
  41. /// <summary>
  42. /// Controlled with curved
  43. /// </summary>
  44. Curve,
  45. /// <summary>
  46. /// Random distribution
  47. /// </summary>
  48. Random
  49. }
  50. /// <summary>
  51. /// SRPLensFlareType which can be an image of a procedural shape
  52. /// If change order or add new member, need to update preview
  53. /// shader: LensFlareDataDrivenPreview.shader
  54. /// </summary>
  55. [System.Serializable]
  56. [GenerateHLSL]
  57. public enum SRPLensFlareType
  58. {
  59. /// <summary>
  60. /// Image from a file or a RenderTexture
  61. /// </summary>
  62. Image,
  63. /// <summary>
  64. /// Procedural Circle
  65. /// </summary>
  66. Circle,
  67. /// <summary>
  68. /// Polygon
  69. /// </summary>
  70. Polygon,
  71. /// <summary>
  72. /// shape as a ring
  73. /// </summary>
  74. Ring,
  75. /// <summary>
  76. /// Hoop
  77. /// </summary>
  78. LensFlareDataSRP
  79. }
  80. /// <summary>
  81. /// SRPLensFlareColorType describe how to colorize LensFlare
  82. /// </summary>
  83. [System.Serializable]
  84. [GenerateHLSL]
  85. public enum SRPLensFlareColorType
  86. {
  87. /// <summary>
  88. /// Constant Color
  89. /// </summary>
  90. Constant = 0,
  91. /// <summary>
  92. /// Radial Gradient
  93. /// </summary>
  94. RadialGradient,
  95. /// <summary>
  96. /// Angular Gradient
  97. /// </summary>
  98. AngularGradient
  99. }
  100. /// <summary>
  101. /// LensFlareDataElementSRP defines collection of parameters describing the behavior a Lens Flare Element.
  102. /// </summary>
  103. [System.Serializable]
  104. public sealed class LensFlareDataElementSRP
  105. {
  106. /// <summary> Initialize default values </summary>
  107. public LensFlareDataElementSRP()
  108. {
  109. lensFlareDataSRP = null;
  110. visible = true;
  111. localIntensity = 1.0f;
  112. position = 0.0f;
  113. positionOffset = new Vector2(0.0f, 0.0f);
  114. angularOffset = 0.0f;
  115. translationScale = new Vector2(1.0f, 1.0f);
  116. lensFlareTexture = null;
  117. uniformScale = 1.0f;
  118. sizeXY = Vector2.one;
  119. allowMultipleElement = false;
  120. count = 5;
  121. rotation = 0.0f;
  122. preserveAspectRatio = false;
  123. // Ring
  124. ringThickness = 0.25f;
  125. // Hoop
  126. hoopFactor = 1.0f;
  127. // Shimmer
  128. noiseAmplitude = 1.0f;
  129. noiseFrequency = 1;
  130. noiseSpeed = 0;
  131. shapeCutOffSpeed = 0.0f;
  132. shapeCutOffRadius = 10.0f;
  133. tintColorType = SRPLensFlareColorType.Constant;
  134. tint = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  135. tintGradient = new TextureGradient(
  136. new GradientColorKey[] { new GradientColorKey(Color.black, 0.0f), new GradientColorKey(Color.white, 1.0f) },
  137. new GradientAlphaKey[] { new GradientAlphaKey(0.0f, 0.0f), new GradientAlphaKey(1.0f, 1.0f) });
  138. blendMode = SRPLensFlareBlendMode.Additive;
  139. autoRotate = false;
  140. isFoldOpened = true;
  141. flareType = SRPLensFlareType.Circle;
  142. distribution = SRPLensFlareDistribution.Uniform;
  143. lengthSpread = 1f;
  144. colorGradient = new Gradient();
  145. colorGradient.SetKeys(new GradientColorKey[] { new GradientColorKey(Color.white, 0.0f), new GradientColorKey(Color.white, 1.0f) },
  146. new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(1.0f, 1.0f) });
  147. positionCurve = new AnimationCurve(new Keyframe(0.0f, 0.0f, 1.0f, 1.0f), new Keyframe(1.0f, 1.0f, 1.0f, -1.0f));
  148. scaleCurve = new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 1.0f));
  149. uniformAngle = 0.0f;
  150. uniformAngleCurve = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(1.0f, 0.0f));
  151. // Random
  152. seed = 0;
  153. intensityVariation = 0.75f;
  154. positionVariation = new Vector2(1.0f, 0.0f);
  155. scaleVariation = 1.0f;
  156. rotationVariation = 180.0f;
  157. // Distortion
  158. enableRadialDistortion = false;
  159. targetSizeDistortion = Vector2.one;
  160. distortionCurve = new AnimationCurve(new Keyframe(0.0f, 0.0f, 1.0f, 1.0f), new Keyframe(1.0f, 1.0f, 1.0f, -1.0f));
  161. distortionRelativeToCenter = false;
  162. // Parameters for Procedural
  163. fallOff = 1.0f;
  164. edgeOffset = 0.1f;
  165. sdfRoundness = 0.0f;
  166. sideCount = 6;
  167. inverseSDF = false;
  168. }
  169. /// <summary>
  170. /// Clone the current LensFlareDataElementSRP.
  171. /// </summary>
  172. /// <returns>Cloned LensFlareDataElementSRP.</returns>
  173. public LensFlareDataElementSRP Clone()
  174. {
  175. LensFlareDataElementSRP clone = new LensFlareDataElementSRP();
  176. clone.lensFlareDataSRP = lensFlareDataSRP;
  177. clone.visible = visible;
  178. clone.localIntensity = localIntensity;
  179. clone.position = position;
  180. clone.positionOffset = positionOffset;
  181. clone.angularOffset = angularOffset;
  182. clone.translationScale = translationScale;
  183. clone.lensFlareTexture = lensFlareTexture;
  184. clone.uniformScale = uniformScale;
  185. clone.sizeXY = sizeXY;
  186. clone.allowMultipleElement = allowMultipleElement;
  187. clone.count = count;
  188. clone.rotation = rotation;
  189. clone.preserveAspectRatio = preserveAspectRatio;
  190. clone.ringThickness = ringThickness;
  191. clone.hoopFactor = hoopFactor;
  192. clone.noiseAmplitude = noiseAmplitude;
  193. clone.noiseFrequency = noiseFrequency;
  194. clone.noiseSpeed = noiseSpeed;
  195. clone.shapeCutOffSpeed = shapeCutOffSpeed;
  196. clone.shapeCutOffRadius = shapeCutOffRadius;
  197. clone.tintColorType = tintColorType;
  198. clone.tint = tint;
  199. clone.tintGradient = new TextureGradient(tintGradient.colorKeys, tintGradient.alphaKeys, tintGradient.mode, tintGradient.colorSpace, tintGradient.textureSize);
  200. clone.tintGradient = new TextureGradient(tintGradient.colorKeys, tintGradient.alphaKeys);
  201. clone.blendMode = blendMode;
  202. clone.autoRotate = autoRotate;
  203. clone.isFoldOpened = isFoldOpened;
  204. clone.flareType = flareType;
  205. clone.distribution = distribution;
  206. clone.lengthSpread = lengthSpread;
  207. clone.colorGradient = new Gradient();
  208. clone.colorGradient.SetKeys(colorGradient.colorKeys, colorGradient.alphaKeys);
  209. clone.colorGradient.mode = colorGradient.mode;
  210. clone.colorGradient.colorSpace = colorGradient.colorSpace;
  211. clone.positionCurve = new AnimationCurve(positionCurve.keys);
  212. clone.scaleCurve = new AnimationCurve(scaleCurve.keys);
  213. clone.uniformAngle = uniformAngle;
  214. clone.uniformAngleCurve = new AnimationCurve(uniformAngleCurve.keys);
  215. clone.seed = seed;
  216. clone.intensityVariation = intensityVariation;
  217. clone.positionVariation = positionVariation;
  218. clone.scaleVariation = scaleVariation;
  219. clone.rotationVariation = rotationVariation;
  220. clone.enableRadialDistortion = enableRadialDistortion;
  221. clone.targetSizeDistortion = targetSizeDistortion;
  222. clone.distortionCurve = new AnimationCurve(distortionCurve.keys);
  223. clone.distortionRelativeToCenter = distortionRelativeToCenter;
  224. clone.fallOff = fallOff;
  225. clone.edgeOffset = edgeOffset;
  226. clone.sdfRoundness = sdfRoundness;
  227. clone.sideCount = sideCount;
  228. clone.inverseSDF = inverseSDF;
  229. return clone;
  230. }
  231. /// <summary> Current Element is himselft another LensFlareDataSRP </summary>
  232. public LensFlareDataSRP lensFlareDataSRP;
  233. /// <summary> Visibility checker for current element </summary>
  234. public bool visible;
  235. /// <summary> Position </summary>
  236. public float position;
  237. /// <summary> Position offset </summary>
  238. public Vector2 positionOffset;
  239. /// <summary> Angular offset </summary>
  240. public float angularOffset;
  241. /// <summary> Translation Scale </summary>
  242. public Vector2 translationScale;
  243. // For Hoop
  244. /// <summary>Ring thickness</summary>
  245. [Range(0.0f, 1.0f)]
  246. public float ringThickness;
  247. /// <summary>Hoop thickness</summary>
  248. [Range(-1.0f, 1.0f)]
  249. public float hoopFactor;
  250. // For Ring
  251. /// <summary>Noise parameter amplitude</summary>
  252. public float noiseAmplitude;
  253. /// <summary>Noise parameter frequency</summary>
  254. public int noiseFrequency;
  255. /// <summary>Noise parameter Speed</summary>
  256. public float noiseSpeed;
  257. /// <summary>To simulate the cutoff of the flare by the circular shape of the lens. How quickly this cutoff happen.</summary>
  258. public float shapeCutOffSpeed;
  259. /// <summary>To simulate the cutoff of the flare by the circular shape of the lens.</summary>
  260. public float shapeCutOffRadius;
  261. [Min(0), SerializeField, FormerlySerializedAs("localIntensity")]
  262. float m_LocalIntensity;
  263. /// <summary> Intensity of this element </summary>
  264. public float localIntensity
  265. {
  266. get => m_LocalIntensity;
  267. set => m_LocalIntensity = Mathf.Max(0, value);
  268. }
  269. /// <summary> Texture used to for this Lens Flare Element </summary>
  270. public Texture lensFlareTexture;
  271. /// <summary> Uniform scale applied </summary>
  272. public float uniformScale;
  273. /// <summary> Scale size on each dimension </summary>
  274. public Vector2 sizeXY;
  275. /// <summary> Enable multiple elements </summary>
  276. public bool allowMultipleElement;
  277. [Min(1), SerializeField, FormerlySerializedAs("count")]
  278. int m_Count;
  279. /// <summary> Element can be repeated 'count' times </summary>
  280. public int count
  281. {
  282. get => m_Count;
  283. set => m_Count = Mathf.Max(1, value);
  284. }
  285. /// <summary> Preserve Aspect Ratio </summary>
  286. public bool preserveAspectRatio;
  287. /// <summary> Local rotation of the texture </summary>
  288. public float rotation;
  289. /// <summary>Specify how to tint flare.</summary>
  290. public SRPLensFlareColorType tintColorType;
  291. /// <summary> Tint of the texture can be modulated by the light we are attached to</summary>
  292. public Color tint;
  293. /// <summary> Tint radially of the texture can be modulated by the light we are attached to . </summary>
  294. public TextureGradient tintGradient;
  295. /// <summary> Blend mode used </summary>
  296. public SRPLensFlareBlendMode blendMode;
  297. /// <summary> Rotate the texture relative to the angle on the screen (the rotation will be added to the parameter 'rotation') </summary>
  298. public bool autoRotate;
  299. /// <summary> FlareType used </summary>
  300. public SRPLensFlareType flareType;
  301. /// <summary> Modulate by light color if the asset is used in a 'SRP Lens Flare Source Override' </summary>
  302. public bool modulateByLightColor;
  303. #pragma warning disable 0414 // never used (editor state)
  304. /// <summary> Internal value use to store the state of minimized or maximized LensFlareElement </summary>
  305. [SerializeField]
  306. bool isFoldOpened;
  307. #pragma warning restore 0414
  308. /// <summary> SRPLensFlareDistribution defined how we spread the flare element when count > 1 </summary>
  309. public SRPLensFlareDistribution distribution;
  310. /// <summary> Length to spread the distribution of flares, spread start at 'starting position' </summary>
  311. public float lengthSpread;
  312. /// <summary> Curve describing how to place flares distribution (Used only for Uniform and Curve 'distribution') </summary>
  313. public AnimationCurve positionCurve;
  314. /// <summary> Curve describing how to scale flares distribution (Used only for Uniform and Curve 'distribution') </summary>
  315. public AnimationCurve scaleCurve;
  316. /// <summary> Seed used to seed randomness </summary>
  317. public int seed;
  318. /// <summary> Colors used uniformly for Uniform or Curve Distribution and Random when the distribution is 'Random'. </summary>
  319. public Gradient colorGradient;
  320. [Range(0, 1), SerializeField, FormerlySerializedAs("intensityVariation")]
  321. float m_IntensityVariation;
  322. /// <summary> Scale factor applied on the variation of the intensities. </summary>
  323. public float intensityVariation
  324. {
  325. get => m_IntensityVariation;
  326. set => m_IntensityVariation = Mathf.Max(0, value);
  327. }
  328. /// <summary> Scale factor applied on the variation of the positions. </summary>
  329. public Vector2 positionVariation;
  330. /// <summary> Coefficient applied on the variation of the scale (relative to the current scale). </summary>
  331. public float scaleVariation;
  332. /// <summary> Scale factor applied on the variation of the rotation (relative to the current rotation or auto-rotate). </summary>
  333. public float rotationVariation;
  334. /// <summary> True to use or not the radial distortion. </summary>
  335. public bool enableRadialDistortion;
  336. /// <summary> Target size used on the edge of the screen. </summary>
  337. public Vector2 targetSizeDistortion;
  338. /// <summary> Curve blending from screen center to the edges of the screen. </summary>
  339. public AnimationCurve distortionCurve;
  340. /// <summary> If true the distortion is relative to center of the screen otherwise relative to lensFlare source screen position. </summary>
  341. public bool distortionRelativeToCenter;
  342. [Range(0, 1), SerializeField, FormerlySerializedAs("fallOff")]
  343. float m_FallOff;
  344. /// <summary> Fall of the gradient used for the Procedural Flare. </summary>
  345. public float fallOff
  346. {
  347. get => m_FallOff;
  348. set => m_FallOff = Mathf.Clamp01(value);
  349. }
  350. [Range(0, 1), SerializeField, FormerlySerializedAs("edgeOffset")]
  351. float m_EdgeOffset;
  352. /// <summary> Gradient Offset used for the Procedural Flare. </summary>
  353. public float edgeOffset
  354. {
  355. get => m_EdgeOffset;
  356. set => m_EdgeOffset = Mathf.Clamp01(value);
  357. }
  358. [Min(3), SerializeField, FormerlySerializedAs("sideCount")]
  359. int m_SideCount;
  360. /// <summary> Side count of the regular polygon generated. </summary>
  361. public int sideCount
  362. {
  363. get => m_SideCount;
  364. set => m_SideCount = Mathf.Max(3, value);
  365. }
  366. [Range(0, 1), SerializeField, FormerlySerializedAs("sdfRoundness")]
  367. float m_SdfRoundness;
  368. /// <summary> Roundness of the polygon flare (0: Sharp Polygon, 1: Circle). </summary>
  369. public float sdfRoundness
  370. {
  371. get => m_SdfRoundness;
  372. set => m_SdfRoundness = Mathf.Clamp01(value);
  373. }
  374. /// <summary> Inverse the gradient direction. </summary>
  375. public bool inverseSDF;
  376. /// <summary> Uniform angle (in degrees) used with multiple element enabled with Uniform distribution. </summary>
  377. public float uniformAngle;
  378. /// <summary> Uniform angle (remap from -180.0f to 180.0f) used with multiple element enabled with Curve distribution. </summary>
  379. public AnimationCurve uniformAngleCurve;
  380. }
  381. /// <summary> LensFlareDataSRP defines a Lens Flare with a set of LensFlareDataElementSRP </summary>
  382. [System.Serializable]
  383. public sealed class LensFlareDataSRP : ScriptableObject
  384. {
  385. /// <summary> Initialize default value </summary>
  386. public LensFlareDataSRP()
  387. {
  388. elements = null;
  389. }
  390. /// <summary>
  391. /// Check if we have at last one 'modulatedByLightColor' enabled.
  392. /// </summary>
  393. /// <returns>true if we have at least one 'modulatedByLightColor' on the asset.</returns>
  394. public bool HasAModulateByLightColorElement()
  395. {
  396. if (elements != null)
  397. {
  398. foreach (LensFlareDataElementSRP e in elements)
  399. {
  400. if (e.modulateByLightColor)
  401. return true;
  402. }
  403. }
  404. return false;
  405. }
  406. /// <summary> List of LensFlareDataElementSRP </summary>
  407. public LensFlareDataElementSRP[] elements;
  408. }
  409. }