Skip to content

Instantly share code, notes, and snippets.

@FVSHaLuan
Last active August 26, 2015 02:52
Show Gist options
  • Save FVSHaLuan/4c6c8010f01ec5ae10d6 to your computer and use it in GitHub Desktop.
Save FVSHaLuan/4c6c8010f01ec5ae10d6 to your computer and use it in GitHub Desktop.
** Function: create mesh data which describes a plane with a hole (rectangle) inside.
using UnityEngine;
using System.Collections;
/* by FVS - HaLuan */
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class TutorialFogController : MonoBehaviour
{
public void SetTutorialFog(Vector2 overallSize, Vector2 spotlightPosition, Vector2 spotlightSize)
{
GetComponent<MeshFilter>().mesh = GetMesh(overallSize, spotlightPosition, spotlightSize);
}
Mesh GetMesh(Vector2 overallSize, Vector2 spotlightPosition, Vector2 spotlightSize)
{
Mesh mesh = new Mesh();
#region Size
float haftX1 = overallSize.x / 2.0f;
float haftY1 = overallSize.y / 2.0f;
float haftX2 = spotlightSize.x / 2.0f;
float haftY2 = spotlightSize.y / 2.0f;
#endregion
#region Vertices
Vector3 p0 = new Vector2(-haftX1, -haftY1);
Vector3 p1 = new Vector2(haftX1, -haftY1);
Vector3 p2 = new Vector2(haftX1, haftY1);
Vector3 p3 = new Vector2(-haftX1, haftY1);
Vector3 q0 = new Vector2(-haftX1, -haftY2 + spotlightPosition.y);
Vector3 q1 = new Vector2(haftX1, -haftY2 + spotlightPosition.y);
Vector3 q2 = new Vector2(haftX1, haftY2 + spotlightPosition.y);
Vector3 q3 = new Vector2(-haftX1, haftY2 + spotlightPosition.y);
Vector3 r0 = new Vector2(-haftX2, -haftY2) + spotlightPosition;
Vector3 r1 = new Vector2(haftX2, -haftY2) + spotlightPosition;
Vector3 r2 = new Vector2(haftX2, haftY2) + spotlightPosition;
Vector3 r3 = new Vector2(-haftX2, haftY2) + spotlightPosition;
mesh.vertices = new Vector3[]
{
p0,p1,p2,p3,
q0,q1,q2,q3,
r0,r1,r2,r3,
};
#endregion
#region Triangles
mesh.triangles = new int[]
{
// Top
3,2,7,
2,6,7,
// Bottom
4,1,0,
5,1,4,
// Left
11,8,4,
7,11,4,
// Right
6,5,9,
10,6,9
};
#endregion
///
return mesh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment