No BackFace Culling Shader for Unity

Unity doesn’t have a way within the program to turn off BackFace culling. You can do it but it requires code. There is no little button to check. Like most others, I didn’t realize this until I already had spent many long hours optimizing the model with the assumption that I could just change the setting as in other 3d applications.

In my case it was the hair I wanted to show backfaces on. And I really didn’t want to double the amount of faces because I had to go a little high to begin with in order to get the style I wanted. For this character, I wanted a fiery redhead.

Anyway, as a matter of principle I needed to solve the problem. But have been putting it off so I could complete the level. The level is done, and I am on to the character. I finally figured out how to do it and the code is below, just in case you are stuck on the same problem.

Github Download Link: https://github.com/ColoradoStark/2-Sided-Shader-For-Unity3d

Here is the Before and after photo.

IN UNITY WITH BACKFACE CULLING OFF:

BackFace-Culling-Off-Hair

As a Brunette

BackFace-Culling-Off

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SAME MODEL WITH UNITY STANDARD BACKFACE CULLING
You can see that half of my hairstyle disappears with the standard shaders

BackFace-Culling-On

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

HERE IS MY CODE: JUST in case you are having the same problem

 

Shader "Hair-Shader-No-BackFace-Culling" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}

SubShader {
Cull Off
Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
LOD 400

CGPROGRAM
#pragma surface surf BlinnPhong alphatest:_Cutoff
#pragma exclude_renderers flash

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
half _Shininess;

struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};

void surf (Input IN, inout SurfaceOutput o) {
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = tex.rgb * _Color.rgb;
o.Gloss = tex.a;
o.Alpha = tex.a * _Color.a;
o.Specular = _Shininess;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}

FallBack "Transparent/Cutout/VertexLit"
}

Code Screenshot (In Full Technicolor)

Unity-Backface-Shader-Source-Code

4 thoughts on “No BackFace Culling Shader for Unity

Leave a Reply

Your email address will not be published. Required fields are marked *