2023-01-13 11:08:56 +01:00
|
|
|
class_name EditableTerrain extends Node
|
|
|
|
|
2023-01-13 18:32:48 +01:00
|
|
|
const _DEFAULT_COLOR := Color(0.0, 0.0, 0.0, 0.5)
|
|
|
|
|
|
|
|
##
|
|
|
|
## 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
|
|
|
## Terrain texture channel mixing values.
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
2023-01-13 18:32:48 +01:00
|
|
|
## Affects the color that terrain is mixed into during editing operations like [method paint].
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
|
|
|
@export
|
|
|
|
var channels := Vector3.ZERO
|
|
|
|
|
|
|
|
##
|
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
|
|
|
##
|
|
|
|
@export
|
|
|
|
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-13 18:32:48 +01:00
|
|
|
func _init() -> void:
|
|
|
|
_image.fill(_DEFAULT_COLOR)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
##
|
2023-01-13 18:32:48 +01:00
|
|
|
## Mixes the texture [member channels] and raises the terrain by [code]elevation_level[/code] in a
|
|
|
|
## brush pattern masked by [code]brush_mask[/code] to the worldspace [code]point[/code] with
|
|
|
|
## [code]brush_intensity[/code] as the intensity of the applied effects for a single paint.
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
2023-01-13 18:32:48 +01:00
|
|
|
## For continuous painting, a delta value may be supplied to [code]brush_intensity[/code] to avoid
|
|
|
|
## issues with variable-rate paint updates.
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
|
|
|
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
|
|
|
|
|
2023-01-13 18:32:48 +01:00
|
|
|
var draw_area := Rect2i(Vector2i.ZERO, size).intersection(
|
|
|
|
Rect2i((point - Vector2i(brush_size * 0.5)) + Vector2i(size * 0.5), brush_size))
|
2023-01-13 11:08:56 +01:00
|
|
|
|
|
|
|
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)))
|
|
|
|
|
2023-01-13 18:32:48 +01:00
|
|
|
instance.terrain_map.update(_image)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
##
|
|
|
|
## Mixes the texture [member channels] and smooths the terrain to [code]smoothing_level[/code] in a
|
|
|
|
## brush pattern masked by [code]brush_mask[/code] to the worldspace [code]point[/code] with
|
|
|
|
## [code]brush_intensity[/code] as the intensity of the applied effects for a single paint.
|
|
|
|
##
|
|
|
|
## For continuous smoothing, a delta value may be supplied to [code]brush_intensity[/code] to avoid
|
|
|
|
## issues with variable-rate smooth updates.
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
|
|
|
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)))
|
2023-01-13 18:32:48 +01:00
|
|
|
|
|
|
|
instance.terrain_map.update(_image)
|