From 64d0280b1fdcf8c967b3c85831a622c461b6b44c Mon Sep 17 00:00:00 2001 From: kayomn Date: Fri, 13 Jan 2023 10:08:56 +0000 Subject: [PATCH] Refactor terrain map code into own file --- editor.scn | 4 +- editor/editable_terrain.gd | 100 +++++++++++++++++++++++++++++++++++++ project.godot | 6 +++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 editor/editable_terrain.gd diff --git a/editor.scn b/editor.scn index 3f3637f..7107b41 100644 --- a/editor.scn +++ b/editor.scn @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:3993b07a19be59808c02d1e6c008fe0772bef23a9e6bced821639d9a7d3b81b3 -size 7836 +oid sha256:c94d6be56f13b82f5ddf4c6904a65e6b213dc2ae16cdd837b112f31e80ac8439 +size 6867 diff --git a/editor/editable_terrain.gd b/editor/editable_terrain.gd new file mode 100644 index 0000000..1a0f5d7 --- /dev/null +++ b/editor/editable_terrain.gd @@ -0,0 +1,100 @@ +class_name EditableTerrain extends Node + +var _image := Image.create(1, 1, false, Image.FORMAT_RGBAF) + +## +## +## +@export +var channels := Vector3.ZERO + +## +## +## +@export +var size := Vector2i.ZERO: + get: + return size + + set(value): + _image.resize(value.x, value.y) + _image.fill(Color(0.0, 0.0, 0.0, 0.5)) + + size = value + +## +## +## +func get_image() -> void: + return _image + +## +## +## +func paint(point: Vector2i, elevation_level: float, + brush_mask: Image, brush_scale: float, brush_intensity: float) -> void: + + # Convert worldspace point to image-space coordinates for brush and calculate drawable area. + var brush_mask_size := brush_mask.get_size() + var brush_size := brush_mask_size * brush_scale + var image_size := _image.get_size() + + var draw_area := Rect2i(Vector2i.ZERO, image_size).intersection( + Rect2i((point - Vector2i(brush_size * 0.5)) + Vector2i(image_size * 0.5), brush_size)) + + if draw_area.has_area(): + var scaled_brush_mask_size := brush_mask_size / draw_area.size + + elevation_level = clampf(elevation_level, -1.0, 1.0) + + for y in draw_area.size.y: + var brush_mask_y := int(y * scaled_brush_mask_size.y) + + for x in draw_area.size.x: + var terrain_map_coord := draw_area.position + Vector2i(x, y) + var pixel := _image.get_pixelv(terrain_map_coord) + + var mask := brush_mask.get_pixel( + int(x * scaled_brush_mask_size.x), brush_mask_y).a + + var mask_intensity := brush_intensity * mask + + _image.set_pixelv(terrain_map_coord, Color( + lerpf(pixel.r, channels.x, mask_intensity), + lerpf(pixel.g, channels.y, mask_intensity), + lerpf(pixel.b, channels.z, mask_intensity), + lerpf(pixel.a, pixel.a + (elevation_level * mask), brush_intensity))) + +## +## +## +func smooth(point: Vector2i, smoothing_level: float, + brush_mask: Image, brush_scale: float, brush_intensity: float) -> void: + + # Convert worldspace point to image-space coordinates for brush and calculate drawable area. + var brush_mask_size := brush_mask.get_size() + var brush_size := brush_mask_size * brush_scale + var terrain_map_size := _image.get_size() + + var draw_area := Rect2i(Vector2i.ZERO, terrain_map_size).intersection( + Rect2i((point - Vector2i(brush_size * 0.5)) +\ + Vector2i(terrain_map_size * 0.5), brush_size)) + + if draw_area.has_area(): + var scaled_brush_mask_size := brush_mask_size / draw_area.size + + for y in draw_area.size.y: + var brush_mask_y := int(y * scaled_brush_mask_size.y) + + for x in draw_area.size.x: + var terrain_map_coord := draw_area.position + Vector2i(x, y) + var pixel := _image.get_pixelv(terrain_map_coord) + + var mask_intensity := brush_intensity * brush_mask.get_pixel( + int(x * scaled_brush_mask_size.x), brush_mask_y).a + + _image.set_pixelv(terrain_map_coord, Color( + lerpf(pixel.r, channels.x, mask_intensity), + lerpf(pixel.g, channels.y, mask_intensity), + lerpf(pixel.b, channels.z, mask_intensity), + lerpf(pixel.a, smoothing_level, mask_intensity))) diff --git a/project.godot b/project.godot index 7e196d1..b91a50f 100644 --- a/project.godot +++ b/project.godot @@ -9,6 +9,11 @@ config_version=5 _global_script_classes=[{ +"base": "Node", +"class": &"EditableTerrain", +"language": &"GDScript", +"path": "res://editor/editable_terrain.gd" +}, { "base": "Node3D", "class": &"PlayerController", "language": &"GDScript", @@ -25,6 +30,7 @@ _global_script_classes=[{ "path": "res://terrain_instance_3d.gd" }] _global_script_class_icons={ +"EditableTerrain": "", "PlayerController": "", "Settings": "", "TerrainInstance3D": ""