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