How do I get the Texture2D object of a mesh RID?

Godot Version

4.4

Question

I try to understand the source code of this engine and it is very very confusing.
Currently I want to fetch all the textures of a single mesh (I have the RID of this mesh). Could please someone explain me, how do I get all the Texture2D elements of a single mesh. Here a sample code.

RenderingServer *rendering_server = RS::get_singleton();
RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
RID mesh = ...;
if (mesh.is_valid()) {
  int surface_count = rendering_server->mesh_get_surface_count(mesh);
  if (surface_count > 0) {
    for (int i = 0; i < surface_count; i++) {
      // How do I get the RID of the texture of this surface?
      RID texture_rid = /* What should I do? */;
      if (texture_rid.is_valid()) {
        Texture2D texture = texture_storage->texture_2d_get(texture_rid);
      }
    }
  }
}

Your probably have to rummage around in its material.

Hello, thank you very much for your answer. I tried this RID already and they are valid. But I cannot find any interface etc. to get the texture with the RID, which I get from mesh_surface_get_material.

for (int i = 0; i < surface_count; i++) {
  RID material = rendering_server->mesh_surface_get_material(mesh, i);
  if (material.is_valid()) {
    texture_storage->texture_2d_get(...) // this one wants the texture_rid.
  }
}

I think that I need to access somehow the albedo parameter of this material. but idk how. I can’t find any function which looks useful. I found this suspicious function, but idk whats the parameter name and I cannot find the magic-string (if it contains the albedo): rendering_server->material_get_param(..., ...);. It returns just a Variant so I do not know if it actually contains any useful information for me.

In GDScript it is super easy to get the texture / texture_rid. But in the source code it is like looking for a needle in a haystack. Why is it coded so extremely inconvenient. Cannot find anything similar in the code.

	if node is MeshInstance3D:
		var mesh = node.mesh
		if mesh:
			var surface_count = mesh.get_surface_count()
			for i in range(surface_count):
				var material = mesh.surface_get_material(i)
				if material and material is StandardMaterial3D:
					var texture = material.albedo_texture