• 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 tBurger · Feb 22, 2016 at 02:31 PM · uishaderssortingsorting ordercustom-shader

UI Canvas sorting with custom UI Shader

So, I'm currently optimizing the shaders for a project and I'm stuck when it comes to custom UI shaders.

I tried to write a Shader that basically does the same as the standard UI Shader from Unity but without all the special cases like masks and whatnot. So I came up with the Shader below.

Problem is: It breaks the Unity internal canvas z-sorting system somehow. So Images with a material attached with this shader seem to sort randomly and I don't know why. Can somebody help me?

Here's the code:

 Shader "Mobile/UI/Default"
  {  
         Properties
         {
                 [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         }
         SubShader
         {
                 Tags 
                 { 
                         "Queue"="Geometry" 
                         "RenderType"="Opaque"
                         "ForceNoShadowCasting" = "True"
                         "IgnoreProjector"="True"
                 }                
                 
                 Pass
                 {
                         Blend SrcAlpha OneMinusSrcAlpha
                         Cull Back
 
                                 // I tried to edit these with no results
                         ZWrite Off
                         ZTest Off
 
 
 
                         CGPROGRAM
                         #pragma vertex vert
                         #pragma fragment frag
 
                         #pragma target 2.0
                         #pragma glsl_no_auto_normalization
 
                         #pragma only_renderers gles gles3 metal d3d11 glcore
 
 
 
                         sampler2D _MainTex;
 
 
 
                         struct VertexInput
                         {
                             fixed4 position:POSITION;
                             fixed2 uv:TEXCOORD0;
                             fixed4 tintColor:COLOR;
                         };
 
                         struct VertexOutput
                         {
                             fixed4 position : SV_POSITION;
                             fixed2 uv:TEXCOORD0;
                             fixed4 tintColor:COLOR;  
                         };
 
                         VertexOutput vert (VertexInput input)
                         {
                                 VertexOutput output;
                                 output.position = mul (UNITY_MATRIX_MVP, input.position);
                                 output.uv = input.uv;
                                 output.tintColor = input.tintColor;
 
                                 return output;
                         }
 
                         fixed4 frag (VertexOutput input) : COLOR
                         {
                                 fixed4 fragmentColor = tex2D (_MainTex, input.uv) * input.tintColor;
 
                                 return fragmentColor;
                         }
                         ENDCG
                 }
         }   
  } 
Comment
Add comment · Show 1
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 tBurger · Feb 23, 2016 at 11:05 AM 0
Share

I found something strange...

I've created a new Shader and copied the code over. So in the end I got 2 Shader files with different names but otherwise identical code. With the first Shader the sorting is broken with the second one it isn't!

So Unity must have some kind of Shader/$$anonymous$$aterial cache that may cache some things internally. Does anyone have a clue about that?

1 Reply

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

Answer by Plawius · Jul 12, 2016 at 12:06 PM

Hey! Open .mat file and remove this line:

 m_CustomRenderQueue: #something#

Now it should work.

Don't ask, just old Unity bug.

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 tBurger · Jul 13, 2016 at 10:31 AM 0
Share

Thanks, I will try this next time this becomes important. We moved on to other projects and left the UI System behind for now. :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom shader remove shine 2 Answers

Manually z-sorting opaque geometry 1 Answer

5.3 : Display particles over an Image component 1 Answer

How to achieve this desired render order with the new 4.6 UI? 2 Answers

Can a UI element create transparency in another under it? 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