2023-01-13 11:08:56 +01:00
|
|
|
class_name EditableTerrain extends Node
|
|
|
|
|
2023-01-13 18:32:48 +01:00
|
|
|
##
|
|
|
|
## Blank sample value.
|
|
|
|
##
|
|
|
|
const BLANK := Color(0.0, 0.0, 0.0, 0.0)
|
|
|
|
|
2023-01-13 11:08:56 +01:00
|
|
|
var _image := Image.create(1, 1, false, Image.FORMAT_RGBAF)
|
|
|
|
|
|
|
|
##
|
2023-01-13 18:32:48 +01:00
|
|
|
## Tracked [TerrainInstance3D] to echo all terrain editing changes to.
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
2023-01-13 18:32:48 +01:00
|
|
|
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.
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
|
|
|
var size := Vector2i.ZERO:
|
|
|
|
get:
|
|
|
|
return size
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
_image.resize(value.x, value.y)
|
2023-01-13 18:32:48 +01:00
|
|
|
|
|
|
|
if instance != null:
|
|
|
|
instance.size = value
|
|
|
|
instance.terrain_map = ImageTexture.create_from_image(_image)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
size = value
|
|
|
|
|
2023-01-16 01:58:11 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
return Rect2i(Vector2i.ZERO, size).intersection(
|
|
|
|
Rect2i((point - Vector2i(scaled_mask_size * 0.5)) + Vector2i(size * 0.5), scaled_mask_size))
|
|
|
|
|
2023-01-13 18:32:48 +01:00
|
|
|
func _init() -> void:
|
2023-01-16 01:58:11 +01:00
|
|
|
clear(BLANK)
|
|
|
|
|
|
|
|
##
|
|
|
|
## Clears the image to the value of [code]clear_color[/code].
|
|
|
|
##
|
|
|
|
func clear(clear_color: Color) -> void:
|
|
|
|
_image.fill(clear_color)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
2023-01-16 01:58:11 +01:00
|
|
|
func erase(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void:
|
|
|
|
var draw_area := _get_draw_area(point, mask, scale)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-16 01:58:11 +01:00
|
|
|
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))
|
|
|
|
|
|
|
|
instance.terrain_map.update(_image)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-16 01:58:11 +01:00
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
func lower(point: Vector2i, mask: Image, scale: float, intensity: float, delta: float) -> void:
|
|
|
|
var draw_area := _get_draw_area(point, mask, scale)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
if draw_area.has_area():
|
2023-01-16 01:58:11 +01:00
|
|
|
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)))
|
|
|
|
|
|
|
|
instance.terrain_map.update(_image)
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
func paint_blue(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, lerpf(pixel.b, intensity, delta *\
|
|
|
|
mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a),
|
|
|
|
pixel.a))
|
|
|
|
|
|
|
|
instance.terrain_map.update(_image)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-16 01:58:11 +01:00
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
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
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
for y in draw_area.size.y:
|
2023-01-16 01:58:11 +01:00
|
|
|
var brush_mask_y := int(y * mask_ratio.y)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-01-16 01:58:11 +01:00
|
|
|
_image.set_pixelv(terrain_map_coord, Color(
|
|
|
|
pixel.r, lerpf(pixel.g, intensity, delta *\
|
|
|
|
mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a),
|
|
|
|
pixel.b, pixel.a))
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-16 01:58:11 +01:00
|
|
|
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)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
_image.set_pixelv(terrain_map_coord, Color(
|
2023-01-16 01:58:11 +01:00
|
|
|
lerpf(pixel.r, intensity, delta *\
|
|
|
|
mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a),
|
|
|
|
pixel.g, pixel.b, pixel.a))
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-16 01:58:11 +01:00
|
|
|
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)))
|
|
|
|
|
|
|
|
instance.terrain_map.update(_image)
|
2023-01-13 18:32:48 +01:00
|
|
|
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
2023-01-13 18:32:48 +01:00
|
|
|
## Samples the color value in the editable terrain at the worldspace [code]point[/code], returning
|
|
|
|
## its respective [Color] value.
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
2023-01-13 18:32:48 +01:00
|
|
|
## 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
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
2023-01-16 01:58:11 +01:00
|
|
|
func smooth(point: Vector2i, mask: Image, scale: float, level: float, delta: float) -> void:
|
|
|
|
var draw_area := _get_draw_area(point, mask, scale)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
if draw_area.has_area():
|
2023-01-16 01:58:11 +01:00
|
|
|
var mask_ratio := mask.get_size() / draw_area.size
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
for y in draw_area.size.y:
|
2023-01-16 01:58:11 +01:00
|
|
|
var brush_mask_y := int(y * mask_ratio.y)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-01-16 01:58:11 +01:00
|
|
|
_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)))
|
2023-01-13 18:32:48 +01:00
|
|
|
|
|
|
|
instance.terrain_map.update(_image)
|