You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Gridmap/ProceduralGenerator.gd

52 lines
1.6 KiB

extends GridMap
export(int) var ChunkWidth: int = 0
export(int) var ChunkHeight: int = 0
var heightMapTexture = NoiseTexture.new()
onready var player = $Player
func _ready():
randomize()
heightMapTexture.width = 512
heightMapTexture.height = 512
heightMapTexture.noise = OpenSimplexNoise.new()
heightMapTexture.noise.seed = randi()
print("Seed: " + str(heightMapTexture.noise.seed))
setGridChunk(0, ChunkWidth, 0, ChunkHeight)
func generateGridChunk(playerPos: Vector3) -> void:
clear()
var chunkCenter = world_to_map(playerPos)
var rowStart: float = chunkCenter.z - (ChunkHeight / 2)
var rowEnd: float = chunkCenter.z + (ChunkHeight / 2)
var columnStart: float = chunkCenter.x - (ChunkWidth / 2)
var columnEnd: float = chunkCenter.x + (ChunkWidth / 2)
setGridChunk(columnStart, columnEnd, rowStart, rowEnd)
func setGridChunk(columnStart: float, columnEnd: float, rowStart: float, rowEnd: float) -> void:
if columnStart < 0 :
columnStart = 0
if rowStart < 0 :
rowStart = 0
if columnEnd > heightMapTexture.width :
columnEnd = heightMapTexture.width - 1
if rowEnd > heightMapTexture.height :
rowEnd = heightMapTexture.height - 1
for mz in range(rowStart, rowEnd):
for mx in range(columnStart, columnEnd):
var noiseValue: float = heightMapTexture.noise.get_noise_2d(mx, mz)
var weight: float = (noiseValue + 1) / 2
var height: float = floor(lerp(0, 8, weight)) * 2
for my in range(0, height):
var meshID = floor(my / 2)
set_cell_item(mx, my * 2, mz, meshID, 0)
func _on_MapUpdateTimer_timeout():
generateGridChunk(player.global_transform.origin)