Light2DLookupTexture.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using UnityEditor;
  2. using System.IO;
  3. namespace UnityEngine.Experimental.Rendering.Universal
  4. {
  5. internal static class Light2DLookupTexture
  6. {
  7. static Texture2D s_PointLightLookupTexture;
  8. static Texture2D s_FalloffLookupTexture;
  9. static public Texture2D CreatePointLightLookupTexture()
  10. {
  11. const float WIDTH = 256;
  12. const float HEIGHT = 256;
  13. TextureFormat textureFormat = TextureFormat.ARGB32;
  14. if (SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf))
  15. textureFormat = TextureFormat.RGBAHalf;
  16. else if (SystemInfo.SupportsTextureFormat(TextureFormat.RGBAFloat))
  17. textureFormat = TextureFormat.RGBAFloat;
  18. s_PointLightLookupTexture = new Texture2D((int)WIDTH, (int)HEIGHT, textureFormat, false);
  19. s_PointLightLookupTexture.filterMode = FilterMode.Bilinear;
  20. s_PointLightLookupTexture.wrapMode = TextureWrapMode.Clamp;
  21. if (s_PointLightLookupTexture != null)
  22. {
  23. Vector2 center = new Vector2(WIDTH / 2, HEIGHT / 2);
  24. for (int y = 0; y < HEIGHT; y++)
  25. {
  26. for (int x = 0; x < WIDTH; x++)
  27. {
  28. Vector2 pos = new Vector2(x, y);
  29. float distance = Vector2.Distance(pos, center);
  30. Vector2 relPos = pos - center;
  31. Vector2 direction = center - pos;
  32. direction.Normalize();
  33. // red = 1-0 distance
  34. // green = 1-0 angle
  35. // blue = direction.x
  36. // alpha = direction.y
  37. float red;
  38. if (x == WIDTH - 1 || y == HEIGHT - 1)
  39. red = 0;
  40. else
  41. red = Mathf.Clamp(1 - (2.0f * distance / WIDTH), 0.0f, 1.0f);
  42. float cosAngle = Vector2.Dot(Vector2.down, relPos.normalized);
  43. float angle = Mathf.Acos(cosAngle) / Mathf.PI; // 0-1
  44. float green = Mathf.Clamp(1 - angle, 0.0f, 1.0f);
  45. float blue = direction.x;
  46. float alpha = direction.y;
  47. Color color = new Color(red, green, blue, alpha);
  48. s_PointLightLookupTexture.SetPixel(x, y, color);
  49. }
  50. }
  51. }
  52. s_PointLightLookupTexture.Apply();
  53. return s_PointLightLookupTexture;
  54. }
  55. static public Texture2D CreateFalloffLookupTexture()
  56. {
  57. const float WIDTH = 2048;
  58. const float HEIGHT = 192;
  59. TextureFormat textureFormat = TextureFormat.ARGB32;
  60. s_FalloffLookupTexture = new Texture2D((int)WIDTH, (int)HEIGHT-64, textureFormat, false);
  61. s_FalloffLookupTexture.filterMode = FilterMode.Bilinear;
  62. s_FalloffLookupTexture.wrapMode = TextureWrapMode.Clamp;
  63. if (s_FalloffLookupTexture != null)
  64. {
  65. for(int y=0;y<HEIGHT;y++)
  66. {
  67. float baseValue = (float)(y+32) / (float)(HEIGHT+64);
  68. float lineValue = -baseValue + 1;
  69. float exponent = Mathf.Log(lineValue) / Mathf.Log(baseValue);
  70. if (y == HEIGHT - 1)
  71. textureFormat = TextureFormat.ARGB32;
  72. for (int x=0;x<WIDTH;x++)
  73. {
  74. float t = (float)x / (float)WIDTH;
  75. float red = Mathf.Pow(t, exponent);
  76. Color color = new Color(red, 0, 0, 1);
  77. if(y >= 32 && y < 160)
  78. s_FalloffLookupTexture.SetPixel(x, y-32, color);
  79. }
  80. }
  81. }
  82. s_FalloffLookupTexture.Apply();
  83. return s_FalloffLookupTexture;
  84. }
  85. //#if UNITY_EDITOR
  86. // [MenuItem("Light2D Debugging/Write Light Texture")]
  87. // static public void WriteLightTexture()
  88. // {
  89. // var path = EditorUtility.SaveFilePanel("Save texture as PNG", "", "LightLookupTexture.exr", "png");
  90. // CreatePointLightLookupTexture();
  91. // byte[] imgData = s_PointLightLookupTexture.EncodeToEXR(Texture2D.EXRFlags.CompressRLE);
  92. // if (imgData != null)
  93. // File.WriteAllBytes(path, imgData);
  94. // }
  95. // [MenuItem("Light2D Debugging/Write Falloff Texture")]
  96. // static public void WriteCurveTexture()
  97. // {
  98. // var path = EditorUtility.SaveFilePanel("Save texture as PNG", "", "FalloffLookupTexture.png", "png");
  99. // CreateFalloffLookupTexture();
  100. // byte[] imgData = s_FalloffLookupTexture.EncodeToPNG();
  101. // if (imgData != null)
  102. // File.WriteAllBytes(path, imgData);
  103. // }
  104. //#endif
  105. }
  106. }