- Home /
Rotate a mesh on world y axis using a CG vertex shader?
I have a bunch of 3d textMesh objects, each with a single letter on a single quad. What I am trying to do with them is not exactly billboarding but the same idea: Putting the letter facing upward, then rotating it on y so its base is facing the camera, and tilting it up 10º on its local x axis to angle toward the camera a little more.
This works fine with a script that goes through every letter object every frame and says transform.LookAt (a world point below the main camera) but I know it is possible to do the same thing inside the shader and it would be much faster.
I'm comfortable enough with CG shaders now to do most of the color stuff I want to do, but I don't understand all the model view projection matrix arcana and how they can be used to move verts around.
I found this example of rotating vertices around each axis with sine time, in a surface shader, but I still can't figure out from there how to achieve something like "Transform.LookAt" in CG.
Answer by Owen-Reynolds · Oct 18, 2013 at 12:09 AM
Yes, the modelView matrix is tricky to understand. Very, very roughly (I have done similar with non-Unity shaders, but it's been a while):
Grab the 3DText shader (Font.shader.) We know that shows 3DText, and luckily, it has a vertex shader in it. Switch the material for your textMeshes to this. Change the vert shader to apply only the world position, but not the rotation. That will put the object in the right spot, but leave it in it's "natural" rotation, which is probably facing the camera. Then look up "rotation matrix" (should be 3x3) and set up one for the 10 degree tilt. Arg, then apply the perspective matrix (all in the vert shader.) Look up how to apply a matrix to a vert, and the order, and how to chain them (it uses the MUL command, same as the original shader.)
I don't think there's a position-only matrix in Unity, but using the rightmost column of WorldView will give that (look up Translation matrix. It explains what the important numbers do and how to use them.) It may be the "natural" facing of a TextMesh is upwards, meaning you'll see the edge, which is nothing. In that case, experiment with applying various rotation matrixes (maybe -90 on x?) to angle it forward. You may not be able to check in Scene View (the shader may keep twisting it away from you) so have to trial and error.
Of course, the optimization rule applies: if LookAt is fast enough, just leave it. Unless you really, really want a mini-project to learn shader matrixes.
Thanks- I decided that rotating all the billboards in a script was fast enough, and added a check to only rotate the ones whose renderers are visible. I was disappointed by how my game was chugging on a tablet PC and so went on a re-writing-everything spree. I hoped at first I could learn how matrix transforms work but no, reading about linear algebra still gives me a headache and it's just not worth it at this time.
Your answer
Follow this Question
Related Questions
Opengl es 2.0 Shader Vertex Rotation Issue 1 Answer
Quaternion Multiplication in a shader. 1 Answer
Rotate a vertex about "u" Axis 1 Answer
Is the trail emitted by the Trail Renderer "billboard"? 1 Answer
Billboard grass rotate weird 1 Answer