TC_RawImage.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.IO;
  5. namespace TerrainComposer2
  6. {
  7. [ExecuteInEditMode]
  8. public class TC_RawImage : MonoBehaviour
  9. {
  10. public enum ByteOrder { Windows, Mac };
  11. public bool isResourcesFolder = false;
  12. public string path;
  13. public string filename;
  14. public int referenceCount;
  15. public Int2 resolution;
  16. public bool squareResolution;
  17. public ByteOrder byteOrder;
  18. public Texture2D tex;
  19. public Texture2D tex2;
  20. public bool isDestroyed, callDestroy;
  21. void Awake()
  22. {
  23. if (isDestroyed)
  24. {
  25. LoadRawImage(path);
  26. TC_Settings.instance.rawFiles.Add(this);
  27. }
  28. if (!callDestroy) { TC.RefreshOutputReferences(TC.allOutput); referenceCount = 0; }
  29. else callDestroy = false;
  30. }
  31. void OnDestroy()
  32. {
  33. TC_Compute.DisposeTexture(ref tex);
  34. TC_Compute.DisposeTexture(ref tex2);
  35. #if UNITY_EDITOR
  36. if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode && !callDestroy) TC.RefreshOutputReferences(TC.allOutput);
  37. #else
  38. if (!callDestroy) TC.RefreshOutputReferences(TC.allOutput);
  39. #endif
  40. }
  41. void DestroyMe()
  42. {
  43. // Debug.Log("Destroy RawImage");
  44. TC_Settings settings = TC_Settings.instance;
  45. if (settings == null) return;
  46. if (settings.rawFiles == null) return;
  47. int index = settings.rawFiles.IndexOf(this);
  48. if (index != -1) settings.rawFiles.RemoveAt(index);
  49. #if UNITY_EDITOR
  50. UnityEditor.Undo.DestroyObjectImmediate(gameObject);
  51. #else
  52. Destroy(gameObject);
  53. #endif
  54. }
  55. public void UnregisterReference()
  56. {
  57. // Debug.Log(referenceCount);
  58. --referenceCount;
  59. // Debug.Log(referenceCount);
  60. if (referenceCount > 0) return;
  61. isDestroyed = true;
  62. callDestroy = true;
  63. DestroyMe();
  64. }
  65. public bool GetFileResolution()
  66. {
  67. if (!TC.FileExists(path)) return false;
  68. long length = TC.GetFileLength(path);
  69. GetResolutionFromLength(length);
  70. return true;
  71. }
  72. public void GetResolutionFromLength(long length)
  73. {
  74. float res = Mathf.Sqrt(length / 2);
  75. if (res == Mathf.Floor(res)) squareResolution = true; else squareResolution = false;
  76. resolution = new Int2(res, res);
  77. }
  78. public void LoadRawImage(string path)
  79. {
  80. this.path = path;
  81. string fullPath = Application.dataPath.Replace("Assets", "/") + path;
  82. // Debug.Log(fullPath);
  83. if (tex != null) return;
  84. #if UNITY_EDITOR
  85. if (!isResourcesFolder)
  86. {
  87. if (!TC.FileExists(fullPath)) return;
  88. }
  89. #endif
  90. TC_Reporter.Log("Load Raw file " + fullPath);
  91. // Debug.Log(bytes.Length);
  92. byte[] bytes = null;
  93. if (isResourcesFolder)
  94. {
  95. // Debug.Log("LoadRawImage " + path);
  96. TextAsset textAsset = Resources.Load<TextAsset>(path);
  97. if (textAsset != null) bytes = textAsset.bytes;
  98. else Debug.Log("Can't find file");
  99. }
  100. else
  101. {
  102. #if !UNITY_WEBPLAYER
  103. bytes = File.ReadAllBytes(fullPath);
  104. #else
  105. // TC.AddMessage("You are in Webplayer build mode, loading from disk is protected in this mode and stamp textures don't work.\nThis will be fixed.\n\nFor now another build mode in needed.", 0, 5);
  106. WWW request = new WWW("file:///" + fullPath);
  107. while (!request.isDone) { }
  108. if (request.error != null) TC.AddMessage(request.error);
  109. bytes = request.bytes;
  110. #endif
  111. }
  112. if (bytes == null) return;
  113. if (bytes.Length == 0) return;
  114. GetResolutionFromLength(bytes.Length);
  115. #if UNITY_EDITOR_OSX
  116. byte[] bytes1 = new byte[bytes.Length / 2];
  117. byte[] bytes2 = new byte[bytes.Length / 2];
  118. for (int i = 0; i < bytes.Length / 2; i++)
  119. {
  120. bytes1[i] = bytes[i * 2];
  121. bytes2[i] = bytes[(i * 2) + 1];
  122. }
  123. tex = new Texture2D(resolution.x, resolution.y, TextureFormat.Alpha8, false, true);
  124. tex.hideFlags = HideFlags.DontSave;
  125. tex2 = new Texture2D(resolution.x, resolution.y, TextureFormat.Alpha8, false, true);
  126. tex2.hideFlags = HideFlags.DontSave;
  127. tex2.LoadRawTextureData(bytes2);
  128. tex2.Apply();
  129. tex.LoadRawTextureData(bytes1);
  130. #else
  131. tex = new Texture2D(resolution.x, resolution.y, TextureFormat.R16, false, true);
  132. tex.hideFlags = HideFlags.DontSave;
  133. tex.LoadRawTextureData(bytes);
  134. #endif
  135. tex.Apply();
  136. // For use of mipmap
  137. //rt = new RenderTexture(resolution.x, resolution.y, 0, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear);
  138. //rt.useMipMap = true;
  139. //rt.hideFlags = HideFlags.DontSave;
  140. //rt.Create();
  141. // Graphics.Blit(tex2, rt);
  142. // Debug.Log("Load");
  143. }
  144. }
  145. }