From 6a4cb1a4317cc0faa7fd228afbd80a1d47a43623 Mon Sep 17 00:00:00 2001 From: kayomn Date: Fri, 13 Jan 2023 17:32:48 +0000 Subject: [PATCH] Fix leaky abstractions in EditableTerrain --- editor.scn | 4 +-- editor/editable_terrain.gd | 68 +++++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/editor.scn b/editor.scn index 7107b41..5e9f5be 100644 --- a/editor.scn +++ b/editor.scn @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c94d6be56f13b82f5ddf4c6904a65e6b213dc2ae16cdd837b112f31e80ac8439 -size 6867 +oid sha256:84bbd898923e8925575ebb3da006cf31db975f1c4f01267f65c36b41f4b0aa9e +size 6804 diff --git a/editor/editable_terrain.gd b/editor/editable_terrain.gd index 1a0f5d7..9367480 100644 --- a/editor/editable_terrain.gd +++ b/editor/editable_terrain.gd @@ -1,15 +1,37 @@ class_name EditableTerrain extends Node +const _DEFAULT_COLOR := Color(0.0, 0.0, 0.0, 0.5) + +## +## Blank sample value. +## +const BLANK := Color(0.0, 0.0, 0.0, 0.0) + var _image := Image.create(1, 1, false, Image.FORMAT_RGBAF) ## +## Terrain texture channel mixing values. ## +## Affects the color that terrain is mixed into during editing operations like [method paint]. ## @export var channels := Vector3.ZERO ## +## Tracked [TerrainInstance3D] to echo all terrain editing changes to. ## +var instance: TerrainInstance3D = null: + get: + return instance + + set(value): + if (value != null) and (value != instance): + value.terrain_map = ImageTexture.create_from_image(_image) + + instance = value + +## +## Width and height of the editable terrain in units. ## @export var size := Vector2i.ZERO: @@ -18,18 +40,23 @@ var size := Vector2i.ZERO: set(value): _image.resize(value.x, value.y) - _image.fill(Color(0.0, 0.0, 0.0, 0.5)) + + if instance != null: + instance.size = value + instance.terrain_map = ImageTexture.create_from_image(_image) size = value -## -## -## -func get_image() -> void: - return _image +func _init() -> void: + _image.fill(_DEFAULT_COLOR) ## +## Mixes the texture [member channels] and raises the terrain by [code]elevation_level[/code] in a +## brush pattern masked by [code]brush_mask[/code] to the worldspace [code]point[/code] with +## [code]brush_intensity[/code] as the intensity of the applied effects for a single paint. ## +## For continuous painting, a delta value may be supplied to [code]brush_intensity[/code] to avoid +## issues with variable-rate paint updates. ## func paint(point: Vector2i, elevation_level: float, brush_mask: Image, brush_scale: float, brush_intensity: float) -> void: @@ -37,10 +64,9 @@ func paint(point: Vector2i, elevation_level: float, # 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)) + var draw_area := Rect2i(Vector2i.ZERO, size).intersection( + Rect2i((point - Vector2i(brush_size * 0.5)) + Vector2i(size * 0.5), brush_size)) if draw_area.has_area(): var scaled_brush_mask_size := brush_mask_size / draw_area.size @@ -65,8 +91,30 @@ func paint(point: Vector2i, elevation_level: float, lerpf(pixel.b, channels.z, mask_intensity), lerpf(pixel.a, pixel.a + (elevation_level * mask), brush_intensity))) + instance.terrain_map.update(_image) + ## +## Samples the color value in the editable terrain at the worldspace [code]point[/code], returning +## its respective [Color] value. ## +## For sample coordinates outside of the terrain map worldspace, [code]BLANK[/code] is returned +## instead. +## +func sample(point: Vector2i) -> Color: + var image_point := point + Vector2i(size * 0.5) + + if Rect2i(Vector2i.ZERO, size).has_point(image_point): + return _image.get_pixelv(image_point) + + return BLANK + +## +## Mixes the texture [member channels] and smooths the terrain to [code]smoothing_level[/code] in a +## brush pattern masked by [code]brush_mask[/code] to the worldspace [code]point[/code] with +## [code]brush_intensity[/code] as the intensity of the applied effects for a single paint. +## +## For continuous smoothing, a delta value may be supplied to [code]brush_intensity[/code] to avoid +## issues with variable-rate smooth updates. ## func smooth(point: Vector2i, smoothing_level: float, brush_mask: Image, brush_scale: float, brush_intensity: float) -> void: @@ -98,3 +146,5 @@ func smooth(point: Vector2i, smoothing_level: float, lerpf(pixel.g, channels.y, mask_intensity), lerpf(pixel.b, channels.z, mask_intensity), lerpf(pixel.a, smoothing_level, mask_intensity))) + + instance.terrain_map.update(_image)