Fix heightmap generation with trees

This commit is contained in:
2024-09-27 23:02:24 +02:00
parent f747dd5b46
commit 68241ae415
25 changed files with 43 additions and 26 deletions

View File

@@ -29,8 +29,10 @@ class World:
editor = Editor(buffering=True) editor = Editor(buffering=True)
buildArea = editor.getBuildArea() buildArea = editor.getBuildArea()
self.coordinates_min = [min(buildArea.begin[i], buildArea.last[i]) for i in range(3)] self.coordinates_min = [
self.coordinates_max = [max(buildArea.begin[i], buildArea.last[i]) for i in range(3)] min(buildArea.begin[i], buildArea.last[i]) for i in range(3)]
self.coordinates_max = [
max(buildArea.begin[i], buildArea.last[i]) for i in range(3)]
self.length_x = self.coordinates_max[0] - self.coordinates_min[0] + 1 self.length_x = self.coordinates_max[0] - self.coordinates_min[0] + 1
self.length_y = self.coordinates_max[1] - self.coordinates_min[1] + 1 self.length_y = self.coordinates_max[1] - self.coordinates_min[1] + 1
@@ -62,7 +64,8 @@ class World:
Add block or list of block to the volume. Add block or list of block to the volume.
""" """
self.volume[volumeCoordinates[0]][volumeCoordinates[1]][volumeCoordinates[2]] = None self.volume[volumeCoordinates[0]][volumeCoordinates[1]
][volumeCoordinates[2]] = None
def getBlockFromCoordinates(self, coordinates): def getBlockFromCoordinates(self, coordinates):
""" """
@@ -85,9 +88,11 @@ class World:
for j in range(-1, 2): for j in range(-1, 2):
for k in range(-1, 2): for k in range(-1, 2):
if not (i == 0 and j == 0 and k == 0): if not (i == 0 and j == 0 and k == 0):
coordinates = (Block.coordinates[0] + i, Block.coordinates[1] + j, Block.coordinates[2] + k) coordinates = (
Block.coordinates[0] + i, Block.coordinates[1] + j, Block.coordinates[2] + k)
if self.isInVolume(coordinates): if self.isInVolume(coordinates):
Block.addNeighbors([self.getBlockFromCoordinates(coordinates)]) Block.addNeighbors(
[self.getBlockFromCoordinates(coordinates)])
def setVolume(self): def setVolume(self):
""" """
@@ -99,7 +104,8 @@ class World:
for x in range(self.coordinates_min[0], self.coordinates_max[0] + 1): for x in range(self.coordinates_min[0], self.coordinates_max[0] + 1):
for y in range(self.coordinates_min[1], self.coordinates_max[1] + 1): for y in range(self.coordinates_min[1], self.coordinates_max[1] + 1):
for z in range(self.coordinates_min[2], self.coordinates_max[2] + 1): for z in range(self.coordinates_min[2], self.coordinates_max[2] + 1):
self.addBlocks([Block((x, y, z), editor.getBlock((x, y, z)).id)]) self.addBlocks(
[Block((x, y, z), editor.getBlock((x, y, z)).id)])
def getData(self): def getData(self):
""" """
@@ -120,8 +126,10 @@ class World:
slice = editor.loadWorldSlice(buildRect) slice = editor.loadWorldSlice(buildRect)
heightmapData = list(np.array(slice.heightmaps["MOTION_BLOCKING_NO_LEAVES"], dtype=np.uint8)) heightmapData = list(
treesmapData = list(np.array(slice.heightmaps["MOTION_BLOCKING"], dtype=np.uint8)) np.array(slice.heightmaps["MOTION_BLOCKING_NO_LEAVES"], dtype=np.uint16))
treesmapData = list(
np.array(slice.heightmaps["MOTION_BLOCKING"], dtype=np.uint16))
for x in range(0, xzDistance[0]): for x in range(0, xzDistance[0]):
for z in range(0, xzDistance[1]): for z in range(0, xzDistance[1]):
@@ -148,7 +156,8 @@ class World:
# print('getData for tree', xzStart[0] + x + i, k, xzStart[1] + z + j) # print('getData for tree', xzStart[0] + x + i, k, xzStart[1] + z + j)
blockNeighbor = slice.getBlock((x + i, k, z + j)) blockNeighbor = slice.getBlock(
(x + i, k, z + j))
if blockNeighbor.id not in lookup.TREES: if blockNeighbor.id not in lookup.TREES:
height += k height += k
number += 1 number += 1
@@ -162,7 +171,9 @@ class World:
else: else:
watermap.putpixel((x, z), 0) watermap.putpixel((x, z), 0)
self.addBlocks([Block((xzStart[0] + x, 100, xzStart[1] + z), block)]) # y set to 100 for 2D # y set to 100 for 2D
self.addBlocks(
[Block((xzStart[0] + x, 100, xzStart[1] + z), block)])
return heightmap, watermap, treesmap return heightmap, watermap, treesmap
@@ -213,7 +224,8 @@ class World:
slice = editor.loadWorldSlice(buildRect) slice = editor.loadWorldSlice(buildRect)
heightmapData = list(np.array(slice.heightmaps["MOTION_BLOCKING_NO_LEAVES"], dtype=np.uint8)) heightmapData = list(
np.array(slice.heightmaps["MOTION_BLOCKING_NO_LEAVES"], dtype=np.uint8))
for x in range(0, xzDistance[0]): for x in range(0, xzDistance[0]):
for z in range(0, xzDistance[1]): for z in range(0, xzDistance[1]):

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 89 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 370 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 89 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 379 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 89 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 90 KiB

After

Width:  |  Height:  |  Size: 962 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 103 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 90 KiB

After

Width:  |  Height:  |  Size: 962 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 899 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 89 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.8 KiB

After

Width:  |  Height:  |  Size: 107 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 228 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 89 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 219 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 89 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 102 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 308 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 202 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 149 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 89 B

View File

@@ -31,14 +31,16 @@ def remove_trees(heightmap: Union[str, Image], treesmap: Union[str, Image], mask
if mask.getpixel((x, z)) != 0 and treesmap.getpixel((x, z)) > 0: if mask.getpixel((x, z)) != 0 and treesmap.getpixel((x, z)) > 0:
tree_area = morphology.flood(treesmap, (z, x), tolerance=1) tree_area = morphology.flood(treesmap, (z, x), tolerance=1)
blend = img.blend(img.fromarray(tree_area).convert('L'), removed_treesmap.convert('L'), 0.5) blend = img.blend(img.fromarray(tree_area).convert(
'L'), removed_treesmap.convert('L'), 0.5)
array = np.array(blend.convert('L')) array = np.array(blend.convert('L'))
bool_array = array > 1 bool_array = array > 1
removed_treesmap = img.fromarray(bool_array) removed_treesmap = img.fromarray(bool_array)
y = heightmap.getpixel((x, z)) y = heightmap.getpixel((x, z))
y_top = removed_treesmap.getpixel((x, z)) y_top = removed_treesmap.getpixel((x, z))
geometry.placeLine(editor, (start[0] + x, y+1, start[1] + z), (start[0] + x, y_top, start[1] + z), Block('air')) geometry.placeLine(
editor, (start[0] + x, y+1, start[1] + z), (start[0] + x, y_top, start[1] + z), Block('air'))
removed_treesmap.save('./world_maker/data/removed_treesmap.png') removed_treesmap.save('./world_maker/data/removed_treesmap.png')
print("[Remove tree] Done.") print("[Remove tree] Done.")
@@ -82,11 +84,14 @@ def smooth_terrain(heightmap: Union[str, Image], heightmap_smooth: Union[str, Im
if block.id not in smoothable_blocks: if block.id not in smoothable_blocks:
continue continue
if delta > 0: if delta > 0:
geometry.placeLine(editor, (start[0] + x, y, start[1] + z), (start[0] + x, y_smooth, start[1] + z), Block('air')) geometry.placeLine(
editor.placeBlock((start[0] + x, y_smooth, start[1] + z), block) editor, (start[0] + x, y, start[1] + z), (start[0] + x, y_smooth, start[1] + z), Block('air'))
editor.placeBlock(
(start[0] + x, y_smooth, start[1] + z), block)
else: else:
geometry.placeLine(editor, (start[0] + x, y, start[1] + z), (start[0] + x, y_smooth, start[1] + z), block) geometry.placeLine(
editor, (start[0] + x, y, start[1] + z), (start[0] + x, y_smooth, start[1] + z), block)
smooth_terrain_delta.save('./world_maker/data/smooth_terrain_delta.png') smooth_terrain_delta.save('./world_maker/data/smooth_terrain_delta.png')
print("[Smooth terrain] Done.") print("[Smooth terrain] Done.")