How to color a 3D model using raycast

Godot Version

4.5

Question

Hello. I have the following script . But I have absolutely no idea how to write a shader. Please suggest resources that would help me understand this.

extends MeshInstance3D

var img : Image # mask
var imageTexture : ImageTexture # Bese texture

var rect : Rect2i
var brush : Image
var pixels : int

@onready var brush_textur = preload(“res://brush_blood.png”)

func _create_image():
var mat = get_active_material(0)
var base_texture = mat.get_shader_parameter(“base_textur”)

img = base_texture.get_image() #copy base texture

pixels = img.get_width()

imageTexture = ImageTexture.create_from_image(img) 
mat.set_shader_parameter("Mask_texture", imageTexture)

func _brush():
brush = brush_textur.get_image()

rect = Rect2i(Vector2i(0, 0), Vector2i(3, 3))

func _ready():

_brush()
_create_image()

func draw_on_sprite(coords: Vector2):

if img.is_compressed():
	img.decompress()

if img.get_format() != Image.FORMAT_RGBA8:
	img.convert(Image.FORMAT_RGBA8)
if brush.get_format() != Image.FORMAT_RGBA8:
	brush.convert(Image.FORMAT_RGBA8)

var pixsel_coords = ((coords * pixels) - Vector2(1.5, 1.5)).round()

img.blend_rect(brush, rect, Vector2i(pixsel_coords.x, pixsel_coords.y))
imageTexture = ImageTexture.create_from_image(img)

var mat = get_active_material(0)
if mat:
	mat.set_shader_parameter("Mask_texture", imageTexture)
	print("обновлена")
else:
	print("не найден")

Describe what exactly are you trying to achieve.

I have a mesh that shoots with Raycast3D in Staticbody3D with a collision. I take the material and copy it(img = base_texture.get_image()) and I’m creating a mask on which I plan to draw using raycast (imageTexture = ImageTexture.create_from_image(img) ). My brush is png picture(brush = brush_textur.get_image()). In the draw_on_sprite function, I determine where and how I will draw, updating the material. My function on raycast3D

(func _physics_process(delta):
if is_instance_valid(blade):
time_accumulated += delta
var offset_x: float = sin(time_accumulated * speed) * amplitude
blade.position.x = start_position.x + offset_x

if not raycast.is_colliding():
	return
	
var target = raycast.get_collider()
print("Работает,", target.name) 

if target.name == "StaticBodyARM":
	var mesh = target.get_child(0)
	if mesh.has_method("draw_on_sprite"):
		var hit_position = raycast.get_collision_point()
		var coords = Vector2(hit_position.x, hit_position.z)
		mesh.draw_on_sprite(coords))

And I don’t understand how I should continue the shader.

shader_type spatial;

uniform sampler2D base_textur : source_color;
uniform sampler2D Mask_texture;

void fragment() {
vec4 base_color = texture(base_textur, UV);
vec4 mask_color = texture(Mask_texture, UV);
}

My task is to paint on a mask with a brush.

Could you suggest some resources for learning about shaders?

Use DrawableTexture2D.

Thank you