Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing function CreateRuntimeAssets multiple unity loadable assets at a time #1232

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 144 additions & 115 deletions unity/Assets/Scripts/BaseFPSAgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7116,147 +7116,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 {
Lucaweihs marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -7960,6 +7963,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
Loading