summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Navmesh/Jobs/JobCalculateTriangleConnections.cs
blob: b0da1ed81de21889ea487f6d94b40af941ab1b41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine.Assertions;

namespace Pathfinding.Graphs.Navmesh.Jobs {
	/// <summary>
	/// Calculates node connections between triangles within each tile.
	/// Connections between tiles are handled at a later stage in <see cref="JobConnectTiles"/>.
	/// </summary>
	[BurstCompile]
	public struct JobCalculateTriangleConnections : IJob {
		[ReadOnly]
		public NativeArray<TileMesh.TileMeshUnsafe> tileMeshes;
		[WriteOnly]
		public NativeArray<TileNodeConnectionsUnsafe> nodeConnections;

		public struct TileNodeConnectionsUnsafe {
			/// <summary>Stream of packed connection edge infos (from <see cref="Connection.PackShapeEdgeInfo"/>)</summary>
			public Unity.Collections.LowLevel.Unsafe.UnsafeAppendBuffer neighbours;
			/// <summary>Number of neighbours for each triangle</summary>
			public Unity.Collections.LowLevel.Unsafe.UnsafeAppendBuffer neighbourCounts;
		}

		public void Execute () {
			Assert.AreEqual(tileMeshes.Length, nodeConnections.Length);

			var nodeRefs = new NativeParallelHashMap<int2, uint>(128, Allocator.Temp);
			bool duplicates = false;
			for (int ti = 0; ti < tileMeshes.Length; ti++) {
				nodeRefs.Clear();
				var tile = tileMeshes[ti];
				var numIndices = tile.triangles.Length / sizeof(int);
				var neighbours = new Unity.Collections.LowLevel.Unsafe.UnsafeAppendBuffer(numIndices * 2 * 4, 4, Allocator.Persistent);
				var neighbourCounts = new Unity.Collections.LowLevel.Unsafe.UnsafeAppendBuffer(numIndices * 4, 4, Allocator.Persistent);
				const int TriangleIndexBits = 28;
				unsafe {
					Assert.IsTrue(numIndices % 3 == 0);
					var triangles = (int*)tile.triangles.Ptr;
					for (int i = 0, j = 0; i < numIndices; i += 3, j++) {
						duplicates |= !nodeRefs.TryAdd(new int2(triangles[i+0], triangles[i+1]), (uint)j | (0 << TriangleIndexBits));
						duplicates |= !nodeRefs.TryAdd(new int2(triangles[i+1], triangles[i+2]), (uint)j | (1 << TriangleIndexBits));
						duplicates |= !nodeRefs.TryAdd(new int2(triangles[i+2], triangles[i+0]), (uint)j | (2 << TriangleIndexBits));
					}

					for (int i = 0; i < numIndices; i += 3) {
						var cnt = 0;
						for (int edge = 0; edge < 3; edge++) {
							if (nodeRefs.TryGetValue(new int2(triangles[i+((edge+1) % 3)], triangles[i+edge]), out var match)) {
								var other = match & ((1 << TriangleIndexBits) - 1);
								var otherEdge = (int)(match >> TriangleIndexBits);
								neighbours.Add(other);
								var edgeInfo = Connection.PackShapeEdgeInfo((byte)edge, (byte)otherEdge, true, true, true);
								neighbours.Add((int)edgeInfo);
								cnt += 1;
							}
						}
						neighbourCounts.Add(cnt);
					}
				}
				nodeConnections[ti] = new TileNodeConnectionsUnsafe {
					neighbours = neighbours,
					neighbourCounts = neighbourCounts,
				};
			}

			if (duplicates) {
				UnityEngine.Debug.LogWarning("Duplicate triangle edges were found in the input mesh. These have been removed. Are you sure your mesh is suitable for being used as a navmesh directly?\nThis could be caused by the mesh's normals not being consistent.");
			}
		}
	}
}