How to add a texture image to a shader

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

How do I specify a texture to use within a shader? I have a flat mesh with a SpatialMaterial. Within that material, I have a texture I want to move. The effect I’m after is to have the texture appear to slide off of the surface of the mesh. From everything I’ve read, this is only possible within shaders (which I’ve just started learning).

However, Godot allows either a SpatialMaterial or a ShaderMaterial, but not both. As such, my SpatialMaterial texture goes away when I replace it with a ShaderMaterial. That means I have to somehow load the texture from within my shader, but I have no idea how to accomplish this.

Can anyone point me in the right direction?

:bust_in_silhouette: Reply From: omggomb

You need to define a sampler2D uniform in the shader.

For convenience you can convert a SpatialMaterial to a ShaderMaterial so you don’t have to start from scratch: https://docs.godotengine.org/en/stable/tutorials/shading/shader_materials.html#converting-to-shadermaterial

The docs have a somewhat hidden guide on how to use sampler2D uniforms, see this section: https://docs.godotengine.org/en/stable/tutorials/shading/your_first_shader/your_first_spatial_shader.html#noise-heightmap. It teaches how to define the sampler and how to read color values from it.

Thank you very much for the pointers! I have glanced over the pages you linked to, and they look like they will get me headed in the right direction.

stormreaver | 2020-11-19 14:18