Skip to content

Instantly share code, notes, and snippets.

@mattvr
Created June 29, 2016 04:29
Show Gist options
  • Save mattvr/8cdcc922d1a75d0a7a7abf5d46e23ef0 to your computer and use it in GitHub Desktop.
Save mattvr/8cdcc922d1a75d0a7a7abf5d46e23ef0 to your computer and use it in GitHub Desktop.
Curved plane for Unity
/*
MIT License
Copyright (c) 2016 Matt Favero
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class CurvedPlane : MonoBehaviour
{
private class MeshData
{
public Vector3[] Vertices { get; set; }
public int[] Triangles { get; set; }
}
[SerializeField] private float height = 1f;
[SerializeField] private float radius = 2f;
[SerializeField] [Range(1, 1024)] private int numSegments = 16;
[SerializeField] [Range(0f, 360f)] private float curvatureDegrees = 60f;
[SerializeField] private bool useArc = true;
private MeshData plane;
void Start()
{
Generate();
}
void OnValidate ()
{
Generate();
}
[ContextMenu("Generate")]
private void Generate()
{
GenerateScreen();
UpdateMeshFilter();
}
private void UpdateMeshFilter()
{
var filter = GetComponent<MeshFilter>();
var mesh = new Mesh
{
vertices = plane.Vertices,
triangles = plane.Triangles
};
filter.mesh = mesh;
}
private void GenerateScreen()
{
plane = new MeshData
{
Vertices = new Vector3[(numSegments + 2)*2],
Triangles = new int[numSegments*6]
};
int i,j;
for (i = j = 0; i < numSegments+1; i++)
{
GenerateVertexPair(ref i, ref j);
if (i < numSegments)
{
GenerateLeftTriangle(ref i, ref j);
GenerateRightTriangle(ref i, ref j);
}
}
}
private void GenerateVertexPair(ref int i, ref int j)
{
float amt = ((float)i) / numSegments;
float arcDegrees = curvatureDegrees * Mathf.Deg2Rad;
float theta = -0.5f + amt;
var x = useArc ? Mathf.Sin(theta * arcDegrees) * radius : (-0.5f * radius) + (amt * radius);
var z = Mathf.Cos(theta * arcDegrees) * radius;
plane.Vertices[i] = new Vector3(x, height / 2f, z);
plane.Vertices[i + numSegments + 1] = new Vector3(x, -height / 2f, z);
}
private void GenerateLeftTriangle(ref int i, ref int j)
{
plane.Triangles[j++] = i;
plane.Triangles[j++] = i + 1;
plane.Triangles[j++] = i + numSegments + 1;
}
private void GenerateRightTriangle(ref int i, ref int j)
{
plane.Triangles[j++] = i + 1;
plane.Triangles[j++] = i + numSegments + 2;
plane.Triangles[j++] = i + numSegments + 1;
}
}
@BenHarksdorf
Copy link

I would suggest adding UVs to it.
You would just need to ad a uv array in the Mesh Data Set class, fill it like this after you set the vertices:

plane.UVs[i] = new Vector2(i / (float)numSegments, 1);
plane.UVs[i + numSegments + 1] = new Vector2(i / (float)numSegments, 0);

and then attach it to the mesh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment