NavMeshSurface.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using System.Collections.Generic;
  2. namespace UnityEngine.AI
  3. {
  4. public enum CollectObjects
  5. {
  6. All = 0,
  7. Volume = 1,
  8. Children = 2,
  9. }
  10. [ExecuteInEditMode]
  11. [DefaultExecutionOrder(-102)]
  12. [AddComponentMenu("Navigation/NavMeshSurface", 30)]
  13. [HelpURL("https://github.com/Unity-Technologies/NavMeshComponents#documentation-draft")]
  14. public class NavMeshSurface : MonoBehaviour
  15. {
  16. [SerializeField]
  17. int m_AgentTypeID;
  18. public int agentTypeID { get { return m_AgentTypeID; } set { m_AgentTypeID = value; } }
  19. [SerializeField]
  20. CollectObjects m_CollectObjects = CollectObjects.All;
  21. public CollectObjects collectObjects { get { return m_CollectObjects; } set { m_CollectObjects = value; } }
  22. [SerializeField]
  23. Vector3 m_Size = new Vector3(10.0f, 10.0f, 10.0f);
  24. public Vector3 size { get { return m_Size; } set { m_Size = value; } }
  25. [SerializeField]
  26. Vector3 m_Center = new Vector3(0, 2.0f, 0);
  27. public Vector3 center { get { return m_Center; } set { m_Center = value; } }
  28. [SerializeField]
  29. LayerMask m_LayerMask = ~0;
  30. public LayerMask layerMask { get { return m_LayerMask; } set { m_LayerMask = value; } }
  31. [SerializeField]
  32. NavMeshCollectGeometry m_UseGeometry = NavMeshCollectGeometry.RenderMeshes;
  33. public NavMeshCollectGeometry useGeometry { get { return m_UseGeometry; } set { m_UseGeometry = value; } }
  34. [SerializeField]
  35. int m_DefaultArea;
  36. public int defaultArea { get { return m_DefaultArea; } set { m_DefaultArea = value; } }
  37. [SerializeField]
  38. bool m_IgnoreNavMeshAgent = true;
  39. public bool ignoreNavMeshAgent { get { return m_IgnoreNavMeshAgent; } set { m_IgnoreNavMeshAgent = value; } }
  40. [SerializeField]
  41. bool m_IgnoreNavMeshObstacle = true;
  42. public bool ignoreNavMeshObstacle { get { return m_IgnoreNavMeshObstacle; } set { m_IgnoreNavMeshObstacle = value; } }
  43. [SerializeField]
  44. bool m_OverrideTileSize;
  45. public bool overrideTileSize { get { return m_OverrideTileSize; } set { m_OverrideTileSize = value; } }
  46. [SerializeField]
  47. int m_TileSize = 256;
  48. public int tileSize { get { return m_TileSize; } set { m_TileSize = value; } }
  49. [SerializeField]
  50. bool m_OverrideVoxelSize;
  51. public bool overrideVoxelSize { get { return m_OverrideVoxelSize; } set { m_OverrideVoxelSize = value; } }
  52. [SerializeField]
  53. float m_VoxelSize;
  54. public float voxelSize { get { return m_VoxelSize; } set { m_VoxelSize = value; } }
  55. // Currently not supported advanced options
  56. [SerializeField]
  57. bool m_BuildHeightMesh;
  58. public bool buildHeightMesh { get { return m_BuildHeightMesh; } set { m_BuildHeightMesh = value; } }
  59. // Reference to whole scene navmesh data asset.
  60. [UnityEngine.Serialization.FormerlySerializedAs("m_BakedNavMeshData")]
  61. [SerializeField]
  62. NavMeshData m_NavMeshData;
  63. public NavMeshData navMeshData { get { return m_NavMeshData; } set { m_NavMeshData = value; } }
  64. // Do not serialize - runtime only state.
  65. NavMeshDataInstance m_NavMeshDataInstance;
  66. Vector3 m_LastPosition = Vector3.zero;
  67. Quaternion m_LastRotation = Quaternion.identity;
  68. static readonly List<NavMeshSurface> s_NavMeshSurfaces = new List<NavMeshSurface>();
  69. public static List<NavMeshSurface> activeSurfaces
  70. {
  71. get { return s_NavMeshSurfaces; }
  72. }
  73. void OnEnable()
  74. {
  75. Register(this);
  76. AddData();
  77. }
  78. void OnDisable()
  79. {
  80. RemoveData();
  81. Unregister(this);
  82. }
  83. public void AddData()
  84. {
  85. if (m_NavMeshDataInstance.valid)
  86. return;
  87. if (m_NavMeshData != null)
  88. {
  89. m_NavMeshDataInstance = NavMesh.AddNavMeshData(m_NavMeshData, transform.position, transform.rotation);
  90. m_NavMeshDataInstance.owner = this;
  91. }
  92. m_LastPosition = transform.position;
  93. m_LastRotation = transform.rotation;
  94. }
  95. public void RemoveData()
  96. {
  97. m_NavMeshDataInstance.Remove();
  98. m_NavMeshDataInstance = new NavMeshDataInstance();
  99. }
  100. public NavMeshBuildSettings GetBuildSettings()
  101. {
  102. var buildSettings = NavMesh.GetSettingsByID(m_AgentTypeID);
  103. if (buildSettings.agentTypeID == -1)
  104. {
  105. Debug.LogWarning("No build settings for agent type ID " + agentTypeID, this);
  106. buildSettings.agentTypeID = m_AgentTypeID;
  107. }
  108. if (overrideTileSize)
  109. {
  110. buildSettings.overrideTileSize = true;
  111. buildSettings.tileSize = tileSize;
  112. }
  113. if (overrideVoxelSize)
  114. {
  115. buildSettings.overrideVoxelSize = true;
  116. buildSettings.voxelSize = voxelSize;
  117. }
  118. return buildSettings;
  119. }
  120. public void BuildNavMesh()
  121. {
  122. var sources = CollectSources();
  123. // Use unscaled bounds - this differs in behaviour from e.g. collider components.
  124. // But is similar to reflection probe - and since navmesh data has no scaling support - it is the right choice here.
  125. var sourcesBounds = new Bounds(m_Center, Abs(m_Size));
  126. if (m_CollectObjects == CollectObjects.All || m_CollectObjects == CollectObjects.Children)
  127. {
  128. sourcesBounds = CalculateWorldBounds(sources);
  129. }
  130. var data = NavMeshBuilder.BuildNavMeshData(GetBuildSettings(),
  131. sources, sourcesBounds, transform.position, transform.rotation);
  132. if (data != null)
  133. {
  134. data.name = gameObject.name;
  135. RemoveData();
  136. m_NavMeshData = data;
  137. if (isActiveAndEnabled)
  138. AddData();
  139. }
  140. }
  141. public AsyncOperation UpdateNavMesh(NavMeshData data)
  142. {
  143. var sources = CollectSources();
  144. // Use unscaled bounds - this differs in behaviour from e.g. collider components.
  145. // But is similar to reflection probe - and since navmesh data has no scaling support - it is the right choice here.
  146. var sourcesBounds = new Bounds(m_Center, Abs(m_Size));
  147. if (m_CollectObjects == CollectObjects.All || m_CollectObjects == CollectObjects.Children)
  148. sourcesBounds = CalculateWorldBounds(sources);
  149. return NavMeshBuilder.UpdateNavMeshDataAsync(data, GetBuildSettings(), sources, sourcesBounds);
  150. }
  151. static void Register(NavMeshSurface surface)
  152. {
  153. if (s_NavMeshSurfaces.Count == 0)
  154. NavMesh.onPreUpdate += UpdateActive;
  155. if (!s_NavMeshSurfaces.Contains(surface))
  156. s_NavMeshSurfaces.Add(surface);
  157. }
  158. static void Unregister(NavMeshSurface surface)
  159. {
  160. s_NavMeshSurfaces.Remove(surface);
  161. if (s_NavMeshSurfaces.Count == 0)
  162. NavMesh.onPreUpdate -= UpdateActive;
  163. }
  164. static void UpdateActive()
  165. {
  166. for (var i = 0; i < s_NavMeshSurfaces.Count; ++i)
  167. s_NavMeshSurfaces[i].UpdateDataIfTransformChanged();
  168. }
  169. void AppendModifierVolumes(ref List<NavMeshBuildSource> sources)
  170. {
  171. // Modifiers
  172. List<NavMeshModifierVolume> modifiers;
  173. if (m_CollectObjects == CollectObjects.Children)
  174. {
  175. modifiers = new List<NavMeshModifierVolume>(GetComponentsInChildren<NavMeshModifierVolume>());
  176. modifiers.RemoveAll(x => !x.isActiveAndEnabled);
  177. }
  178. else
  179. {
  180. modifiers = NavMeshModifierVolume.activeModifiers;
  181. }
  182. foreach (var m in modifiers)
  183. {
  184. if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
  185. continue;
  186. if (!m.AffectsAgentType(m_AgentTypeID))
  187. continue;
  188. var mcenter = m.transform.TransformPoint(m.center);
  189. var scale = m.transform.lossyScale;
  190. var msize = new Vector3(m.size.x * Mathf.Abs(scale.x), m.size.y * Mathf.Abs(scale.y), m.size.z * Mathf.Abs(scale.z));
  191. var src = new NavMeshBuildSource();
  192. src.shape = NavMeshBuildSourceShape.ModifierBox;
  193. src.transform = Matrix4x4.TRS(mcenter, m.transform.rotation, Vector3.one);
  194. src.size = msize;
  195. src.area = m.area;
  196. sources.Add(src);
  197. }
  198. }
  199. List<NavMeshBuildSource> CollectSources()
  200. {
  201. var sources = new List<NavMeshBuildSource>();
  202. var markups = new List<NavMeshBuildMarkup>();
  203. List<NavMeshModifier> modifiers;
  204. if (m_CollectObjects == CollectObjects.Children)
  205. {
  206. modifiers = new List<NavMeshModifier>(GetComponentsInChildren<NavMeshModifier>());
  207. modifiers.RemoveAll(x => !x.isActiveAndEnabled);
  208. }
  209. else
  210. {
  211. modifiers = NavMeshModifier.activeModifiers;
  212. }
  213. foreach (var m in modifiers)
  214. {
  215. if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
  216. continue;
  217. if (!m.AffectsAgentType(m_AgentTypeID))
  218. continue;
  219. var markup = new NavMeshBuildMarkup();
  220. markup.root = m.transform;
  221. markup.overrideArea = m.overrideArea;
  222. markup.area = m.area;
  223. markup.ignoreFromBuild = m.ignoreFromBuild;
  224. markups.Add(markup);
  225. }
  226. if (m_CollectObjects == CollectObjects.All)
  227. {
  228. NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
  229. }
  230. else if (m_CollectObjects == CollectObjects.Children)
  231. {
  232. NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
  233. }
  234. else if (m_CollectObjects == CollectObjects.Volume)
  235. {
  236. Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
  237. var worldBounds = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
  238. NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
  239. }
  240. if (m_IgnoreNavMeshAgent)
  241. sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent<NavMeshAgent>() != null));
  242. if (m_IgnoreNavMeshObstacle)
  243. sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent<NavMeshObstacle>() != null));
  244. AppendModifierVolumes(ref sources);
  245. return sources;
  246. }
  247. static Vector3 Abs(Vector3 v)
  248. {
  249. return new Vector3(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
  250. }
  251. static Bounds GetWorldBounds(Matrix4x4 mat, Bounds bounds)
  252. {
  253. var absAxisX = Abs(mat.MultiplyVector(Vector3.right));
  254. var absAxisY = Abs(mat.MultiplyVector(Vector3.up));
  255. var absAxisZ = Abs(mat.MultiplyVector(Vector3.forward));
  256. var worldPosition = mat.MultiplyPoint(bounds.center);
  257. var worldSize = absAxisX * bounds.size.x + absAxisY * bounds.size.y + absAxisZ * bounds.size.z;
  258. return new Bounds(worldPosition, worldSize);
  259. }
  260. Bounds CalculateWorldBounds(List<NavMeshBuildSource> sources)
  261. {
  262. // Use the unscaled matrix for the NavMeshSurface
  263. Matrix4x4 worldToLocal = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
  264. worldToLocal = worldToLocal.inverse;
  265. var result = new Bounds();
  266. foreach (var src in sources)
  267. {
  268. switch (src.shape)
  269. {
  270. case NavMeshBuildSourceShape.Mesh:
  271. {
  272. var m = src.sourceObject as Mesh;
  273. result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, m.bounds));
  274. break;
  275. }
  276. case NavMeshBuildSourceShape.Terrain:
  277. {
  278. // Terrain pivot is lower/left corner - shift bounds accordingly
  279. var t = src.sourceObject as TerrainData;
  280. result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, new Bounds(0.5f * t.size, t.size)));
  281. break;
  282. }
  283. case NavMeshBuildSourceShape.Box:
  284. case NavMeshBuildSourceShape.Sphere:
  285. case NavMeshBuildSourceShape.Capsule:
  286. case NavMeshBuildSourceShape.ModifierBox:
  287. result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, new Bounds(Vector3.zero, src.size)));
  288. break;
  289. }
  290. }
  291. // Inflate the bounds a bit to avoid clipping co-planar sources
  292. result.Expand(0.1f);
  293. return result;
  294. }
  295. bool HasTransformChanged()
  296. {
  297. if (m_LastPosition != transform.position) return true;
  298. if (m_LastRotation != transform.rotation) return true;
  299. return false;
  300. }
  301. void UpdateDataIfTransformChanged()
  302. {
  303. if (HasTransformChanged())
  304. {
  305. RemoveData();
  306. AddData();
  307. }
  308. }
  309. #if UNITY_EDITOR
  310. bool UnshareNavMeshAsset()
  311. {
  312. // Nothing to unshare
  313. if (m_NavMeshData == null)
  314. return false;
  315. // Prefab parent owns the asset reference
  316. var prefabType = UnityEditor.PrefabUtility.GetPrefabType(this);
  317. if (prefabType == UnityEditor.PrefabType.Prefab)
  318. return false;
  319. // An instance can share asset reference only with its prefab parent
  320. var prefab = UnityEditor.PrefabUtility.GetPrefabParent(this) as NavMeshSurface;
  321. if (prefab != null && prefab.navMeshData == navMeshData)
  322. return false;
  323. // Don't allow referencing an asset that's assigned to another surface
  324. for (var i = 0; i < s_NavMeshSurfaces.Count; ++i)
  325. {
  326. var surface = s_NavMeshSurfaces[i];
  327. if (surface != this && surface.m_NavMeshData == m_NavMeshData)
  328. return true;
  329. }
  330. // Asset is not referenced by known surfaces
  331. return false;
  332. }
  333. void OnValidate()
  334. {
  335. if (UnshareNavMeshAsset())
  336. {
  337. Debug.LogWarning("Duplicating NavMeshSurface does not duplicate the referenced navmesh data", this);
  338. m_NavMeshData = null;
  339. }
  340. var settings = NavMesh.GetSettingsByID(m_AgentTypeID);
  341. if (settings.agentTypeID != -1)
  342. {
  343. // When unchecking the override control, revert to automatic value.
  344. const float kMinVoxelSize = 0.01f;
  345. if (!m_OverrideVoxelSize)
  346. m_VoxelSize = settings.agentRadius / 3.0f;
  347. if (m_VoxelSize < kMinVoxelSize)
  348. m_VoxelSize = kMinVoxelSize;
  349. // When unchecking the override control, revert to default value.
  350. const int kMinTileSize = 16;
  351. const int kMaxTileSize = 1024;
  352. const int kDefaultTileSize = 256;
  353. if (!m_OverrideTileSize)
  354. m_TileSize = kDefaultTileSize;
  355. // Make sure tilesize is in sane range.
  356. if (m_TileSize < kMinTileSize)
  357. m_TileSize = kMinTileSize;
  358. if (m_TileSize > kMaxTileSize)
  359. m_TileSize = kMaxTileSize;
  360. }
  361. }
  362. #endif
  363. }
  364. }