- Home /
Give textmesh pro text a billboard effect in shader (Always lookAt camera)
Hey,
I'm working project where I need a floating text effect. But my target hardware is extremely limited and I cant afford to rotate the texts through script to lookAt the camera always. I tried to alter the TextMeshPro/Mobile/Distance Field shader to get that effect and got close. But its not perfect. There is always offset in both axes and rotation pivot seems to be elsewhere. So the text leaves its rect when I look at it from different angles. This is the code I added in the shader right before vertex shader output is added.
vPosition = mul(UNITY_MATRIX_P, mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0)) - float4(vert.x, vert.y , vert.z, 0.0) * float4(-unity_ObjectToWorld[0].x , -unity_ObjectToWorld[1].y , unity_ObjectToWorld[1].z, 0.0));
I need help. Any guidance would be very much appreciated.
how is rotating a Ui element or the whole canvas straining performance?
Answer by clabe45 · Feb 23, 2019 at 10:37 PM
Match the text's rotation with that of the camera
public class BillboardText : MonoBehaviour {
void Update() {
transform.rotation = Camera.main.transform.rotation;
}
}
Answer by outoftunegames · Dec 14, 2020 at 07:11 PM
Almar, has this shader been updated to work with URP?
It would be great to get it working in our current game, we've reverted to aligning the transform, but the your solution worked great before we updated to URP! :)
Answer by cspeer504 · Jul 10, 2018 at 11:48 PM
This isn't a shader-based solution, but just in case this might help you:
If you parent the text's transform to the camera's transform, you only have to call lookAt once, in the start function. For example, here is a monoBehavior you can use and attach to your TextMeshPro object:
using UnityEngine;
public class BillboardText : MonoBehaviour
{
void Start ()
{
transform.SetParent(Camera.main.transform);
transform.LookAt(Camera.main.transform);
transform.localEulerAngles = new Vector3(180f, 180f, 180f);
}
}
I have to rotate the text afterwards because it shows completely flip side for me. So I left that in in case it helps you.
Answer by Almar · Jul 05, 2019 at 12:16 PM
Sorry for necro'ing this question, but it might help people:
I just put a shader gist online for this. The important part is to disable the dynamic batching!
See: https://gist.github.com/Spongert/b52a24aa110933a918cf47c777fea1c8
Your answer
Follow this Question
Related Questions
Problem with unity tree billboarding 0 Answers
Need help with billboard shader 0 Answers