3D TextMesh in Scene

Hello,

I have been playing with 3D Text (via GameObject/Create Other/3D Text) because from what I read in the documentation this could be used within a scene to display text. My issue is that when i attach this text to an object and move the object behind other objects the text is still visible.

My test scene is rather simple and consists of a sphere and a 3D Text object, I move the 3D text object back and forth along the Z axis, but this only has the effect of making the text smaller but still visible even though it is behind the sphere.

The reason I would like to do this is so that I can have a “label” hover above an object as the object moves around the scene, and if the object were to move behind another object (i.e. a building) the text would also move behind the building and not be visible.

Does anyone know how to get the text to be part of the 3D scene so that it is occluded by objects which appear in front of the text?

Thanks

1 Like

3D Text is rendered on top of everything else, yes. I don’t know if there’s a way to partially occlude it, but you can raycast or linecast to the camera and if anything is blocking, it doesn’t display.

Hello,

Thanks for your reply but the documentation states the following (Unity - Manual: Text Mesh component (legacy)):

If the TextMesh can be used for road signs, to me, that indicates that it is occluded by other parts of the world, i.e. a road sign behind a truck/building/hill/etc.

It is true however that 2D GUIText is drawn in front of everything else.

Please correct me if I am wrong and have misunderstood what the documentation for TextMeshes is saying.

Cheers

1 Like

This is because the ‘ZTest’ value in the built-in Text shader is set to ‘Always’. Create a copy of the Text shader in your assets directory, and set the ZTest to LEqual and it should work fine.

I’m not entirely sure why ZTest is set to Always for the 3D text shader, actually I agree that the 3D text (being in 3D space) should be occluded by objects in front of it. Perhaps it is because this same shader is used for both 2D GUI text and 3D text?

Thanks for that skitz.

Will add that to the project now.

Thanks skitz.

Could you please also tell us how to copy the Gui/Text shader to the asset directory.
I don’t know where to find the original asset.

Thanks again

Check out this thread in the Shaderlab section, it has all the Unity built-in shaders:

http://forum.unity3d.com/viewtopic.php?t=2150

Thanks again for everything. It works !!!

To summarize :

The Full code off the shader(unity2.1) is then :

Shader "GUI/Text Shader Z" {
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 Off ZTest Always ZWrite Off Fog { Mode Off }
	Blend SrcAlpha OneMinusSrcAlpha
	Pass {
		ZTest LEqual
		Color [_Color]
		SetTexture [_MainTex] {
			combine primary, texture * primary
		}
	}
}
}

so good code!!

is this just supposed to work once you have a copy of the font.shader in your assets folder? I have followed these instructions but it doesn’t seem to make any difference to any 3d text I have in my scene.

Is there a way to apply the shader to 3d text as a component?

Thanks

I found this post very useful, thanks!

Just add the shader to a new material that you add as a component in turn to your text mesh.

I just encountered this problem myself, and found this thread. Thank you so much, also for summarizing the steps at the end! This was really very helpful.

Hello, I have the same problem and the described fix seems to work - almost:
As soon as I attach the new material to the TextMesh it is shown behind the other objects as expected, unfortunately it’s also not readable anymore. Just big white blocks… What am I doing wrong?

Hello and welcome, ExitBG.

Could this be an issue with the font? Are you just using the default font, or one of your own?

Hello andeeee, thanks for the response.
I created a prefab and added a default “3D Text”.
I instantiate it with a script: Instantiate(Resources.Load("MyText"));
If I change the font or the renderer material of the prefab the text becomes unreadable, just the default settings work.

When you change the Material (the font texture) used by the TextMesh make sure to change the Font (same place you set the string to display) used in the TextMesh aswell. if they are not thesame the text will be unreadable.

So now this is what I do:

  1. Create custom material (assets->create->material)
  2. Assign shader to the material (“ZTest LEqual”)
  3. Create custom font (assets->create->custom Font)
  4. Assign the new material to the new font
  5. Assign the new font to text mesh
  6. Assign the new material to text mesh renderer

This is what happens:
After step 5 there is readable text, but it shows a part of the font alphabet, not the text it should show.
After step 6 this text is not readable anymore, just white blocks.

OK, got it working now. In case others run into the same problem: I had to copy an existing font into the asset directory first. The font will show-up as node with its material and the shader as children. (Somehow this failed when I tried it earlier - or I just failed to see the children)
From then on it’s pretty straight forward:

  1. Assign shader to the font material (“ZTest LEqual”)
  2. Assign font to text mesh
  3. Assign the font material to text mesh renderer

Thanks Jørgen!

In the past I have had a modified text shader attached to my font material, as described above. When I upgraded to 2.6, evidently the fonts were re-imported and now use the standard text shader. The shader is grayed out and I can’t find a way to change it back to my modified shader.

Does anyone else have this problem or know what to do (or try)?.

You should contact support@unity3d.com about this issue.