GodotComponentTest/scenes/World.gd

46 lines
1016 B
GDScript

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()
noise_generator.seed = -1626828106#randi()
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:
return heightmap.get_pixel(coord.x, coord.y).r * 3 - 1.5