CreateLUT.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. // =================================
  7. // Define namespace.
  8. // =================================
  9. namespace MirzaBeig
  10. {
  11. namespace Scripting
  12. {
  13. namespace Effects
  14. {
  15. // =================================
  16. // Classes.
  17. // =================================
  18. //[ExecuteInEditMode]
  19. //[System.Serializable]
  20. //[RequireComponent(typeof(ParticleSystem))]
  21. static public class CreateLUT
  22. {
  23. // =================================
  24. // Nested classes and structures.
  25. // =================================
  26. // ...
  27. // =================================
  28. // Variables.
  29. // =================================
  30. // ...
  31. // =================================
  32. // Functions.
  33. // =================================
  34. // ...
  35. public static void fromGradient(int steps, Gradient gradient, ref Texture2D texture)
  36. {
  37. if (texture)
  38. {
  39. MonoBehaviour.Destroy(texture);
  40. }
  41. texture = new Texture2D(steps, 1);
  42. // Assign the first and last beforehand.
  43. // Min LUT width resolution is 2, so this will work without problems.
  44. texture.SetPixel(0, 0, gradient.Evaluate(0.0f));
  45. texture.SetPixel(steps - 1, 0, gradient.Evaluate(1.0f));
  46. // Then fill the middle (if any -> meaning if resolution > 2).
  47. for (int i = 1; i < steps - 1; i++)
  48. {
  49. Color colour = gradient.Evaluate(i / (float)steps);
  50. texture.SetPixel(i, 0, colour);
  51. }
  52. texture.Apply();
  53. }
  54. public static void fromAnimationCurve(int steps, AnimationCurve curve, ref Texture2D texture)
  55. {
  56. if (texture)
  57. {
  58. MonoBehaviour.Destroy(texture);
  59. }
  60. texture = new Texture2D(steps, 1);
  61. // Assign the first and last beforehand.
  62. // Min LUT width resolution is 2, so this will work without problems.
  63. texture.SetPixel(0, 0, new Color(0.0f, 0.0f, 0.0f, curve.Evaluate(0.0f)));
  64. texture.SetPixel(steps - 1, 0, new Color(0.0f, 0.0f, 0.0f, curve.Evaluate(1.0f)));
  65. // Then fill the middle (if any -> meaning if resolution > 2).
  66. for (int i = 1; i < steps - 1; i++)
  67. {
  68. float value = curve.Evaluate(i / (float)steps);
  69. texture.SetPixel(i, 0, new Color(0.0f, 0.0f, 0.0f, value));
  70. }
  71. texture.Apply();
  72. }
  73. // =================================
  74. // End functions.
  75. // =================================
  76. }
  77. // =================================
  78. // End namespace.
  79. // =================================
  80. }
  81. }
  82. }
  83. // =================================
  84. // --END-- //
  85. // =================================