I have a custom shader which basically just renders transparent textures with a tint, plus a special transparency effect specific to the needs of our game. It works great on everything, except it appears totally invisible and nothing is rendered when I want to use it on a particle renderer or on a Line Renderer. Problem is I’m not well-versed in CG at all and I’m not certain what the problem could be.
Edit: see thread below… seems to likely be Unity affecting the MVP data passed to the shader. More info needed!
Is there any obvious common problem anyone can think of that might be causing this?
Shader is below. Note: _EntityPos is sent to the shader by an external script. The shader just fades the alpha per-pixel based on the distance of the player.
Shader "Custom/ProximityTransparency" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Tint Color (RGB)", Color) = (1,0,0,1)
_EntityPos ("Entity Position", Vector) = (0,0,0)
_VisibleRange ("Fully Visible Range", Float) = 1
_TransDistance ("Transparent Distance", Float) = 37
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="GlowMap" }
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Pass {
CGPROGRAM
// Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members fpos)
#pragma exclude_renderers xbox360
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
float4 pos : POSITION;
float3 fpos;
half2 uv : TEXCOORD0;
};
v2f vert (appdata_img v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
float4 temp = mul (_Object2World, v.vertex);
o.fpos = temp.xyz / temp.w;
o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
return o;
}
sampler2D _MainTex;
float4 _Color;
float _VisibleRange;
float _TransDistance;
float3 _EntityPos;
float4 frag( v2f i ) : COLOR
{
// grab the color from the texture and tint it by _Color
float4 c = tex2D (_MainTex, i.uv) * _Color;
// get the distance between the surface fragment and the entity position
float dist = distance (i.fpos, _EntityPos);
// lerp the alpha (negative and values over 1f will just be clamped to 0 and 1)
dist -= _VisibleRange;
dist /= (_TransDistance - _VisibleRange);
dist /= 2; // This line and the next reduce the range to 0 to 0.5 alpha rather than fully visible
c.a *= 0.5 - dist;
// return the color
return c;
}
ENDCG
}
}
FallBack "Fallback Invisible"
}