Editor Terrain Brush #2

Merged
kayomn merged 27 commits from editor-terrain-brush into main 2023-01-19 01:51:25 +01:00
2 changed files with 45 additions and 164 deletions
Showing only changes of commit 81abb0d8d9 - Show all commits

BIN
editor.scn (Stored with Git LFS)

Binary file not shown.

View File

@ -36,13 +36,11 @@ var size := Vector2i.ZERO:
size = value size = value
func _get_draw_area(point: Vector2i, mask: Image, scale: float) -> Rect2i: func _get_brush_area(point: Vector2i, mask_size: Vector2i, scale: float) -> Rect2i:
# Convert worldspace point to image-space coordinates for brush and calculate drawable area. # Convert worldspace point to image-space coordinates for mask.
var mask_size := mask.get_size() var scaled_mask_size := Vector2i(mask_size * scale)
var scaled_mask_size := mask_size * scale
return Rect2i(Vector2i.ZERO, size).intersection( return Rect2i((point + Vector2i(size * 0.5)) - Vector2i(scaled_mask_size * 0.5), scaled_mask_size)
Rect2i((point - Vector2i(scaled_mask_size * 0.5)) + Vector2i(size * 0.5), scaled_mask_size))
func _init() -> void: func _init() -> void:
clear(BLANK) 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: func oscillate(point: Vector2i, level: float,
var draw_area := _get_draw_area(point, mask, scale) mask: Image, scale: float, intensity: float, delta: float) -> void:
if draw_area.has_area(): var mask_size := mask.get_size()
var mask_ratio := mask.get_size() / draw_area.size var brush_source := _get_brush_area(point, mask_size, scale)
for y in draw_area.size.y: if brush_source.has_area():
var brush_mask_y := int(y * mask_ratio.y) var brush_target := Rect2i(Vector2i.ZERO, size).intersection(brush_source)
for x in draw_area.size.x: if brush_target.has_area():
var terrain_map_coord := draw_area.position + Vector2i(x, y) var mask_ratio := mask_size / brush_source.size
var pixel := _image.get_pixelv(terrain_map_coord) var offset := brush_target.position - brush_source.position
_image.set_pixelv(terrain_map_coord, Color(pixel.r, pixel.g, pixel.b, for y in brush_target.size.y:
lerpf(pixel.a, pixel.a - (intensity * var mask_y := int((offset.y + y) * mask_ratio.y)
mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a), delta)).clamp())
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: if instance != null:
instance.terrain_map.update(_image) 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: func paint(point: Vector2i, color: Color,
var draw_area := _get_draw_area(point, mask, scale) mask: Image, scale: float, intensity: float, delta: float) -> void:
if draw_area.has_area(): var mask_size := mask.get_size()
var mask_ratio := mask.get_size() / draw_area.size var brush_source := _get_brush_area(point, mask_size, scale)
for y in draw_area.size.y: if brush_source.has_area():
var brush_mask_y := int(y * mask_ratio.y) var brush_target := Rect2i(Vector2i.ZERO, size).intersection(brush_source)
for x in draw_area.size.x: if brush_target.has_area():
var terrain_map_coord := draw_area.position + Vector2i(x, y) var mask_ratio := mask_size / brush_source.size
var pixel := _image.get_pixelv(terrain_map_coord) var offset := brush_target.position - brush_source.position
var mask_intensity_delta := intensity *\ for y in brush_target.size.y:
delta * mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a var mask_y := int((offset.y + y) * mask_ratio.y)
_image.set_pixelv(terrain_map_coord, Color( for x in brush_target.size.x:
lerpf(pixel.r, 0.0, mask_intensity_delta), var coord := brush_target.position + Vector2i(x, y)
lerpf(pixel.g, 0.0, mask_intensity_delta), var pixel := _image.get_pixelv(coord)
lerpf(pixel.b, 1.0, mask_intensity_delta),
pixel.a).clamp())
if instance != null: var mask_intensity_delta := intensity * delta *\
instance.terrain_map.update(_image) mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a
## _image.set_pixelv(coord, Color(
## lerpf(pixel.r, color.r, mask_intensity_delta),
## lerpf(pixel.g, color.g, mask_intensity_delta),
func paint_erase(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void: lerpf(pixel.b, color.b, mask_intensity_delta), pixel.a).clamp())
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())
if instance != null: if instance != null:
instance.terrain_map.update(_image) instance.terrain_map.update(_image)
@ -228,26 +132,3 @@ func sample(point: Vector2i) -> Color:
return _image.get_pixelv(image_point) return _image.get_pixelv(image_point)
return BLANK 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)