Procedural Mesh Problems
I am honestly stumped, there is clearly some stupid easy problem with my code that I just can't see because iv'e been trying to fix it for ages now. What my code should do;
Raycast from each corner of the viewport and find the position that ray collides with the terrain (working)
Send an array of those points to another script (working)
Use those points to as the vertices of a mesh that shows what the camera can view (not working)
So the vertices are in the right place, I have a draw gizmos lines that shows a square that is exactly what the player can view but yet the mesh doesn't work, it shows up as a stretched triangle is the camera goes out of bounds and one corner of the camera has nothing to collide with but otherwise it isn't drawn. If I put in some dummy vertices it gets drawn fine. Here are the code snippets, please help me; 1.
private Vector3 TLray ()
{
Ray TLCam = cam.ViewportPointToRay (new Vector3(0, 1, 0));
RaycastHit TLhit;
if (Physics.Raycast (TLCam, out TLhit))
{
//TLHitPos = new Vector3 (TLhit.point.x, 1, TLHitPos.y);
Vector3 TLHitPos = new Vector3 (TLhit.point.x, TLhit.point.y, TLhit.point.z);
Debug.Log ("TL" + TLHitPos.ToString ());
return(TLHitPos);
}
else
return(Vector3.one);
}
(For each corner) 2.
Vector3 [] corners = new Vector3[4]{TLray(),TRray(),BLray(),BRray()};
Minimap.SetRectSize (corners);
3.
public GameObject ViewGO;
private MeshFilter ViewMf;
private Mesh ViewMesh;
public Material mat;
private int[] tris = new int[6]{0,2,3,3,1,0};
private Vector2[] uv = new Vector2[4]{ new Vector2 (0, 0), new Vector2 (0, 1), new Vector2 (1, 1), new Vector2 (1, 0) };
void Start ()
{
ViewMf = ViewGO.AddComponent (typeof(MeshFilter)) as MeshFilter;
MeshRenderer rend = ViewGO.AddComponent (typeof(MeshRenderer)) as MeshRenderer;
ViewMesh = new Mesh ();
ViewMesh.vertices = verts;
ViewMf.mesh = ViewMesh;
rend.material = mat;
ViewMesh.uv = uv;
ViewMesh.triangles = tris;
ViewMesh.RecalculateBounds ();
ViewMesh.RecalculateNormals();
}
public void SetRectSize (Vector3 [] verts )
{
Verts = verts;
ViewMesh = ViewMf.sharedMesh;
ViewMesh.Clear();
ViewMesh.vertices = verts;
ViewMf.mesh = ViewMesh;
ViewMesh.uv = uv;
ViewMesh.triangles = tris;
ViewMesh.RecalculateBounds ();
ViewMesh.RecalculateNormals();
}
These are just the relevant snippets. Please help, is it my tris order or something else that shows my naivety?
The gizmo lines being drawn;
One tri shows up if the camera doesn't have anything to collide with on one corner or edge; And the out of bounds vertices become vector3.one. Does this mean my verts are incorrect or the tri order is incorrect maybe?
This works fine;
void OnDrawGizmos ()
{
Gizmos.color = Color.red;
Gizmos.DrawLine (Verts [0], Verts [1]);
Gizmos.DrawLine (Verts [0], Verts [2]);
Gizmos.DrawLine (Verts [1], Verts [3]);
Gizmos.DrawLine (Verts [2], Verts [3]);
}
Your answer
Follow this Question
Related Questions
How is a Mesh Built? 1 Answer
How can I identify the location of mesh triangles at run time? (Procedural Generation) 1 Answer
Some triangles are black on a mesh generated by script 1 Answer
Creating triangles around a mesh using an array of vertices 1 Answer
Getting face of mesh when you already have one tri 0 Answers