/// <summary> /// A visualization method called in OnDrawGizmos - only used for debugging. /// </summary> public void DebugRender() { if (_grid == null) { return; } Vector3 extraHeight = new Vector3(0f, 0.01f, 0f); foreach (var c in _cellDirsSet) { Vector3 pos = c.Key + extraHeight; VectorFieldCell fieldCell = c.Value; if (fieldCell.pathPortalIndex == -1) { Gizmos.color = Color.white; } else { Gizmos.color = Color.blue; } Gizmos.DrawSphere(pos, 0.25f); Vector3 fieldCellDirection = fieldCell.direction.normalized; Gizmos.DrawLine(pos, pos + fieldCellDirection); } }
private void SmoothFields() { // loop through all the cells that have been fast marched... foreach (var pair in _fastMarchedCellsSet) { Vector3 pos = pair.Key; var cell = _grid.GetCell(pos); int smoothVectors = 1; Vector3 smoothVector = pair.Value; // get all cell neighbours, if there are no unwalkable (or missing neighbours, if built-in containment is true) bool noUnwalkables = GetCellNeighboursAndUnwalkability(cell, _tempWalkableNeighbours, !_allowDiagonals); if (noUnwalkables) { // visit all the neighbours and smooth out using their directions int walkableNeighboursCount = _tempWalkableNeighbours.count; for (int i = 0; i < walkableNeighboursCount; i++) { var neighbour = _tempWalkableNeighbours[i]; Vector3 neighbourPos = neighbour.position; Vector3 dir = GetFastMarchedCellAtPos(neighbourPos); if (dir.sqrMagnitude == 0f) { // if the neighbour has not been visited, then ignore it continue; } smoothVectors++; smoothVector += dir; } smoothVector /= smoothVectors; } // set the vector field cell direction _cellDirsSet[pos] = new VectorFieldCell(smoothVector); } }
private void SmoothFields() { // iterate over all fast marched cells - this uses an enumerator and thus allocates a small amount of memory foreach (var pair in _fastMarchedCellsSet) { Vector3 pos = pair.Key; var cell = GetCellAtPos(pos); int smoothVectors = 1; PlaneVector smoothVector = pair.Value; // get all cell neighbours, if there are no unwalkable (or missing neighbours, if built-in containment is true) bool noUnwalkables = GetCellNeighboursAndUnwalkability(cell, _tempWalkableNeighbours, !_allowDiagonals); if (noUnwalkables) { int walkableNeighboursCount = _tempWalkableNeighbours.count; for (int i = 0; i < walkableNeighboursCount; i++) { var neighbour = _tempWalkableNeighbours[i]; Vector3 neighbourPos = neighbour.position; PlaneVector dir = GetFastMarchedCellAtPos(neighbourPos); if (dir.sqrMagnitude == 0f) { // if the neighbour has not been fast marched, then ignore it continue; } smoothVectors++; smoothVector += dir; } smoothVector /= smoothVectors; } // set the vector field cell direction _cellDirsSet[pos] = new VectorFieldCell(smoothVector); } }
private void HandlePortals() { // loop through all nodes in the path, except the first and last - as they can never be portals int pathCount = _currentPath.count - 1; for (int i = 1; i < pathCount; i++) { var node = _currentPath[i] as PortalCell; if (node == null) { // if the node is not a portal cell, then ignore it continue; } // find all the portal neighbour nodes int neighbourCount = node.neighbourNodes.Length; for (int j = 0; j < neighbourCount; j++) { var neighbour = node.neighbourNodes[j]; Vector3 neighbourPos = neighbour.position; if (!_groupBounds.Contains(neighbourPos)) { // ignore portal neighbour nodes that lie outside of the group bounds area continue; } // all portal neighbour nodes within the group bounds point to the same index in the path, the index where the portal is VectorFieldCell fieldCell; if (_cellDirsSet.TryGetValue(neighbourPos, out fieldCell)) { // ignore nodes not in the vector field _cellDirsSet[neighbourPos] = new VectorFieldCell(fieldCell.direction, i); } } } }
private void SmoothFields() { // iterate over all fast marched cells - this uses an enumerator and thus allocates a small amount of memory foreach (var pair in _fastMarchedCellsSet) { Vector3 pos = pair.Key; var cell = _grid.GetCell(pos); int smoothVectors = 1; PlaneVector smoothVector = pair.Value; // get all cell neighbours, if there are no unwalkable (or missing neighbours, if built-in containment is true) bool noUnwalkables = GetCellNeighboursAndUnwalkability(cell, _tempWalkableNeighbours, !_allowDiagonals); if (noUnwalkables) { int walkableNeighboursCount = _tempWalkableNeighbours.count; for (int i = 0; i < walkableNeighboursCount; i++) { var neighbour = _tempWalkableNeighbours[i]; Vector3 neighbourPos = neighbour.position; PlaneVector dir = GetFastMarchedCellAtPos(neighbourPos); if (dir.sqrMagnitude == 0f) { // if the neighbour has not been fast marched, then ignore it continue; } smoothVectors++; smoothVector += dir; } smoothVector /= smoothVectors; } // set the vector field cell direction _cellDirsSet[pos] = new VectorFieldCell(smoothVector); } }