Skip to content

Instantly share code, notes, and snippets.

@gutoplusgamedev
Last active December 22, 2015 21:23
Show Gist options
  • Save gutoplusgamedev/74a447a4a5a01746bd6f to your computer and use it in GitHub Desktop.
Save gutoplusgamedev/74a447a4a5a01746bd6f to your computer and use it in GitHub Desktop.
Mesh GenerateSightMesh (int iterations)
{
Mesh m = new Mesh ();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Color> colours = new List<Color>();
Vector3 startingPoint = transform.position;
vertices.Add (startingPoint);
float angleStep = apertureAngle / iterations;
RaycastHit hit;
for (int i = 0; i <= iterations + 1; i++)
{
float angle = (-apertureAngle * 0.5f) + (i * angleStep);
Vector3 sightVector = Quaternion.AngleAxis (angle, transform.up) * transform.forward;
if (Physics.Raycast (startingPoint, sightVector, out hit, maxSightDistance))
{
vertices.Add (startingPoint + sightVector * hit.distance);
}
else
{
vertices.Add (startingPoint + sightVector * maxSightDistance);
}
if (i >= 2)
{
triangles.Add (0);
triangles.Add (i - 1);
triangles.Add (i);
}
}
m.vertices = vertices.ToArray();
m.triangles = triangles.ToArray();
return m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment