2023-01-18 18:15:41 +01:00
|
|
|
class_name DynamicTerrainInstance3D extends TerrainInstance3D
|
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-18 18:15:41 +01:00
|
|
|
var _editable_image: Image
|
2023-01-13 11:08:56 +01:00
|
|
|
|
2023-01-18 18:15:41 +01:00
|
|
|
var _editable_texture: ImageTexture
|
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-18 18:15:41 +01:00
|
|
|
return Rect2i((point + Vector2i(get_size() * 0.5)) - Vector2i(scaled_mask_size * 0.5), scaled_mask_size)
|
2023-01-16 01:58:11 +01:00
|
|
|
|
2023-01-13 18:32:48 +01:00
|
|
|
func _init() -> void:
|
2023-01-18 18:15:41 +01:00
|
|
|
super._init()
|
|
|
|
|
|
|
|
_editable_image = Image.create(1, 1, false, Image.FORMAT_RGBAF)
|
|
|
|
_editable_texture = ImageTexture.create_from_image(_editable_image)
|
|
|
|
|
|
|
|
super.resize(Vector2i.ONE)
|
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-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-19 01:49:44 +01:00
|
|
|
func oscillate(point: Vector2i, level: float, 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-18 18:15:41 +01:00
|
|
|
var brush_target := Rect2i(Vector2i.ZERO, get_size()).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-19 01:49:44 +01:00
|
|
|
func paint(point: Vector2i, color: Color, 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-18 18:15:41 +01:00
|
|
|
var brush_target := Rect2i(Vector2i.ZERO, get_size()).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-19 01:49:44 +01:00
|
|
|
## Attempts to resize the terrain geometry and map to be equal to [code]size[/code] (in-engine
|
|
|
|
## units), with [code]Vector2i.ONE[/code] as the minimum size.
|
2023-01-18 18:15:41 +01:00
|
|
|
##
|
2023-01-19 01:49:44 +01:00
|
|
|
## In the process of growing the terrain map, existing edge data will be copied to fill it out.
|
2023-01-18 18:15:41 +01:00
|
|
|
##
|
|
|
|
func resize(size: Vector2i) -> void:
|
|
|
|
super.resize(size)
|
|
|
|
|
|
|
|
var actual_size := get_size()
|
|
|
|
|
|
|
|
_editable_image.resize(actual_size.x, actual_size.y)
|
|
|
|
|
|
|
|
_editable_texture = ImageTexture.create_from_image(_editable_image)
|
|
|
|
|
|
|
|
update_terrain(_editable_texture)
|
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:
|
2023-01-18 18:15:41 +01:00
|
|
|
var size := get_size()
|
2023-01-13 18:32:48 +01:00
|
|
|
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
|