• 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
10
Question by DaveA · Jan 26, 2011 at 12:49 AM · shaderrenderingtextz-depth

Text mesh (font material) renders on top of everything, any way to change that?

I have these objects with some text on them floating in my space (labeling things as it were). My problem is that apparently, text is drawn last, so it is visible always, even if it should be occluded by intervening solid objects. Is there a way to fix that? Do I have to muck with the shader on it?

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 MFKJ · Jun 11, 2020 at 08:48 AM 0
Share

Its 2020, For new Text $$anonymous$$esh Pro you can use distance field shader here is the demo https://youtu.be/7HnixlwlQ5Q

7 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jerrytjohn · Jun 09, 2017 at 08:58 PM

 Shader "GUI/3D Text Shader - Cull Back" {
     Properties{
         _MainTex("Font Texture", 2D) = "white" {}
     _Color("Text Color", Color) = (1,1,1,1)
     }
 
         SubShader{
         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
         Lighting Off Cull Back ZWrite Off Fog{ Mode Off }
 
         Blend SrcAlpha OneMinusSrcAlpha
         Pass{
             Color[_Color]
             ColorMaterial AmbientAndDiffuse
             SetTexture[_MainTex]{
                 combine primary, texture * primary
             }
         }
     }
 }
 
Comment
Add comment · 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
1

Answer by marcel_z · Nov 13, 2018 at 05:52 PM

As some people might still want an answer to this, the TextMesh Color seems to set the Vertex colors of the generated mesh, so the "_Color" field in the shader is useless and you should instead add the Vertex Color in the shader.

Simple solution - use the builtin "Font.shader" and remove the the "ZTest Always" and the useless "_Color" parts:

 Shader "GUI/3D Text Shader"
 {
     Properties
     {
         _MainTex("Font Texture", 2D) = "white" {}
     }
 
     SubShader
     {
         Tags
         {
             "Queue" = "Transparent"
             "IgnoreProjector" = "True"
             "RenderType" = "Transparent"
             "PreviewType" = "Plane"
         }
         Lighting Off
         Cull Off
         ZWrite Off
         Fog { Mode Off }
         Blend SrcAlpha OneMinusSrcAlpha
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
             #include "UnityCG.cginc"
 
             struct appdata_t {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
                 float2 texcoord : TEXCOORD0;
                 UNITY_VERTEX_INPUT_INSTANCE_ID
             };
 
             struct v2f {
                 float4 vertex : SV_POSITION;
                 fixed4 color : COLOR;
                 float2 texcoord : TEXCOORD0;
                 UNITY_VERTEX_OUTPUT_STEREO
             };
 
             sampler2D _MainTex;
             uniform float4 _MainTex_ST;
 
             v2f vert(appdata_t v)
             {
                 v2f o;
                 UNITY_SETUP_INSTANCE_ID(v);
                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.color = v.color;
                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target
             {
                 fixed4 col = i.color;
                 col.a *= tex2D(_MainTex, i.texcoord).a;
                 return col;
             }
             ENDCG
         }
     }
 }

Edit: alternatively without the CGPROGRAM

 Shader "GUI/3D Text Shader"
 {
     Properties
     {
         _MainTex("Font Texture", 2D) = "white" {}
     }
 
     SubShader
     {
         Tags
         { 
             "Queue" = "Transparent" 
             "IgnoreProjector" = "True" 
             "RenderType" = "Transparent" 
         }
         Lighting Off
         Cull Off 
         ZWrite Off 
         Fog{ Mode Off }
 
         Blend SrcAlpha OneMinusSrcAlpha
         Pass
         {
             ColorMaterial AmbientAndDiffuse
             SetTexture[_MainTex]
             {
                 combine primary, texture * primary
             }
         }
     }
 }
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 TaehoonYoon · Aug 24, 2020 at 05:22 AM 0
Share

That's exactly what I was looking for. Thank you!

  • ‹
  • 1
  • 2

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

16 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

Related Questions

2.5D perspective rendering order challenges 2 Answers

Camera with shader? 2 Answers

Screen freezes after running the game [ Shaders problem ] 0 Answers

U5 shader coming out solid black on newer iOS devices (iPad 2 Air / iPhone 6) 1 Answer

Subtractive Rendering? 4 Answers


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