How much shaders I need to create?

Hi everyone!
Sorry for my not native English.
Just for fun I am creating 3d game.
Today I already has 3 shaders – 1 main shader for game scene, 1 shader only for textures:

layout (location = 0) in vec3 position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texcoord;

out vec4 u_color;
out vec2 u_texcoord;

, 1 shader for button – it is like shader for textures only, but with addition uniform 4f vector (for hover color when button hovered). (Example: button has 3 AttribPointer - position, color and texcoord, but if button is hovered, i want usage anouther color, and for this color i use uniform 4f vector.

I think its not correct way, how I need to do this?

You probably only need one shader. How many you use depends largely upon how much of the work you want to move to the GPU.

E.g. for changing the colour of the button when hovered, you can just change the colour attribute of the vertices.

thank you!
I thought using uniforms is more productive than changing an attribute much times

Well, you could go the other way around and always pass a color uniform variable and use it as a “tint” (i.e. multiply the vertex color with the uniform color) and for cases that do not require it you just set the uniform to white, so that is does not modify the vertex color.
In general having multiple shaders is fine. If the number of them starts to become unwieldy look if there are some that have very similar functionality so that you could combine them - just like you would refactor code when you have multiple functions with similar behavior that you combine into a more general function (possibly with additional parameters). And just like with the code refactoring case it is a tradeoff between the complexity of the more general function (or shader) and the number of highly specialized (but simpler) functions (or shaders).

Unless you’re doing it a lot, it won’t matter. Changing which button is highlighted will occur at most once per frame, and probably somewhat less frequently.

Also: rendering multiple objects in a single draw call is more efficient than using one draw call per object. And uniforms are … uniform (constant) throughout a draw call.

It could be easy and fast to draw all buttons to their default color. Hoovering-above, selecting and such happens on a cpu-detected button that you can ‘re-draw’ after the first time around. That’ll be two switches of a color uniform (or attribute).

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.