How to smooth out voxel terrain with half-blocks

Godot Version

Godot4.6

Question

I am currenty trying to generate voxel terrain. I want to smooth out the steps between blocks by generating half-blocks. Right now, I am generating half-blocks by using if frac.. , however failing to achieve the result i am aiming for.

How can I:

  1. Generate half-blocks only in a single line at the step edges, rather than forming full surfaces?
  2. Prevent half-blocks from being generated on the top and bottom surfaces of the terrain?

I would greatly appreciate any advice or suggestions. Thank you in advance.

func terrain_generation():

	init_height_map() 
	generate_height_map() 

	for x in SIZE: 
		for z in SIZE: 

			var height = height_map[x][z] 

			var base_h = int(floor(height)) 
			var frac = height - base_h 

			base_h = clamp(base_h, 0, HEIGHT - 1) 

			for y in HEIGHT:

				if y > base_h: 
					if y <= SEA_LEVEL: 
						blocks[x][y][z] = 4 
					else:
						blocks[x][y][z] = 0 

				elif y == base_h: # 地表
					if height < SEA_LEVEL:
						blocks[x][y][z] = 3 
					else:
						blocks[x][y][z] = 1 

				elif y > base_h - 3: 
					if height < SEA_LEVEL:
						blocks[x][y][z] = 3 
					else:
						blocks[x][y][z] = 2 

				else:
					blocks[x][y][z] = 3 

			if frac > 0.75: #Generate Half-Block
				if base_h + 1 < HEIGHT:
					if height >= SEA_LEVEL:
						blocks[x][base_h + 1][z] = 5

Looks like you should add a second pass to smooth the terrain, iterating on every single position and asking for adjacents values, if the difference is less than a threshold you add a half block on the position and keep iterating. You need to decide if the check will be done on which adjacent tiles, lets say, right or right and bottom.
As you stated, respecting the exception cases like bottom and top.

1 Like

Instead of using if frac… i made a function which checks the presence of a taller block next to any block and place the half-block. no need to check it twice and now half-blocks are perfectly generated! Thank you for giving me an idea.

1 Like

Awesome!!!

1 Like