Trouble with submeshes at runtime
Hello, i am having a bit of trouble with setting the triangles of a submesh at runtime. What i have is a sphere which i made in blender, what I'm doing is creating all the materials and submesh indexes at the beginning then i am creating regions which contain a list of the triangle indexes that are within that region. What the problem is, is that once the region is created and i attempt to set the triangles of that region to the correct submesh index, it just doesn't render the new material. I very well may be missing something but I've been going around in circles with this if someone could point me in the right direction it would be most appreciated. (as a note, i am not receiving any errors and the regions do contain all the triangles that they are supposed to)
Here is how I'm attempting to map out the sphere.
void CreateAllSubmeshes (List<Material> mats){
List<Material> curMats = this.GetComponent<Renderer> ().materials.ToList();
curMats.AddRange (mats);
int mi = 0;
foreach (Material mat in mats) {
materialDictionary.Add(mat.name, mat);
submeshDictionary.Add(mat.name, mi);
mi++;
}
currentMesh.mesh.subMeshCount = mi + 1;
this.GetComponent<Renderer>().materials = curMats.ToArray();
currentMesh.mesh.RecalculateNormals ();
StartCoroutine (CreateLandscapes ());
}
IEnumerator CreateLandscapes (){
List<Material> curmats = new List<Material> ();
curmats = this.GetComponent<Renderer> ().materials.ToList ();
for(int mi = 0; mi < curmats.Count; mi++){
if(curmats[mi].name.Equals("Water (Instance)") || curmats[mi].name.Equals("Bedrock (Instance)") || curmats[mi].name.Equals("Default (Instance)")){
curmats.RemoveAt(mi);
}
}
int l = 0;
while (l < curmats.Count) {
Landscape newLandscape = new Landscape();
newLandscape.submeshIndex = l;
newLandscape.SetLandscapeMaterial(curmats[l]);
landscapesList.Add(newLandscape);
l++;
yield return null;
}
StartCoroutine(CreateRegions ());
}
IEnumerator CreateRegions (){ List triangles = currentMesh.sharedMesh.triangles.ToList(); int trisPerRegion = (currentMesh.sharedMesh.triangles.Length/3) / regionCount; int r = 0; while(r < regionCount){ int index = r (trisPerRegion 3); List regionTris = triangles.GetRange(index, trisPerRegion*3); CreateRegion (regionTris, landscapesList[Random.Range(0, landscapesList.Count - 1)]); r++; yield return null; }
int totalmapedtriangles = 0;
foreach (PlanetRegion reg in Regions) {
totalmapedtriangles = totalmapedtriangles + reg.triangles.Count;
}
print ("Total Mapped Triangles: " + totalmapedtriangles + " and Total Mesh Triangles: "+ currentMesh.mesh.triangles.Length);
}
public void AddToSubMesh (string matName, int[] tris){
Mesh curMesh = currentMesh.mesh;
foreach (string n in submeshDictionary.Keys) {
if(matName.Equals(n + " (Instance)")){
List<int> currentSubmeshTris = curMesh.GetTriangles(submeshDictionary[n]).ToList();
currentSubmeshTris.AddRange(tris);
curMesh.SetTriangles(currentSubmeshTris.ToArray(), submeshDictionary[n]);
print ("triangles mapped: " + currentSubmeshTris.Count + " as " + n);
}
}
currentMesh.mesh = curMesh;
currentMesh.mesh.triangles = curMesh.triangles;
currentMesh.mesh.RecalculateNormals ();
}
public class PlanetaryRegion { public void SetTris (List _triangles, PlanetaryGrid _pGrid){ triangles = _triangles; pGrid = _pGrid; pGrid.AddToSubMesh (landscape.landscapeMaterial.name, _triangles.ToArray()); StartCoroutine (CreateTris (triangles)); } }
I don't know if I am just calling something in the wrong order or when i call mesh.setTriangles i need to update the mesh somehow. Thanks for you help.
~Soos
Your answer
Follow this Question
Related Questions
Material not rendering correctly on Submesh 0 Answers
How do i create sub meshes? 1 Answer
Android plugin - how to create a daily notification 0 Answers
Sprite didn't change more than once 0 Answers
Can't see GameObjects In Scene View 2 Answers