| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | /* |
| | 4 | | * PrimitiveMeshBuilder originated in the DecentralandUnityPlugin that can be found at: |
| | 5 | | * https://github.com/fairwood/DecentralandUnityPlugin |
| | 6 | | */ |
| | 7 | |
|
| | 8 | | namespace DCL.Helpers |
| | 9 | | { |
| | 10 | | public class PrimitiveMeshBuilder |
| | 11 | | { |
| | 12 | | public static Mesh BuildSphere(float radius) |
| | 13 | | { |
| 1 | 14 | | Mesh mesh = new Mesh(); |
| 1 | 15 | | mesh.name = "DCL Sphere"; |
| | 16 | |
|
| | 17 | | //float radius = 1f; |
| | 18 | | // Longitude ||| |
| 1 | 19 | | int nbLong = 24; |
| | 20 | | // Latitude --- |
| 1 | 21 | | int nbLat = 16; |
| | 22 | |
|
| | 23 | | #region Vertices |
| | 24 | |
|
| 1 | 25 | | Vector3[] vertices = new Vector3[(nbLong + 1) * nbLat + 2]; |
| 1 | 26 | | float _pi = Mathf.PI; |
| 1 | 27 | | float _2pi = _pi * 2f; |
| | 28 | |
|
| 1 | 29 | | vertices[0] = Vector3.up * radius; |
| 34 | 30 | | for (int lat = 0; lat < nbLat; lat++) |
| | 31 | | { |
| 16 | 32 | | float a1 = _pi * (float) (lat + 1) / (nbLat + 1); |
| 16 | 33 | | float sin1 = Mathf.Sin(a1); |
| 16 | 34 | | float cos1 = Mathf.Cos(a1); |
| | 35 | |
|
| 832 | 36 | | for (int lon = 0; lon <= nbLong; lon++) |
| | 37 | | { |
| 400 | 38 | | float a2 = _2pi * (float) (lon == nbLong ? 0 : lon) / nbLong; |
| 400 | 39 | | float sin2 = Mathf.Sin(a2); |
| 400 | 40 | | float cos2 = Mathf.Cos(a2); |
| | 41 | |
|
| 400 | 42 | | vertices[lon + lat * (nbLong + 1) + 1] = new Vector3(sin1 * cos2, cos1, sin1 * sin2) * radius; |
| | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| 1 | 46 | | vertices[vertices.Length - 1] = Vector3.up * -radius; |
| | 47 | |
|
| | 48 | | #endregion |
| | 49 | |
|
| | 50 | | #region Normales |
| | 51 | |
|
| 1 | 52 | | Vector3[] normales = new Vector3[vertices.Length]; |
| 806 | 53 | | for (int n = 0; n < vertices.Length; n++) |
| | 54 | | { |
| 402 | 55 | | normales[n] = vertices[n].normalized; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | #endregion |
| | 59 | |
|
| | 60 | | #region UVs |
| | 61 | |
|
| 1 | 62 | | Vector2[] uvs = new Vector2[vertices.Length]; |
| 1 | 63 | | uvs[0] = Vector2.up; |
| 1 | 64 | | uvs[uvs.Length - 1] = Vector2.zero; |
| 34 | 65 | | for (int lat = 0; lat < nbLat; lat++) |
| | 66 | | { |
| 832 | 67 | | for (int lon = 0; lon <= nbLong; lon++) |
| | 68 | | { |
| 400 | 69 | | uvs[lon + lat * (nbLong + 1) + 1] = |
| | 70 | | new Vector2(1f - (float) lon / nbLong, (float) (lat + 1) / (nbLat + 1)); |
| | 71 | | } |
| | 72 | | } |
| | 73 | | //uvs[lon + lat * (nbLong + 1) + 1] = new Vector2( (float)lon / nbLong, 1f - (float)(lat+1) / (nbLat+1) ); |
| | 74 | |
|
| | 75 | | #endregion |
| | 76 | |
|
| | 77 | | #region Triangles |
| | 78 | |
|
| 1 | 79 | | int nbFaces = vertices.Length; |
| 1 | 80 | | int nbTriangles = nbFaces * 2; |
| 1 | 81 | | int nbIndexes = nbTriangles * 3; |
| 1 | 82 | | int[] triangles = new int[nbIndexes]; |
| | 83 | |
|
| | 84 | | //Top Cap |
| 1 | 85 | | int i = 0; |
| 50 | 86 | | for (int lon = 0; lon < nbLong; lon++) |
| | 87 | | { |
| 24 | 88 | | triangles[i++] = lon + 2; |
| 24 | 89 | | triangles[i++] = lon + 1; |
| 24 | 90 | | triangles[i++] = 0; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | //Middle |
| 32 | 94 | | for (int lat = 0; lat < nbLat - 1; lat++) |
| | 95 | | { |
| 750 | 96 | | for (int lon = 0; lon < nbLong; lon++) |
| | 97 | | { |
| 360 | 98 | | int current = lon + lat * (nbLong + 1) + 1; |
| 360 | 99 | | int next = current + nbLong + 1; |
| | 100 | |
|
| 360 | 101 | | triangles[i++] = current; |
| 360 | 102 | | triangles[i++] = current + 1; |
| 360 | 103 | | triangles[i++] = next + 1; |
| | 104 | |
|
| 360 | 105 | | triangles[i++] = current; |
| 360 | 106 | | triangles[i++] = next + 1; |
| 360 | 107 | | triangles[i++] = next; |
| | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| | 111 | | //Bottom Cap |
| 50 | 112 | | for (int lon = 0; lon < nbLong; lon++) |
| | 113 | | { |
| 24 | 114 | | triangles[i++] = vertices.Length - 1; |
| 24 | 115 | | triangles[i++] = vertices.Length - (lon + 2) - 1; |
| 24 | 116 | | triangles[i++] = vertices.Length - (lon + 1) - 1; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | #endregion |
| | 120 | |
|
| 1 | 121 | | mesh.vertices = vertices; |
| 1 | 122 | | mesh.normals = normales; |
| 1 | 123 | | mesh.uv = uvs; |
| 1 | 124 | | mesh.triangles = triangles; |
| | 125 | |
|
| 1 | 126 | | mesh.RecalculateBounds(); |
| 1 | 127 | | return mesh; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | public static Mesh BuildPlane(float _size) |
| | 131 | | { |
| 35 | 132 | | Mesh mesh = new Mesh(); |
| 35 | 133 | | mesh.name = "DCL Plane"; |
| 35 | 134 | | Vector3[] vertices = new Vector3[8]; |
| 35 | 135 | | Vector3[] normals = new Vector3[8]; |
| 35 | 136 | | Vector2[] uvs = new Vector2[8]; |
| 35 | 137 | | Color[] colors = new Color[8]; |
| | 138 | |
|
| 35 | 139 | | int[] tris = new int[4 * 3]; |
| | 140 | |
|
| 35 | 141 | | int vIndex = 0; |
| 35 | 142 | | Vector3 start = new Vector3(-_size / 2, _size / 2, 0); |
| 35 | 143 | | vertices[vIndex++] = new Vector3(-start.x, -start.y, 0); |
| 35 | 144 | | vertices[vIndex++] = new Vector3(start.x, -start.y, 0); |
| 35 | 145 | | vertices[vIndex++] = new Vector3(start.x, start.y, 0); |
| 35 | 146 | | vertices[vIndex++] = new Vector3(-start.x, start.y, 0); |
| | 147 | |
|
| 35 | 148 | | vertices[vIndex++] = new Vector3(-start.x, -start.y, 0); |
| 35 | 149 | | vertices[vIndex++] = new Vector3(start.x, -start.y, 0); |
| 35 | 150 | | vertices[vIndex++] = new Vector3(start.x, start.y, 0); |
| 35 | 151 | | vertices[vIndex++] = new Vector3(-start.x, start.y, 0); |
| | 152 | |
|
| 35 | 153 | | vIndex = 0; |
| 35 | 154 | | uvs[vIndex++] = new Vector2(0f, 1f); |
| 35 | 155 | | uvs[vIndex++] = new Vector2(1f, 1f); |
| 35 | 156 | | uvs[vIndex++] = new Vector2(1f, 0f); |
| 35 | 157 | | uvs[vIndex++] = new Vector2(0f, 0f); |
| | 158 | |
|
| 35 | 159 | | uvs[vIndex++] = new Vector2(0f, 1f); |
| 35 | 160 | | uvs[vIndex++] = new Vector2(1f, 1f); |
| 35 | 161 | | uvs[vIndex++] = new Vector2(1f, 0f); |
| 35 | 162 | | uvs[vIndex++] = new Vector2(0f, 0f); |
| | 163 | |
|
| 35 | 164 | | vIndex = 0; |
| 35 | 165 | | normals[vIndex++] = Vector3.forward; |
| 35 | 166 | | normals[vIndex++] = Vector3.forward; |
| 35 | 167 | | normals[vIndex++] = Vector3.forward; |
| 35 | 168 | | normals[vIndex++] = Vector3.forward; |
| | 169 | |
|
| 35 | 170 | | normals[vIndex++] = Vector3.back; |
| 35 | 171 | | normals[vIndex++] = Vector3.back; |
| 35 | 172 | | normals[vIndex++] = Vector3.back; |
| 35 | 173 | | normals[vIndex++] = Vector3.back; |
| | 174 | |
|
| 35 | 175 | | vIndex = 0; |
| 35 | 176 | | colors[vIndex++] = Color.white; |
| 35 | 177 | | colors[vIndex++] = Color.white; |
| 35 | 178 | | colors[vIndex++] = Color.white; |
| 35 | 179 | | colors[vIndex++] = Color.white; |
| | 180 | |
|
| 35 | 181 | | colors[vIndex++] = Color.white; |
| 35 | 182 | | colors[vIndex++] = Color.white; |
| 35 | 183 | | colors[vIndex++] = Color.white; |
| 35 | 184 | | colors[vIndex++] = Color.white; |
| | 185 | |
|
| 35 | 186 | | int cnt = 0; |
| 35 | 187 | | tris[cnt++] = 2; |
| 35 | 188 | | tris[cnt++] = 1; |
| 35 | 189 | | tris[cnt++] = 0; |
| 35 | 190 | | tris[cnt++] = 3; |
| 35 | 191 | | tris[cnt++] = 2; |
| 35 | 192 | | tris[cnt++] = 0; |
| | 193 | |
|
| 35 | 194 | | tris[cnt++] = 4 + 1; |
| 35 | 195 | | tris[cnt++] = 4 + 2; |
| 35 | 196 | | tris[cnt++] = 4 + 0; |
| 35 | 197 | | tris[cnt++] = 4 + 2; |
| 35 | 198 | | tris[cnt++] = 4 + 3; |
| 35 | 199 | | tris[cnt++] = 4 + 0; |
| | 200 | |
|
| 35 | 201 | | mesh.vertices = vertices; |
| 35 | 202 | | mesh.normals = normals; |
| 35 | 203 | | mesh.uv = uvs; |
| 35 | 204 | | mesh.colors = colors; |
| | 205 | |
|
| 35 | 206 | | mesh.triangles = tris; |
| 35 | 207 | | return mesh; |
| | 208 | | } |
| | 209 | |
|
| | 210 | | public static Mesh BuildCube(float _size) |
| | 211 | | { |
| 1 | 212 | | Mesh mesh = new Mesh(); |
| 1 | 213 | | mesh.name = "DCL Box"; |
| 1 | 214 | | Vector3[] vertices = new Vector3[24]; //top bottom left right front back |
| 1 | 215 | | Vector3[] normals = new Vector3[24]; |
| 1 | 216 | | Vector2[] uvs = new Vector2[24]; |
| 1 | 217 | | Vector2[] uvs2 = new Vector2[24]; |
| 1 | 218 | | int[] tris = new int[12 * 3]; |
| | 219 | |
|
| 1 | 220 | | int vIndex = 0; |
| | 221 | | //top and bottom |
| 1 | 222 | | Vector3 start = new Vector3(-_size / 2, _size / 2, _size / 2); |
| 1 | 223 | | vertices[vIndex++] = start; |
| 1 | 224 | | vertices[vIndex++] = start + Vector3.right * _size; |
| 1 | 225 | | vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size; |
| 1 | 226 | | vertices[vIndex++] = start + Vector3.back * _size; |
| | 227 | |
|
| 1 | 228 | | start = new Vector3(-_size / 2, -_size / 2, _size / 2); |
| 1 | 229 | | vertices[vIndex++] = start; |
| 1 | 230 | | vertices[vIndex++] = start + Vector3.right * _size; |
| 1 | 231 | | vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size; |
| 1 | 232 | | vertices[vIndex++] = start + Vector3.back * _size; |
| | 233 | |
|
| | 234 | | //left and right |
| 1 | 235 | | start = new Vector3(-_size / 2, _size / 2, _size / 2); |
| 1 | 236 | | vertices[vIndex++] = start; |
| 1 | 237 | | vertices[vIndex++] = start + Vector3.back * _size; |
| 1 | 238 | | vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size; |
| 1 | 239 | | vertices[vIndex++] = start + Vector3.down * _size; |
| | 240 | |
|
| 1 | 241 | | start = new Vector3(_size / 2, _size / 2, _size / 2); |
| 1 | 242 | | vertices[vIndex++] = start; |
| 1 | 243 | | vertices[vIndex++] = start + Vector3.back * _size; |
| 1 | 244 | | vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size; |
| 1 | 245 | | vertices[vIndex++] = start + Vector3.down * _size; |
| | 246 | |
|
| | 247 | | //front and back |
| 1 | 248 | | start = new Vector3(-_size / 2, _size / 2, _size / 2); |
| 1 | 249 | | vertices[vIndex++] = start; |
| 1 | 250 | | vertices[vIndex++] = start + Vector3.right * _size; |
| 1 | 251 | | vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size; |
| 1 | 252 | | vertices[vIndex++] = start + Vector3.down * _size; |
| | 253 | |
|
| 1 | 254 | | start = new Vector3(-_size / 2, _size / 2, -_size / 2); |
| 1 | 255 | | vertices[vIndex++] = start; |
| 1 | 256 | | vertices[vIndex++] = start + Vector3.right * _size; |
| 1 | 257 | | vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size; |
| 1 | 258 | | vertices[vIndex++] = start + Vector3.down * _size; |
| | 259 | |
|
| | 260 | | //uv |
| 1 | 261 | | vIndex = 0; |
| | 262 | | //top and bottom |
| 1 | 263 | | uvs[vIndex++] = new Vector2(1f, 1f); |
| 1 | 264 | | uvs[vIndex++] = new Vector2(1f, 0f); |
| 1 | 265 | | uvs[vIndex++] = new Vector2(0f, 0f); |
| 1 | 266 | | uvs[vIndex++] = new Vector2(0f, 1f); |
| | 267 | |
|
| 1 | 268 | | uvs[vIndex++] = new Vector2(1f, 0f); |
| 1 | 269 | | uvs[vIndex++] = new Vector2(1f, 1f); |
| 1 | 270 | | uvs[vIndex++] = new Vector2(0f, 1f); |
| 1 | 271 | | uvs[vIndex++] = new Vector2(0f, 0f); |
| | 272 | |
|
| | 273 | | //left and right |
| 1 | 274 | | uvs[vIndex++] = new Vector2(1f, 1f); |
| 1 | 275 | | uvs[vIndex++] = new Vector2(1f, 0f); |
| 1 | 276 | | uvs[vIndex++] = new Vector2(0f, 0f); |
| 1 | 277 | | uvs[vIndex++] = new Vector2(0f, 1f); |
| | 278 | |
|
| 1 | 279 | | uvs[vIndex++] = new Vector2(1f, 0f); |
| 1 | 280 | | uvs[vIndex++] = new Vector2(1f, 1f); |
| 1 | 281 | | uvs[vIndex++] = new Vector2(0f, 1f); |
| 1 | 282 | | uvs[vIndex++] = new Vector2(0f, 0f); |
| | 283 | |
|
| | 284 | | //front and back |
| 1 | 285 | | uvs[vIndex++] = new Vector2(0f, 0f); |
| 1 | 286 | | uvs[vIndex++] = new Vector2(1f, 0f); |
| 1 | 287 | | uvs[vIndex++] = new Vector2(1f, 1f); |
| 1 | 288 | | uvs[vIndex++] = new Vector2(0f, 1f); |
| | 289 | |
|
| 1 | 290 | | uvs[vIndex++] = new Vector2(0f, 1f); |
| 1 | 291 | | uvs[vIndex++] = new Vector2(1f, 1f); |
| 1 | 292 | | uvs[vIndex++] = new Vector2(1f, 0f); |
| 1 | 293 | | uvs[vIndex++] = new Vector2(0f, 0f); |
| | 294 | |
|
| | 295 | | //uv2 |
| 1 | 296 | | vIndex = 0; |
| | 297 | | //top and bottom |
| 1 | 298 | | uvs2[vIndex++] = new Vector2(1f, 1f); |
| 1 | 299 | | uvs2[vIndex++] = new Vector2(1f, 0f); |
| 1 | 300 | | uvs2[vIndex++] = new Vector2(0f, 0f); |
| 1 | 301 | | uvs2[vIndex++] = new Vector2(0f, 1f); |
| | 302 | |
|
| 1 | 303 | | uvs2[vIndex++] = new Vector2(1f, 0f); |
| 1 | 304 | | uvs2[vIndex++] = new Vector2(1f, 1f); |
| 1 | 305 | | uvs2[vIndex++] = new Vector2(0f, 1f); |
| 1 | 306 | | uvs2[vIndex++] = new Vector2(0f, 0f); |
| | 307 | |
|
| | 308 | | //left and right |
| 1 | 309 | | uvs2[vIndex++] = new Vector2(1f, 1f); |
| 1 | 310 | | uvs2[vIndex++] = new Vector2(1f, 0f); |
| 1 | 311 | | uvs2[vIndex++] = new Vector2(0f, 0f); |
| 1 | 312 | | uvs2[vIndex++] = new Vector2(0f, 1f); |
| | 313 | |
|
| 1 | 314 | | uvs2[vIndex++] = new Vector2(1f, 0f); |
| 1 | 315 | | uvs2[vIndex++] = new Vector2(1f, 1f); |
| 1 | 316 | | uvs2[vIndex++] = new Vector2(0f, 1f); |
| 1 | 317 | | uvs2[vIndex++] = new Vector2(0f, 0f); |
| | 318 | |
|
| | 319 | | //front and back |
| 1 | 320 | | uvs2[vIndex++] = new Vector2(0f, 0f); |
| 1 | 321 | | uvs2[vIndex++] = new Vector2(1f, 0f); |
| 1 | 322 | | uvs2[vIndex++] = new Vector2(1f, 1f); |
| 1 | 323 | | uvs2[vIndex++] = new Vector2(0f, 1f); |
| | 324 | |
|
| 1 | 325 | | uvs2[vIndex++] = new Vector2(0f, 1f); |
| 1 | 326 | | uvs2[vIndex++] = new Vector2(1f, 1f); |
| 1 | 327 | | uvs2[vIndex++] = new Vector2(1f, 0f); |
| 1 | 328 | | uvs2[vIndex++] = new Vector2(0f, 0f); |
| | 329 | |
|
| | 330 | | //normal |
| 1 | 331 | | vIndex = 0; |
| | 332 | | //top and bottom |
| 1 | 333 | | normals[vIndex++] = Vector3.up; |
| 1 | 334 | | normals[vIndex++] = Vector3.up; |
| 1 | 335 | | normals[vIndex++] = Vector3.up; |
| 1 | 336 | | normals[vIndex++] = Vector3.up; |
| | 337 | |
|
| 1 | 338 | | normals[vIndex++] = Vector3.down; |
| 1 | 339 | | normals[vIndex++] = Vector3.down; |
| 1 | 340 | | normals[vIndex++] = Vector3.down; |
| 1 | 341 | | normals[vIndex++] = Vector3.down; |
| | 342 | |
|
| | 343 | | //left and right |
| 1 | 344 | | normals[vIndex++] = Vector3.left; |
| 1 | 345 | | normals[vIndex++] = Vector3.left; |
| 1 | 346 | | normals[vIndex++] = Vector3.left; |
| 1 | 347 | | normals[vIndex++] = Vector3.left; |
| | 348 | |
|
| 1 | 349 | | normals[vIndex++] = Vector3.right; |
| 1 | 350 | | normals[vIndex++] = Vector3.right; |
| 1 | 351 | | normals[vIndex++] = Vector3.right; |
| 1 | 352 | | normals[vIndex++] = Vector3.right; |
| | 353 | |
|
| | 354 | | //front and back |
| 1 | 355 | | normals[vIndex++] = Vector3.forward; |
| 1 | 356 | | normals[vIndex++] = Vector3.forward; |
| 1 | 357 | | normals[vIndex++] = Vector3.forward; |
| 1 | 358 | | normals[vIndex++] = Vector3.forward; |
| | 359 | |
|
| 1 | 360 | | normals[vIndex++] = Vector3.back; |
| 1 | 361 | | normals[vIndex++] = Vector3.back; |
| 1 | 362 | | normals[vIndex++] = Vector3.back; |
| 1 | 363 | | normals[vIndex++] = Vector3.back; |
| | 364 | |
|
| | 365 | |
|
| 1 | 366 | | int cnt = 0; |
| | 367 | |
|
| | 368 | | //top and bottom |
| 1 | 369 | | tris[cnt++] = 0; |
| 1 | 370 | | tris[cnt++] = 1; |
| 1 | 371 | | tris[cnt++] = 2; |
| 1 | 372 | | tris[cnt++] = 0; |
| 1 | 373 | | tris[cnt++] = 2; |
| 1 | 374 | | tris[cnt++] = 3; |
| | 375 | |
|
| 1 | 376 | | tris[cnt++] = 4 + 0; |
| 1 | 377 | | tris[cnt++] = 4 + 2; |
| 1 | 378 | | tris[cnt++] = 4 + 1; |
| 1 | 379 | | tris[cnt++] = 4 + 0; |
| 1 | 380 | | tris[cnt++] = 4 + 3; |
| 1 | 381 | | tris[cnt++] = 4 + 2; |
| | 382 | |
|
| | 383 | | //left and right |
| 1 | 384 | | tris[cnt++] = 8 + 0; |
| 1 | 385 | | tris[cnt++] = 8 + 1; |
| 1 | 386 | | tris[cnt++] = 8 + 2; |
| 1 | 387 | | tris[cnt++] = 8 + 0; |
| 1 | 388 | | tris[cnt++] = 8 + 2; |
| 1 | 389 | | tris[cnt++] = 8 + 3; |
| | 390 | |
|
| 1 | 391 | | tris[cnt++] = 12 + 0; |
| 1 | 392 | | tris[cnt++] = 12 + 2; |
| 1 | 393 | | tris[cnt++] = 12 + 1; |
| 1 | 394 | | tris[cnt++] = 12 + 0; |
| 1 | 395 | | tris[cnt++] = 12 + 3; |
| 1 | 396 | | tris[cnt++] = 12 + 2; |
| | 397 | |
|
| | 398 | | //front and back |
| 1 | 399 | | tris[cnt++] = 16 + 0; |
| 1 | 400 | | tris[cnt++] = 16 + 2; |
| 1 | 401 | | tris[cnt++] = 16 + 1; |
| 1 | 402 | | tris[cnt++] = 16 + 0; |
| 1 | 403 | | tris[cnt++] = 16 + 3; |
| 1 | 404 | | tris[cnt++] = 16 + 2; |
| | 405 | |
|
| 1 | 406 | | tris[cnt++] = 20 + 0; |
| 1 | 407 | | tris[cnt++] = 20 + 1; |
| 1 | 408 | | tris[cnt++] = 20 + 2; |
| 1 | 409 | | tris[cnt++] = 20 + 0; |
| 1 | 410 | | tris[cnt++] = 20 + 2; |
| 1 | 411 | | tris[cnt++] = 20 + 3; |
| | 412 | |
|
| 1 | 413 | | mesh.vertices = vertices; |
| 1 | 414 | | mesh.normals = normals; |
| 1 | 415 | | mesh.uv = uvs; |
| 1 | 416 | | mesh.uv2 = uvs2; |
| | 417 | |
|
| 1 | 418 | | mesh.triangles = tris; |
| | 419 | |
|
| 1 | 420 | | return mesh; |
| | 421 | | } |
| | 422 | |
|
| | 423 | | public static Mesh BuildConeOrCylinder(int numVertices, float radiusTop, float radiusBottom, float length, |
| | 424 | | float openingAngle, bool outside, bool inside, bool isCylinder, Vector3 offsetPos = default(Vector3)) |
| | 425 | | { |
| 18 | 426 | | if (openingAngle > 0 && openingAngle < 180) |
| | 427 | | { |
| 0 | 428 | | radiusTop = 0; |
| 0 | 429 | | radiusBottom = length * Mathf.Tan(openingAngle * Mathf.Deg2Rad / 2); |
| | 430 | | } |
| | 431 | |
|
| 18 | 432 | | string meshName = isCylinder |
| | 433 | | ? "DCL Cylinder" |
| | 434 | | : "DCL Cone" + numVertices + "v" + radiusTop + "t" + radiusBottom + "b" + length + "l" + length + |
| | 435 | | (outside ? "o" : "") + (inside ? "i" : ""); |
| | 436 | | //string meshPrefabPath = "Assets/Decentraland/Internal/" + meshName + ".asset"; |
| 18 | 437 | | Mesh mesh = null; //(Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabPath, typeof(Mesh)); |
| 18 | 438 | | if (mesh == null) |
| | 439 | | { |
| 18 | 440 | | int numVertices2 = numVertices + 1; |
| | 441 | |
|
| 18 | 442 | | mesh = new Mesh(); |
| 18 | 443 | | mesh.name = meshName; |
| | 444 | | // can't access Camera.current |
| | 445 | | //newCone.transform.position = Camera.current.transform.position + Camera.current.transform.forward * 5. |
| 18 | 446 | | int multiplier = (outside ? 1 : 0) + (inside ? 1 : 0); |
| 18 | 447 | | int offset = (outside && inside ? 2 * numVertices2 : 0); |
| | 448 | |
|
| 18 | 449 | | bool bTopCap = isCylinder ? true : false; |
| 18 | 450 | | bool bBottomCap = true; |
| | 451 | |
|
| 18 | 452 | | Vector3[] vertices = |
| | 453 | | new Vector3[ |
| | 454 | | 2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) + |
| | 455 | | (bBottomCap ? (numVertices + 1) : 0)]; |
| | 456 | | // 0..n-1: top, n..2n-1: bottom |
| 18 | 457 | | Vector3[] normals = |
| | 458 | | new Vector3[ |
| | 459 | | 2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) + |
| | 460 | | (bBottomCap ? (numVertices + 1) : 0)]; |
| 18 | 461 | | Vector2[] uvs = |
| | 462 | | new Vector2[ |
| | 463 | | 2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) + |
| | 464 | | (bBottomCap ? (numVertices + 1) : 0)]; |
| | 465 | | int[] tris; |
| 18 | 466 | | float slope = Mathf.Atan((radiusBottom - radiusTop) / length); // (rad difference)/height |
| 18 | 467 | | float slopeSin = Mathf.Sin(slope); |
| 18 | 468 | | float slopeCos = Mathf.Cos(slope); |
| | 469 | | int i; |
| | 470 | |
|
| 1836 | 471 | | for (i = 0; i < numVertices; i++) |
| | 472 | | { |
| 900 | 473 | | float angle = 2 * Mathf.PI * i / numVertices; |
| 900 | 474 | | float angleSin = Mathf.Sin(angle); |
| 900 | 475 | | float angleCos = Mathf.Cos(angle); |
| 900 | 476 | | float angleHalf = 2 * Mathf.PI * (i + 0.5f) / numVertices; // for degenerated normals at cone tips |
| 900 | 477 | | float angleHalfSin = Mathf.Sin(angleHalf); |
| 900 | 478 | | float angleHalfCos = Mathf.Cos(angleHalf); |
| | 479 | |
|
| 900 | 480 | | vertices[i] = new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos; |
| 900 | 481 | | vertices[i + numVertices2] = |
| | 482 | | new Vector3(radiusBottom * angleCos, 0, radiusBottom * angleSin) + offsetPos; |
| | 483 | |
|
| 900 | 484 | | if (radiusTop == 0) |
| | 485 | | { |
| 300 | 486 | | normals[i] = new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos); |
| 300 | 487 | | } |
| | 488 | | else |
| | 489 | | { |
| 600 | 490 | | normals[i] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos); |
| | 491 | | } |
| | 492 | |
|
| 900 | 493 | | if (radiusBottom == 0) |
| | 494 | | { |
| 300 | 495 | | normals[i + numVertices2] = |
| | 496 | | new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos); |
| 300 | 497 | | } |
| | 498 | | else |
| | 499 | | { |
| 600 | 500 | | normals[i + numVertices2] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos); |
| | 501 | | } |
| | 502 | |
|
| 900 | 503 | | uvs[i] = new Vector2(1.0f - 1.0f * i / numVertices, 1); |
| 900 | 504 | | uvs[i + numVertices2] = new Vector2(1.0f - 1.0f * i / numVertices, 0); |
| | 505 | |
|
| 900 | 506 | | if (outside && inside) |
| | 507 | | { |
| | 508 | | // vertices and uvs are identical on inside and outside, so just copy |
| 0 | 509 | | vertices[i + 2 * numVertices2] = vertices[i]; |
| 0 | 510 | | vertices[i + 3 * numVertices2] = vertices[i + numVertices2]; |
| 0 | 511 | | uvs[i + 2 * numVertices2] = uvs[i]; |
| 0 | 512 | | uvs[i + 3 * numVertices2] = uvs[i + numVertices2]; |
| | 513 | | } |
| | 514 | |
|
| 900 | 515 | | if (inside) |
| | 516 | | { |
| | 517 | | // invert normals |
| 0 | 518 | | normals[i + offset] = -normals[i]; |
| 0 | 519 | | normals[i + numVertices2 + offset] = -normals[i + numVertices2]; |
| | 520 | | } |
| | 521 | | } |
| | 522 | |
|
| 18 | 523 | | vertices[numVertices] = vertices[0]; |
| 18 | 524 | | vertices[numVertices + numVertices2] = vertices[0 + numVertices2]; |
| 18 | 525 | | uvs[numVertices] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 1); |
| 18 | 526 | | uvs[numVertices + numVertices2] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 0); |
| 18 | 527 | | normals[numVertices] = normals[0]; |
| 18 | 528 | | normals[numVertices + numVertices2] = normals[0 + numVertices2]; |
| | 529 | |
|
| | 530 | |
|
| 18 | 531 | | int coverTopIndexStart = 2 * multiplier * numVertices2; |
| 18 | 532 | | int coverTopIndexEnd = 2 * multiplier * numVertices2 + numVertices; |
| | 533 | |
|
| 18 | 534 | | if (bTopCap) |
| | 535 | | { |
| 612 | 536 | | for (i = 0; i < numVertices; i++) |
| | 537 | | { |
| 300 | 538 | | float angle = 2 * Mathf.PI * i / numVertices; |
| 300 | 539 | | float angleSin = Mathf.Sin(angle); |
| 300 | 540 | | float angleCos = Mathf.Cos(angle); |
| | 541 | |
|
| 300 | 542 | | vertices[coverTopIndexStart + i] = |
| | 543 | | new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos; |
| 300 | 544 | | normals[coverTopIndexStart + i] = new Vector3(0, 1, 0); |
| 300 | 545 | | uvs[coverTopIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f); |
| | 546 | | } |
| | 547 | |
|
| 6 | 548 | | vertices[coverTopIndexStart + numVertices] = new Vector3(0, length, 0) + offsetPos; |
| 6 | 549 | | normals[coverTopIndexStart + numVertices] = new Vector3(0, 1, 0); |
| 6 | 550 | | uvs[coverTopIndexStart + numVertices] = new Vector2(0.5f, 0.5f); |
| | 551 | | } |
| | 552 | |
|
| | 553 | |
|
| 18 | 554 | | int coverBottomIndexStart = coverTopIndexStart + (bTopCap ? 1 : 0) * (numVertices + 1); |
| 18 | 555 | | int coverBottomIndexEnd = coverBottomIndexStart + numVertices; |
| 18 | 556 | | if (bBottomCap) |
| | 557 | | { |
| 1836 | 558 | | for (i = 0; i < numVertices; i++) |
| | 559 | | { |
| 900 | 560 | | float angle = 2 * Mathf.PI * i / numVertices; |
| 900 | 561 | | float angleSin = Mathf.Sin(angle); |
| 900 | 562 | | float angleCos = Mathf.Cos(angle); |
| | 563 | |
|
| 900 | 564 | | vertices[coverBottomIndexStart + i] = |
| | 565 | | new Vector3(radiusBottom * angleCos, 0f, radiusBottom * angleSin) + |
| | 566 | | offsetPos; |
| 900 | 567 | | normals[coverBottomIndexStart + i] = new Vector3(0, -1, 0); |
| 900 | 568 | | uvs[coverBottomIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f); |
| | 569 | | } |
| | 570 | |
|
| 18 | 571 | | vertices[coverBottomIndexStart + numVertices] = new Vector3(0, 0f, 0) + offsetPos; |
| 18 | 572 | | normals[coverBottomIndexStart + numVertices] = new Vector3(0, -1, 0); |
| 18 | 573 | | uvs[coverBottomIndexStart + numVertices] = new Vector2(0.5f, 0.5f); |
| | 574 | | } |
| | 575 | |
|
| 18 | 576 | | mesh.vertices = vertices; |
| 18 | 577 | | mesh.normals = normals; |
| 18 | 578 | | mesh.uv = uvs; |
| | 579 | |
|
| | 580 | | // create triangles |
| | 581 | | // here we need to take care of point order, depending on inside and outside |
| 18 | 582 | | int cnt = 0; |
| 18 | 583 | | if (radiusTop == 0) |
| | 584 | | { |
| | 585 | | // top cone |
| 6 | 586 | | tris = |
| | 587 | | new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) + |
| | 588 | | ((bBottomCap ? 1 : 0) * numVertices * 3) |
| | 589 | | ]; |
| 6 | 590 | | if (outside) |
| | 591 | | { |
| 612 | 592 | | for (i = 0; i < numVertices; i++) |
| | 593 | | { |
| 300 | 594 | | tris[cnt++] = i + numVertices2; |
| 300 | 595 | | tris[cnt++] = i; |
| 300 | 596 | | tris[cnt++] = i + 1 + numVertices2; |
| | 597 | | // if(i==numVertices-1) |
| | 598 | | // tris[cnt++]=numVertices; |
| | 599 | | // else |
| | 600 | | // tris[cnt++]=i+1+numVertices; |
| | 601 | | } |
| | 602 | | } |
| | 603 | |
|
| 6 | 604 | | if (inside) |
| | 605 | | { |
| 0 | 606 | | for (i = offset; i < numVertices + offset; i++) |
| | 607 | | { |
| 0 | 608 | | tris[cnt++] = i; |
| 0 | 609 | | tris[cnt++] = i + numVertices2; |
| 0 | 610 | | tris[cnt++] = i + 1 + numVertices2; |
| | 611 | | // if(i==numVertices-1+offset) |
| | 612 | | // tris[cnt++]=numVertices+offset; |
| | 613 | | // else |
| | 614 | | // tris[cnt++]=i+1+numVertices; |
| | 615 | | } |
| | 616 | | } |
| 0 | 617 | | } |
| 12 | 618 | | else if (radiusBottom == 0) |
| | 619 | | { |
| | 620 | | // bottom cone |
| 6 | 621 | | tris = |
| | 622 | | new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) + |
| | 623 | | ((bBottomCap ? 1 : 0) * numVertices * 3) |
| | 624 | | ]; |
| 6 | 625 | | if (outside) |
| | 626 | | { |
| 612 | 627 | | for (i = 0; i < numVertices; i++) |
| | 628 | | { |
| 300 | 629 | | tris[cnt++] = i; |
| 300 | 630 | | tris[cnt++] = i + 1; |
| | 631 | | // if(i==numVertices-1) |
| | 632 | | // tris[cnt++]=0; |
| | 633 | | // else |
| | 634 | | // tris[cnt++]=i+1; |
| 300 | 635 | | tris[cnt++] = i + numVertices2; |
| | 636 | | } |
| | 637 | | } |
| | 638 | |
|
| 6 | 639 | | if (inside) |
| | 640 | | { |
| 0 | 641 | | for (i = offset; i < numVertices + offset; i++) |
| | 642 | | { |
| | 643 | | // if(i==numVertices-1+offset) |
| | 644 | | // tris[cnt++]=offset; |
| | 645 | | // else |
| | 646 | | // tris[cnt++]=i+1; |
| 0 | 647 | | tris[cnt++] = i + 1; |
| 0 | 648 | | tris[cnt++] = i; |
| 0 | 649 | | tris[cnt++] = i + numVertices2; |
| | 650 | | } |
| | 651 | | } |
| 0 | 652 | | } |
| | 653 | | else |
| | 654 | | { |
| | 655 | | // truncated cone |
| 6 | 656 | | tris = |
| | 657 | | new int[numVertices2 * 6 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) + |
| | 658 | | ((bBottomCap ? 1 : 0) * numVertices * 3) |
| | 659 | | ]; |
| 6 | 660 | | if (outside) |
| | 661 | | { |
| 612 | 662 | | for (i = 0; i < numVertices; i++) |
| | 663 | | { |
| 300 | 664 | | int ip1 = i + 1; |
| | 665 | | // if(ip1==numVertices) |
| | 666 | | // ip1=0; |
| | 667 | |
|
| 300 | 668 | | tris[cnt++] = i; |
| 300 | 669 | | tris[cnt++] = ip1; |
| 300 | 670 | | tris[cnt++] = i + numVertices2; |
| | 671 | |
|
| 300 | 672 | | tris[cnt++] = ip1 + numVertices2; |
| 300 | 673 | | tris[cnt++] = i + numVertices2; |
| 300 | 674 | | tris[cnt++] = ip1; |
| | 675 | | } |
| | 676 | | } |
| | 677 | |
|
| 6 | 678 | | if (inside) |
| | 679 | | { |
| 0 | 680 | | for (i = offset; i < numVertices + offset; i++) |
| | 681 | | { |
| 0 | 682 | | int ip1 = i + 1; |
| | 683 | | // if(ip1==numVertices+offset) |
| | 684 | | // ip1=offset; |
| | 685 | |
|
| 0 | 686 | | tris[cnt++] = ip1; |
| 0 | 687 | | tris[cnt++] = i; |
| 0 | 688 | | tris[cnt++] = i + numVertices2; |
| | 689 | |
|
| 0 | 690 | | tris[cnt++] = i + numVertices2; |
| 0 | 691 | | tris[cnt++] = ip1 + numVertices2; |
| 0 | 692 | | tris[cnt++] = ip1; |
| | 693 | | } |
| | 694 | | } |
| | 695 | | } |
| | 696 | |
|
| 18 | 697 | | if (bTopCap) |
| | 698 | | { |
| 612 | 699 | | for (i = 0; i < numVertices; ++i) |
| | 700 | | { |
| 300 | 701 | | int next = coverTopIndexStart + i + 1; |
| | 702 | |
|
| 300 | 703 | | if (next == coverTopIndexEnd) |
| | 704 | | { |
| 6 | 705 | | next = coverTopIndexStart; |
| | 706 | | } |
| | 707 | |
|
| 300 | 708 | | tris[cnt++] = next; |
| 300 | 709 | | tris[cnt++] = coverTopIndexStart + i; |
| 300 | 710 | | tris[cnt++] = coverTopIndexEnd; |
| | 711 | | } |
| | 712 | | } |
| | 713 | |
|
| 18 | 714 | | if (bBottomCap) |
| | 715 | | { |
| 1836 | 716 | | for (i = 0; i < numVertices; ++i) |
| | 717 | | { |
| 900 | 718 | | int next = coverBottomIndexStart + i + 1; |
| 900 | 719 | | if (next == coverBottomIndexEnd) |
| | 720 | | { |
| 18 | 721 | | next = coverBottomIndexStart; |
| | 722 | | } |
| | 723 | |
|
| 900 | 724 | | tris[cnt++] = coverBottomIndexEnd; |
| 900 | 725 | | tris[cnt++] = coverBottomIndexStart + i; |
| 900 | 726 | | tris[cnt++] = next; |
| | 727 | | } |
| | 728 | | } |
| | 729 | |
|
| 18 | 730 | | mesh.triangles = tris; |
| | 731 | | //AssetDatabase.CreateAsset(mesh, meshPrefabPath); |
| | 732 | | //AssetDatabase.SaveAssets(); |
| | 733 | | } |
| | 734 | |
|
| 18 | 735 | | return mesh; |
| | 736 | | } |
| | 737 | |
|
| | 738 | | public static Mesh BuildCone(int numVertices, float radiusTop, float radiusBottom, float length, |
| | 739 | | float openingAngle, bool outside, bool inside) |
| | 740 | | { |
| 12 | 741 | | return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside, |
| | 742 | | false, |
| | 743 | | new Vector3(0f, -length / 2, 0f)); |
| | 744 | | } |
| | 745 | |
|
| | 746 | | public static Mesh BuildCylinder(int numVertices, float radiusTop, float radiusBottom, float length, |
| | 747 | | float openingAngle, bool outside, bool inside) |
| | 748 | | { |
| 6 | 749 | | return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside, |
| | 750 | | true, |
| | 751 | | new Vector3(0f, -length / 2, 0f)); |
| | 752 | | } |
| | 753 | | } |
| | 754 | | } |