232 lines
6.6 KiB
GDScript
232 lines
6.6 KiB
GDScript
class_name EditableTerrain extends Node
|
|
|
|
##
|
|
## Blank sample value.
|
|
##
|
|
const BLANK := Color(0.0, 0.0, 0.0, 0.0)
|
|
|
|
var _image := Image.create(1, 1, false, Image.FORMAT_RGBAF)
|
|
|
|
##
|
|
## 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.
|
|
##
|
|
var size := Vector2i.ZERO:
|
|
get:
|
|
return size
|
|
|
|
set(value):
|
|
_image.resize(value.x, value.y)
|
|
|
|
if instance != null:
|
|
instance.size = value
|
|
instance.terrain_map = ImageTexture.create_from_image(_image)
|
|
|
|
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
|
|
|
|
return Rect2i(Vector2i.ZERO, size).intersection(
|
|
Rect2i((point - Vector2i(scaled_mask_size * 0.5)) + Vector2i(size * 0.5), scaled_mask_size))
|
|
|
|
func _init() -> void:
|
|
clear(BLANK)
|
|
|
|
##
|
|
## Clears the image to the value of [code]clear_color[/code].
|
|
##
|
|
func clear(clear_color: Color) -> void:
|
|
_image.fill(clear_color)
|
|
|
|
##
|
|
##
|
|
##
|
|
func 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))
|
|
|
|
instance.terrain_map.update(_image)
|
|
|
|
##
|
|
##
|
|
##
|
|
func lower(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)
|
|
|
|
##
|
|
##
|
|
##
|
|
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)
|
|
|
|
##
|
|
##
|
|
##
|
|
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)
|
|
|
|
_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))
|
|
|
|
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)
|
|
|
|
_image.set_pixelv(terrain_map_coord, Color(
|
|
lerpf(pixel.r, intensity, delta *\
|
|
mask.get_pixel(int(x * mask_ratio.x), brush_mask_y).a),
|
|
pixel.g, pixel.b, pixel.a))
|
|
|
|
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)
|
|
|
|
##
|
|
## 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
|
|
|
|
##
|
|
##
|
|
##
|
|
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)))
|
|
|
|
instance.terrain_map.update(_image)
|