설명 없음
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.

Light2DLookupTexture.cs 5.0KB

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