Hi,
I've just started Unity Android development and found out that one of the modified shaders that our project is using is causing the devices to crash.
The shader is a modified cutout soft-edge shader. I removed the color component out of the shader computation (at least I think I did) because I didn't need to do any color modulation.
When using the vanilla Soft-Edge shader it works fine, but only when I try to use this optimized soft-edge shader I run into problems. For now I'm just using the vanilla one. However, is there a reason why this version doesn't work? It works fine in the Editor, it just creates a Signal 11 crash when I run it on my HTC Nexus One device. Any help would be appreciated so that I can better understand what I did wrong.
Thanks!
Properties {
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
Lighting off
// first pass:
// render any pixels that are more than [_Cutoff] opaque
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Cutoff;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
float4 _Color;
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.texcoord);
clip(col.a - _Cutoff);
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;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Cutoff;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
float4 _Color;
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.texcoord);
clip(-(col.a - _Cutoff));
return col;
}
ENDCG
}
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
Lighting off
// first pass:
// render any pixels that are more than [_Cutoff] opaque
Pass {
AlphaTest Greater [_Cutoff]
SetTexture [_MainTex] {
combine texture
}
}
// Second pass:
// render the semitransparent details.
Pass {
// Dont write to the depth buffer
ZWrite off
// Only render pixels less or equal to the value
AlphaTest LEqual [_Cutoff]
// Set up alpha blending
Blend SrcAlpha OneMinusSrcAlpha
SetTexture [_MainTex] {
Combine texture
}
}
}
I don't know anything about Android. But the way that Unity handles soft edges is goofy. You shouldn't Alpha Test in the second pass, but rather, use ZTest Less, and multiply the alpha of the texture by 1/_Cutoff. That way, you don't have stupid-looking hard alpha tested edges next to ghostly apparitions of the edge. To do this in fixed function, multiply by .25/_Cutoff instead, and use Quad in the alpha calculation. Also, "RequireOption"="SoftVegetation" is deprecated.
– Jessy