private static void CollectSources(List<NavMeshBuildSource> sources, NavMeshModifier modifier, int area, NavMeshBuilder2dWrapper builder) { var collider = modifier.GetComponent<Collider2D>(); if (collider == null) { return; } if (collider.usedByComposite) { collider = collider.GetComponent<CompositeCollider2D>(); } var src = new NavMeshBuildSource(); src.shape = NavMeshBuildSourceShape.Mesh; src.area = area; Mesh mesh; mesh = builder.GetMesh(collider); if (mesh == null) { Debug.Log($"{collider.name} mesh is null"); return; } if (collider.attachedRigidbody) { src.transform = Matrix4x4.TRS(Vector3.Scale(collider.transform.position, builder.overrideVector), collider.transform.rotation, Vector3.one); } else { src.transform = Matrix4x4.identity; } src.sourceObject = mesh; sources.Add(src); }
private static void CollectSources(List <NavMeshBuildSource> sources, SpriteRenderer sprite, int area, NavMeshBuilder2dWrapper builder) { if (sprite == null) { return; } var src = new NavMeshBuildSource(); src.shape = NavMeshBuildSourceShape.Mesh; src.area = area; Mesh mesh; mesh = builder.GetMesh(sprite.sprite); if (mesh == null) { #if UNITY_EDITOR if (!builder.hideEditorLogs) { Debug.Log($"{sprite.name} mesh is null"); } #endif return; } src.transform = Matrix4x4.TRS(Vector3.Scale(sprite.transform.position, builder.overrideVector), sprite.transform.rotation, sprite.transform.lossyScale); src.sourceObject = mesh; sources.Add(src); }
static private void CollectTileSources(List <NavMeshBuildSource> sources, Tilemap tilemap, int area, NavMeshBuilder2dWrapper builder) { var bound = tilemap.cellBounds; var vec3int = new Vector3Int(0, 0, 0); var size = new Vector3(tilemap.layoutGrid.cellSize.x, tilemap.layoutGrid.cellSize.y, tilemap.layoutGrid.cellSize.y); Mesh sharedMesh = null; Quaternion rot = default; var src = new NavMeshBuildSource(); src.shape = NavMeshBuildSourceShape.Mesh; src.area = area; Mesh mesh; if (builder.useMeshPrefab != null) { sharedMesh = builder.useMeshPrefab.GetComponent <MeshFilter>().sharedMesh; size = builder.useMeshPrefab.transform.localScale; rot = builder.useMeshPrefab.transform.rotation; } for (int i = bound.xMin; i < bound.xMax; i++) { for (int j = bound.yMin; j < bound.yMax; j++) { vec3int.x = i; vec3int.y = j; if (!tilemap.HasTile(vec3int)) { continue; } if (!builder.overrideByGrid && tilemap.GetColliderType(vec3int) == Tile.ColliderType.Sprite) { mesh = builder.GetMesh(tilemap.GetSprite(vec3int)); src.transform = Matrix4x4.Translate(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector)) * tilemap.GetTransformMatrix(vec3int); src.sourceObject = mesh; sources.Add(src); } else if (builder.useMeshPrefab != null || (builder.overrideByGrid && builder.useMeshPrefab != null)) { src.transform = Matrix4x4.TRS(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector), rot, size); src.sourceObject = sharedMesh; sources.Add(src); } else //default to box { var boxsrc = new NavMeshBuildSource(); boxsrc.transform = Matrix4x4.Translate(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector)); boxsrc.shape = NavMeshBuildSourceShape.Box; boxsrc.size = size; boxsrc.area = area; sources.Add(boxsrc); } } } }