- Home /
weird shader problem (correct in editor, weird ingame)
I wrote a simple shader that's basically just bumped-diffuse, but fades out when close to the camera.
It works in the scene (also when playing), but in the game it looks as if the areas that should be transparent is instead the previous frame beeing added additively (relative to the supposed transparency).
I have no idea what's wrong, any ideas ?
here's the shader: Shader "Custom/Planet" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} _Dist ("FadeOut Distance", float) = 20 //_PlanetFogColor ("fog Color", Color) = (1,1,1,1) }
SubShader
{
ZWrite On
Fog { Mode off }
Tags { "RenderType"="Transparent" "Queue" = "Geometry+1" } //Queue is nodig (anders fout)
Blend SrcAlpha OneMinusSrcAlpha
//Tags { "RenderType" = "Opaque" }
//LOD 300
CGPROGRAM
#pragma surface surf Lambert vertex:vert
fixed4 _Color;
sampler2D _MainTex;
sampler2D _BumpMap;
float _Dist;
fixed4 _PlanetFogColor; //global
float _UsePlanetFog; //global (0 of 1, bool bestaat niet)
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
float z;
};
void vert(inout appdata_full v, out Input o)
{
o.z = (mul(UNITY_MATRIX_MVP, v.vertex)).w;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = 1;//c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
if (_UsePlanetFog>0.5)
{
float fade = 1;
if (IN.z<_Dist)
fade = IN.z/_Dist;
fade=sqrt(fade);
fade = fade*1.05-0.05;
if (fade<0) fade=0;
o.Alpha = fade;
////o.Albedo = c.rgb;
//o.Albedo = (c*fade).rgb;//(c*fade + _PlanetFogColor*(1-fade)).rgb;
////o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));//normalize(UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap))*fade + float3(0,0,1)*(1-fade)); //normal ook uitfaden //niet meer nodig
//o.Emission = ((1-fade)*_PlanetFogColor).rgb;//(1-fade)*o.Albedo; //niet ideaal, zou eigenlijk moeten faden naar unlit color, dit is maar een benadering
}
//else
//{
// o.Albedo = c.rgb;
//}
//o.Alpha = 0;
}
ENDCG
}
FallBack "Bumped Diffuse"
}
also, the should fix/change the formatting for code blocks on UnityAnswers, as they never seem to work (Edit: the button dousn't work, but writing it yourself apparently does)
ok, what's up with the code formatting, I fixed it, but it still looks the same afterwards
I've started to use $$anonymous$$onoDevelop to indent once, then copy the code over... Sorry can't help with the Shader though :(
Answer by Steven-1 · May 30, 2012 at 11:16 AM
writing this question I realised I had based this on the builtin bumped-diffuse shader instead of the bumped-transparent, looking at it I noticed I shouldn't have put in "Blend SrcAlpha OneMinusSrcAlpha" as it's a surface shader, and I also had to add "alpha" after "#pragma surface surf Lambert"
woops, not used to writing surface shaders
Answer by CaliCoastReplay · Feb 11, 2018 at 04:12 AM
This can also be caused by failing to have a Render Queue setting above 3000 (Transparent) in the shader's material in the Editor - I experienced it myself today.