- Home /
Pink custom shader on Android build
I found and re-written some custom shader. Added Alpha with sliders etc. It works just nicely in Unity but after Android build it turn out to be all pink.
Here is the shader:
Shader "Lubo/UnlitGradientOneOne"
{
Properties{
_TopColor("Color1", Color) = (1,1,1,1)
_TopColorAlpha ("Color1 Transparency", Range (0.01, 1)) = 0.5
_BottomColor("Color2", Color) = (1,1,1,1)
_BottomColorAlpha ("Color2 Transparency", Range (0.01, 1)) = 0.5
_MainTex ("Main Texture", 2D) = "white" {}
_Alpha ("Object Transparency", Range (0.01, 1)) = 0.5
}
SubShader
{
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
Pass
{
ZWrite Off
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _Alpha;
float _TopColorAlpha;
float _BottomColorAlpha;
fixed4 _TopColor;
fixed4 _BottomColor;
half _Value;
struct v2f {
float4 position : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
fixed4 alpha : _Alpha;
};
v2f vert (appdata_full v)
{
v2f o;
o.position = UnityObjectToClipPos (v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
o.color = lerp (_TopColor * _TopColorAlpha, _BottomColor * _BottomColorAlpha, v.texcoord.y);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float4 color;
color.rgb = i.color.rgb;
color.a = tex2D (_MainTex, i.uv).a * i.color.a * _Alpha;
return color;
}
ENDCG
}
}
}
Tried various solutions:
setting skybox with directional light.
In Edit -> Project settings -> Graphics added my shader to array "Always included shaders".
Moved shader to location where all the other shaders are, thought It might help
Checked build logs and did not found anything suspicious.
I was thinking whether the shader is even compilable or compatible with android devices. Am I missing something? I am new to shaders and does not quite understand if I wrote something bad in there.
EDIT: After two days "awaiting moderation" I finally resolved this problem. And I can not even "reply" to my own thread and mark it as correct answer. Why?
I found out the program was unable to compile it, it simply worked in Unity, but refused to work on any android device.
Steps which led to successful repair:
Found out there is in Unity built-in shader compiler, which will tell you an error. Simply click on shader and there is a button "compile and show code", there is also a small arrow with various options, disable "skip unused shader_features", now compiler will tell you errors when compiling the code.
Based on this thread I found out about "UNITY_INITIALIZE_OUTPUT(v2f, o);" - I am not sure what it does, but it surely helped to resolve an error about not initialized variable "o"
Now the finally resolving step, removing _Alpha variable, since I already programmed the shader to have alpha for each of the colors, the mail alpha was unnecessary
Here is the final code of the shader which works:
Shader "Lubo/UnlitGradientOneOne"
{
Properties{
_TopColor("Color1", Color) = (1,1,1,1)
_TopColorAlpha ("Color1 Transparency", Range (0.01, 1)) = 0.5
_BottomColor("Color2", Color) = (1,1,1,1)
_BottomColorAlpha ("Color2 Transparency", Range (0.01, 1)) = 0.5
_MainTex ("Main Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
Pass
{
ZWrite Off
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _TopColorAlpha;
float _BottomColorAlpha;
fixed4 _TopColor;
fixed4 _BottomColor;
half _Value;
struct v2f {
float4 position : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
};
v2f vert (appdata_full v)
{
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
o.position = UnityObjectToClipPos (v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
o.color = lerp (_TopColor * _TopColorAlpha, _BottomColor * _BottomColorAlpha, v.texcoord.y);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float4 color;
color.rgb = i.color.rgb;
color.a = tex2D (_MainTex, i.uv).a * i.color.a;
return color;
}
ENDCG
}
}
}