class_name EditableTerrain extends Node ## ## Blank sample value. ## const BLANK := Color(0.0, 0.0, 0.0, 0.0) var _image := Image.create(1, 1, false, Image.FORMAT_RGBAF) ## ## Tracked [TerrainInstance3D] to echo all terrain editing changes to. ## 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. ## var size := Vector2i.ZERO: get: return size set(value): _image.resize(value.x, value.y) if instance != null: instance.size = value instance.terrain_map = ImageTexture.create_from_image(_image) size = value func _get_brush_area(point: Vector2i, mask_size: Vector2i, scale: float) -> Rect2i: # Convert worldspace point to image-space coordinates for mask. var scaled_mask_size := Vector2i(mask_size * scale) return Rect2i((point + Vector2i(size * 0.5)) - Vector2i(scaled_mask_size * 0.5), scaled_mask_size) func _init() -> void: clear(BLANK) ## ## Clears the image to the value of [code]clear_color[/code]. ## func clear(clear_color: Color) -> void: _image.fill(clear_color) if instance != null: instance.terrain_map.update(_image) ## ## ## func oscillate(point: Vector2i, level: float, mask: Image, scale: float, intensity: float, delta: float) -> void: var mask_size := mask.get_size() var brush_source := _get_brush_area(point, mask_size, scale) if brush_source.has_area(): var brush_target := Rect2i(Vector2i.ZERO, 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) var pixel := _image.get_pixelv(coord) _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()) if instance != null: instance.terrain_map.update(_image) ## ## ## func paint(point: Vector2i, color: Color, mask: Image, scale: float, intensity: float, delta: float) -> void: var mask_size := mask.get_size() var brush_source := _get_brush_area(point, mask_size, scale) if brush_source.has_area(): var brush_target := Rect2i(Vector2i.ZERO, 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) var pixel := _image.get_pixelv(coord) var mask_intensity_delta := intensity * delta *\ mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a _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()) if instance != null: instance.terrain_map.update(_image) ## ## 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: 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