I use the following code to generate a Cube as a single mesh. My purpose is to generate a sphere from it by normalizing as I have shown in the commented line at line 54 (I just have to do that to all those statements in lines 56 to 62). The problem here is that the mesh changes from a cube to a flat plane as I keep increasing the resolution (parameter given at line 8). (This code was inspired by this video : But I am using the technique in my own way as given by the following code, so that I can generate a single mesh instead of a combination of 6 meshes, this is required for my work) Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Sc_Planet : MonoBehaviour { [Range(2, 512)] public int resolution = 2; [Range(2, 256)] public int radius = 10; MeshFilter meshFilter; void OnValidate() { Initialize(); } void Initialize() { if (meshFilter == null) { GameObject meshObj = new GameObject("mesh_Planet"); meshObj.transform.parent = transform; meshObj.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Standard")); meshFilter = meshObj.AddComponent<MeshFilter>(); meshFilter.sharedMesh = new Mesh(); } int xmax = resolution + 1; int ymax = resolution + 1; float dx = 1.0f / resolution; float dy = 1.0f / resolution; Vector3[] vertsTop = new Vector3[xmax * ymax]; Vector3[] vertsRight = new Vector3[xmax * ymax]; Vector3[] vertsFront = new Vector3[xmax * ymax]; Vector3[] vertsBottom = new Vector3[xmax * ymax]; Vector3[] vertsLeft = new Vector3[xmax * ymax]; Vector3[] vertsBack = new Vector3[xmax * ymax]; for (int y = 0; y < ymax; y++) { for (int x = 0; x < xmax; x++) { float px = dx * x - 0.5f; float py = dy * y - 0.5f; int t = x + y * xmax; //vertsTop[t] = new Vector3(py, 0.5f, px).normalized * radius; vertsTop[t] = new Vector3(py, 0.5f, px); vertsRight[t] = new Vector3(px, py, 0.5f); vertsFront[t] = new Vector3(0.5f, px, py); vertsBottom[t] = new Vector3(px, -0.5f, py); vertsLeft[t] = new Vector3(py, px, -0.5f); vertsBack[t] = new Vector3(-0.5f, py, px); } } List<int> trianglesList = new List<int>(); for (int y = 0; y < ymax - 1; ++y) { for (int x = 0; x < xmax; ++x) { if (x % xmax != xmax - 1) { int f = x + y * xmax; trianglesList.Add(f); trianglesList.Add(f + 1); trianglesList.Add(f + 1 + xmax); trianglesList.Add(f); trianglesList.Add(f + 1 + xmax); trianglesList.Add(f + xmax); } } } List<Vector3> verts = new List<Vector3>(); Dictionary<Vector3, int> vdict = new Dictionary<Vector3, int>(); List<int> triangles = new List<int>(); int nextIndex = 0; void addFace(Vector3 [] in_verts, List<int> in_triangles) { for(int i = 0; i < in_verts.Length; ++i) { if (!vdict.ContainsKey(in_verts[i])) { vdict.Add(in_verts[i], nextIndex); verts.Add(in_verts[i]); ++nextIndex; } } for(int i = 0; i < in_triangles.Count; ++i) { triangles.Add(vdict[in_verts[in_triangles[i]]]); } } addFace(vertsTop, trianglesList); addFace(vertsRight, trianglesList); addFace(vertsFront, trianglesList); addFace(vertsBottom, trianglesList); addFace(vertsLeft, trianglesList); addFace(vertsBack, trianglesList); var mesh = meshFilter.sharedMesh; mesh.Clear(); mesh.vertices = verts.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateNormals(); } } This code works in Blender (I used python to script it on Blender and it works very well for any resolution). The only problem is that when I use this in Unity, the meshes become weird as I have shown in the images I have attached below. At Resolution = 96 : At Resolution = 122 : At Resolution = 182 : At Resolution = 344: Why is this happening? How should I correct it?
Looks like there are two things going on. First of all, you are are proposing to make a HELLACIOUS amount of vertices at 344 resolution. Just from some back of envelope math I would estimate 344x344x2x6, which puts you around 1.4 million verts. From what I can tell, you are using the 16-bit mesh API that limits you to 64k verts total. Even 96x96 quads times six is going to either exceed that or come pretty close to exceeding it. Look in the docs for using the 32-bit API. Second I'm going to guess there is either an outright bug in your transcoding from Python to C#, or else somehow you are reaching the limit of float precision, and Python was handling it by doing internal calculations in a higher precision format (not sure how Python decides nowadays).
Thank you for your answer. I was thinking the same, whether unity have some limit of vertices or triangles. I calculated the amount of vertices with a function as given below and got this: >>> CalcNumVertices(4) 98 >>> CalcNumVertices(256) 393218 >>> CalcNumVertices(512) 1572866 >>> CalcNumVertices(122) 89306 >>> CalcNumVertices(96) 55298 >>> CalcNumVertices(99) 58808 >>> CalcNumVertices(105) 66152 >>> CalcNumVertices(104) 64898 >>> CalcNumVertices(102) 62426 So it seems I exceed the limit (65535) specified in the documentation given in https://docs.unity3d.com/ScriptReference/Rendering.IndexFormat.html
Ok I found the answer. This is exceeding the limit of vertices on unity api for 16-bit based meshes. I had to change it to a 32-bit indexed mesh to correct it. Details are in this docuemntaiton page : https://docs.unity3d.com/ScriptRefe...6401.501737799.1635227368-67181881.1629608252 I just had to add the code : Code (CSharp): mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; That was it. Thank you for help @Kurt-Dekker