I'm trying to write a simple diffuse shader that ignores lighting and ztesting and turns off culling. Additionally it takes in a float that specifyes the alpha value of what to draw. Here is what i have so far:
Shader "FadeOut"
{
Properties {
_DiffuseMap("_DiffuseMap", 2D) = "white" {}
_AlphaFade("_AlphaFade", Float) = 1
}
SubShader {
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Fog { Mode Off }
CGPROGRAM
#pragma surface surf BlinnPhongEditor alpha
#pragma target 3.0
sampler2D _DiffuseMap;
float _AlphaFade;
struct EditorSurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
};
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light) {
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha + Luminance(spec);
return c;
}
inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
viewDir = normalize(viewDir);
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot (s.Normal, lightDir));
float nh = max (0, dot (s.Normal, h));
float3 spec = pow (nh, s.Specular*128.0) * s.Gloss;
half4 res;
res.rgb = _LightColor0.rgb * (diff * atten * 2.0);
res.w = spec * Luminance (_LightColor0.rgb);
return LightingBlinnPhongEditor_PrePass( s, res );
}
struct Input {
float2 uv_DiffuseMap;
};
void surf (Input IN, inout EditorSurfaceOutput o) {
o.Albedo = 0.0;
o.Normal = float3(0.0,0.0,1.0);
o.Emission = 0.0;
o.Gloss = 0.0;
o.Specular = 0.0;
o.Alpha = 1.0;
float4 Sampled2D0=tex2D(_DiffuseMap,IN.uv_DiffuseMap.xy);
float4 Multiply1=float4( Sampled2D0.a) * float4(_AlphaFade);
float4 Saturate0=saturate(Multiply1);
o.Albedo = Sampled2D0;
o.Emission = Sampled2D0;
o.Alpha = Saturate0;
}
ENDCG
}
Fallback "Diffuse"
}
This works to an extent. My issue is when the script runs everything is saturated to green :(
Can anyone help me out?
What you describe is a Transparent/Unlit shader like this:
Shader "Transparent/Unlit" {
Properties {
_Alpha ("Alpha", Range(0,1)) = 1
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader {
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Fog { Mode Off }
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
CGPROGRAM
#pragma surface surf Unlit alpha
sampler2D _MainTex;
float _Alpha;
inline half4 LightingUnlit (SurfaceOutput s, half3 lightDir, half atten) {
half diff = max (0, dot (s.Normal, lightDir));
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
inline half4 LightingUnlit_PrePass (SurfaceOutput s, half4 light) {
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a * _Alpha;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
This won't ignore ambient lighting. It's almost like you want a to customize the standard Transparent/Cut-off/Soft-Edge Unlit. That would look something like:
/*
Renders doubled sides objects without lighting. Useful for
grass, trees or foliage.
This shader renders two passes for all geometry, one
for opaque parts and one with semitransparent details.
This makes it possible to render transparent objects
like grass without them being sorted by depth.
*/
Shader "Transparent/Unlit" {
Properties {
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_Alpha ("Alpha", Range (0,1)) = 1
}
SubShader {
Tags { "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
Lighting off
// Render both front and back facing polygons.
Cull Off
// first pass:
// render any pixels that are more than [_Alpha] opaque
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Alpha;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.texcoord);
col.a *= _Alpha;
return col;
}
ENDCG
}
// Second pass:
// render the semitransparent details.
Pass {
Tags { "RequireOption" = "SoftVegetation" }
// Dont write to the depth buffer
ZWrite off
// Set up alpha blending
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Alpha;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.texcoord);
col.a *= _Alpha;
return col;
}
ENDCG
}
}
SubShader {
Tags { "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
Lighting off
// Render both front and back facing polygons.
Cull Off
// first pass:
Pass {
SetTexture [_MainTex] {}
}
// Second pass:
// render the semitransparent details.
Pass {
Tags { "RequireOption" = "SoftVegetation" }
// Dont write to the depth buffer
ZWrite off
// Set up alpha blending
Blend SrcAlpha OneMinusSrcAlpha
SetTexture [_MainTex] {}
}
}
}
Nothing in your shader will result in over-saturated greens so the problem must lie elsewhere.
My issue is when the script runs everything is saturated to green.
Short answer:
- Check your ambient light settings.
- Check any procedurally generated textures for validity.
- Check your license if you're using GL calls. GL calls are for Unity Pro only.
- Check your texture binding. Your code set "_MainTex" while your shader expose "_DiffuseMap".
- Check your alpha binding. Your code set "_Alpha" while your shader expose "_AlphaFade".
- Check your render passes. You're only rendering pass 0, but using surface shaders which are multipass.
Long answer:

Cube using a material with the original shader posted, with standard assets "glass" texture and AlphaFade value of 1.0f, run in the editor.
I don't know your script (or do you mean your shader?), what texture you're using nor your alpha range but I just tried this with some random texture and fed alpha random values and I saw no green saturation. I don't know what you're talking about. I can't see any obvious errors. How can I reproduce your green saturated state?
A rule of thumb though when colors go wild, is to use saturate() in places such as function returns and parameter passing. If intermediate colors have other color ranges than 0 to 1, often errors can emerge. Make sure all your values are within expected ranges with saturate or clamp.
A possibility that struck me is that your ambient color in Render Settings is at bright green. I tested this and the result changed to behave like this:

Here, strong green ambient is applied to the scene.
Another possibility is that you are creating render/textures improperly. If you know you are using rendertextures or creating textures manually, you might want to suspect that code doing something fishy.
FML...
I right clicked on the shader and made a material out of it. Now everything works as it should... I guess i don't understand how to create a material trough code as well as i tought.
Another odd issue is if i try to convert the shader source into a string and make a material that way i get a "Error in [SHADER] on line1" error....
I'm confused, but something is working.
Thanks for all the help guys!