Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

3D text that takes the depth buffer into account

Discussion in 'Editor & General Support' started by ratamorph, Apr 3, 2008.

Thread Status:
Not open for further replies.
  1. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Hey guys is there a way to make the builtin 3D text take the depth buffer into account so it's clipped by objects in front of it?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Make a new GUI/Text Shader that's a copy of the built-in one, but with "ZTest Always" removed, and use that.

    --Eric
     
    Valia-Lee and angelonit like this.
  3. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Cool, thanks man I'll give it a shot
     
  4. logic-cpp

    logic-cpp

    Joined:
    Mar 11, 2013
    Posts:
    24
    Did as instructed:

    - "Internal-GUITextureClipText.shader" version 4.1.2 of built in shaders
    - Removed all 3 occurences of "ZTest Always"

    Nothing renders...
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  6. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    Can I make a 3d text with rich-edit support that is occluded by geometry ?
     
  7. Joel_Ruiz_WV@yahoo.com

    Joel_Ruiz_WV@yahoo.com

    Joined:
    Jun 10, 2013
    Posts:
    1
    My default shader does not contain ZTest Always in it.
     
  8. ddf

    ddf

    Joined:
    Jul 9, 2011
    Posts:
    54
    Can't be bothered to go through wiki sign-up and anyway this thread is higher in google results...

    The shader on the linked wiki page did not work in Unity 4. Color of 3DText is set with vertex color, not even sure why their font shader requires a font color. So, this is what you want. Without Cull specified it does cull the back faces, so if you want double-side just put Cull Off in there under ZWrite Off

    Code (csharp):
    1.  
    2. Shader "GUI/3DText"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Font Texture", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Transparent" "Queue"="Transparent"}
    12.      
    13.         Pass
    14.         {
    15.             Blend SrcAlpha OneMinusSrcAlpha
    16.             ZWrite Off
    17.          
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #include "UnityCG.cginc"
    22.  
    23.             sampler2D _MainTex;
    24.          
    25.             struct v2f {
    26.                 float4 pos : SV_POSITION;
    27.                 fixed4 color : COLOR;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.          
    31.             struct appdata {
    32.                 float4 vertex : POSITION;
    33.                 fixed4 color : COLOR;
    34.                 float2 texcoord : TEXCOORD0;
    35.             };
    36.  
    37.             float4 _MainTex_ST;
    38.          
    39.             v2f vert (appdata v)
    40.             {
    41.                 v2f o;
    42.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    43.                 o.color = v.color;
    44.                 o.uv    = TRANSFORM_TEX (v.texcoord, _MainTex);
    45.                 return o;
    46.             }
    47.          
    48.             fixed4 frag (v2f o) : COLOR
    49.             {
    50.                 // this gives us text or not based on alpha, apparently
    51.                 o.color.a  *= tex2D( _MainTex, o.uv ).a;
    52.                 return o.color;
    53.             }
    54.             ENDCG
    55.         }
    56.     }
    57. }
    58.  
     
    REVWD, Fattie, Ian094 and 2 others like this.
  9. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    ddf,

    "Can't be bothered to go through wiki sign-up"

    couldn't agree more - thanks a million!
     
    REVWD likes this.
  10. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    I slightly modified the script above of @ddf so that you can color the type. (Hopefully I did this in an OK way!)

    Code (CSharp):
    1. // http://forum.unity3d.com/threads/3d-text-that-takes-the-depth-buffer-into-account.9931/
    2. // slightly modified so that it has a color parameter,
    3. // start with white sprites and you can color them
    4. // if having trouble making font sprite sets http://answers.unity3d.com/answers/1105527/view.html
    5.  
    6. Shader "GUI/Color3DText"
    7. {
    8.     Properties
    9.     {
    10.         _MainTex ("Font Texture", 2D) = "white" {}
    11.        
    12.         _Colorize ("Colorize", Color) = (1,1,1,1)
    13.     }
    14.     SubShader
    15.     {
    16.         Tags { "RenderType"="Transparent" "Queue"="Transparent"}
    17.    
    18.         Pass
    19.         {
    20.             Blend SrcAlpha OneMinusSrcAlpha
    21.             ZWrite Off
    22.        
    23.             CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #include "UnityCG.cginc"
    27.             sampler2D _MainTex;
    28.            
    29.             fixed4 _Colorize;
    30.            
    31.             struct v2f {
    32.                 float4 pos : SV_POSITION;
    33.                 fixed4 color : COLOR;
    34.                 float2 uv : TEXCOORD0;
    35.             };
    36.        
    37.             struct appdata {
    38.                 float4 vertex : POSITION;
    39.                 fixed4 color : COLOR;
    40.                 float2 texcoord : TEXCOORD0;
    41.             };
    42.             float4 _MainTex_ST;
    43.        
    44.             v2f vert (appdata v)
    45.             {
    46.                 v2f o;
    47.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    48.                 o.color = v.color;
    49.                 o.uv    = TRANSFORM_TEX (v.texcoord, _MainTex);
    50.                 return o;
    51.             }
    52.        
    53.             fixed4 frag (v2f o) : COLOR
    54.             {
    55.                 // this gives us text or not based on alpha, apparently
    56.                 o.color.a  *= tex2D( _MainTex, o.uv ).a;
    57.                
    58.                 o.color *= _Colorize;
    59.                
    60.                 return o.color;
    61.             }
    62.             ENDCG
    63.         }
    64.     }
    65. }
    66.  
    67.  
     
    REVWD likes this.
  11. M0rrigan

    M0rrigan

    Joined:
    May 29, 2015
    Posts:
    29
    Hello, thanks for your shader it saved my day. What if I would need the Cull Back? I've tried to add that, but it doesn't work. Am I missing something?
     
  12. jhave_scm

    jhave_scm

    Joined:
    Apr 22, 2017
    Posts:
    1
    Howdy All,

    Same experience. Shader fixes occlusion.
    Funny: this was same solution i used 5 years ago when i tried used text in Unity then.
    Returning to Unity now, I assumed it would be fixed.

    Are there any Unity staff listening to this thread: why is this not already part of basic functionality?

    respects,
    Jhav
     
    Ruslank100, halley and Hamsword like this.
  13. essell

    essell

    Joined:
    Jul 5, 2013
    Posts:
    9
    "Are there any Unity staff listening to this thread: why is this not already part of basic functionality?"

    +1 This is bizarre to me too
     
    halley likes this.
  14. essell

    essell

    Joined:
    Jul 5, 2013
    Posts:
    9
    And now the website that everyone's linking to with the shader is down :/
     
  15. GTRuiz

    GTRuiz

    Joined:
    Jun 10, 2015
    Posts:
    3
    Here is the solution updated to Unity 2017.1.0f3

    Code (CSharp):
    1. Shader "GUI/Depth Text Shader" {
    2.     Properties {
    3.         _MainTex ("Font Texture", 2D) = "white" {}
    4.         _Color ("Text Color", Color) = (1,1,1,1)
    5.     }
    6.  
    7.     SubShader {
    8.  
    9.         Tags {
    10.             "Queue"="Transparent"
    11.             "IgnoreProjector"="True"
    12.             "RenderType"="Transparent"
    13.             "PreviewType"="Plane"
    14.         }
    15.         Lighting Off Cull Off ZTest LEqual ZWrite Off
    16.         Blend SrcAlpha OneMinusSrcAlpha
    17.  
    18.         Pass {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata_t {
    26.                 float4 vertex : POSITION;
    27.                 fixed4 color : COLOR;
    28.                 float2 texcoord : TEXCOORD0;
    29.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    30.             };
    31.  
    32.             struct v2f {
    33.                 float4 vertex : SV_POSITION;
    34.                 fixed4 color : COLOR;
    35.                 float2 texcoord : TEXCOORD0;
    36.                 UNITY_VERTEX_OUTPUT_STEREO
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             uniform float4 _MainTex_ST;
    41.             uniform fixed4 _Color;
    42.  
    43.             v2f vert (appdata_t v)
    44.             {
    45.                 v2f o;
    46.                 UNITY_SETUP_INSTANCE_ID(v);
    47.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);
    49.                 o.color = v.color * _Color;
    50.                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    51.                 return o;
    52.             }
    53.  
    54.             fixed4 frag (v2f i) : SV_Target
    55.             {
    56.                 fixed4 col = i.color;
    57.                 col.a *= tex2D(_MainTex, i.texcoord).a;
    58.                 return col;
    59.             }
    60.             ENDCG
    61.         }
    62.     }
    63. }
     
    Dmitry_Shesterkin likes this.
  16. essell

    essell

    Joined:
    Jul 5, 2013
    Posts:
    9
    Thanks :) But errr I'm not sure how to use it!

    I tried creating a shader to paste this into but wasn't sure which one?
    And then what do I do with it afterwards, etc?

    which shader.jpg

    Thanks in advance.
     
  17. GTRuiz

    GTRuiz

    Joined:
    Jun 10, 2015
    Posts:
    3
    Standard Surface Shader is the right choice, after that copy paste my code and you will have all ready to use in your materials. You will see this new shader on GUI/Depth Text Shader.

    You have to assign font texture to this shader in material editor.
    Sin título.png
     
    Last edited: Aug 3, 2017
    IggySLO likes this.
  18. IggySLO

    IggySLO

    Joined:
    Apr 13, 2017
    Posts:
    1
    Hey GTRuiz, I also want to make this work, but I'm really not getting this.

    So I created a new Standard Surface Shader and I copied that code into it. I hope that's correct ... I also imported a font (containing a font material and a font texture). I don't get the next step though, the one where you tell us to assign a font texture to the shader in material editor. Can you be just a bit more specific with this part?

    Edit: I think I figured it out.
     
    Last edited: Aug 4, 2017
  19. essell

    essell

    Joined:
    Jul 5, 2013
    Posts:
    9
    Yeah I also don't understand what to do next. What did you do, iggy?
     
  20. GTRuiz

    GTRuiz

    Joined:
    Jun 10, 2015
    Posts:
    3
    Sorry guys... I thought it was clear. You have to do this steps.

    1º - Create Standard Surface Shader and copy my code.
    2º - Import your favorite Font.
    3º - Create a new Material.
    4º - In this new Material select GUI/Depth Text Shader.
    5º - Assign Font texture to texture slot in this Material.
    6º - Create a 3D Text on your scene.
    7º - Assign your favourite Font to this 3D Text.
    8º - Assign in Mesh Renderer component your configured Material.
    9º - Enjoy your 3D Text working fine!! :D

    Everything is tested and it is working for me, good luck!
     
    Last edited: Aug 7, 2017
  21. essell

    essell

    Joined:
    Jul 5, 2013
    Posts:
    9
    Awesome, got it working - thank you :)
     
  22. PNUMIA-Rob

    PNUMIA-Rob

    Joined:
    Jan 7, 2015
    Posts:
    33
    @GTRuiz -- Absolutely outstanding! :cool: Thank you very much! :D
     
  23. jerome187

    jerome187

    Joined:
    Apr 16, 2015
    Posts:
    5
    step #5 I do not seem to be able to do, i can open the font and there is font material and font texture in there but they are uneditable
     
  24. Steve_L

    Steve_L

    Joined:
    Jan 13, 2017
    Posts:
    1
    Same here :(
    Did you find a solution?
     
  25. CyaBB

    CyaBB

    Joined:
    Aug 3, 2017
    Posts:
    1
    Thanks mate!


    Follow http://wiki.unity3d.com/index.php?title=3DText these instructions with the given code of https://forum.unity.com/threads/3d-text-that-takes-the-depth-buffer-into-account.9931/#post-3168884
     
  26. Ryebread04

    Ryebread04

    Joined:
    Apr 6, 2016
    Posts:
    4
    I did every step multiple times and all it did was make the text Solid purple with no actual writing
     

    Attached Files:

    Last edited: Apr 13, 2018
  27. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Do have any errors in the console?
    Doe you have the material assigned to the Mesh Render ?
     
  28. Ryebread04

    Ryebread04

    Joined:
    Apr 6, 2016
    Posts:
    4
    I Just redid the steps to check again and realized that I am getting errors for the shader code
    Shader error in 'GUI/Depth Text Shader': unrecognized identifier 'UNITY_VERTEX_INPUT_INSTANCE_ID' at line 29 (on d2d11)
    And if i comment this line out if gives me another error for a different like saying almost the same thing with a different identifier
     
  29. Ryebread04

    Ryebread04

    Joined:
    Apr 6, 2016
    Posts:
    4
    Got Mine To Work. Had To Use The Code From http://wiki.unity3d.com/index.php?title=3DText Instead Of The Code From Above. When I add my custom script to have the name follow my camera it turned the text invis. but when i remove the fix for the 3d text my script works fine. Help?

    Edit: I found out why its invis. It has something on the back of the text to where It hides it because I can see the text on the opposite side in the scene view when i run around it. Still dont know how to fix it tho..

    Final Edit: Fixed By Changing the x and z to -1 instead of +1
     
    Last edited: Apr 13, 2018
  30. Deleted User

    Deleted User

    Guest

    Thanks man, Off all Info and tutorials, this one did it.

     
  31. Golkar_Asghar

    Golkar_Asghar

    Joined:
    Jun 17, 2015
    Posts:
    10
    it's about 5 years old. but thank you for the solution.
     
  32. gameslegend903

    gameslegend903

    Joined:
    Mar 12, 2021
    Posts:
    3
    you guys are awesome
     
Thread Status:
Not open for further replies.