Update 5: At the end of the day, I still have no idea why my first attempt failed, but I switched to the method described here by JoshuaMcKenzie and it seems to be working beautifully on iOS.
Update 4: It looks like my whole issue is that the main texture is reliant on a camera set to Don’t Clear in order to determine where the player has been, but this isn’t a thing on mobile. So now I’m left trying to find a workaround.
Update 3: OK now I’m REALLY confused. I simplified the shader and tested the following 2 scenarios:
(1) The following works (properly detects the blue player ring and sets it to transparent while all other areas are black)
float main = tex2D(_MainTex, i.uv).b; // Main texture checks for blue ring around player
float second = tex2D(_SecondTex, i.uv).b; // Second texture checks for blue ring around player
if(second >= 1){
return fixed4(0,0,0,0);
} else{
return fixed4(0,0,0,1);
}
(2) The following does not work (always returns fully transparent)
float main = tex2D(_MainTex, i.uv).b; // Main texture checks for blue ring around player
float second = tex2D(_SecondTex, i.uv).b; // Second texture checks for blue ring around player
if(main >= 1){
return fixed4(0,0,0,0);
} else{
return fixed4(0,0,0,1);
}
So now I’m left thinking that there’s something about the main texture that iOS doesn’t like. But the main and second textures are identical (clones), and again it works in the editor no problem.
Update 2: Getting closer… I think it might have to do with compression of the texture. Updating that last shader block to the following makes the part that should be black (but was erroneously transparent) change to 0.5f alpha, which I think is a step in the right direction.
fixed4 frag (v2f i) : SV_Target
{
float main = tex2D(_MainTex, i.uv).r > 0.5f ? 1 : 0;
float second = tex2D(_SecondTex, i.uv).b > 0.5f ? 1 : 0;
fixed4 colMain = fixed4(main,0,0,0);
fixed4 colSecond = fixed4(0,0,second,0);
fixed4 col = colMain + colSecond;
col.a = 2.0f - col.r*1.5f - col.b*0.5f;
return fixed4(0,0,0,col.a);
}
Update 1: When I change the last block in the shader to simply return fixed4(0,0,0,255), iOS does render a full black screen. So this really makes me think there’s an issue in that area for iOS related to transparency.
I’m working on a fog of war and got it working perfectly in the editor, but when I built for iOS there was a big issue: the GameObject that overlays the entire scene uses a RawImage with a material and texture that should be black with alpha set to 255 until the player moves over an area, but iOS makes the alpha start at 0.
Here is the expected outcome:

Here it is on iOS:
What’s interesting is that the area that has been explored but is out of immediate range is rendered properly (alpha is about 50%).
Here is the Raw Image on the game object that covers the entire map in fog:

I think the problem has to do with the shader, since the 50% alpha works fine - this makes me think the textures and cameras and everything else are good, but iOS doesn’t like something about the calculated transparency. But I am just starting to learn about shaders, so I can’t make sense of why this would be.
Shader code:
Shader "Hidden/FOWShader"
{
Properties
{
_MainTex ("Main Texture", 2D) = "white" {}
_SecondTex ("Secondary Texture", 2D) = "white" {}
}
SubShader
{
Tags { "Queue"="Transparent+1" } // to cover other transparent non-z-write things
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
sampler2D _SecondTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv) + tex2D(_SecondTex, i.uv);
col.a = 2.0f - col.r*1.5f - col.b*0.5f;
return fixed4(0,0,0,col.a);
}
ENDCG
}
}
}
Any ideas? Want me to show the textures or hierarchy or anything else? Thanks.
