2022-11-20 20:13:42 +01:00
|
|
|
extends Spatial
|
|
|
|
|
|
|
|
export var size: int = 1024
|
|
|
|
var heightmap: Image = Image.new()
|
|
|
|
|
|
|
|
signal world_generated
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
# init_heightmap()
|
|
|
|
init_noisemap()
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
func init_heightmap():
|
|
|
|
heightmap.create(size, size, 0, Image.FORMAT_RF)
|
|
|
|
heightmap.lock()
|
|
|
|
for i in range (size):
|
|
|
|
for j in range (size):
|
|
|
|
if (i + j) % 2 == 0:
|
|
|
|
heightmap.set_pixel(i, j, Color(0.1, 0.0, 0.0))
|
|
|
|
else:
|
|
|
|
heightmap.set_pixel(i, j, Color(0.0, 0.0, 0.0))
|
|
|
|
heightmap.unlock()
|
|
|
|
|
|
|
|
|
|
|
|
func init_noisemap():
|
|
|
|
var noise_texture = NoiseTexture.new()
|
|
|
|
var noise_generator = OpenSimplexNoise.new()
|
|
|
|
|
2022-11-21 20:58:13 +01:00
|
|
|
noise_generator.seed = -1626828106#randi()
|
2022-11-20 20:13:42 +01:00
|
|
|
print ("seed: ", noise_generator.seed)
|
|
|
|
noise_generator.octaves = 3
|
|
|
|
noise_generator.period = 5
|
|
|
|
noise_generator.persistence = 0.5
|
|
|
|
noise_generator.lacunarity = 2
|
|
|
|
noise_texture.seamless = true
|
|
|
|
|
|
|
|
heightmap.copy_from(noise_generator.get_seamless_image(size))
|
|
|
|
|
|
|
|
emit_signal("world_generated")
|
|
|
|
|
|
|
|
|
|
|
|
func get_height(coord: Vector2) -> float:
|
2022-11-21 20:58:13 +01:00
|
|
|
return heightmap.get_pixel(coord.x, coord.y).r * 3 - 1.5
|
2022-11-20 20:13:42 +01:00
|
|
|
|