From 81abb0d8d90dd25dc736f509ea721cd9923f1458 Mon Sep 17 00:00:00 2001 From: kayomn Date: Mon, 16 Jan 2023 23:48:13 +0000 Subject: [PATCH] Fix errors in editable terrain painting calculations --- editor.scn | 4 +- editor/editable_terrain.gd | 205 ++++++++----------------------------- 2 files changed, 45 insertions(+), 164 deletions(-) diff --git a/editor.scn b/editor.scn index 7556254..28398bb 100644 --- a/editor.scn +++ b/editor.scn @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bcf7028b082f7667e47971dacc43da0ac6e70b150f3c39b1df92016f3dd3da27 -size 7744 +oid sha256:558a400fc6ab4940424ae7485c20d64f277eb631a649057e48186308fe9db4f6 +size 7782 diff --git a/editor/editable_terrain.gd b/editor/editable_terrain.gd index 101e6dd..7682acb 100644 --- a/editor/editable_terrain.gd +++ b/editor/editable_terrain.gd @@ -36,13 +36,11 @@ var size := Vector2i.ZERO: size = value -func _get_draw_area(point: Vector2i, mask: Image, scale: float) -> Rect2i: - # Convert worldspace point to image-space coordinates for brush and calculate drawable area. - var mask_size := mask.get_size() - var scaled_mask_size := mask_size * scale +func _get_brush_area(point: Vector2i, mask_size: Vector2i, scale: float) -> Rect2i: + # Convert worldspace point to image-space coordinates for mask. + var scaled_mask_size := Vector2i(mask_size * scale) - return Rect2i(Vector2i.ZERO, size).intersection( - Rect2i((point - Vector2i(scaled_mask_size * 0.5)) + Vector2i(size * 0.5), scaled_mask_size)) + return Rect2i((point + Vector2i(size * 0.5)) - Vector2i(scaled_mask_size * 0.5), scaled_mask_size) func _init() -> void: clear(BLANK) @@ -59,22 +57,29 @@ func clear(clear_color: Color) -> void: ## ## ## -func lower(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void: - var draw_area := _get_draw_area(point, mask, scale) +func oscillate(point: Vector2i, level: float, + mask: Image, scale: float, intensity: float, delta: float) -> void: - if draw_area.has_area(): - var mask_ratio := mask.get_size() / draw_area.size + var mask_size := mask.get_size() + var brush_source := _get_brush_area(point, mask_size, scale) - for y in draw_area.size.y: - var brush_mask_y := int(y * mask_ratio.y) + if brush_source.has_area(): + var brush_target := Rect2i(Vector2i.ZERO, size).intersection(brush_source) - 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) + if brush_target.has_area(): + var mask_ratio := mask_size / brush_source.size + var offset := brush_target.position - brush_source.position - _image.set_pixelv(terrain_map_coord, Color(pixel.r, pixel.g, pixel.b, - lerpf(pixel.a, pixel.a - (intensity * - mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a), delta)).clamp()) + for y in brush_target.size.y: + var mask_y := int((offset.y + y) * mask_ratio.y) + + for x in brush_target.size.x: + var coord := brush_target.position + Vector2i(x, y) + var pixel := _image.get_pixelv(coord) + + _image.set_pixelv(coord, Color(pixel.r, pixel.g, pixel.b, + lerpf(pixel.a, level, intensity * delta *\ + mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a)).clamp()) if instance != null: instance.terrain_map.update(_image) @@ -82,134 +87,33 @@ func lower(point: Vector2i, mask: Image, scale: float, intensity: float, delta: ## ## ## -func paint_blue(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void: - var draw_area := _get_draw_area(point, mask, scale) +func paint(point: Vector2i, color: Color, + mask: Image, scale: float, intensity: float, delta: float) -> void: - if draw_area.has_area(): - var mask_ratio := mask.get_size() / draw_area.size + var mask_size := mask.get_size() + var brush_source := _get_brush_area(point, mask_size, scale) - for y in draw_area.size.y: - var brush_mask_y := int(y * mask_ratio.y) + if brush_source.has_area(): + var brush_target := Rect2i(Vector2i.ZERO, size).intersection(brush_source) - 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) + if brush_target.has_area(): + var mask_ratio := mask_size / brush_source.size + var offset := brush_target.position - brush_source.position - var mask_intensity_delta := intensity *\ - delta * mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a + for y in brush_target.size.y: + var mask_y := int((offset.y + y) * mask_ratio.y) - _image.set_pixelv(terrain_map_coord, Color( - lerpf(pixel.r, 0.0, mask_intensity_delta), - lerpf(pixel.g, 0.0, mask_intensity_delta), - lerpf(pixel.b, 1.0, mask_intensity_delta), - pixel.a).clamp()) + for x in brush_target.size.x: + var coord := brush_target.position + Vector2i(x, y) + var pixel := _image.get_pixelv(coord) - if instance != null: - instance.terrain_map.update(_image) + var mask_intensity_delta := intensity * delta *\ + mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a -## -## -## -func paint_erase(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void: - var draw_area := _get_draw_area(point, mask, scale) - - if draw_area.has_area(): - var mask_ratio := mask.get_size() / draw_area.size - - for y in draw_area.size.y: - var brush_mask_y := int(y * mask_ratio.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_delta := intensity *\ - delta * mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a - - _image.set_pixelv(terrain_map_coord, Color( - lerpf(pixel.r, 0.0, mask_intensity_delta), - lerpf(pixel.g, 0.0, mask_intensity_delta), - lerpf(pixel.b, 0.0, mask_intensity_delta), - pixel.a).clamp()) - - if instance != null: - instance.terrain_map.update(_image) - -## -## -## -func paint_green(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void: - var draw_area := _get_draw_area(point, mask, scale) - - if draw_area.has_area(): - var mask_ratio := mask.get_size() / draw_area.size - - for y in draw_area.size.y: - var brush_mask_y := int(y * mask_ratio.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_delta := intensity *\ - delta * mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a - - _image.set_pixelv(terrain_map_coord, Color( - lerpf(pixel.r, 0.0, mask_intensity_delta), - lerpf(pixel.g, 1.0, mask_intensity_delta), - lerpf(pixel.b, 0.0, mask_intensity_delta), - pixel.a).clamp()) - - if instance != null: - instance.terrain_map.update(_image) - -## -## -## -func paint_red(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void: - var draw_area := _get_draw_area(point, mask, scale) - - if draw_area.has_area(): - var mask_ratio := mask.get_size() / draw_area.size - - for y in draw_area.size.y: - var brush_mask_y := int(y * mask_ratio.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_delta := intensity *\ - delta * mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a - - _image.set_pixelv(terrain_map_coord, Color( - lerpf(pixel.r, 1.0, mask_intensity_delta), - lerpf(pixel.g, 0.0, mask_intensity_delta), - lerpf(pixel.b, 0.0, mask_intensity_delta), - pixel.a).clamp()) - - if instance != null: - instance.terrain_map.update(_image) - -## -## -## -func raise(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void: - var draw_area := _get_draw_area(point, mask, scale) - - if draw_area.has_area(): - var mask_ratio := mask.get_size() / draw_area.size - - for y in draw_area.size.y: - var brush_mask_y := int(y * mask_ratio.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) - - _image.set_pixelv(terrain_map_coord, Color(pixel.r, pixel.g, pixel.b, - lerpf(pixel.a, pixel.a + (intensity * - mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a), delta)).clamp()) + _image.set_pixelv(coord, Color( + lerpf(pixel.r, color.r, mask_intensity_delta), + lerpf(pixel.g, color.g, mask_intensity_delta), + lerpf(pixel.b, color.b, mask_intensity_delta), pixel.a).clamp()) if instance != null: instance.terrain_map.update(_image) @@ -228,26 +132,3 @@ func sample(point: Vector2i) -> Color: return _image.get_pixelv(image_point) return BLANK - -## -## -## -func smooth(point: Vector2i, mask: Image, scale: float, level: float, delta: float) -> void: - var draw_area := _get_draw_area(point, mask, scale) - - if draw_area.has_area(): - var mask_ratio := mask.get_size() / draw_area.size - - for y in draw_area.size.y: - var brush_mask_y := int(y * mask_ratio.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) - - _image.set_pixelv(terrain_map_coord, Color(pixel.r, pixel.g, pixel.b, - lerpf(pixel.a, level, delta * mask.get_pixel( - int(x * mask_ratio.x), brush_mask_y).a)).clamp()) - - if instance != null: - instance.terrain_map.update(_image)