- Home /
Question by
adnan-zaman-13 · Jun 12, 2017 at 09:07 PM ·
cameragameobjectmeshverticesquad
Mesh's corners are not where it's supposed to be
My goal was to make a Quad that would take up the entire screen, regardless of where the camera moved, or zoomed in/zoomed out. I changed the corners of the Quad's mesh to be the corners of the visible game area...but the top right and left corners aren't where they're supposed to be.
I even tried spawning a test game object in the top-right (the same top-right coordinate that the Quad uses) and it actually did show up in the corner, unlike the Quad.
Any ideas what's going on?
Ray topLeft,topRight,botLeft,botRight; //Rays shot out from camera to figure out the 4 corners for viewRect
float distance; //Used to find out how far camera is from xy plane
Vector3 tl,tr,bl,br; //Coordinates of viewRect
GameObject viewRect; //The rect that will represent the viewable part of the game
Plane xy; // The xy plane
Mesh mesh;
Vector3[] vertices = new Vector3[4];
int[] tri = { 0, 3, 1, 3, 2, 1 };
void Start ()
{
mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = tri;
Vector3 pos = Camera.main.transform.position;
pos.z = 0;
viewRect = GameObject.CreatePrimitive(PrimitiveType.Quad);
viewRect.transform.position = pos;
viewRect.tag = "CameraView";
viewRect.GetComponent<MeshFilter>().mesh = mesh;
xy = new Plane(new Vector3(0,0,-1), Vector3.zero);
CurrentView();
}
void LateUpdate ()
{
CurrentView();
}
//Calculates what is visible by the camera
void CurrentView()
{
//Shoots rays out of each corner of the screen
topLeft = Camera.main.ScreenPointToRay(new Vector3(0,Screen.height,0));
topRight = Camera.main.ScreenPointToRay(new Vector3(Screen.width,Screen.height,0));
botLeft = Camera.main.ScreenPointToRay(new Vector3(0,0,0));
botRight = Camera.main.ScreenPointToRay(new Vector3(Screen.width,0,0));
//Calculates topleft coordinate
xy.Raycast(topLeft, out distance);
tl = topLeft.GetPoint(distance);
//Calculates topright coordinate
xy.Raycast(topRight, out distance);
tr = topRight.GetPoint(distance);
//Calculates bottomleft coordinate
xy.Raycast(botLeft, out distance);
bl = botLeft.GetPoint(distance);
//Calculates bottomright coordinate
xy.Raycast(botRight, out distance);
br = botRight.GetPoint(distance);
vertices[0] = bl; vertices[1] = br; vertices[2] = tr; vertices[3] = tl;
tri[0] = 0; tri[1] = 2; tri[2] = 1; tri[3] = 0; tri[4] = 3; tri[5] = 2;
mesh.vertices = vertices;
mesh.triangles = tri;
viewRect.GetComponent<MeshFilter>().mesh = mesh;
GameObject test = GameObject.CreatePrimitive(PrimitiveType.Quad);
test.transform.position = tr;
}
Screenshot of what I was talking about with the test game object.
question.jpg
(35.0 kB)
Comment