Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VoxelBoy · Jun 10, 2010 at 11:54 PM · shaderiphone

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
     }
 }

}

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VoxelBoy · Aug 24, 2010 at 12:53 AM 0
Share

Spot on! Thanks for the very thorough explanation of the problem. Shader works as expected now and the results are great.

avatar image
0

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Jun 11, 2010 at 04:57 AM 0
Share

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.

avatar image VoxelBoy · Jun 11, 2010 at 12:34 PM 0
Share

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.

avatar image Jessy · Aug 20, 2010 at 10:20 AM 0
Share

Sorry, I did not understand that you wanted blending between the textures, AND blending into the scene.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges