game/terrain/dynamic_terrain_instance_3d.gd

124 lines
3.7 KiB
GDScript3
Raw Normal View History

2023-01-18 18:15:41 +01:00
class_name DynamicTerrainInstance3D extends TerrainInstance3D
##
## 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-18 18:15:41 +01:00
var _editable_texture: ImageTexture
2023-01-18 18:15:41 +01:00
func _get_brush_area(point: Vector2i, mask_size: Vector2i, mask_scale: float) -> Rect2i:
# 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
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
##
##
##
func oscillate(point: Vector2i, level: float,
2023-01-18 18:15:41 +01:00
mask: Image, mask_scale: float, intensity: float, delta: float) -> void:
2023-01-16 01:58:11 +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
if brush_source.has_area():
2023-01-18 18:15:41 +01:00
var brush_target := Rect2i(Vector2i.ZERO, get_size()).intersection(brush_source)
if brush_target.has_area():
var mask_ratio := mask_size / brush_source.size
var offset := brush_target.position - brush_source.position
for y in brush_target.size.y:
var mask_y := int((offset.y + y) * mask_ratio.y)
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,
lerpf(pixel.a, level, intensity * delta *\
mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a)).clamp())
2023-01-18 18:15:41 +01:00
_editable_texture.update(_editable_image)
2023-01-16 01:58:11 +01:00
##
##
##
func paint(point: Vector2i, color: Color,
2023-01-18 18:15:41 +01:00
mask: Image, mask_scale: float, intensity: float, delta: float) -> void:
2023-01-16 01:58:11 +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)
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
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
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
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
var mask_intensity_delta := intensity * delta *\
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(
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)
##
##
##
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)
##
## 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:
2023-01-18 18:15:41 +01:00
var size := get_size()
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)
return BLANK