Skip to content

Commit

Permalink
Merge branch 'main' into remove-collider-vis-scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
winthos committed Aug 13, 2024
2 parents 2e7c56a + 6198bd9 commit 5bbb41c
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 124 deletions.
259 changes: 144 additions & 115 deletions unity/Assets/Scripts/BaseFPSAgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6917,147 +6917,150 @@ protected void SafelyComputeNavMeshPath(
navMeshAgent.enabled = true;

navmeshSurfaces =
navmeshSurfaces ?? GameObject.FindObjectsOfType<NavMeshSurfaceExtended>();
navmeshSurfaces
?? GameObject.FindObjectsOfType<NavMeshSurfaceExtended>(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<UnityEngine.AI.NavMeshAgent>().enabled = false;
if (!startWasHit) {
throw new InvalidOperationException(
$"No point on NavMesh near startPosition {startPositionWithFloorY}."
);
if (!startWasHit || !targetWasHit) {
this.GetComponentInChildren<UnityEngine.AI.NavMeshAgent>().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<UnityEngine.AI.NavMeshAgent>().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<UnityEngine.AI.NavMeshAgent>().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<UnityEngine.AI.NavMeshAgent>().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<UnityEngine.AI.NavMeshAgent>().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<UnityEngine.AI.NavMeshAgent>().enabled = false;
}

private void randomizeSmoothness(string objectId) {
Expand Down Expand Up @@ -7761,6 +7764,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<UnityLoadableAsset> 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);
}
Expand Down
8 changes: 6 additions & 2 deletions unity/Assets/Scripts/ProceduralTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,8 @@ public static NavMeshSurfaceExtended activateOnlyNavmeshSurface(
IEnumerable<NavMeshSurfaceExtended> 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}"
Expand Down Expand Up @@ -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<NavMeshSurfaceExtended>(includeInactive: true),
s => s.agentTypeID == agentId
) as NavMeshSurfaceExtended;
}

public static NavMeshBuildSettings navMeshConfigToBuildSettings(
Expand Down
10 changes: 5 additions & 5 deletions unity/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
"com.madsbangh.easybuttons": "https://github.com/madsbangh/EasyButtons.git#upm",
"com.unity.2d.sprite": "1.0.0",
"com.unity.2d.tilemap": "1.0.0",
"com.unity.ai.navigation.components": "https://github.com/Unity-Technologies/NavMeshComponents.git#package",
"com.unity.analytics": "3.6.12",
"com.unity.ide.rider": "3.0.7",
"com.unity.ide.visualstudio": "2.0.12",
"com.unity.ide.vscode": "1.2.4",
"com.unity.ide.visualstudio": "2.0.22",
"com.unity.ide.vscode": "1.2.5",
"com.unity.multiplayer-hlapi": "1.0.8",
"com.unity.postprocessing": "3.1.1",
"com.unity.simulation.capture": "0.0.10-preview.25",
"com.unity.test-framework": "1.1.29",
"com.unity.timeline": "1.6.2",
"com.unity.toolchain.macos-x86_64-linux-x86_64": "1.0.0",
"com.unity.ai.navigation.components": "https://github.com/Unity-Technologies/NavMeshComponents.git#package",
"com.unity.multiplayer-hlapi": "1.0.8",
"com.unity.ugui": "1.0.0",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
Expand Down Expand Up @@ -55,4 +55,4 @@
"com.unity.modules.vr": "1.0.0",
"com.unity.modules.wind": "1.0.0"
}
}
}
4 changes: 2 additions & 2 deletions unity/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"url": "https://packages.unity.com"
},
"com.unity.ide.visualstudio": {
"version": "2.0.12",
"version": "2.0.22",
"depth": 0,
"source": "registry",
"dependencies": {
Expand All @@ -63,7 +63,7 @@
"url": "https://packages.unity.com"
},
"com.unity.ide.vscode": {
"version": "1.2.4",
"version": "1.2.5",
"depth": 0,
"source": "registry",
"dependencies": {},
Expand Down

0 comments on commit 5bbb41c

Please sign in to comment.