Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Written by zylann and available at: https://hterrain-plugin.readthedocs.io/en/latest/#procedural-generation
- # lerp notation updated by Steve Downing
- extends Node3D
- @export var noise_multiplier: float
- # Import classes
- const HTerrain = preload("res://addons/zylann.hterrain/hterrain.gd")
- const HTerrainData = preload("res://addons/zylann.hterrain/hterrain_data.gd")
- const HTerrainTextureSet = preload("res://addons/zylann.hterrain/hterrain_texture_set.gd")
- # You may want to change paths to your own textures
- var grass_texture = load("res://addons/zylann.hterrain/textures/grass.png")
- var sand_texture = load("res://addons/zylann.hterrain/textures/sand.png")
- var leaves_texture = load("res://addons/zylann.hterrain/textures/leaves.png")
- func _ready():
- # Create terrain resource and give it a size.
- # It must be either 513, 1025, 2049 or 4097.
- var terrain_data = HTerrainData.new()
- terrain_data.resize(513)
- var noise = FastNoiseLite.new()
- #var noise_multiplier = 50.0
- # Get access to terrain maps
- var heightmap: Image = terrain_data.get_image(HTerrainData.CHANNEL_HEIGHT)
- var normalmap: Image = terrain_data.get_image(HTerrainData.CHANNEL_NORMAL)
- var splatmap: Image = terrain_data.get_image(HTerrainData.CHANNEL_SPLAT)
- # Generate terrain maps
- # Note: this is an example with some arbitrary formulas,
- # you may want to come up with your owns
- for z in heightmap.get_height():
- for x in heightmap.get_width():
- # Generate height
- var h = noise_multiplier * noise.get_noise_2d(x, z)
- # Getting normal by generating extra heights directly from noise,
- # so map borders won't have seams in case you stitch them
- var h_right = noise_multiplier * noise.get_noise_2d(x + 0.1, z)
- var h_forward = noise_multiplier * noise.get_noise_2d(x, z + 0.1)
- var normal = Vector3(h - h_right, 0.1, h_forward - h).normalized()
- # Generate texture amounts
- var splat = splatmap.get_pixel(x, z)
- var slope = 4.0 * normal.dot(Vector3.UP) - 2.0
- # Sand on the slopes
- var sand_amount = clamp(1.0 - slope, 0.0, 1.0)
- # Leaves below sea level
- var leaves_amount = clamp(0.0 - h, 0.0, 1.0)
- splat = splat.lerp(Color(0,1,0,0), sand_amount)
- splat = splat.lerp(Color(0,0,1,0), leaves_amount)
- heightmap.set_pixel(x, z, Color(h, 0, 0))
- normalmap.set_pixel(x, z, HTerrainData.encode_normal(normal))
- splatmap.set_pixel(x, z, splat)
- # Commit modifications so they get uploaded to the graphics card
- var modified_region = Rect2(Vector2(), heightmap.get_size())
- terrain_data.notify_region_change(modified_region, HTerrainData.CHANNEL_HEIGHT)
- terrain_data.notify_region_change(modified_region, HTerrainData.CHANNEL_NORMAL)
- terrain_data.notify_region_change(modified_region, HTerrainData.CHANNEL_SPLAT)
- # Create texture set
- # NOTE: usually this is not made from script, it can be built with editor tools
- var texture_set = HTerrainTextureSet.new()
- texture_set.set_mode(HTerrainTextureSet.MODE_TEXTURES)
- texture_set.insert_slot(-1)
- texture_set.set_texture(0, HTerrainTextureSet.TYPE_ALBEDO_BUMP, grass_texture)
- texture_set.insert_slot(-1)
- texture_set.set_texture(1, HTerrainTextureSet.TYPE_ALBEDO_BUMP, sand_texture)
- texture_set.insert_slot(-1)
- texture_set.set_texture(2, HTerrainTextureSet.TYPE_ALBEDO_BUMP, leaves_texture)
- # Create terrain node
- var terrain = HTerrain.new()
- terrain.set_shader_type(HTerrain.SHADER_CLASSIC4_LITE)
- terrain.set_data(terrain_data)
- terrain.set_texture_set(texture_set)
- add_child(terrain)
- # No need to call this, but you may need to if you edit the terrain later on
- #terrain.update_collider()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement