- Home /
Combining Mesh's For Mesh Deformation
I'm working on a script that takes several mesh's combines them to make one mesh, then renders that one mesh instead of rendering it separately. Here's what I tried doing:
public List<MeshFilter> terrainSegments;
private MeshFilter terrainMesh;
private Transform thisTerrain;
void Awake () {
thisTerrain = transform;
int i = 0;
while(i < thisTerrain.childCount){
MeshFilter terrChild = thisTerrain.GetChild(i).GetComponent<MeshFilter>();
if(terrChild && thisTerrain.GetChild(i).CompareTag("Terrain Segment")){
terrChild.mesh.name = "Terrain Mesh " + i.ToString();
terrainSegments.Add(terrChild);
terrChild.transform.renderer.enabled = false;
}
i++;
}
CombineInstance[] combine = new CombineInstance[terrainSegments.Count];
terrainMesh = thisTerrain.GetComponent<MeshFilter>();
terrainMesh.mesh.CombineMeshes(combine, false);
thisTerrain.GetComponent<MeshFilter>().mesh = terrainMesh.mesh;
}
Basically once all the mesh's have been combined, the mesh renderer for the parent object should render the combined meshs. But it doesn't. It has a mesh renderer, and a mesh filter, and once this code runs, the mesh filter's mesh is the combined mesh. Why doesn't it render anything?
Edit
I'm apparently using CombineMeshes() wrong. Can't seem to find how to actually combine the meshes.
Edit
I've re-written the script and it works. It combines the mesh's, but unfortunately it doesn't suit what I need it to do. What I need is to have one renderer that renders the entire mesh as one, and keep the other mesh filter's intact. The player responds to the separate mesh filters, but only see's what the combined mesh renderer shows (although what he sees and what he's interacting with are two different things). Since the original mesh's are being deformed, the mesh renderer needs to create a new combined mesh whenever it's deformed. Unfortunately, whenever I run the new code I wrote, it creates a completely new mesh on top of the old one, without replacing the old one. It also doesn't render the deforms all the time. Here's the code I used:
Component[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length) {
combine[i].mesh = meshFilters[i].GetComponent<MeshFilter>().sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
i++;
}
terrain.GetComponent<MeshFilter>().mesh = new Mesh();
terrain.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
terrain.gameObject.active = true;
Here's a webplayer showing what's supposed to happen (click to deform).
Here's a webplayer showing what's actually happening (keep clicking after glitch happens).
Anyone know why this is happening and how to fix it?
mesh renderer has a material assigned?
no warnings in console?
clicking on mesh from meshfilter points to mesh with visible geometry, verts and tris count in inspector?
That seems to be the problem. No warnings, and there's a material assigned. I did a couple debugs and I found that the mesh doesn't have any vertices or triangles. So it says that the mesh has been combined, and a new mesh is in the mesh filter, but it doesn't have any mesh data. What did I do wrong when combining it?
Answer by ThermalFusion · Aug 13, 2012 at 11:24 PM
You were not adding anything into the CombineInstances.
private List terrainSegments; private List allMaterials;
private MeshFilter terrainMesh; private MeshRenderer terrainRenderer; private Transform thisTerrain;
void Awake () { terrainSegments = new List(); allMaterials = new List(); thisTerrain = transform;
foreach (Transform child in thisTerrain){ MeshFilter terrChild = child.GetComponent(); MeshRenderer terrRender = child.GetComponent(); if(terrChild && terrRender && child.CompareTag("Terrain Segment")){ terrainSegments.Add(terrChild); foreach (Material m in terrRender.materials) { allMaterials.Add(m); }
terrChild.renderer.enabled = false;
}
}
CombineInstance[] combine = new CombineInstance[terrainSegments.Count]; Matrix4x4 worldToLocalMatrix; worldToLocalMatrix = thisTerrain.worldToLocalMatrix; for(int c = 0; c < terrainSegments.Count; c ++) { combine[c].mesh = terrainSegments[c].sharedMesh; combine[c].transform = worldToLocalMatrix * terrainSegments[c].transform.localToWorldMatrix; Debug.Log(terrainSegments.Count.ToString()); } terrainMesh = thisTerrain.GetComponent(); terrainMesh.mesh.Clear(); terrainMesh.mesh.name = "Name"; terrainMesh.mesh.CombineMeshes(combine, false, true); Debug.Log(terrainMesh.mesh.vertexCount.ToString()); terrainRenderer = thisTerrain.GetComponent(); terrainRenderer.materials = allMaterials.ToArray(); }
How in the world do you format this correctly, plase change if you can.
Your answer
Follow this Question
Related Questions
combining meshes 0 Answers
Mesh Renderer and Memory 2 Answers
Turn an objects mesh renderer on upon collision. 1 Answer
Changing colour in a mesh? 0 Answers
Mesh render flicker 2 Answers