2

I want to create Shaders in Runtime in Unity. Is there any way? I found that article Creating GLSL Shaders at Runtime in Unity3D but this is no more working because Unity doesn't accept
new Material({shader_code_here});
It requires new Material(Shader.Find("{Shader name in project}");

Any help appreciated, thanks.

Siberhecy
  • 561
  • 1
  • 5
  • 16
  • You can create a material and set its properties in runtime. Do you really need to create the whole shader in runtime? This is not common. – Daniel Apr 06 '20 at 20:39
  • 1
    @Daniel Yes, I need to create Shaders dynamically. – Siberhecy Apr 06 '20 at 20:41
  • @Siberhecy: Are you certain? Are you sure you cannot possibly achieve the effect you're trying to achieve in some other way? – Nicol Bolas Apr 06 '20 at 20:49
  • @NicolBolas Yes because I can't predict what will happen to 3D object. Player will select what should happen to 3D object. Example player selected "Glowing", "UV Animate", "Additional Texture to Main Texture" then Shader will be created according to these variants. That's why I am saying I can't predict. I don't want to use If-else statements because they're bad for performance. – Siberhecy Apr 06 '20 at 20:57
  • 1
    @Siberhecy: "*I don't want to use If-else statements because they're bad for performance.*" First, [no, they're not](https://stackoverflow.com/a/37837060/734069). Second, compiling a shader in the middle of your application is definitely bad for performance. – Nicol Bolas Apr 06 '20 at 20:59
  • What is your platform? – Hamid Yusifli Apr 06 '20 at 21:01
  • @NicolBolas Thanks for Info I'll check for it. Hope you understand what I'm trying to do. – Siberhecy Apr 06 '20 at 21:02
  • I'm building for Windows and Android. – Siberhecy Apr 06 '20 at 21:16
  • You can have a shader for each case (glowing, uv animate, etc). Then, you can attach each shader to a material. In the game, you can have references for each of the materials and in runtime, you can set which material the object will use. – Daniel Apr 06 '20 at 21:34
  • @Daniel Only one shader can be attached to one material. If I apply your way this will look like this. Let say we have 3 variants (Glowing, UV Animate, Color Replace) here is the shader list; • Glowing Shader • UV Animate Shader • Color Replace Shader • Glowing + UV Animate Shader • Glowing + Color Replace Shader • Glowing + UV Animate + Color Replace Shader • UV Animate + Color Replace Shader As you see there is too many Shader list for 3 variants. Think it there will be more variants. – Siberhecy Apr 06 '20 at 21:40
  • That's why I'm saying you should have a single material for a single shader and change the material in run time. Check [this](https://answers.unity.com/questions/1011015/change-material-during-runtime-c.html). – Daniel Apr 06 '20 at 21:48
  • @NicolBolas Of course it is not bad for performance to do dynamic shader compilation. You run compiling ONCE, when the user makes the relevant choice, not in the inner loop. Do you understand the difference between compiling a shader 60 times per second and running a shader 60 times per second? – sinsro May 12 '21 at 14:36
  • @sinsro: Do you understand the difference between a smooth 60fps and 60fps that sometimes drops to 10 for seemingly no reason? – Nicol Bolas May 12 '21 at 14:50
  • @NicolBolas Obviously you do the compilation outside the inner game loop, and you can dynamically pregenerate multiple variants if you need to switch between several in that tight loop later. – sinsro May 25 '21 at 22:26

0 Answers0