in IndoorSceneSynthesis/ConstraintStochasticIndoorSceneGeneration/Custom/CustomScripts/Algorithms/AlfredAgent.cs [748:879]
public void GoToLocation(Vector3 targetLocation, int frameStart = 0, string saveImageFolder = "Assets/Custom/Screenshots/", bool finalLookUp = false) {
//actionList.Clear();
while (viewAngle < 0) {
viewAngle++;
actionList.Add(LowLevelActionType.LookUp);
}
//get shortest path
Vector3 currentLocation = PhysicsController.gameObject.transform.position;
Node startNode = navigationGraph.GetNearestNode(currentLocation);
Node endNode = navigationGraph.GetNearestNode(targetLocation);
navigationGraph.GetShortestPath(startNode, endNode);
Algorithm.Path path = navigationGraph.m_Paths[0];
// foreach (Node node in path.nodes) {
////#if UNITY_EDITOR
//// GameObject _road_sign = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Custom/Debug/road_sign.prefab", typeof(GameObject));
//// var go = Instantiate(_road_sign, node.node_position, Quaternion.identity);
////#endif
// }
//Follow path
int agentRotation = GetControllerRotation();
for (int i = 0; i < path.nodes.Count - 1; ++i) {
Vector3 currentPos = path.nodes[i].node_position;
Vector3 nextPos = path.nodes[i + 1].node_position;
{
int targetRotation = path.GetPathDirectionAtIndex(i);
int rotationIdxV = ((targetRotation - agentRotation) % 4 + 4) % 4;
//Debug.Log(i + "agentrotation targetrot: " + agentRotation + " " + targetRotation + " " + rotationIdxV);
if (rotationIdxV % 4 == 3) {
actionList.Add(LowLevelActionType.RotateLeft);
} else if (rotationIdxV == 2) {
if (UnityEngine.Random.Range(0f, 1f) < 0.5f) {
actionList.Add(LowLevelActionType.RotateRight);
actionList.Add(LowLevelActionType.RotateRight);
} else {
actionList.Add(LowLevelActionType.RotateLeft);
actionList.Add(LowLevelActionType.RotateLeft);
}
} else if (rotationIdxV % 4 == 1) {
actionList.Add(LowLevelActionType.RotateRight);
}
agentRotation = targetRotation;
actionList.Add(LowLevelActionType.MoveAhead);
}
}
//look at target
Vector3 agentLocation = path.nodes[path.nodes.Count - 1].node_position;
float diffX = targetLocation.x - agentLocation.x;
float diffZ = targetLocation.z - agentLocation.z;
int targetRotationF = 0;
if (Mathf.Abs(diffX) > Mathf.Abs(diffZ)) {
if (diffX > 0)
targetRotationF = 1;
else
targetRotationF = -1;
} else {
if (diffZ > 0)
targetRotationF = 0;
else
targetRotationF = 2;
}
int rotationIdx = ((targetRotationF - agentRotation) % 4 + 4) % 4;
if (rotationIdx % 4 == 3) {
actionList.Add(LowLevelActionType.RotateLeft);
} else if (rotationIdx == 2) {
if (UnityEngine.Random.Range(0f, 1f) < 0.5f) {
actionList.Add(LowLevelActionType.RotateRight);
actionList.Add(LowLevelActionType.RotateRight);
} else {
actionList.Add(LowLevelActionType.RotateLeft);
actionList.Add(LowLevelActionType.RotateLeft);
}
} else if (rotationIdx % 4 == 1) {
actionList.Add(LowLevelActionType.RotateRight);
}
agentRotation = targetRotationF;
//TODO: horizon adjust
//if (targetLocation.y < 0.9f) {
// actionList.Add(LowLevelActionType.LookDown);
// viewAngle--;
//}
if (targetLocation.y < 0.6f && !finalLookUp) {
actionList.Add(LowLevelActionType.LookDown);
viewAngle--;
}
if (finalLookUp) {
actionList.Add(LowLevelActionType.LookUp);
actionList.Add(LowLevelActionType.LookUp);
viewAngle += 2 ;
}
// execute action
foreach (LowLevelActionType actionL in actionList) {
//Debug.Log("---------------LowLevelAction----------------" + actionL.ToString());
switch (actionL) {
case LowLevelActionType.MoveAhead:
MoveAhead();
break;
case LowLevelActionType.RotateLeft:
RotateLeft();
break;
case LowLevelActionType.RotateRight:
RotateRight();
break;
case LowLevelActionType.LookUp:
LookUp();
break;
case LowLevelActionType.LookDown:
LookDown();
break;
default:
Debug.LogError("Unknow navigation action");
break;
}
if (frameStart > 0) {
//render frame
RecordFrame(frameStart++, saveImageFolder);
}
}
}