2023-01-25 00:26:17 +01:00
|
|
|
class_name TerrainMapCanvas extends Node
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-13 18:32:48 +01:00
|
|
|
##
|
|
|
|
## Blank sample value.
|
|
|
|
##
|
|
|
|
const BLANK := Color(0.0, 0.0, 0.0, 0.0)
|
|
|
|
|
2023-01-23 19:28:14 +01:00
|
|
|
var _editable_image := Image.create(1, 1, false, Image.FORMAT_RGBAF)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-23 19:28:14 +01:00
|
|
|
var _editable_texture := ImageTexture.create_from_image(_editable_image)
|
|
|
|
|
|
|
|
##
|
2023-01-23 21:29:43 +01:00
|
|
|
## Width and height of the canvas (in pixels).
|
2023-01-23 19:28:14 +01:00
|
|
|
##
|
|
|
|
var size := Vector2i.ONE:
|
|
|
|
set(value):
|
|
|
|
size = Vector2(maxi(size.x, value.x), maxi(size.y, value.y))
|
|
|
|
|
|
|
|
_editable_image.resize(size.x, size.y)
|
|
|
|
_editable_texture.set_image(_editable_image)
|
2023-01-13 18:32:48 +01:00
|
|
|
|
2023-01-18 18:15:41 +01:00
|
|
|
func _get_brush_area(point: Vector2i, mask_size: Vector2i, mask_scale: float) -> Rect2i:
|
2023-01-17 00:48:13 +01:00
|
|
|
# Convert worldspace point to image-space coordinates for mask.
|
2023-01-18 18:15:41 +01:00
|
|
|
var scaled_mask_size := Vector2i(mask_size * mask_scale)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-23 19:28:14 +01:00
|
|
|
return Rect2i((point + Vector2i(size * 0.5)) -
|
|
|
|
Vector2i(scaled_mask_size * 0.5), scaled_mask_size)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-23 19:28:14 +01:00
|
|
|
func _get_editable_area() -> Rect2i:
|
|
|
|
return Rect2i(Vector2i.ZERO, size).grow(-1)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
|
|
|
##
|
2023-01-18 18:15:41 +01:00
|
|
|
## Clears the terrain map to the value of [code]clear_elevation[/code].
|
2023-01-16 01:58:11 +01:00
|
|
|
##
|
2023-01-18 18:15:41 +01:00
|
|
|
func clear(clear_elevation: float) -> void:
|
|
|
|
_editable_image.fill(Color(0.0, 0.0, 0.0, clampf(clear_elevation, 0.0, 1.0)))
|
|
|
|
_editable_texture.update(_editable_image)
|
2023-01-16 19:54:06 +01:00
|
|
|
|
2023-01-23 19:28:14 +01:00
|
|
|
##
|
2023-01-25 00:26:17 +01:00
|
|
|
## Returns the canvas data as a [Texture2D] where RGB channels are encoded in the red, green, and
|
|
|
|
## blue, component of each pixel and the height is the alpha component.
|
|
|
|
##
|
|
|
|
## The returned value is designed for use with a [TerrainInstance3D] or other class compatible with
|
|
|
|
## the encoded format specified above.
|
2023-01-23 19:28:14 +01:00
|
|
|
##
|
2023-01-23 21:31:28 +01:00
|
|
|
func get_texture() -> Texture2D:
|
2023-01-23 19:28:14 +01:00
|
|
|
return _editable_texture
|
|
|
|
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
2023-01-19 01:49:44 +01:00
|
|
|
## Oscillates the height of terrain toward a value of [member height_scale] (multiplied by
|
|
|
|
## [code]level[/code]) around the area of [code]point[/code] determined by [code]mask[/code]
|
|
|
|
## multiplied by [code]mask_scale[/code] at a rate of [code]intensity[/code].
|
2023-01-13 11:08:56 +01:00
|
|
|
##
|
2023-01-23 19:28:14 +01:00
|
|
|
func oscillate(level: float, point: Vector2i, mask: Image, mask_scale: float, intensity: float) -> void:
|
2023-01-17 00:48:13 +01:00
|
|
|
var mask_size := mask.get_size()
|
2023-01-18 18:15:41 +01:00
|
|
|
var brush_source := _get_brush_area(point, mask_size, mask_scale)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-17 00:48:13 +01:00
|
|
|
if brush_source.has_area():
|
2023-01-23 19:28:14 +01:00
|
|
|
var brush_target := _get_editable_area().intersection(brush_source)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-17 00:48:13 +01:00
|
|
|
if brush_target.has_area():
|
|
|
|
var mask_ratio := mask_size / brush_source.size
|
|
|
|
var offset := brush_target.position - brush_source.position
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-17 00:48:13 +01:00
|
|
|
for y in brush_target.size.y:
|
|
|
|
var mask_y := int((offset.y + y) * mask_ratio.y)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-17 00:48:13 +01:00
|
|
|
for x in brush_target.size.x:
|
|
|
|
var coord := brush_target.position + Vector2i(x, y)
|
2023-01-18 18:15:41 +01:00
|
|
|
var pixel := _editable_image.get_pixelv(coord)
|
2023-01-16 19:54:06 +01:00
|
|
|
|
2023-01-18 18:15:41 +01:00
|
|
|
_editable_image.set_pixelv(coord, Color(pixel.r, pixel.g, pixel.b,
|
2023-01-19 01:49:44 +01:00
|
|
|
lerpf(pixel.a, level, intensity *\
|
2023-01-17 00:48:13 +01:00
|
|
|
mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a)).clamp())
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-18 18:15:41 +01:00
|
|
|
_editable_texture.update(_editable_image)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
|
|
|
##
|
2023-01-19 01:49:44 +01:00
|
|
|
## Paints the texture of the terrain toward a value of [code]color[/code] multiplied by
|
|
|
|
## [code]intensity[/code] around the area of [code]point[/code] determined by [code]mask[/code]
|
|
|
|
## multiplied by [code]mask_scale[/code] at a rate of [code]intensity[/code].
|
2023-01-16 01:58:11 +01:00
|
|
|
##
|
2023-01-23 19:28:14 +01:00
|
|
|
func paint(color: Color, point: Vector2i, mask: Image, mask_scale: float, intensity: float) -> void:
|
2023-01-17 00:48:13 +01:00
|
|
|
var mask_size := mask.get_size()
|
2023-01-18 18:15:41 +01:00
|
|
|
var brush_source := _get_brush_area(point, mask_size, mask_scale)
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-17 00:48:13 +01:00
|
|
|
if brush_source.has_area():
|
2023-01-23 19:28:14 +01:00
|
|
|
var brush_target := _get_editable_area().intersection(brush_source)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-17 00:48:13 +01:00
|
|
|
if brush_target.has_area():
|
|
|
|
var mask_ratio := mask_size / brush_source.size
|
|
|
|
var offset := brush_target.position - brush_source.position
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-17 00:48:13 +01:00
|
|
|
for y in brush_target.size.y:
|
|
|
|
var mask_y := int((offset.y + y) * mask_ratio.y)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-17 00:48:13 +01:00
|
|
|
for x in brush_target.size.x:
|
|
|
|
var coord := brush_target.position + Vector2i(x, y)
|
2023-01-18 18:15:41 +01:00
|
|
|
var pixel := _editable_image.get_pixelv(coord)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-19 01:49:44 +01:00
|
|
|
var mask_intensity_delta := intensity *\
|
2023-01-17 00:48:13 +01:00
|
|
|
mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-18 18:15:41 +01:00
|
|
|
_editable_image.set_pixelv(coord, Color(
|
2023-01-17 00:48:13 +01:00
|
|
|
lerpf(pixel.r, color.r, mask_intensity_delta),
|
|
|
|
lerpf(pixel.g, color.g, mask_intensity_delta),
|
|
|
|
lerpf(pixel.b, color.b, mask_intensity_delta), pixel.a).clamp())
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-18 18:15:41 +01:00
|
|
|
_editable_texture.update(_editable_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):
|
2023-01-18 18:15:41 +01:00
|
|
|
return _editable_image.get_pixelv(image_point)
|
2023-01-13 18:32:48 +01:00
|
|
|
|
|
|
|
return BLANK
|