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