How to add texture or shader to planemesh from script?

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

I have tried to use the following code, but it refuses to work.

plane_mesh.material = preload(“res://blue.tres”)

It just renders a white plane.
I have even tried to use it in other scenes but still no luck. I have also tried using the ‘set_surface_material’ after making the plane mesh into a mesh instance, but still doesnt work.

Any help, much appreciate

:bust_in_silhouette: Reply From: Wakatta

So alot can go wrong with what you’ve described.

Make sure what you’re loading is a ShaderMaterial and not a Shader program else you will have additional steps to do.

In the case of ShaderMaterial you will also need to set the default params or you will need to set some values after changing the material, or you can as stated end up with a blank (white) canvas.

Material_Override can also prevent your mat from being visible

var material = preload("res://blue.tres")
plane_mesh.set_surface_material(0, material)


I just tried with a spatial material and that worked so I think my shader code must be wrong.

ollis66 | 2023-02-28 23:44

The beautiful thing about Godot’s editor is shaders will visually update automatically.
Click on your SpatialMaterial and choose Convert to ShaderMaterial, adjust as needed then save and load.

Wakatta | 2023-03-01 12:52