TexturePreview.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. namespace TerrainComposer2
  5. {
  6. [Serializable]
  7. public class TexturePreview
  8. {
  9. public bool edit;
  10. public Texture2D tex;
  11. [NonSerialized]
  12. public byte[] bytes;
  13. float x, y;
  14. public void Init()
  15. {
  16. // Debug.Log("Create");
  17. int resolution = TC_Area2D.current.previewResolution;
  18. if (bytes == null) bytes = new byte[resolution * resolution * 4];
  19. if (tex == null)
  20. {
  21. tex = new Texture2D(resolution, resolution, TextureFormat.RGBA32, false);
  22. tex.hideFlags = HideFlags.DontSave;
  23. tex.name = "texPreview";
  24. }
  25. }
  26. public void Init(int resolution)
  27. {
  28. bool create = false;
  29. if (bytes == null) bytes = new byte[resolution * resolution * 4];
  30. if (tex == null) create = true;
  31. else if (tex.width != resolution)
  32. {
  33. tex.Resize(resolution, resolution);
  34. return;
  35. }
  36. if (create)
  37. {
  38. tex = new Texture2D(resolution, resolution, TextureFormat.RGBA32, false);
  39. tex.hideFlags = HideFlags.DontSave;
  40. tex.name = "texPreview";
  41. }
  42. }
  43. public void ReCreate(int resolution)
  44. {
  45. // Debug.Log("recreate preview tex");
  46. bytes = new byte[resolution * resolution * 4];
  47. tex = new Texture2D(resolution, resolution, TextureFormat.RGBA32, false);
  48. tex.hideFlags = HideFlags.DontSave;
  49. tex.name = "texPreview";
  50. }
  51. public void UploadTexture()
  52. {
  53. tex.LoadRawTextureData(bytes);
  54. tex.Apply(false);
  55. }
  56. public void SetPixel(float v)
  57. {
  58. x = TC_Area2D.current.previewPos.x;
  59. y = TC_Area2D.current.previewPos.y;
  60. int px = (int)x;
  61. int py = (int)y;
  62. if (px > 127 || px < 0) return;
  63. if (py > 127 || py < 0) return;
  64. py *= 512;
  65. px *= 4;
  66. Color32 color = Color.white * v;
  67. if (v > 1) color = Color.Lerp(Color.red, new Color(1, 0, 1), Mathw.Clamp01(v - 1));
  68. else if (v < 0) color = Color.Lerp(Color.cyan, Color.blue, Mathw.Clamp01(v * -1));
  69. bytes[px + py] = (byte)(color.r);
  70. bytes[px + py + 1] = (byte)(color.g);
  71. bytes[px + py + 2] = (byte)(color.b);
  72. bytes[px + py + 3] = 1;
  73. }
  74. public void SetPixelColor(Color color)
  75. {
  76. // if (tex == null) Create(128);
  77. x = TC_Area2D.current.previewPos.x;
  78. y = TC_Area2D.current.previewPos.y;
  79. int px = (int)x;
  80. int py = (int)y;
  81. if (px > 127 || px < 0) return;
  82. if (py > 127 || py < 0) return;
  83. py *= 512;
  84. px *= 4;
  85. bytes[px + py] = (byte)(color.r * 255);
  86. bytes[px + py + 1] = (byte)(color.g * 255);
  87. bytes[px + py + 2] = (byte)(color.b * 255);
  88. bytes[px + py + 3] = 1;
  89. }
  90. public void SetPixelColor(int px, int py, Color color)
  91. {
  92. py *= tex.width * 4;
  93. px *= 4;
  94. bytes[px + py] = (byte)(color.r * 255);
  95. bytes[px + py + 1] = (byte)(color.g * 255);
  96. bytes[px + py + 2] = (byte)(color.b * 255);
  97. bytes[px + py + 3] = 1;
  98. }
  99. }
  100. }