How To Assign An Image as Albedo Texture in Godot 4.0 with C#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By icecrowned

Hello,

How do we assign an image to a materials albedo texture in C#? I couldn’t see in documentation. Thanks.

:bust_in_silhouette: Reply From: spaceyjase
var mesh = GetNode<MeshInstance3D>("path/to/mesh");
var material = mesh.GetActiveMaterial(0) as StandardMaterial3D; // or your shader...
material!.AlbedoTexture = texture; // shader parameter, etc.

There isn’t any AlbedoTexture2D showing up in C#. Besides, I want to create texture from Image class.

icecrowned | 2023-03-17 17:00

My bad, it should be AlbedoTexture as documented an using C# conventions (edited): BaseMaterial3D — Godot Engine (latest) documentation in English

How you want to create the texture is entirely up to you but the code is correct: Assign a texture to Albedo in Godot 4 · GitHub

spaceyjase | 2023-03-17 17:14

AlbedoTexture accepts Texture2D. My question is, how do you create a Texture2D using Image class from Godot 4.0? None of the functions available seems to work.

icecrowned | 2023-03-17 17:58

To be fair, that’s a different question, “How to create a Texture from an Image”. Can you open another question for visibility with your code/what you’ve tried? Cool, ta.

spaceyjase | 2023-03-17 18:12

Assigning an Image as AlbedoTexture is pretty much same thing as creating a Texture from Image, because I was already asking the intermediary steps. GDScript has create from image but C# doesn’t.

icecrowned | 2023-03-17 18:17

Perhaps you mean ImageTexture?

[Export] private Image image;

public override void _Ready()
{
	var newImage = ImageTexture.CreateFromImage(image);
	
	var mesh = GetNode<MeshInstance3D>("MeshInstance3D");
	var material = mesh.GetActiveMaterial(0) as StandardMaterial3D;
	material!.AlbedoTexture = newImage;
}

spaceyjase | 2023-03-17 18:47

Yes, thanks for the answer. I think this was what i was looking for, i must have missed it. You can add this to original code for everyone.

icecrowned | 2023-03-17 18:57