I am trying to generate a mesh that i dependent on a box’s orientation accordingly to an orographic camera. I seem to be able to correctly point out four corners of the mesh that i want to use (top, bottom, right and left) and can correctly draw a lines using Debug.DrawLine that shows that i get the desired positions i want my mesh to be drawn to, but everything does not seem to work when i create an actual mesh.
The red lines are the drawn rays that starts and ends at the correct points, while the green is a simple polygon i drew from two points to another. The green is clearly incorrect and rotates with the mesh instead of sticking to the points that the red lines do.
If anyone could explain to me why these points are incorrect when drawing i would appreciate it a lot.
The following code is very crude and needs to be optimized, but here it is:
using UnityEngine;
using System.Collections;
public class DynamicCollider : MonoBehaviour
{
private GameObject camera;
private MeshCollider meshCollider;
public int[] tris;
private Vector3[] vertices;
void Start()
{
meshCollider = this.gameObject.AddComponent<MeshCollider>();
}
void Update()
{
camera = GameObject.FindGameObjectWithTag("MainCamera");
Mesh m = makeMesh(this.gameObject);
meshCollider.sharedMesh = m;
}
Mesh makeMesh (GameObject theObject)
{
vertices = new Vector3[8];
Matrix4x4 thisMatrix = theObject.transform.localToWorldMatrix;
Quaternion storedRotation = theObject.transform.rotation;
theObject.transform.rotation = Quaternion.identity;
Vector3 extents = theObject.collider.bounds.extents;
Vector3 objPos = this.transform.position;
Vector3 camPos = camera.transform.position - objPos;
vertices[0] = thisMatrix.MultiplyPoint3x4(extents);
vertices[1] = thisMatrix.MultiplyPoint3x4(new Vector3(-extents.x, extents.y, extents.z));
vertices[2] = thisMatrix.MultiplyPoint3x4(new Vector3(extents.x, extents.y, -extents.z));
vertices[3] = thisMatrix.MultiplyPoint3x4(new Vector3(-extents.x, extents.y, -extents.z));
vertices[4] = thisMatrix.MultiplyPoint3x4(new Vector3(extents.x, -extents.y, extents.z));
vertices[5] = thisMatrix.MultiplyPoint3x4(new Vector3(-extents.x, -extents.y, extents.z));
vertices[6] = thisMatrix.MultiplyPoint3x4(new Vector3(extents.x, -extents.y, -extents.z));
vertices[7] = thisMatrix.MultiplyPoint3x4(-extents);
theObject.transform.rotation = storedRotation;
Vector3[] sortedVectors = new Vector3[8];
sortedVectors[0] = camera.camera.WorldToViewportPoint(vertices[0]);
sortedVectors[1] = camera.camera.WorldToViewportPoint(vertices[1]);
sortedVectors[2] = camera.camera.WorldToViewportPoint(vertices[2]);
sortedVectors[3] = camera.camera.WorldToViewportPoint(vertices[3]);
sortedVectors[4] = camera.camera.WorldToViewportPoint(vertices[4]);
sortedVectors[5] = camera.camera.WorldToViewportPoint(vertices[5]);
sortedVectors[6] = camera.camera.WorldToViewportPoint(vertices[6]);
sortedVectors[7] = camera.camera.WorldToViewportPoint(vertices[7]);
Vector3[] finalVectors = new Vector3[4];
int end = sortedVectors.Length;
Vector3 leftValue = new Vector3(1,0,0);
Vector3 rightValue = new Vector3(0,0,0);
Vector3 topValue = new Vector3(0,1,0);
Vector3 bottomValue = new Vector3(0,0,0);
int leftPoint = 0;
int rightPoint = 0;
int topPoint = 0;
int bottomPoint = 0;
for(int i = 0; i < end; i++)
{
// Find the topmost point
if (topValue.y>sortedVectors[i].y)
{
topValue=sortedVectors[i];
finalVectors[2] = vertices[i];
topPoint = i;
}
// Find the bottommost point
if (bottomValue.y<sortedVectors[i].y)
{
bottomValue=sortedVectors[i];
finalVectors[3] = vertices[i];
bottomPoint = i;
}
// Find the leftmost point
if (leftValue.x>sortedVectors[i].x)
{
leftValue=sortedVectors[i];
finalVectors[0] = vertices[i];
leftPoint = i;
}
// Find the rightmost point
if (rightValue.x<sortedVectors[i].x)
{
rightValue=sortedVectors[i];
finalVectors[1] = vertices[i];
rightPoint = i;
}
}
// Final array of Vectors
Vector3[] drawingVectors = new Vector3[8];
for(int i = 0; i < 4 ; i++)
{
int baseCount = i * 2;
Vector3 orgPos = finalVectors[i];
drawingVectors[baseCount] = new Vector3(orgPos.x, orgPos.y, orgPos.z - 100);
drawingVectors[baseCount + 1] = new Vector3(orgPos.x, orgPos.y, orgPos.z + 100);
Debug.DrawLine(drawingVectors[baseCount], drawingVectors[baseCount + 1], Color.red);
}
Mesh m = new Mesh();
m.name = "DynamicCollider";
m.vertices = drawingVectors;
m.triangles = tris;
return m;
}
void OnGUI ()
{
GUI.color = new Color(0,0,0,1);
int end = vertices.Length;
for(int i = 0; i < end; i++)
{
Vector3 local = camera.camera.WorldToScreenPoint(vertices[i]);
GUI.Label(new Rect(local.x,Screen.height - local.y,30,30), i.ToString());
}
}
}
