public UnityEngine.Mesh ToUnity(Utils.Progress progress = null) { if (unityMesh == null) { unityMesh = new UnityEngine.Mesh(); if (name != null) { unityMesh.name = name; } if (vertices != null) { unityMesh.vertices = vertices; } if (normals != null) { unityMesh.normals = normals; } if (tangents != null) { unityMesh.tangents = tangents; } if (uv1 != null) { unityMesh.uv = uv1; } if (uv2 != null) { unityMesh.uv2 = uv2; } if (colors != null) { unityMesh.colors = colors; } if (submeshes != null) { int nb_submeshes = submeshes.Length; unityMesh.subMeshCount = nb_submeshes; for (int i = 0; i < nb_submeshes; i++) { SubMesh submesh = submeshes[i]; unityMesh.SetIndices(submesh.triangles, submesh.topology, i); } } unityMesh.RecalculateBounds(); #if !UNITY_5_5_OR_NEWER unityMesh.Optimize(); #endif if (progress != null) { progress.Update(1); } } return(unityMesh); }
public UnityEngine.Mesh GetOrCreateCube(float xScale, float yScale, float zScale) { lock (padlock) { if (this.cachedCubeMeshes == null) { this.cachedCubeMeshes = new Dictionary <Tuple <float, float, float>, UnityEngine.Mesh>(); } Tuple <float, float, float> key = new Tuple <float, float, float>(xScale, yScale, zScale); if (!this.cachedCubeMeshes.ContainsKey(key)) { //First create primitive geometry then apply scale UnityEngine.Mesh cubeMesh = new UnityEngine.Mesh(); UnityEngine.Vector3[] vertices = { new float3(0, yScale, 0), new float3(0, 0, 0), new float3(xScale, yScale, 0), new float3(xScale, 0, 0), new float3(0, 0, zScale), new float3(xScale, 0, zScale), new float3(0, yScale, zScale), new float3(xScale, yScale, zScale), new float3(0, yScale, 0), new float3(xScale, yScale, 0), new float3(0, yScale, 0), new float3(0, yScale, zScale), new float3(xScale, yScale, 0), new float3(xScale, yScale, zScale), }; cubeMesh.vertices = vertices; int[] triangles = { 0, 2, 1, // front 1, 2, 3, 4, 5, 6, // back 5, 7, 6, 6, 7, 8, //top 7, 9, 8, 1, 3, 4, //bottom 3, 5, 4, 1, 11, 10, // left 1, 4, 11, 3, 12, 5, //right 5, 12, 13 }; cubeMesh.triangles = triangles; UnityEngine.Vector2[] uv = { new float2(0, 0.66f), new float2(0.25f, 0.66f), new float2(0, 0.33f), new float2(0.25f, 0.33f), new float2(0.5f, 0.66f), new float2(0.5f, 0.33f), new float2(0.75f, 0.66f), new float2(0.75f, 0.33f), new float2(1, 0.66f), new float2(1, 0.33f), new float2(0.25f, 1), new float2(0.5f, 1), new float2(0.25f, 0), new float2(0.5f, 0), }; cubeMesh.Optimize(); cubeMesh.RecalculateNormals(); cubeMesh.RecalculateBounds(); this.cachedCubeMeshes.Add(key, cubeMesh); } return(this.cachedCubeMeshes[key]); } }