An alternative solution is to check the visibility of the nodes between the paths (using the line drawing algorithm), and if two nodes are mutually visible, delete all nodes between them. (I will explain this algorithm in detail in the additional information.)
*Additional information*Additional information: How to get a triangle based navmesh from a grid based map?
Option 2: Add a third-party pathfinding module to your engine, such as recastnavigation. It also needs to generate the data it needs through the grid. For example, after generating triangles, convert them to .obj files, and then import them into recast to generate navmesh. The advantage of this is that there are out-of-the-box optimizations available, such as triangle merging, baking different meshes through agent radius, etc.
*Additional information2 If I adopt grid-based pathfinding, how can I reduce the path using the straight line drawing algorithm?
The basic strategy is to check if the nodes of the path obtained by pathfinding are visible between them, and if they are visible, delete all nodes between them. Two nodes are visible if the line between them does not pass through the block.

Bresenham's_line_algorithm is used to rasterize the straight line represented by the geometry into grid coordinates. In this way, we can get the coordinates that need to be detected.

If all the coordinates that need to be detected(The yellow grids in the picture above) are not blocking, then it can be seen that the two nodes are visible.
How to check the whole path? A locally optimal strategy is to start from the starting point, find the visible point farthest from the current point, and then set this point as the current point to repeat until the current point is the end point.

We represent this process in pseudocode:
path = [(1,1),(1,2),...] //original path
reducedPath = [path[0]] //path after optimization
curIndex = 0 //starting point
while(true) {
bool isOK = false
for(int i = len(path) - 1; i > curIndex; i--) {
//Traverse in reverse order to find the farthest visible point
if (isVisible(path[curIndex], path[i])) { //Visibility detection using line drawing algorithm
reducedPath.add(path[i])
curIndex = i
isOK = true
break
}
}
if !isOK {
//No optimization available, start searching from the next node
curIndex++
if (curIndex == len(path)-1) { //The last point has been reached, end the iteration
break
}
}
}
