How do you use "UnityEngine.Rendering.CullMode.Off"?
I know you can switch culling off using a shader, but it would be better to be able to just switch culling off through script than to have to make shaders that have culling set to Off. The Unity script reference mentions "UnityEngine.Rendering.CullMode.Off", but gives little-to-no information about it or how to implement it.
Answer by ASGS_DumbFox · Mar 10, 2021 at 08:02 PM
Hey @Honorsoft
I came across your posting while Also looking for my own solution to doing this and figured out how CullMode works! You can see the forum that I used here link text.
As Glitchers has posted in the original forum using:
Shader "Example/CullControl" {
Properties
{
[Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull", Float) = 0
}
SubShader
{
Cull [_Cull]
}
}
in the shader allows you have create an accessible parameter in your shader (this only works on custom shaders you've created). This parameter can then be accessed through code as such:
rend.material.SetFloat("_Cull", 2);
once you have a reference to a renderer you can access that renderers material property and change the float value of the "_Cull" parameter. This leads back to the API info about Cull Mode and effectively it goes: 0 - No Culling, 1 - Front Face Culling, 2 - Back Face Culling.
Hope that helps!!
Thanks a lot for this answer, and I love that you showed an example of accessing a shader variable from script (which is always my preference). I see a lot of other people online that are/were having trouble with back-face culling, I have said before (to Unity also) that I am surprised that there is no checkbox or anything in the material inspector for culling, since it seems common to need to turn culling off for things like hair, capes, and the walls of structures, etc. But I guess your example shows you could add a checkbox to the shader for culling ( [ToggleOff]
). Your example also gives more control than just toggling culling on/off, that is also handy. If someone needed to have back-face culling for efficiency, then they could even dynamically control the culling setting during runtime using the SetFloat. Thanks.
*By the way, I forgot I wrote this question and I have advanced in Unity a lot since then, however I am still looking for a good 2-sided shader that is close to Unity's standard shader. Just to summarize my problem from the start (for you or anyone having the same issues or who knows what I may be doing wrong), I edited the Unity 5 standard shader to add culling off in the sub-shader section which worked great in Unity 5 only. So I edited a Unity 2018 standard shader the same way and that seemed to work but the other side of the object was semi-transparent (even when opaque). Maybe I didn't put the "Cull Off" in the right place? I will try this method you showed though, thanks again.