Fix errors in editable terrain painting calculations

This commit is contained in:
kayomn 2023-01-16 23:48:13 +00:00
parent 0b19b97293
commit 81abb0d8d9
2 changed files with 45 additions and 164 deletions

BIN
editor.scn (Stored with Git LFS)

Binary file not shown.

View File

@ -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)