RealWorldTerrainMonoBase.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace InfinityCode.RealWorldTerrain
  7. {
  8. /// <summary>
  9. /// This class contains all the information about the terrain and Real World Terrain settings.
  10. /// </summary>
  11. public abstract class RealWorldTerrainMonoBase : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// Coordinates borders of terrain
  15. /// </summary>
  16. // public Rect area;
  17. public Bounds bounds;
  18. public bool generateGrass;
  19. public bool generatedRivers;
  20. public bool generateRoads;
  21. /// <summary>
  22. /// Specifies whether to create textures
  23. /// </summary>
  24. public bool generateTextures;
  25. public bool generateTrees;
  26. /// <summary>
  27. /// Specifies whether to create buildings
  28. /// </summary>
  29. public bool generatedBuildings;
  30. /// <summary>
  31. /// Specifies whether to create grass
  32. /// </summary>
  33. public bool generatedGrass;
  34. /// <summary>
  35. /// Specifies whether to create textures
  36. /// </summary>
  37. public bool generatedTextures;
  38. /// <summary>
  39. /// Specifies whether to create trees
  40. /// </summary>
  41. public bool generatedTrees;
  42. /// <summary>
  43. /// Maximal value of elevation
  44. /// </summary>
  45. public double maxElevation;
  46. /// <summary>
  47. /// Minimal value of elevation
  48. /// </summary>
  49. public double minElevation;
  50. /// <summary>
  51. /// Reference to the preferences
  52. /// </summary>
  53. public RealWorldTerrainPrefsBase prefs;
  54. /// <summary>
  55. /// Scale of terrains
  56. /// </summary>
  57. public Vector3 scale;
  58. /// <summary>
  59. /// Size of terrains in world units
  60. /// </summary>
  61. public Vector3 size;
  62. /// <summary>
  63. /// Top latitude
  64. /// </summary>
  65. public double topLatitude;
  66. /// <summary>
  67. /// Top latitude in Mercator projection (0-1)
  68. /// </summary>
  69. public double topMercator;
  70. /// <summary>
  71. /// Left longitude
  72. /// </summary>
  73. public double leftLongitude;
  74. /// <summary>
  75. /// Left longitude in Mercator projection (0-1)
  76. /// </summary>
  77. public double leftMercator;
  78. /// <summary>
  79. /// Bottom latitude
  80. /// </summary>
  81. public double bottomLatitude;
  82. /// <summary>
  83. /// Bottom latitude in Mercator projection (0-1)
  84. /// </summary>
  85. public double bottomMercator;
  86. /// <summary>
  87. /// Right longitude
  88. /// </summary>
  89. public double rightLongitude;
  90. /// <summary>
  91. /// Right longitude in Mercator projection (0-1)
  92. /// </summary>
  93. public double rightMercator;
  94. /// <summary>
  95. /// Width. Right longitude - left longitude
  96. /// </summary>
  97. public double width;
  98. /// <summary>
  99. /// Height. Top latitude - bottom latitude
  100. /// </summary>
  101. public double height;
  102. #if BUILDR2
  103. public List<RealWorldTerrainBuildR2Material> buildR2Facades;
  104. #endif
  105. private Dictionary<string, object> customFields;
  106. public double mercatorWidth;
  107. public double mercatorHeight;
  108. public object this[string key]
  109. {
  110. get
  111. {
  112. if (customFields == null) return null;
  113. object value;
  114. if (!customFields.TryGetValue(key, out value)) return null;
  115. return value;
  116. }
  117. set
  118. {
  119. if (customFields == null) customFields = new Dictionary<string, object>();
  120. if (value != null) customFields[key] = value;
  121. else customFields.Remove(key);
  122. }
  123. }
  124. public void ClearCustomFields()
  125. {
  126. customFields = null;
  127. }
  128. /// <summary>
  129. /// Checks whether the coordinate in terrain area.
  130. /// </summary>
  131. /// <param name="coordinates">Coordinate</param>
  132. /// <returns>True - coordinate in area, False - otherwise.</returns>
  133. public bool Contains(Vector2 coordinates)
  134. {
  135. return Contains(coordinates.x, coordinates.y);
  136. }
  137. /// <summary>
  138. /// Checks whether the coordinate in terrain area.
  139. /// </summary>
  140. /// <param name="lng">Longitude</param>
  141. /// <param name="lat">Latitude</param>
  142. /// <returns>True - coordinate in area, False - otherwise.</returns>
  143. public bool Contains(double lng, double lat)
  144. {
  145. return leftLongitude <= lng && rightLongitude >= lng && topLatitude >= lat && bottomLatitude <= lat;
  146. }
  147. /// <summary>
  148. /// Get altitude by location (coordinates).
  149. /// </summary>
  150. /// <param name="lng">Longitude</param>
  151. /// <param name="lat">Latitude</param>
  152. /// <returns>Altitude (meters)</returns>
  153. public double GetAltitudeByCoordinates(double lng, double lat)
  154. {
  155. if (!Contains(lng, lat)) return 0;
  156. Vector3 worldPosition;
  157. GetWorldPosition(lng, lat, out worldPosition);
  158. Bounds b = new Bounds(bounds.center + transform.position, bounds.size);
  159. Vector3 offset = worldPosition - b.min;
  160. if (prefs.resultType == RealWorldTerrainResultType.terrain)
  161. {
  162. RealWorldTerrainItem currentItem = GetItemByWorldPosition(worldPosition);
  163. return offset.y / currentItem.terrainData.size.y * (currentItem.maxElevation - currentItem.minElevation);
  164. }
  165. return worldPosition.y / b.size.y * (maxElevation - minElevation) + minElevation;
  166. }
  167. /// <summary>
  168. /// Get altitude by location (coordinates).
  169. /// </summary>
  170. /// <param name="lng">Longitude</param>
  171. /// <param name="lat">Latitude</param>
  172. /// <returns>Altitude (meters)</returns>
  173. [Obsolete("Use GetAltitudeByCoordinates")]
  174. public double GetAltitudeByLocation(double lng, double lat)
  175. {
  176. return GetAltitudeByCoordinates(lng, lat);
  177. }
  178. /// <summary>
  179. /// Get altitude by Unity World Position.
  180. /// </summary>
  181. /// <param name="worldPosition">Unity World Position</param>
  182. /// <returns>Altitude (meters)</returns>
  183. public double GetAltitudeByWorldPosition(Vector3 worldPosition)
  184. {
  185. Bounds b = new Bounds(bounds.center + transform.position, bounds.size);
  186. Vector3 offset = worldPosition - b.min;
  187. if (offset.x < 0 || offset.z < 0) return 0;
  188. if (offset.x > b.size.x || offset.z > b.size.z) return 0;
  189. if (prefs.resultType == RealWorldTerrainResultType.terrain)
  190. {
  191. RealWorldTerrainItem currentItem = GetItemByWorldPosition(worldPosition);
  192. return offset.y / currentItem.terrainData.size.y * (currentItem.maxElevation - currentItem.minElevation);
  193. }
  194. return worldPosition.y / b.size.y * (maxElevation - minElevation) + minElevation;
  195. }
  196. /// <summary>
  197. /// Get coordinates under mouse cursor
  198. /// </summary>
  199. /// <param name="coordinates">Geographical coordinates</param>
  200. /// <param name="cam">Camera</param>
  201. /// <returns>True - success, False - otherwise</returns>
  202. public bool GetCoordinatesUnderCursor(out Vector2 coordinates, Camera cam = null)
  203. {
  204. return GetCoordinatesByScreenPosition(Input.mousePosition, out coordinates, cam);
  205. }
  206. /// <summary>
  207. /// Converts the screen coordinates into geographic coordinates.
  208. /// </summary>
  209. /// <param name="screenPosition">Position in screen space</param>
  210. /// <param name="coordinates">Geographic coordinates</param>
  211. /// <param name="cam">Camera</param>
  212. /// <returns>True - screen coordinates on terrains, False - otherwise</returns>
  213. public bool GetCoordinatesByScreenPosition(Vector2 screenPosition, out Vector2 coordinates, Camera cam = null)
  214. {
  215. if (cam == null) cam = Camera.main;
  216. coordinates = Vector2.zero;
  217. RaycastHit[] hits = Physics.RaycastAll(cam.ScreenPointToRay(screenPosition));
  218. for (int i = 0; i < hits.Length; i++)
  219. {
  220. RaycastHit hit = hits[i];
  221. if (hit.collider is TerrainCollider || hit.collider is MeshCollider)
  222. {
  223. RealWorldTerrainItem item = hit.transform.GetComponent<RealWorldTerrainItem>();
  224. if (item != null) return GetCoordinatesByWorldPosition(hit.point, out coordinates);
  225. }
  226. }
  227. return false;
  228. }
  229. /// <summary>
  230. /// Converts the screen coordinates into geographic coordinates.
  231. /// </summary>
  232. /// <param name="screenPosition">Position in screen space</param>
  233. /// <param name="longitude">Longitude</param>
  234. /// <param name="latitude">Latitude</param>
  235. /// <param name="altitude">Altitude</param>
  236. /// <param name="cam">Camera</param>
  237. /// <returns>True - screen coordinates on terrains, False - otherwise</returns>
  238. public bool GetCoordinatesByScreenPosition(Vector2 screenPosition, out double longitude, out double latitude, out double altitude, Camera cam = null)
  239. {
  240. if (cam == null) cam = Camera.main;
  241. longitude = latitude = altitude = 0;
  242. RaycastHit[] hits = Physics.RaycastAll(cam.ScreenPointToRay(screenPosition));
  243. for (int i = 0; i < hits.Length; i++)
  244. {
  245. RaycastHit hit = hits[i];
  246. if (hit.collider is TerrainCollider || hit.collider is MeshCollider)
  247. {
  248. RealWorldTerrainItem item = hit.transform.GetComponent<RealWorldTerrainItem>();
  249. if (item != null) return GetCoordinatesByWorldPosition(hit.point, out longitude, out latitude, out altitude);
  250. }
  251. }
  252. return false;
  253. }
  254. /// <summary>
  255. /// Converts the world coordinates into geographic coordinates.
  256. /// </summary>
  257. /// <param name="worldPosition">Position in Unity World Space</param>
  258. /// <param name="coordinates">Geographic coordinates</param>
  259. /// <returns>True - world coordinates on terrains, False - otherwise</returns>
  260. public bool GetCoordinatesByWorldPosition(Vector3 worldPosition, out Vector2 coordinates)
  261. {
  262. coordinates = new Vector2();
  263. double lng, lat;
  264. bool result = GetCoordinatesByWorldPosition(worldPosition, out lng, out lat);
  265. coordinates.x = (float)lng;
  266. coordinates.y = (float)lat;
  267. return result;
  268. }
  269. /// <summary>
  270. /// Converts the world coordinates into geographic coordinates.
  271. /// </summary>
  272. /// <param name="worldPosition">Position in Unity World Space</param>
  273. /// <param name="longitude">Longitude</param>
  274. /// <param name="latitude">Latitude</param>
  275. /// <returns>True - world coordinates on terrains, False - otherwise</returns>
  276. public bool GetCoordinatesByWorldPosition(Vector3 worldPosition, out double longitude, out double latitude)
  277. {
  278. Bounds b = new Bounds(bounds.center + transform.position, bounds.size);
  279. double wrx = (worldPosition.x - b.min.x) / b.size.x;
  280. double wrz = (b.max.z - worldPosition.z) / b.size.z;
  281. double px = (rightMercator - leftMercator) * wrx + leftMercator;
  282. double pz = (bottomMercator - topMercator) * wrz + topMercator;
  283. RealWorldTerrainUtils.MercatToLatLong(px, pz, out longitude, out latitude);
  284. return b.Contains(worldPosition);
  285. }
  286. /// <summary>
  287. /// Converts the world coordinates into geographic coordinates.
  288. /// </summary>
  289. /// <param name="worldPosition">Position in Unity World Space</param>
  290. /// <param name="longitude">Longitude</param>
  291. /// <param name="latitude">Latitude</param>
  292. /// <param name="altitude">Altitude</param>
  293. /// <returns>True - world coordinates on terrains, False - otherwise</returns>
  294. public bool GetCoordinatesByWorldPosition(Vector3 worldPosition, out double longitude, out double latitude, out double altitude)
  295. {
  296. altitude = 0;
  297. Bounds b = new Bounds(bounds.center + transform.position, bounds.size);
  298. double wrx = (worldPosition.x - b.min.x) / b.size.x;
  299. double wrz = (b.max.z - worldPosition.z) / b.size.z;
  300. double px1 = (rightMercator - leftMercator) * wrx + leftMercator;
  301. double pz = (bottomMercator - topMercator) * wrz + topMercator;
  302. RealWorldTerrainUtils.MercatToLatLong(px1, pz, out longitude, out latitude);
  303. if (b.min.x > worldPosition.x || b.max.x < worldPosition.x || b.min.z > worldPosition.z || b.max.z < worldPosition.z)
  304. {
  305. return false;
  306. }
  307. Vector3 offset = worldPosition - b.min;
  308. if (prefs.resultType == RealWorldTerrainResultType.terrain)
  309. {
  310. RealWorldTerrainItem currentItem = GetItemByWorldPosition(worldPosition);
  311. altitude = (offset.y + currentItem.transform.position.y) / currentItem.terrainData.size.y * (currentItem.maxElevation - currentItem.minElevation) + currentItem.minElevation;
  312. }
  313. else
  314. {
  315. altitude = worldPosition.y / b.size.y * (maxElevation - minElevation) + minElevation;
  316. }
  317. return true;
  318. }
  319. /// <summary>
  320. /// Get RealWorldTerrainItem by Unity World Position.
  321. /// </summary>
  322. /// <param name="worldPosition">Position in Unity World Space</param>
  323. /// <returns>Instance of RealWorldTerrainItem</returns>
  324. public abstract RealWorldTerrainItem GetItemByWorldPosition(Vector3 worldPosition);
  325. /// <summary>
  326. /// Get Unity World Position by geographic coordinates.
  327. /// </summary>
  328. /// <param name="lng">Longitude</param>
  329. /// <param name="lat">Latitde</param>
  330. /// <param name="worldPosition">Position in Unity World Space</param>
  331. /// <returns>True - success, False - otherwise</returns>
  332. public abstract bool GetWorldPosition(double lng, double lat, out Vector3 worldPosition);
  333. /// <summary>
  334. /// Get Unity World Position by geographic coordinates.
  335. /// </summary>
  336. /// <param name="lng">Longitude</param>
  337. /// <param name="lat">Latitude</param>
  338. /// <param name="altitude">Altitude</param>
  339. /// <param name="worldPosition">Position in Unity World Space</param>
  340. /// <returns>True - success, False - otherwise</returns>
  341. public bool GetWorldPosition(double lng, double lat, double altitude, out Vector3 worldPosition)
  342. {
  343. bool result = GetWorldPosition(lng, lat, out worldPosition);
  344. if (result) worldPosition.y = (float)(bounds.size.y * ((altitude - minElevation) / (maxElevation - minElevation)));
  345. return result;
  346. }
  347. /// <summary>
  348. /// Get Unity World Position by geographic coordinates.
  349. /// </summary>
  350. /// <param name="coordinates">Geographic coordinates</param>
  351. /// <param name="worldPosition">Position in Unity World Space</param>
  352. /// <returns></returns>
  353. public abstract bool GetWorldPosition(Vector2 coordinates, out Vector3 worldPosition);
  354. public void SetCoordinates(double x1, double y1, double x2, double y2, double tlx, double tly, double brx, double bry)
  355. {
  356. leftMercator = x1;
  357. topMercator = y1;
  358. rightMercator = x2;
  359. bottomMercator = y2;
  360. leftLongitude = tlx;
  361. rightLongitude = brx;
  362. topLatitude = tly;
  363. bottomLatitude = bry;
  364. width = rightLongitude - leftLongitude;
  365. height = bottomLatitude - topLatitude;
  366. mercatorWidth = rightMercator - leftMercator;
  367. mercatorHeight = bottomMercator - topMercator;
  368. }
  369. }
  370. }