Search Unity

Can you access shader backface culling property with c#?

Discussion in 'Shaders' started by KinanEldari, Sep 23, 2016.

  1. KinanEldari

    KinanEldari

    Joined:
    Apr 13, 2016
    Posts:
    2
    Hey folks, I realise this question can be asked in both the shaders and the coding sections of the forum, so I just picked one.

    Anyway, the question in the title already asks it, but I know for a fact that you can change the render queue order of objects through C# code, as I have managed to solve a render order problem with that. However, that's only part of my required sollution. After applying a lot of google-fu whenever I had the time, I have come up empty. I know that there is a Cull option in shader code, but I've yet been able to successfully compile the shader successfully after editing the original Unity 5 shader, because either I end up with a completely empty MonoDevelop and a broken shader file, or the shader simply won't be run by Unity.

    I'm using the Mobile\Particles\AlphaBlended shader to make (partially) transparent planes more manageable for mobile devices, but it has the backface culling property set to off. I would like to know; is there an easy way to copy that shader and make the necessarry edit? Or can I perhaps change the shader property through C# code like I can with renderQueue? The Unity documentation seems awfully sparse on the subject.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    There isn't a built in way to change the culling of a shader from c#, no, but it is possible to do it with a custom shader and a shader property which you can modify from c#.

    You can download the built in shader source files from the Unity download archive page, then make a copy of the shader you want and find:

    Cull Off

    Change that line to Cull [_Cull] and in the Properties block add:
    _Cull ("Culling", Float) = 2.0

    After that you would be able to use myMaterial.SetFloat("_Cull", value); to change the culling mode. It's an enum value for back, front, and off, possibly in that order, but you'd have to experiment.
     
  3. KinanEldari

    KinanEldari

    Joined:
    Apr 13, 2016
    Posts:
    2
    Thank you, I'll give that a look.