3 namespace droid.Runtime.Utilities.GameObjects.Plotting {
9 const float height = 1f;
10 const float bottom_radius = .25f;
11 const float top_radius = .05f;
12 const int nb_sides = 18;
13 const int nb_height_seg = 1;
15 const int nb_vertices_cap = nb_sides + 1;
20 var vertices =
new Vector3[nb_vertices_cap + nb_vertices_cap + nb_sides * nb_height_seg * 2 + 2];
22 const float _2_pi = Mathf.PI * 2f;
25 vertices[vert++] =
new Vector3(0f, 0f, 0f);
26 while (vert <= nb_sides) {
27 var rad = (float)vert / nb_sides * _2_pi;
28 vertices[vert] =
new Vector3(Mathf.Cos(rad) * bottom_radius, 0f, Mathf.Sin(rad) * bottom_radius);
33 vertices[vert++] =
new Vector3(0f, height, 0f);
34 while (vert <= nb_sides * 2 + 1) {
35 var rad = (float)(vert - nb_sides - 1) / nb_sides * _2_pi;
36 vertices[vert] =
new Vector3(Mathf.Cos(rad) * top_radius, height, Mathf.Sin(rad) * top_radius);
42 while (vert <= vertices.Length - 4) {
43 var rad = (float)v / nb_sides * _2_pi;
44 vertices[vert] =
new Vector3(Mathf.Cos(rad) * top_radius, height, Mathf.Sin(rad) * top_radius);
45 vertices[vert + 1] =
new Vector3(Mathf.Cos(rad) * bottom_radius, 0, Mathf.Sin(rad) * bottom_radius);
50 vertices[vert] = vertices[nb_sides * 2 + 2];
51 vertices[vert + 1] = vertices[nb_sides * 2 + 3];
58 var normales =
new Vector3[vertices.Length];
62 while (vert <= nb_sides) {
63 normales[vert++] = Vector3.down;
67 while (vert <= nb_sides * 2 + 1) {
68 normales[vert++] = Vector3.up;
73 while (vert <= vertices.Length - 4) {
74 var rad = (float)v / nb_sides * _2_pi;
75 var cos = Mathf.Cos(rad);
76 var sin = Mathf.Sin(rad);
78 normales[vert] =
new Vector3(cos, 0f, sin);
79 normales[vert + 1] = normales[vert];
85 normales[vert] = normales[nb_sides * 2 + 2];
86 normales[vert + 1] = normales[nb_sides * 2 + 3];
92 var uvs =
new Vector2[vertices.Length];
96 uvs[u++] =
new Vector2(0.5f, 0.5f);
97 while (u <= nb_sides) {
98 var rad = (float)u / nb_sides * _2_pi;
99 uvs[u] =
new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
104 uvs[u++] =
new Vector2(0.5f, 0.5f);
105 while (u <= nb_sides * 2 + 1) {
106 var rad = (float)u / nb_sides * _2_pi;
107 uvs[u] =
new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
113 while (u <= uvs.Length - 4) {
114 var t = (float)u_sides / nb_sides;
115 uvs[u] =
new Vector3(t, 1f);
116 uvs[u + 1] =
new Vector3(t, 0f);
121 uvs[u] =
new Vector2(1f, 1f);
122 uvs[u + 1] =
new Vector2(1f, 0f);
128 var nb_triangles = nb_sides + nb_sides + nb_sides * 2;
129 var triangles =
new int[nb_triangles * 3 + 3];
134 while (tri < nb_sides - 1) {
136 triangles[i + 1] = tri + 1;
137 triangles[i + 2] = tri + 2;
143 triangles[i + 1] = tri + 1;
144 triangles[i + 2] = 1;
150 while (tri < nb_sides * 2) {
151 triangles[i] = tri + 2;
152 triangles[i + 1] = tri + 1;
153 triangles[i + 2] = nb_vertices_cap;
158 triangles[i] = nb_vertices_cap + 1;
159 triangles[i + 1] = tri + 1;
160 triangles[i + 2] = nb_vertices_cap;
166 while (tri <= nb_triangles) {
167 triangles[i] = tri + 2;
168 triangles[i + 1] = tri + 1;
169 triangles[i + 2] = tri + 0;
173 triangles[i] = tri + 1;
174 triangles[i + 1] = tri + 2;
175 triangles[i + 2] = tri + 0;
182 mesh.vertices = vertices;
183 mesh.normals = normales;
185 mesh.triangles = triangles;
187 mesh.RecalculateBounds();
194 var mesh =
new Mesh();
205 var vertices =
new Vector3[(nb_long + 1) * nb_lat + 2];
206 const float pi = Mathf.PI;
207 const float _2_pi = pi * 2f;
209 vertices[0] = Vector3.up * radius;
210 for (var lat = 0; lat < nb_lat; lat++) {
211 var a1 = pi * (lat + 1) / (nb_lat + 1);
212 var sin1 = Mathf.Sin(a1);
213 var cos1 = Mathf.Cos(a1);
215 for (var lon = 0; lon <= nb_long; lon++) {
216 var a2 = _2_pi * (lon == nb_long ? 0 : lon) / nb_long;
217 var sin2 = Mathf.Sin(a2);
218 var cos2 = Mathf.Cos(a2);
220 vertices[lon + lat * (nb_long + 1) + 1] =
new Vector3(sin1 * cos2, cos1, sin1 * sin2) * radius;
224 vertices[vertices.Length - 1] = Vector3.up * -radius;
230 var normales =
new Vector3[vertices.Length];
231 for (var n = 0; n < vertices.Length; n++) {
232 normales[n] = vertices[n].normalized;
239 var uvs =
new Vector2[vertices.Length];
241 uvs[uvs.Length - 1] = Vector2.zero;
242 for (var lat = 0; lat < nb_lat; lat++) {
243 for (var lon = 0; lon <= nb_long; lon++) {
244 uvs[lon + lat * (nb_long + 1) + 1] =
245 new Vector2((
float)lon / nb_long, 1f - (float)(lat + 1) / (nb_lat + 1));
253 var nb_faces = vertices.Length;
254 var nb_triangles = nb_faces * 2;
255 var nb_indexes = nb_triangles * 3;
256 var triangles =
new int[nb_indexes];
260 for (var lon = 0; lon < nb_long; lon++) {
261 triangles[i++] = lon + 2;
262 triangles[i++] = lon + 1;
267 for (var lat = 0; lat < nb_lat - 1; lat++) {
268 for (var lon = 0; lon < nb_long; lon++) {
269 var current = lon + lat * (nb_long + 1) + 1;
270 var next = current + nb_long + 1;
272 triangles[i++] = current;
273 triangles[i++] = current + 1;
274 triangles[i++] = next + 1;
276 triangles[i++] = current;
277 triangles[i++] = next + 1;
278 triangles[i++] = next;
283 for (var lon = 0; lon < nb_long; lon++) {
284 triangles[i++] = vertices.Length - 1;
285 triangles[i++] = vertices.Length - (lon + 2) - 1;
286 triangles[i++] = vertices.Length - (lon + 1) - 1;
291 mesh.vertices = vertices;
292 mesh.normals = normales;
294 mesh.triangles = triangles;
296 mesh.RecalculateBounds();