PagedLodFileLoader.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.Threading;
  6. using System.IO;
  7. namespace AIPagedLod
  8. {
  9. public class TileObjectInfo : IDisposable
  10. {
  11. public Vector3[] mVertices = null;
  12. public int[] mTriangles = null;
  13. public Vector2[] mTexCoords = null;
  14. public byte[] mImageData = null;
  15. public int mImageSize = 0;
  16. public int mImageResolutionS = 0;
  17. public int mImageResolutionT = 0;
  18. public int mTextureFormat = 0;
  19. public TextureFormat mUnityTextureFormat;
  20. public void Dispose()
  21. {
  22. mVertices = null;
  23. mTriangles = null;
  24. mTexCoords = null;
  25. mImageData = null;
  26. }
  27. }
  28. public class PagedLodFileLoader : IDisposable
  29. {
  30. public string mFileName = string.Empty;
  31. public string mTileName = string.Empty;
  32. public string mTileDirPath = string.Empty;
  33. public int mLevel = 0;
  34. public bool mIsFinished = false;
  35. public bool mIsLoaded = false;
  36. public ManualResetEvent mLoadedEvent = new ManualResetEvent(false);
  37. public bool mRequestResult = false;
  38. public bool mIsDaJiangData = false;
  39. public static Material mMatPrefab = null;
  40. public List<TileObjectInfo> mObjectInfoList = new List<TileObjectInfo>();
  41. public void StartLoad()
  42. {
  43. mIsFinished = true;
  44. mRequestResult = true;
  45. }
  46. public virtual void LoadObjectMeshInfo()
  47. {
  48. }
  49. public virtual void LoadObjectBasicInfo()
  50. {
  51. }
  52. public void BuildObjectForThread(GameObject renderNode, int meshLayer = -1)
  53. {
  54. if (mMatPrefab == null)
  55. {
  56. mMatPrefab = Resources.Load("PagedLodMat") as Material;
  57. }
  58. if (mObjectInfoList.Count == 0)
  59. {
  60. Debug.Log(" ************* mObjectInfoList.Count == 0 " + renderNode.transform.parent.gameObject.name);
  61. }
  62. for (int i = 0; i < mObjectInfoList.Count; ++i)
  63. {
  64. TileObjectInfo info = mObjectInfoList[i];
  65. GameObject meshGameObject = null;
  66. if (MeshPool.mInstance != null)
  67. {
  68. meshGameObject = MeshPool.mInstance.GetMeshObject();
  69. }
  70. TextureFormat textureFormat = info.mUnityTextureFormat;
  71. if (meshGameObject == null)
  72. {
  73. meshGameObject = new GameObject("Mesh" + i.ToString());
  74. meshGameObject.transform.parent = renderNode.transform;
  75. meshGameObject.transform.localPosition = Vector3.zero;
  76. meshGameObject.transform.localRotation = Quaternion.identity;
  77. meshGameObject.transform.localScale = Vector3.one;
  78. meshGameObject.SetActive(true);
  79. Mesh mesh = new Mesh();
  80. if (info.mVertices.Length <= 65000)
  81. {
  82. mesh.vertices = info.mVertices;
  83. mesh.triangles = info.mTriangles;
  84. mesh.uv = info.mTexCoords;
  85. }
  86. else
  87. {
  88. Debug.Log(renderNode.transform.parent.name + " Mesh Vertex Count > 65000");
  89. }
  90. Texture2D texture2D = new Texture2D(info.mImageResolutionS, info.mImageResolutionT, textureFormat, false);
  91. if (info.mImageData.Length > 0)
  92. {
  93. texture2D.LoadRawTextureData(info.mImageData);
  94. }
  95. else
  96. {
  97. meshGameObject.SetActive(false);
  98. }
  99. texture2D.Apply();
  100. MeshFilter meshFilter = meshGameObject.AddComponent<MeshFilter>();
  101. meshFilter.sharedMesh = mesh;
  102. MeshRenderer meshRenderer = meshGameObject.AddComponent<MeshRenderer>();
  103. Material material = new Material(mMatPrefab);
  104. material.SetTexture("_MainTex", texture2D);
  105. meshRenderer.sharedMaterial = material;
  106. //mesh.RecalculateNormals();
  107. if (meshLayer == -1)
  108. {
  109. meshLayer = PagedLodConfig.mInstance == null ? 8 : PagedLodConfig.mInstance.mMeshLayer;
  110. }
  111. meshGameObject.layer = meshLayer;
  112. }
  113. else
  114. {
  115. meshGameObject.SetActive(true);
  116. meshGameObject.transform.parent = renderNode.transform;
  117. meshGameObject.transform.localPosition = Vector3.zero;
  118. meshGameObject.transform.localRotation = Quaternion.identity;
  119. meshGameObject.transform.localScale = Vector3.one;
  120. MeshFilter meshFilter = meshGameObject.GetComponent<MeshFilter>();
  121. MeshRenderer meshRenderer = meshGameObject.GetComponent<MeshRenderer>();
  122. Mesh mesh = meshFilter.sharedMesh;
  123. mesh.Clear();
  124. mesh.vertices = info.mVertices;
  125. mesh.triangles = info.mTriangles;
  126. mesh.uv = info.mTexCoords;
  127. Texture2D texture2D = meshRenderer.sharedMaterial.GetTexture("_MainTex") as Texture2D;
  128. if (textureFormat == TextureFormat.DXT5 || texture2D.format == TextureFormat.DXT5)
  129. {
  130. UnityEngine.Object.Destroy(texture2D);
  131. texture2D = new Texture2D(info.mImageResolutionS, info.mImageResolutionT, textureFormat, false);
  132. texture2D.LoadRawTextureData(info.mImageData);
  133. texture2D.Apply();
  134. meshRenderer.sharedMaterial.SetTexture("_MainTex", texture2D);
  135. }
  136. else
  137. {
  138. if (info.mImageData.Length > 0)
  139. {
  140. #if (UNITY_2021_1_OR_NEWER)
  141. texture2D.Reinitialize(info.mImageResolutionS, info.mImageResolutionT, textureFormat, false);
  142. #else
  143. texture2D.Resize(info.mImageResolutionS, info.mImageResolutionT, textureFormat, false);
  144. #endif
  145. texture2D.LoadRawTextureData(info.mImageData);
  146. texture2D.Apply();
  147. //meshFilter.sharedMesh.RecalculateNormals();
  148. meshGameObject.SetActive(true);
  149. }
  150. else
  151. {
  152. meshGameObject.SetActive(false);
  153. }
  154. }
  155. }
  156. }
  157. }
  158. public void Dispose()
  159. {
  160. foreach (var info in mObjectInfoList)
  161. {
  162. info.Dispose();
  163. }
  164. mLoadedEvent = null;
  165. }
  166. public virtual bool IsInvalidMesh()
  167. {
  168. return false;
  169. }
  170. public static PagedLodFileLoader GetFileLoader(string fileName)
  171. {
  172. if (PagedLodConfig.mInstance.mTileDataType == TileDataType.OSGB)
  173. return new OsgbFileLoader(fileName);
  174. else
  175. return new B3dmFileLoader(fileName);
  176. }
  177. public static PagedLodFileLoader GetPagedLodFileLoader(FileInfo fileInfo, int level, string TileDirPath, bool IsDaJiangData = false)
  178. {
  179. PagedLodFileLoader pagedLodLoader = GetFileLoader(fileInfo.FullName);
  180. pagedLodLoader.mIsDaJiangData = IsDaJiangData;
  181. pagedLodLoader.mTileName = fileInfo.Name.Replace(PagedLodConfig.mInstance.GetFileSufix(), "");
  182. pagedLodLoader.mTileDirPath = TileDirPath;
  183. pagedLodLoader.mLevel = level;
  184. return pagedLodLoader;
  185. }
  186. }
  187. }