- Home /
Drawing a screen space rectangle with Graphics.DrawMeshNow
Hi,
I want to draw a screen space rect based on a unity rect. Currently I do this :
Vector3[] vertices = new Vector3[4]; vertices[0] = new Vector3(testPos.xMin, testPos.yMin, Camera.current.nearClipPlane); vertices[1] = new Vector3(testPos.xMin, testPos.yMax, Camera.current.nearClipPlane); vertices[2] = new Vector3(testPos.xMax, testPos.yMax, Camera.current.nearClipPlane); vertices[3] = new Vector3(testPos.xMax, testPos.yMin, Camera.current.nearClipPlane); for (int i = 0; i < 4; i++) { vertices[i] = Camera.current.ViewportToWorldPoint(vertices[i]); } mesh.vertices = vertices;
testMaterial.SetPass(0);
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
This works as expected. However, I would prefer not to set the vertices each time, but rather set the world matrix of the object differently each frame. That way I could create the mesh once and use it each time. I don't understand exactly how to work it out. Can someone lend a hand?
Thanks!
Answer by cjmarsh · Jan 06, 2011 at 11:37 AM
If your goal is to simply render the mesh in the same scale in front of the camera, I don't think you need the matrix at all. You should be able to draw the mesh using only this:
Graphics.DrawMeshNow(mesh, Camera.current.transform.position, Camera.current.transform.rotation);
If your mesh is initially positioned on the camera's nearClipPlane the way that you want it then it should work as intended. I believe this works because the scale is the extra dimension that the matrix is capable of handling but the constant z-value of the camera's plane negates the need for it. Let me know if my math is rusty or if it doesn't work the way you had in mind.
Your answer
Follow this Question
Related Questions
Where to call Graphics.DrawMesh to successfully draw during editor pause? 0 Answers
Changing draw order of meshes in Graphics.DrawMesh() 1 Answer
Will Camera.RenderWithShader work with Graphics.DrawMesh (and DrawMeshInstanced)? 0 Answers
How to attach a mesh created with Graphics.DrawMesh to a GameObject? 1 Answer
How to access full Shuriken simulation mesh for use with DrawMeshNow? 0 Answers