khanat-client/scenes/decors/terrains/terrain.gd

37 lines
1.3 KiB
GDScript3
Raw Normal View History

extends Spatial
var square_grid = preload( "res://ressources/scripts/square_grid.gd" ).new()
export( Vector2 ) var size = Vector2( 512, 512 )
export( float ) var cell_size = 1.0
export( String ) var heightmap_filename = "dunes_heightmap.png"
var heightmap_image = null
func _ready():
self.heightmap_image = Image.new()
self.heightmap_image.load( "res://assets/decors/terrains/heightmaps/" + self.heightmap_filename )
square_grid.set_size( self.heightmap_image.get_size() )
square_grid.cell_size = self.cell_size
$terrain.mesh.size = self.size
func get_height( p_position ):
var cell = self.square_grid.get_cell_at( p_position )
var ratio = Vector2( self.heightmap_image.get_size().x/ self.size.x, self.heightmap_image.get_size().y/self.size.y )
var offset = ( Vector2(self.heightmap_image.get_size().x/2, self.heightmap_image.get_size().y/2 ) )
# var coords = (Vector2( cell.coords.x, cell.coords.z ) + offset) * ratio
var coords = (Vector2( cell.coords.x, cell.coords.z )* ratio) +offset
var pixel_coords = (Vector2( p_position.x, p_position.z ) + offset)
heightmap_image.lock()
# var pixel = heightmap_image.get_pixel( pixel_coords.x, pixel_coords.y )
var pixel = heightmap_image.get_pixel( coords.x, coords.y )
heightmap_image.unlock()
return pixel.r * 100.0