From eaa960f7aa7170511b0459e5151478bfffff85ca Mon Sep 17 00:00:00 2001 From: lucaw Date: Tue, 13 Aug 2024 11:08:36 -0700 Subject: [PATCH 1/2] Adding the CreateRuntimeAssets function back that was lost in merge 3e66d4354bcee315dff5aca4eee98ba410f62dc8 --- .../Assets/Scripts/BaseFPSAgentController.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/unity/Assets/Scripts/BaseFPSAgentController.cs b/unity/Assets/Scripts/BaseFPSAgentController.cs index 2d8ce92c60..0246f6a67e 100644 --- a/unity/Assets/Scripts/BaseFPSAgentController.cs +++ b/unity/Assets/Scripts/BaseFPSAgentController.cs @@ -7960,6 +7960,32 @@ public ActionFinished CreateRuntimeAsset( return new ActionFinished(success: true, actionReturn: assetData); } + public class UnityLoadableAsset { + public string id; + public string dir; + public string extension = ".msgpack.gz"; + + public ObjectAnnotations annotations = null; + } + + public ActionFinished CreateRuntimeAssets( + List assets, + string dir = null + ) { + foreach (var asset in assets) { + var actionFinished = CreateRuntimeAsset( + id: asset.id, + dir: dir ?? asset.dir, + extension: asset.extension, + annotations: asset.annotations + ); + if (!actionFinished.success) { + return actionFinished; + } + } + return ActionFinished.Success; + } + public void GetStreamingAssetsPath() { actionFinished(success: true, actionReturn: Application.streamingAssetsPath); } From bfdffefc19494d2e807133a11741d18054a09a86 Mon Sep 17 00:00:00 2001 From: lucaw Date: Tue, 13 Aug 2024 12:21:41 -0700 Subject: [PATCH 2/2] Attempting to fix weird shortest path behavior. --- .../Assets/Scripts/BaseFPSAgentController.cs | 233 +++++++++--------- unity/Assets/Scripts/ProceduralTools.cs | 8 +- 2 files changed, 124 insertions(+), 117 deletions(-) diff --git a/unity/Assets/Scripts/BaseFPSAgentController.cs b/unity/Assets/Scripts/BaseFPSAgentController.cs index 0246f6a67e..268a983376 100644 --- a/unity/Assets/Scripts/BaseFPSAgentController.cs +++ b/unity/Assets/Scripts/BaseFPSAgentController.cs @@ -7116,147 +7116,150 @@ protected void SafelyComputeNavMeshPath( navMeshAgent.enabled = true; navmeshSurfaces = - navmeshSurfaces ?? GameObject.FindObjectsOfType(); + navmeshSurfaces + ?? GameObject.FindObjectsOfType(includeInactive: true); - var activeNavMesh = ProceduralTools.activateOnlyNavmeshSurface( - navmeshSurfaces, - navMeshId - ); - // var yPos = activeNavMesh.buildSettings.agentHeight / 2.0f; + try { + var activeNavMesh = ProceduralTools.activateOnlyNavmeshSurface( + navmeshSurfaces, + navMeshId + ); - var pathStartPosition = new Vector3(start.x, start.y, start.z); - var pathTargetPosition = new Vector3(target.x, start.y, target.z); + var pathStartPosition = new Vector3(start.x, start.y, start.z); + var pathTargetPosition = new Vector3(target.x, start.y, target.z); - if (sampleFromNavmesh) { - float floorY = Math.Min( - getFloorY(start.x, start.y, start.z), - getFloorY(target.x, target.y, target.z) - ); - Vector3 startPositionWithFloorY = new Vector3(start.x, floorY, start.z); - Debug.Log($"----- Navmesh floorY {floorY.ToString("F6")}"); - Vector3 targetPositionWithFloorY = new Vector3(target.x, floorY, target.z); - - NavMeshHit startHit; - // startPosition.y = 0.167557f; - bool startWasHit = UnityEngine.AI.NavMesh.SamplePosition( - startPositionWithFloorY, - out startHit, - Math.Max(0.2f, allowedError), - UnityEngine.AI.NavMesh.AllAreas - ); + if (sampleFromNavmesh) { + float floorY = Math.Min( + getFloorY(start.x, start.y, start.z), + getFloorY(target.x, target.y, target.z) + ); + Vector3 startPositionWithFloorY = new Vector3(start.x, floorY, start.z); + Debug.Log($"----- Navmesh floorY {floorY.ToString("F6")}"); + Vector3 targetPositionWithFloorY = new Vector3(target.x, floorY, target.z); + + NavMeshHit startHit; + // startPosition.y = 0.167557f; + bool startWasHit = UnityEngine.AI.NavMesh.SamplePosition( + startPositionWithFloorY, + out startHit, + Math.Max(0.2f, allowedError), + UnityEngine.AI.NavMesh.AllAreas + ); - NavMeshHit targetHit; - bool targetWasHit = UnityEngine.AI.NavMesh.SamplePosition( - targetPositionWithFloorY, - out targetHit, - Math.Max(0.2f, allowedError), - UnityEngine.AI.NavMesh.AllAreas - ); + NavMeshHit targetHit; + bool targetWasHit = UnityEngine.AI.NavMesh.SamplePosition( + targetPositionWithFloorY, + out targetHit, + Math.Max(0.2f, allowedError), + UnityEngine.AI.NavMesh.AllAreas + ); - pathStartPosition = startHit.position; - pathTargetPosition = targetHit.position; + pathStartPosition = startHit.position; + pathTargetPosition = targetHit.position; - if (!startWasHit || !targetWasHit) { - this.GetComponentInChildren().enabled = false; - if (!startWasHit) { - throw new InvalidOperationException( - $"No point on NavMesh near startPosition {startPositionWithFloorY}." - ); + if (!startWasHit || !targetWasHit) { + this.GetComponentInChildren().enabled = false; + if (!startWasHit) { + throw new InvalidOperationException( + $"No point on NavMesh near startPosition {startPositionWithFloorY}." + ); + } + if (!targetWasHit) { + throw new InvalidOperationException( + $"No point on NavMesh near targetPosition {targetPositionWithFloorY}." + ); + } } - if (!targetWasHit) { + + float startOffset = Vector3.Distance( + pathStartPosition, + new Vector3( + startPositionWithFloorY.x, + startHit.position.y, + startPositionWithFloorY.z + ) + ); + float targetOffset = Vector3.Distance( + pathTargetPosition, + new Vector3( + targetPositionWithFloorY.x, + targetHit.position.y, + targetPositionWithFloorY.z + ) + ); + if (startOffset > allowedError && targetOffset > allowedError) { + this.GetComponentInChildren().enabled = false; + var extraDebug = !string.IsNullOrEmpty(debugTargetObjectId) + ? $" For objectId: '{debugTargetObjectId}'" + : ""; throw new InvalidOperationException( - $"No point on NavMesh near targetPosition {targetPositionWithFloorY}." + $"Closest point on NavMesh was too far from the agent: " + + $" (startPosition={startPositionWithFloorY.ToString("F3")}," + + $" closest navmesh position {pathStartPosition.ToString("F3")}) and" + + $" (targetPosition={targetPositionWithFloorY.ToString("F3")}," + + $" closest navmesh position {pathTargetPosition.ToString("F3")})." + + $"{extraDebug}" ); } } - float startOffset = Vector3.Distance( - pathStartPosition, - new Vector3( - startPositionWithFloorY.x, - startHit.position.y, - startPositionWithFloorY.z - ) +#if UNITY_EDITOR + Debug.Log( + $"Attempting to find path from {pathStartPosition.ToString("F6")} to {pathTargetPosition.ToString("F6")}." + ); + Debug.Log( + $"NavmeshAgent start position {navMeshAgent.transform.position.ToString("F6")}" ); - float targetOffset = Vector3.Distance( +#endif + + var prevPosition = this.transform.position; + var prevId = navMeshAgent.agentTypeID; + this.transform.position = pathStartPosition; + // navMeshAgent.radius = 2.0f; + Physics.SyncTransforms(); + // navMeshAgent.agentTypeID = + + // Useless more of unity's broken APIS for runtime >:( + // NavMeshQueryFilter queryFilter = new NavMeshQueryFilter() { + // agentTypeID = queryAgentId, + // areaMask = navMesh.layerMask + // }; + bool pathSuccess = UnityEngine.AI.NavMesh.CalculatePath( + pathStartPosition, pathTargetPosition, - new Vector3( - targetPositionWithFloorY.x, - targetHit.position.y, - targetPositionWithFloorY.z - ) + UnityEngine.AI.NavMesh.AllAreas, + path ); - if (startOffset > allowedError && targetOffset > allowedError) { - this.GetComponentInChildren().enabled = false; + +#if UNITY_EDITOR + Debug.Log( + $"-----Navmesh Pathsuccess {pathSuccess} path status: {path.status} corner lenght {path.corners.Count()} corners: {string.Join(", ", path.corners.Select(c => c.ToString("F6")))}" + ); +#endif + + this.transform.position = prevPosition; + // bool pathSuccess = UnityEngine.AI.NavMesh.CalculatePath( + // startHit.position, targetHit.position, UnityEngine.AI.NavMesh.AllAreas, path + // ); + if (path.status != UnityEngine.AI.NavMeshPathStatus.PathComplete) { var extraDebug = !string.IsNullOrEmpty(debugTargetObjectId) ? $" For objectId: '{debugTargetObjectId}'" : ""; + this.GetComponentInChildren().enabled = false; throw new InvalidOperationException( - $"Closest point on NavMesh was too far from the agent: " - + $" (startPosition={startPositionWithFloorY.ToString("F3")}," - + $" closest navmesh position {pathStartPosition.ToString("F3")}) and" - + $" (targetPosition={targetPositionWithFloorY.ToString("F3")}," - + $" closest navmesh position {pathTargetPosition.ToString("F3")})." + $"Could not find path between {pathStartPosition.ToString("F6")}" + + $" and {pathTargetPosition.ToString("F6")} using the NavMesh." + $"{extraDebug}" ); } - } - #if UNITY_EDITOR - Debug.Log( - $"Attempting to find path from {pathStartPosition.ToString("F6")} to {pathTargetPosition.ToString("F6")}." - ); - Debug.Log( - $"NavmeshAgent start position {navMeshAgent.transform.position.ToString("F6")}" - ); + VisualizePath(pathStartPosition, path); #endif - - var prevPosition = this.transform.position; - var prevId = navMeshAgent.agentTypeID; - this.transform.position = pathStartPosition; - // navMeshAgent.radius = 2.0f; - Physics.SyncTransforms(); - // navMeshAgent.agentTypeID = - - // Useless more of unity's broken APIS for runtime >:( - // NavMeshQueryFilter queryFilter = new NavMeshQueryFilter() { - // agentTypeID = queryAgentId, - // areaMask = navMesh.layerMask - // }; - bool pathSuccess = UnityEngine.AI.NavMesh.CalculatePath( - pathStartPosition, - pathTargetPosition, - UnityEngine.AI.NavMesh.AllAreas, - path - ); - -#if UNITY_EDITOR - Debug.Log( - $"-----Navmesh Pathsuccess {pathSuccess} path status: {path.status} corner lenght {path.corners.Count()} corners: {string.Join(", ", path.corners.Select(c => c.ToString("F6")))}" - ); -#endif - - ProceduralTools.activateAllNavmeshSurfaces(navmeshSurfaces); - - this.transform.position = prevPosition; - // bool pathSuccess = UnityEngine.AI.NavMesh.CalculatePath( - // startHit.position, targetHit.position, UnityEngine.AI.NavMesh.AllAreas, path - // ); - if (path.status != UnityEngine.AI.NavMeshPathStatus.PathComplete) { - var extraDebug = !string.IsNullOrEmpty(debugTargetObjectId) - ? $" For objectId: '{debugTargetObjectId}'" - : ""; this.GetComponentInChildren().enabled = false; - throw new InvalidOperationException( - $"Could not find path between {pathStartPosition.ToString("F6")}" - + $" and {pathTargetPosition.ToString("F6")} using the NavMesh." - + $"{extraDebug}" - ); + } finally { + // Always make sure we reenable the navmeshes. + ProceduralTools.activateAllNavmeshSurfaces(navmeshSurfaces); } -#if UNITY_EDITOR - VisualizePath(pathStartPosition, path); -#endif - this.GetComponentInChildren().enabled = false; } private void randomizeSmoothness(string objectId) { diff --git a/unity/Assets/Scripts/ProceduralTools.cs b/unity/Assets/Scripts/ProceduralTools.cs index a1c7746a10..95eb72b1af 100644 --- a/unity/Assets/Scripts/ProceduralTools.cs +++ b/unity/Assets/Scripts/ProceduralTools.cs @@ -1947,6 +1947,8 @@ public static NavMeshSurfaceExtended activateOnlyNavmeshSurface( IEnumerable navmeshSurfaces, int? navMeshId = null ) { + activateAllNavmeshSurfaces(navmeshSurfaces); + #if UNITY_EDITOR Debug.Log( $"-----Navmesh Query {navMeshId} navmesh count: {navmeshSurfaces.Count()} extended active count: {NavMeshSurfaceExtended.activeSurfaces.Count} navmesh active count: {NavMeshSurface.activeSurfaces.Count}" @@ -2003,8 +2005,10 @@ public static int getNavMeshAgentId(int? navMeshId = null) { } public static NavMeshSurfaceExtended getNavMeshSurfaceForAgentId(int agentId) { - return NavMeshSurface.activeSurfaces.Find(s => s.agentTypeID == agentId) - as NavMeshSurfaceExtended; + return Array.Find( + GameObject.FindObjectsOfType(includeInactive: true), + s => s.agentTypeID == agentId + ) as NavMeshSurfaceExtended; } public static NavMeshBuildSettings navMeshConfigToBuildSettings(