public BuildingTile GetOrCreateBuildingTile(Vector3 coord) { var coords = CalcBuildingTile(coord).ToArray(); if (!coords.Any()) { return(null); } var bounds = BoundsHelper.CalcBounds(coords); var tile = _tiles.Search(bounds).FirstOrDefault(); if (tile != null) { return(tile); } var tileGo = Instantiate(buildingTilePrefab, transform); tile = tileGo.GetComponent <BuildingTile>(); // TODO maybe pass ground data in var mesher = tileGo.GetComponent <BuildingTileMesher>(); mesher.ground = ground; tile.coords = coords; tile.bounds = bounds; _tiles.Add(bounds, tile); return(tile); }
private void Update() { if (!dirty) { return; } if (tile.coords == null || tile.coords.Length == 0) { return; } var meshBuilder = new CubeMeshBuilder(); var bounds = BoundsHelper.CalcBounds(tile.coords); var size = bounds.size; for (var i = 0; i < size.x; i++) { for (var j = 0; j < size.y; j++) { for (var k = 0; k < size.z; k++) { var c = bounds.position + new Vector3Int(i, j, k); var isGround = ground.IsGround(c); if (!isGround) { continue; } var dirs = new[] { Vector3Int.left, Vector3Int.right, new Vector3Int(0, 0, 1), new Vector3Int(0, 0, -1), Vector3Int.up }; foreach (var dir in dirs) { var next = c + dir; if (ground.IsGround(next)) { continue; } var axis = dir.x != 0 ? Axis.X : dir.y != 0 ? Axis.Y : Axis.Z; var front = axis == Axis.X ? dir.x > 0 : axis == Axis.Y ? dir.y > 0 : dir.z > 0; meshBuilder.AddQuad(axis, front, c, 0); } } } } var mesh = meshBuilder.Build(); meshFilter.mesh = mesh; dirty = false; }