Hello! We have a custom alphatest shader. It take alpha from another texture (not from MainTex.a) and controlled by vertex color. But Unity Ignor this alpha for Baked lighting or ray casting. Unity use only alpha from main texture and cutoff value. Is it possble to tell unity use different alpha? Code (CSharp): Shader "Custom/Unlit Cutout Step (Supports Lightmap)" { Properties { _MainTex("Texture", 2D) = "white" {} _AlphaTex("Alpha", 2D) = "grey" {} _Cutoff("Alpha cutoff", Range(0,1)) = 0.5 } SubShader { Tags{ "Queue" = "Geometry"} Pass { Tags{ "LightMode" = "Always"} // AlphaTest Greater .5 CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #pragma multi_compile _ LIGHTMAP_ON struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float2 uv1 : TEXCOORD1; float4 vertexColor : COLOR; }; struct v2f { float2 uv : TEXCOORD0; float2 uv1 : TEXCOORD3; UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; float4 vertexColor : COLOR; }; sampler2D _MainTex; float4 _MainTex_ST; fixed _Cutoff; sampler2D _AlphaTex; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); UNITY_TRANSFER_FOG(o,o.vertex); o.uv1 = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; o.vertexColor = v.vertexColor; return o; } fixed4 frag (v2f i): SV_Target { fixed4 color = tex2D(_MainTex, i.uv); fixed alpha = tex2D(_AlphaTex, i.uv); #if !UNITY_STANDALONE_WIN fixed3 lightmapColor = 2.0f * UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy); //decode ligthmap #else fixed3 lightmapColor = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy)); //decode ligthmap #endif #ifdef LIGHTMAP_ON color.rgb *= lightmapColor; #endif UNITY_APPLY_FOG(i.fogCoord, color); clip(i.vertexColor.r - alpha); return color; } ENDCG } } Fallback "Mobile/Unlit (Suports Lightmap)" } qw
I try it; Work fine in realtime, but seems to be ignored for baking light( Code (CSharp): Properties { _MainTex("Texture", 2D) = "white" {} _AlphaTex("Alpha", 2D) = "grey" {} _Cutoff("Alpha cutoff", Range(0,1)) = 0.5 } SubShader { Tags{ "Queue" = "Geometry"} Pass { Tags{ "LightMode" = "Always"} // AlphaTest Greater .5 CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #pragma multi_compile _ LIGHTMAP_ON struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float2 uv1 : TEXCOORD1; float4 vertexColor : COLOR; }; struct v2f { float2 uv : TEXCOORD0; float2 uv1 : TEXCOORD3; UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; float4 vertexColor : COLOR; }; sampler2D _MainTex; float4 _MainTex_ST; fixed _Cutoff; sampler2D _AlphaTex; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); UNITY_TRANSFER_FOG(o,o.vertex); o.uv1 = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; o.vertexColor = v.vertexColor; return o; } fixed4 frag (v2f i): SV_Target { fixed4 color = tex2D(_MainTex, i.uv); fixed alpha = tex2D(_AlphaTex, i.uv); clip(i.vertexColor.r - alpha); #if !UNITY_STANDALONE_WIN fixed3 lightmapColor = 2.0f * UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy); //decode ligthmap #else fixed3 lightmapColor = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy)); //decode ligthmap #endif #ifdef LIGHTMAP_ON color.rgb *= lightmapColor; #endif UNITY_APPLY_FOG(i.fogCoord, color); return color; } ENDCG } Pass { Name "Caster" Tags { "LightMode" = "ShadowCaster" } Offset 1, 1 Fog {Mode Off} ZWrite On ZTest LEqual Cull Off CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcaster #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float4 normal : NORMAL; float4 vertexColor : COLOR; }; struct v2f { V2F_SHADOW_CASTER; float2 uv : TEXCOORD0; float4 vertexColor : COLOR2; fixed3 normal : TEXCOORD3; }; float4 _MainTex_ST; sampler2D _AlphaTex; v2f vert( appdata v ) { v2f o; TRANSFER_SHADOW_CASTER(o) o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.normal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal)); o.vertexColor = v.vertexColor; return o; } float4 frag( v2f i ) : COLOR { fixed4 alpha = tex2D( _AlphaTex, i.uv ); clip(i.vertexColor.r - alpha.r); SHADOW_CASTER_FRAGMENT(i) } ENDCG } // Pass to render object as a shadow collector Pass { Name "ShadowCollector" Tags { "LightMode" = "ShadowCollector" } Fog {Mode Off} ZWrite On ZTest LEqual CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcollector #define SHADOW_COLLECTOR_PASS #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float4 normal : NORMAL; float4 vertexColor : COLOR; }; struct v2f { V2F_SHADOW_COLLECTOR; float2 uv : TEXCOORD5; float4 vertexColor : COLOR2; fixed3 normal : TEXCOORD6; }; float4 _MainTex_ST; sampler2D _AlphaTex; v2f vert (appdata v) { v2f o; TRANSFER_SHADOW_COLLECTOR(o) o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.normal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal)); o.vertexColor = v.vertexColor; return o; } fixed4 frag (v2f i) : COLOR { fixed4 alpha = tex2D( _AlphaTex, i.uv ); clip(i.vertexColor.r - alpha.r); SHADOW_COLLECTOR_FRAGMENT(i) } ENDCG } } // Fallback "Mobile/Unlit (Suports Lightmap)" }
Should be fine and it's a lot easier compared to customizing all passes. For light baking there is yet another shader pass.
Also we use raytrace for lighting dynamic models on the scene, and unity give incorrect result for surface opacity too. (outline when select show "real opacity") And on trasparent areas person becomes dark. PS Trying to create surface shader. Result is the same.