<p>I've tried looking everywhere, but I just don't get it. How is multiple shaders assigned to a mesh if you can only bind a single shader in OpenGL?</p>

<p>Assume I have a mesh with a texture shader and now I do a ray-cast from the mouse position to select the object from the editor. In this case I would like to apply a selection shader that highlights that the mesh is selected, but still preserves the texture shader of the mesh. </p>

<p>I want to apply the selection shader on top of the texture shader of the mesh. How is this generally done in game engines?</p>

<p>One way of doing this would be to make a completely third shader that is combination of both shaders and just apply the third shader. But that doesn't seem efficient and would become tedious when trying to add new shaders; now you have to specify a selection shader for every shader in the game, in case it's selected in the editor.</p>

<p>Any help is appreciated and if you can guide to some reading material that would be amazing.</p>

CC BY-SA 4.0
2019-10-05T20:10:42.863Z
"*mesh with a texture shader*" What is a "texture shader"?
Nicol Bolas
CC BY-SA 4.0
2019-10-05T20:35:40.360Z
"mesh with a texture shader" what I meant with that was a shader that takes in a sampler2D texture and samples the coordinates and outputs the frag color.
FableProtector
CC BY-SA 4.0
2019-10-05T21:38:52.653Z
You can use the stencil buffer to create a mask. Then use that mask to apply your selection shader only on the part of the screen that the mask covers.
lightxbulb
CC BY-SA 4.0
2019-10-10T14:17:01.297Z
Just because you think X is one thing does not mean a lower level system considees this to be true. Nothing stops the game engine from drawing the thing you call a mesh as 2 separate meshes, or lump what you call 2 shaders into one.
joojaa
<opengl><rendering><shader><mesh>
How to render multiple shaders per mesh
FableProtector
# How to render multiple shaders per mesh

(Asked by: username_0 on 2019-10-05)

I've tried looking everywhere, but I just don't get it. How is multiple shaders assigned to a mesh if you can only bind a single shader in OpenGL?

Assume I have a mesh with a texture shader and now I do a ray-cast from the mouse position to select the object from the editor. In this case I would like to apply a selection shader that highlights that the mesh is selected, but still preserves the texture shader of the mesh.

I want to apply the selection shader on top of the texture shader of the mesh. How is this generally done in game engines?

One way of doing this would be to make a completely third shader that is combination of both shaders and just apply the third shader. But that doesn't seem efficient and would become tedious when trying to add new shaders; now you have to specify a selection shader for every shader in the game, in case it's selected in the editor.

Any help is appreciated and if you can guide to some reading material that would be amazing.

**Tags:** opengl, rendering, shader, mesh

> **Comments:**
> **username_1**: "*mesh with a texture shader*" What is a "texture shader"?  
> **username_0**: "mesh with a texture shader" what I meant with that was a shader that takes in a sampler2D texture and samples the coordinates and outputs the frag color.  
> **username_2**: You can use the stencil buffer to create a mask. Then use that mask to apply your selection shader only on the part of the screen that the mask covers.  
> **username_3**: Just because you think X is one thing does not mean a lower level system considees this to be true. Nothing stops the game engine from drawing the thing you call a mesh as 2 separate meshes, or lump what you call 2 shaders into one.  

---

## 0 answers

