SHOW:
|
|
- or go back to the newest paste.
1 | using UnityEngine; | |
2 | using System.Collections; | |
3 | ||
4 | public class CreateCircleScript: MonoBehaviour { | |
5 | ||
6 | public int edges = 4; | |
7 | public float radius = 5.000000f; | |
8 | private float degresInRadians, pi = 3.14f; | |
9 | Mesh mesh = null; | |
10 | ||
11 | // Use this for initialization | |
12 | void Start () { | |
13 | if(mesh == null) { | |
14 | mesh = gameObject.AddComponent<MeshFilter>().mesh; | |
15 | gameObject.AddComponent<MeshRenderer>(); | |
16 | } | |
17 | mesh = new Mesh(); | |
18 | mesh.name = "Circle"; | |
19 | ||
20 | Vector3[] vertices = new Vector3[edges]; | |
21 | Vector3[] normals = new Vector3[edges]; | |
22 | Vector2[] uvs = new Vector2[edges]; | |
23 | int[] triangles = new int[(edges-2)*3]; | |
24 | ||
25 | degresInRadians = (2*pi)/edges; | |
26 | for(short index = 0; index < edges; ++index) | |
27 | { | |
28 | float angleInRadians = degresInRadians * index; | |
29 | vertices[index] = new Vector3(radius * Mathf.Cos(angleInRadians), radius * Mathf.Sin(angleInRadians), 0); | |
30 | } | |
31 | ||
32 | for(short index = 0; index < edges-2; ++index) | |
33 | { | |
34 | triangles[index*3] = 0; | |
35 | triangles[index*3+1] = index+2; | |
36 | triangles[index*3+2] = index+1; | |
37 | } | |
38 | ||
39 | Vector3 normal,side1,side2; | |
40 | side1 = vertices[1] - vertices[0]; | |
41 | side2 = vertices[edges-1] - vertices[0]; | |
42 | normal = -Vector3.Cross(side1,side2).normalized; | |
43 | ||
44 | for(short index = 0; index < edges; ++index) | |
45 | normals[index] = normal; | |
46 | ||
47 | for(short index = 0; index < edges; ++index) | |
48 | { | |
49 | uvs[index] = new Vector2(vertices[index].x, vertices[index].y); | |
50 | } | |
51 | ||
52 | mesh.vertices = vertices; | |
53 | mesh.triangles = triangles; | |
54 | mesh.normals = normals; | |
55 | mesh.uv = uvs; | |
56 | gameObject.GetComponent<MeshFilter>().mesh = mesh; | |
57 | ||
58 | gameObject.renderer.material.shader = Shader.Find("Diffuse"); | |
59 | } | |
60 | ||
61 | int backEdge = 4; | |
62 | float backRadius = 5.000000f; | |
63 | // Update is called once per frame | |
64 | void Update () { | |
65 | if(edges != backEdge) { | |
66 | if(edges < 3) | |
67 | edges = 3; | |
68 | else { | |
69 | backEdge = edges; | |
70 | GetComponent<MeshFilter>().mesh.Clear(); | |
71 | Start(); | |
72 | } | |
73 | } | |
74 | else if(radius != backRadius) { | |
75 | if(radius < 1.000000f) | |
76 | - | radius = 1; |
76 | + | radius = 1.000000f; |
77 | else { | |
78 | backRadius = radius; | |
79 | setAndGetradiusOfVertices(radius); | |
80 | } | |
81 | } | |
82 | ||
83 | } | |
84 | ||
85 | void setAndGetradiusOfVertices(float radius2) { | |
86 | float angleRadians; | |
87 | degresInRadians = 2*pi/edges; | |
88 | Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh; | |
89 | Vector3[] vertices = mesh.vertices; | |
90 | Vector2[] uv = mesh.uv; | |
91 | for(int index = 0; index < edges; ++index) | |
92 | { | |
93 | angleRadians = degresInRadians * index; | |
94 | vertices[index] = new Vector3(radius2 * Mathf.Cos(angleRadians),radius2 * Mathf.Sin(angleRadians),0); | |
95 | uv[index] = new Vector2(vertices[index].x,vertices[index].y); | |
96 | mesh.vertices = vertices; | |
97 | mesh.uv = uv; | |
98 | } | |
99 | } | |
100 | } |