I would like to know if it’s possible to change the texture of a material using code. I am downloading images from our website and want to set them as textures for an Instance3D. Is this possible?
I already have a function that downloads the images as ImageTexture, but whenever I try to assign them as an override material, I end up with a magenta plane.
func set_texture(texture: ImageTexture, target: String):
var root = self.get_parent()
var debugSprite = root.find_child("Sprite3D") as Sprite3D
debugSprite.texture = texture
var target_node = root.find_child(target)
if target_node != null:
var image_plane = target_node.find_child("Plane") as MeshInstance3D
if image_plane != null:
print("Ziel gefunden! Textur wird angewendet.")
var material = StandardMaterial3D.new()
material.albedo_texture = texture
image_plane.set_surface_override_material(0, material)
else:
print("Keine Plane unter dem Zielknoten gefunden.")
else:
print("Zielknoten nicht gefunden.")
If loading an image from resource it will be CompressedTexture2D instead of ImageTexture. The code below works for me:
var material = StandardMaterial3D.new()
var texture = load("res://assets/sprites/project_icon.png") as CompressedTexture2D
material.albedo_texture = texture
$Plane.set_surface_override_material(0, material)