- Home /
Shader looks different on iPhone and Editor
I wrote a simple combiner shader that blends two transparent textures. It's actually very similar to the first one Jessy wrote in this thread: http://forum.unity3d.com/viewtopic.php?t=33727
My problem is that the shader works perfectly in the Unity iPhone editor (with gfx emulation on) but it doesn't work right on the iPhone.
Here is the shader:
Shader "My Shaders/Mix Textures" {
Properties { _Mix ("Mix", Range(0,1)) = 0 _Tex1 ("Texture 1", 2D) = "white" {} _Tex2 ("Texture 2", 2D) = "black" {} }
SubShader { Pass { Lighting Off Cull Off Fog {Mode Off}
Blend SrcAlpha OneMinusSrcAlpha
SetTexture[_Tex1] {
ConstantColor (1,1,1,1)
Combine texture * constant
}
SetTexture[_Tex2] {
ConstantColor (1,1,1,[_Mix])
Combine texture lerp(constant) previous
}
}
} FallBack "Transparent/Diffuse"
}
As you can see, the first SetTexture result is overwritten by the second SetTexture at a ratio determined by _Mix. On the iPhone, when _Mix is 0, all I get is pure black whereas the first texture is what should be showing. When _Mix is 1, the second texture shows correctly.
Another thing I noticed is the Range value of _Mix seems to be quantized severely on the iPhone. Hard-setting the value to 0.1 gives me pure black (same as setting it to 0.) I remember from the dot3 normalmap test on the iPhone that the range of 0..1 might only have 8 levels. I'd appreciate it if someone could confirm this.
My biggest concern of course is the shader not working properly when _Mix is 0.
Any ideas? Thanks.
Answer by Jessy · Aug 20, 2010 at 10:33 AM
The MBXLite GPU (pre-3GS) only supports one Constant Color. The last Constant Color defined is the one used any time you use the command. So, as it is, you're multiplying the alpha channel of Texture 1 by zero when Mix is zero. So you won't see any contribution of it. You mentioned that it was black; the only reason it would be black is that whatever was rendered before it was black. If this happened to get rendered before all your other scene geometry, and your camera cleared to a solid color of black, this could happen.
Fortunately, the multiplication in your first texture stage is useless anyway, so we can throw that out. We can also ensure that we won't be blending to the background by putting the shader in a later queue (I think ZWrite off is a safer bet too, but there's no foolproof way to do alpha blending - see what is preferable to you). Using a later queue for blended meshes can make the game run better, also, because it allows tile-based deferred rendering to work its magic better in the Geometry queue. There's not a point in using a fallback because the iPhone hardware won't get worse than it was when it first came out. Fog and lighting are off by default so you don't need those lines. I don't know which texture is "more important" to you, but I always make one of the textures _MainTex to allow for easier scripting. Undo that if you find it confusing, or switch the variables around if Texture 1 is the "more important".
There is no quantization error apparent to my eyes on my iPod touch 2G.
Shader "My Shaders/Mix Transparent Textures" {
Properties { _Mix ("Mix", Range(0,1)) = 0 _Tex1 ("Texture 1", 2D) = "white" _MainTex ("Texture 2", 2D) = "black" }
SubShader { Tags {Queue = Transparent} Cull Off ZWrite Off Blend SrcAlpha OneMinusSrcAlpha
Pass
{
SetTexture[_Tex1]
SetTexture[_MainTex]
{
ConstantColor (1,1,1,[_Mix])
Combine texture Lerp(constant) previous
}
}
}
}
Spot on! Thanks for the very thorough explanation of the problem. Shader works as expected now and the results are great.
Answer by Tetrad · Jun 11, 2010 at 12:40 AM
First question I would ask, are the issues still there when you turn off blending?
It's good that you ask this for a couple reasons. One is that I don't see why "Blend SrcAlpha One$$anonymous$$inusSrcAlpha" is necessary - it looks like it should do nothing but slow down the shader. Second is that we need to make sure you can display this texture on the iPhone at all; you may need to "Enable Set/GetPixels" I have to do that when I procedurally generate textures on the device itself, and there could be other cases where it's necessary. In the cases where it is, you'll get nothing but black otherwise.
Turning off blending fixes the issue of the first texture not appearing, however I need blending for making these textures appear transparent against their background. Jessy, why is Blend unnecessary? Is there another way to use the alpha channel of the textures for transparency? Thanks.
Sorry, I did not understand that you wanted blending between the textures, AND blending into the scene.
Your answer
![](https://koobas.hobune.stream/wayback/20220613061850im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with texture blending and vertex color shader for iOS 1 Answer
Why do Zwrite, Ztest, and Colormask simplifications not optimize shader performance? 3 Answers
Can not be compiled when running on Android for each texture shader 8 Galaxy Tab 3 Answers
Iphone - Blend two textures + Additive blending 3 Answers
Unity iPhone: difference between shader and iPhone-shader? 1 Answer