# How to set custom shader with VTK 8.1(C++)

**URL:** https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122
**Category:** Support
**Created:** [January 8, 2025, 5:52am UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122 "2025-01-08T05:52:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![1078586743](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/838e76/32.png) [@1078586743](https://discourse.vtk.org/u/1078586743)
#### Post date: [January 8, 2025, 5:52am UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122/1 "2025-01-08T05:52:37Z")

</div>

Hi,everyone  
How can i set custom shader with VTK8.1?  
I know it’s probably using vtkShaderProgram,but i can’t find any example.  
Could you please provide insights for achieving this? Your guidance would be greatly appreciated.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 8, 2025, 10:32pm UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122/2 "2025-01-08T22:32:07Z")

</div>

Hello,

To set shaders for your mapper in VTK 8.1, please take a look at [Rendering/OpenGL2/Testing/Cxx/TestUserShader.cxx · v8.1.0 · VTK / VTK · GitLab](https://gitlab.kitware.com/vtk/vtk/-/blob/v8.1.0/Rendering/OpenGL2/Testing/Cxx/TestUserShader.cxx) and [Rendering/OpenGL2/Testing/Cxx/TestUserShader2.cxx · v8.1.0 · VTK / VTK · GitLab](https://gitlab.kitware.com/vtk/vtk/-/blob/v8.1.0/Rendering/OpenGL2/Testing/Cxx/TestUserShader2.cxx) .

best,

PC

---

<div class="post-metadata">

### Author: ![1078586743](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/838e76/32.png) [@1078586743](https://discourse.vtk.org/u/1078586743)
#### Post date: [January 9, 2025, 2:08am UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122/3 "2025-01-09T02:08:23Z")

</div>

Thanks for your reply.  
I can’t find the method of `AddShaderReplacement`,but i will try SetVertexShaderCode and SetFragmentShaderCode.  
Hope it works.  
Thanks again for your help

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 9, 2025, 11:19am UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122/4 "2025-01-09T11:19:50Z")

</div>

Hello,

`AddShaderReplacement()` belongs to the `vtkOpenGLShaderProperty` class, which was introduced in VTK 9.

best,

PC

---

<div class="post-metadata">

### Author: ![1078586743](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/838e76/32.png) [@1078586743](https://discourse.vtk.org/u/1078586743)
#### Post date: [January 10, 2025, 3:38am UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122/5 "2025-01-10T03:38:36Z")

</div>

Hello,  
Thanks for your reply.  
I use AddVertexShaderReplacement and AddFragmentShaderReplacement in VTK(9.3.0), which can work well.But while replaced with SetFragmentShaderCode and SetVertexShaderCode, if i set opacity and get closer to the model, it will be low FPS.  
In NVIDIA Nsight Graphics, i find this:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/d/d68a6dbcc51ea215ccbf208fd24cadba820cbe02.png)  
glClear() and Draw many times, could you tell me what’s wrong with it.  
It will have the same issue in VTK(8.1.0).  
Any reply would be greatly appreciated

---

<div class="post-metadata">

### Author: ![1078586743](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/838e76/32.png) [@1078586743](https://discourse.vtk.org/u/1078586743)
#### Post date: [January 10, 2025, 6:33am UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122/6 "2025-01-10T06:33:45Z")

</div>

Sorry,i found this was a Depthpeeling problem, I did not set the reference in the shader

---

<div class="post-metadata">

### Author: ![1078586743](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/838e76/32.png) [@1078586743](https://discourse.vtk.org/u/1078586743)
#### Post date: [January 10, 2025, 6:37am UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122/7 "2025-01-10T06:37:59Z")

</div>

How do I set the uniform attribute in VTK(8.1.0).  
The method of SetUniform is only found in vtkShaderProgram, but i don’t know how to use vtkShaderProgram.No other method related to ShaderProgram was found.  
Any reply would be greatly appreciated

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 6, 2025, 1:46pm UTC](https://discourse.vtk.org/t/how-to-set-custom-shader-with-vtk-8-1-c/15122/8 "2025-03-06T13:46:20Z")

</div>

Hello,

Sorry for the late reply. Examples of usage of `vtkShaderProgram::SetUniform*(...)` can be found in the example codes shared further above:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/1/1e984477304374b489a0c360f16259f32ab57059.png)

best,

PC
