- Home /
Modifying plane to fit view frustrum issue
Hi there, I am currently trying to fit the Unity's default plane (The one with 121 vertices) to fit the frustrum of a camera (Not the main camera). I know I am nearly there, but there is something that bugs me. Whenever I manipulate my mesh through its vertices, at the point, the mesh simply disappears.
The code I am using is this:
using UnityEngine;
using System.Collections;
public class TestMapScript : MonoBehaviour {
public Camera mapCamera;
public GameObject texture;
void Start()
{
MeshFilter textureMesh = texture.GetComponent<MeshFilter>();
Vector3[] arrayOfvertices = textureMesh.mesh.vertices;
for (int i = 0; i < arrayOfvertices.Length; i++)
{
arrayOfvertices[i].x += 5;
arrayOfvertices[i].z += 5;
arrayOfvertices[i].y = arrayOfvertices[i].z;
arrayOfvertices[i].z = 0;
arrayOfvertices[i] = (arrayOfvertices[i] / 10);
arrayOfvertices[i] = mapCamera.ViewportToWorldPoint(arrayOfvertices[i]);
Debug.DrawRay(arrayOfvertices[i], Vector3.up*100, Color.red, 1000f);
}
textureMesh.mesh.Clear();
textureMesh.mesh.vertices = arrayOfvertices;
/*textureMesh.mesh.uv = textureMesh.sharedMesh.uv;
textureMesh.mesh.triangles = textureMesh.sharedMesh.triangles;*/
textureMesh.mesh.RecalculateNormals();
textureMesh.mesh.RecalculateBounds();
}
}
What the script basically do is it takes texture (I know silly name) and fit it's vertices into the view frustrum of the mapCamera. Through some testing I have figured out that the mesh seems to be properly manipulated during these vertice manipulations:
arrayOfvertices[i].x += 5;
arrayOfvertices[i].z += 5;
arrayOfvertices[i].y = arrayOfvertices[i].z;
arrayOfvertices[i].z = 0;
arrayOfvertices[i] = (arrayOfvertices[i] / 10);
But as soon as I utilize a matrix:
arrayOfvertices[i] = mapCamera.ViewportToWorldPoint(arrayOfvertices[i]);
It disappears.
By using the Debug.DrawRay(); I see the vertices are placed correctly on the view frustrum as expected, but its not visible.
Any idea what causes this? What have I forgotten when utilizing the matrix? Any help would be appreciated.
PS. My own personal idea is that after the matrix have been applied to all the vertices, the UV/Normal/Triangles or something have been reordered, but I can't figure out whether this is the actual problem. If this was the case, I would at least expect some sort of distorted plane.
Can you view the plane from the other side? Also, whats the problem with just scaling it?
The plane is not visible on the other side. There is properly the possibility of scaling, but I can't figure out the scaling factors. The Camera frustrum resolution is dynamic and may change depending on user setup, and so I can't figure out the scaling factors for the plane to match the orthogonal size of the camera.
If you are dealing with an Orthographic camera, Camera.orthographicSize is 1/2 of the vertical size the camera is viewing. So you want to size your mesh to have a vertical size twice this value. You get the horizontal size by multiplying vertical size by Screen.width/Screen.height. Do you need the calculations for a perspective camera as well?
(Just FYI) if you can already calculate the vertex positions according to your view, then with a simple bit of maths it's easy to calculate the scaling :)
Your answer
Follow this Question
Related Questions
Mesh's corners are not where it's supposed to be 0 Answers
Flattening a mesh by deforming it 0 Answers
Adding vertices to procedural mesh generator 2 Answers
Mesh is deleting itself when the camera approaches it 2 Answers
Camera frustums + custom field of view 2 Answers