Created
December 10, 2016 01:46
-
-
Save jiulongw/028cb27c777572e5a54cd5bc671ed38e to your computer and use it in GitHub Desktop.
Create mesh sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void CreateMesh() | |
{ | |
var start = Quaternion.AngleAxis(-45, Vector3.up) * Vector3.forward * 6f; | |
var end = Quaternion.AngleAxis(45, Vector3.up) * Vector3.forward * 6f; | |
var control = Vector3.forward * 6f; | |
var points1 = GetPointsInCurve(start, end, control, 20); | |
start = Quaternion.AngleAxis(15, Vector3.right) * start; | |
end = Quaternion.AngleAxis(15, Vector3.right) * end; | |
control = Quaternion.AngleAxis(15, Vector3.right) * control; | |
var points2 = GetPointsInCurve(start, end, control, 20); | |
var verts = new Vector3[40]; | |
Array.Copy(points1, 0, verts, 0, 20); | |
Array.Copy(points2, 0, verts, 20, 20); | |
int[] indices = new int[6 * 20]; | |
for (int i = 0; i < 20 - 1; i++) | |
{ | |
var p1 = i; | |
var p2 = i + 1; | |
var p3 = 20 + i; | |
var p4 = 20 + i + 1; | |
indices[6 * i] = p1; | |
indices[6 * i + 1] = p2; | |
indices[6 * i + 2] = p3; | |
indices[6 * i + 3] = p3; | |
indices[6 * i + 4] = p2; | |
indices[6 * i + 5] = p4; | |
} | |
var mesh = new Mesh(); | |
mesh.vertices = verts; | |
mesh.triangles = indices; | |
mesh.RecalculateBounds(); | |
mesh.RecalculateNormals(); | |
mesh.Optimize(); | |
gameObject.GetComponent<MeshFilter>().mesh = mesh; | |
} | |
private Vector3[] GetPointsInCurve(Vector3 start, Vector3 end, Vector3 control, int count) | |
{ | |
var points = new Vector3[count]; | |
for (var i = 0; i < count; i++) | |
{ | |
var t = (float)i / (float)(count - 1); | |
var p = start * (1f - t) * (1f - t) + control * 2f * (1f - t) * t + end * t * t; | |
points[i] = p; | |
} | |
return points; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment