OsgbFileLoader.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*************************************************************************
  2. * Copyright © 2018-2023 Liwen All rights reserved.
  3. *------------------------------------------------------------------------
  4. * File : OsgbFileLoader.cs
  5. * Description : load osgb format tile info
  6. *------------------------------------------------------------------------
  7. * Author : Liwen
  8. * Version : 1.0.0
  9. * Date : 1/5/2019
  10. * Description : Initial development version.
  11. *************************************************************************/
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using UnityEngine;
  15. using System;
  16. using System.Runtime.InteropServices;
  17. using System.Linq;
  18. using System.IO;
  19. namespace AIPagedLod
  20. {
  21. [System.Serializable]
  22. public class PagedLodRangeInfo
  23. {
  24. public float mSelfMinValue;
  25. public float mSelfMaxValue;
  26. public float mChildMinValue;
  27. public float mChildMaxValue;
  28. }
  29. public class OsgbFileLoader : PagedLodFileLoader
  30. {
  31. public List<string> mChildFiles = new List<string>();
  32. public List<PagedLodRangeInfo> mChildRangeValues = new List<PagedLodRangeInfo>();
  33. public float mBoundRadius;
  34. public Vector3 mBoundsCenter = Vector3.zero;
  35. public Vector3 mBoundsBoxMin = Vector3.zero;
  36. public Vector3 mBoundsBoxMax = Vector3.zero;
  37. public OsgbFileLoader(string fileName)
  38. {
  39. mFileName = fileName;
  40. }
  41. public override void LoadObjectMeshInfo()
  42. {
  43. IntPtr ptr = OsgbLoader.OpenOsgbFile(mFileName);
  44. if (ptr == IntPtr.Zero)
  45. {
  46. Debug.LogError("OpenOsgbFile failure " + mFileName);
  47. return;
  48. }
  49. int meshCount = OsgbLoader.GetMeshCount(ptr);
  50. for (int i = 0; i < meshCount; ++i)
  51. {
  52. TileObjectInfo info = new TileObjectInfo();
  53. info.mVertices = OsgbLoader.GetVertexs(ptr, i,mIsDaJiangData);
  54. info.mTriangles = OsgbLoader.GetTriangles(ptr, i, mIsDaJiangData);
  55. info.mTexCoords = OsgbLoader.GetTexCoords(ptr, i);
  56. info.mImageResolutionS = OsgbLoader.GetImageResolutionS(ptr, i);
  57. info.mImageResolutionT = OsgbLoader.GetImageResolutionT(ptr, i);
  58. info.mImageData = OsgbLoader.GetImageData(ptr, i);
  59. info.mTextureFormat = OsgbLoader.GetImageFormat(ptr, i);
  60. info.mUnityTextureFormat = TextureFormat.RGB24;
  61. if (info.mTextureFormat == 33779)
  62. {
  63. info.mUnityTextureFormat = TextureFormat.DXT5;
  64. }
  65. mObjectInfoList.Add(info);
  66. }
  67. OsgbLoader.CloseOsgbFile(ptr);
  68. }
  69. public override void LoadObjectBasicInfo()
  70. {
  71. IntPtr ptr = OsgbLoader.OpenOsgbFileEx(mFileName);
  72. if (ptr == IntPtr.Zero)
  73. {
  74. Debug.LogError("OpenOsgbFile failure " + mFileName);
  75. return;
  76. }
  77. int childCount = OsgbLoader.GetChildFileCount(ptr);
  78. for (int i = 0; i < childCount; ++i)
  79. {
  80. mChildFiles.Add(OsgbLoader.GetChildTileFile(ptr, i));
  81. float[] rangeValues = new float[4];
  82. OsgbLoader.GetRangeList(ptr, i, ref rangeValues[0]);
  83. PagedLodRangeInfo rangeInfo = new PagedLodRangeInfo();
  84. rangeInfo.mSelfMinValue = rangeValues[0];
  85. rangeInfo.mSelfMaxValue = rangeValues[1];
  86. rangeInfo.mChildMinValue = rangeValues[2];
  87. rangeInfo.mChildMaxValue = rangeValues[3];
  88. mChildRangeValues.Add(rangeInfo);
  89. }
  90. if (childCount > 0)
  91. {
  92. mBoundRadius = OsgbLoader.GetBoundRadius(ptr);
  93. float[] center = new float[3];
  94. float[] min = new float[3];
  95. float[] max = new float[3];
  96. OsgbLoader.GetBoundsBox(ptr, ref center[0], ref min[0], ref max[0]);
  97. mBoundsCenter = new Vector3(center[0], -center[1], -center[2]); //局部坐标
  98. mBoundsBoxMin = new Vector3(min[0], -min[1], min[2]); //局部坐标
  99. mBoundsBoxMax = new Vector3(max[0], -max[1], max[2]); //局部坐标
  100. }
  101. OsgbLoader.CloseOsgbFile(ptr);
  102. }
  103. }
  104. }