101 lines
2.9 KiB
GDScript
101 lines
2.9 KiB
GDScript
class_name EditableTerrain extends Node
|
|
|
|
var _image := Image.create(1, 1, false, Image.FORMAT_RGBAF)
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var channels := Vector3.ZERO
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var size := Vector2i.ZERO:
|
|
get:
|
|
return size
|
|
|
|
set(value):
|
|
_image.resize(value.x, value.y)
|
|
_image.fill(Color(0.0, 0.0, 0.0, 0.5))
|
|
|
|
size = value
|
|
|
|
##
|
|
##
|
|
##
|
|
func get_image() -> void:
|
|
return _image
|
|
|
|
##
|
|
##
|
|
##
|
|
func paint(point: Vector2i, elevation_level: float,
|
|
brush_mask: Image, brush_scale: float, brush_intensity: float) -> void:
|
|
|
|
# Convert worldspace point to image-space coordinates for brush and calculate drawable area.
|
|
var brush_mask_size := brush_mask.get_size()
|
|
var brush_size := brush_mask_size * brush_scale
|
|
var image_size := _image.get_size()
|
|
|
|
var draw_area := Rect2i(Vector2i.ZERO, image_size).intersection(
|
|
Rect2i((point - Vector2i(brush_size * 0.5)) + Vector2i(image_size * 0.5), brush_size))
|
|
|
|
if draw_area.has_area():
|
|
var scaled_brush_mask_size := brush_mask_size / draw_area.size
|
|
|
|
elevation_level = clampf(elevation_level, -1.0, 1.0)
|
|
|
|
for y in draw_area.size.y:
|
|
var brush_mask_y := int(y * scaled_brush_mask_size.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 := brush_mask.get_pixel(
|
|
int(x * scaled_brush_mask_size.x), brush_mask_y).a
|
|
|
|
var mask_intensity := brush_intensity * mask
|
|
|
|
_image.set_pixelv(terrain_map_coord, Color(
|
|
lerpf(pixel.r, channels.x, mask_intensity),
|
|
lerpf(pixel.g, channels.y, mask_intensity),
|
|
lerpf(pixel.b, channels.z, mask_intensity),
|
|
lerpf(pixel.a, pixel.a + (elevation_level * mask), brush_intensity)))
|
|
|
|
##
|
|
##
|
|
##
|
|
func smooth(point: Vector2i, smoothing_level: float,
|
|
brush_mask: Image, brush_scale: float, brush_intensity: float) -> void:
|
|
|
|
# Convert worldspace point to image-space coordinates for brush and calculate drawable area.
|
|
var brush_mask_size := brush_mask.get_size()
|
|
var brush_size := brush_mask_size * brush_scale
|
|
var terrain_map_size := _image.get_size()
|
|
|
|
var draw_area := Rect2i(Vector2i.ZERO, terrain_map_size).intersection(
|
|
Rect2i((point - Vector2i(brush_size * 0.5)) +\
|
|
Vector2i(terrain_map_size * 0.5), brush_size))
|
|
|
|
if draw_area.has_area():
|
|
var scaled_brush_mask_size := brush_mask_size / draw_area.size
|
|
|
|
for y in draw_area.size.y:
|
|
var brush_mask_y := int(y * scaled_brush_mask_size.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 := brush_intensity * brush_mask.get_pixel(
|
|
int(x * scaled_brush_mask_size.x), brush_mask_y).a
|
|
|
|
_image.set_pixelv(terrain_map_coord, Color(
|
|
lerpf(pixel.r, channels.x, mask_intensity),
|
|
lerpf(pixel.g, channels.y, mask_intensity),
|
|
lerpf(pixel.b, channels.z, mask_intensity),
|
|
lerpf(pixel.a, smoothing_level, mask_intensity)))
|