class_name TerrainMapCanvas extends Node ## ## Blank sample value. ## const BLANK := Color(0.0, 0.0, 0.0, 0.0) var _editable_image := Image.create(1, 1, false, Image.FORMAT_RGBAF) var _editable_texture := ImageTexture.create_from_image(_editable_image) ## ## Width and height of the canvas (in pixels). ## 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) func _get_brush_area(point: Vector2i, mask_size: Vector2i, mask_scale: float) -> Rect2i: # Convert worldspace point to image-space coordinates for mask. var scaled_mask_size := Vector2i(mask_size * mask_scale) return Rect2i((point + Vector2i(size * 0.5)) - Vector2i(scaled_mask_size * 0.5), scaled_mask_size) func _get_editable_area() -> Rect2i: return Rect2i(Vector2i.ZERO, size).grow(-1) ## ## Clears the terrain map to the value of [code]clear_elevation[/code]. ## 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) ## ## 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. ## func get_texture() -> Texture2D: return _editable_texture ## ## 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]. ## func oscillate(level: float, point: Vector2i, mask: Image, mask_scale: float, intensity: float) -> void: var mask_size := mask.get_size() var brush_source := _get_brush_area(point, mask_size, mask_scale) if brush_source.has_area(): var brush_target := _get_editable_area().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 := _editable_image.get_pixelv(coord) _editable_image.set_pixelv(coord, Color(pixel.r, pixel.g, pixel.b, lerpf(pixel.a, level, intensity *\ mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a)).clamp()) _editable_texture.update(_editable_image) ## ## 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]. ## func paint(color: Color, point: Vector2i, mask: Image, mask_scale: float, intensity: float) -> void: var mask_size := mask.get_size() var brush_source := _get_brush_area(point, mask_size, mask_scale) if brush_source.has_area(): var brush_target := _get_editable_area().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 := _editable_image.get_pixelv(coord) var mask_intensity_delta := intensity *\ mask.get_pixel(int((offset.x + x) * mask_ratio.x), mask_y).a _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()) _editable_texture.update(_editable_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 _editable_image.get_pixelv(image_point) return BLANK