- Home /
Copying mesh and changing vertex colors doesn't work?
I'm having a bit of an issue with copying meshes and modifying them. What seems to be happening is that the mesh is successfully copied, evidenced by the affected blocks I'm setting the new mesh to being visible and having " pit2" at the end of their name. However, the vertex colors don't seem to be showing up in some cases. Any build will not show the vertex colors and in the editor, the first run will not show the pit colors, but every other run will show them with the correct vertex color even when Camera.main.backgroundColor has been changed. So, in other words, it works as intended on the second in-editor run and every run after that, but not on the first run or in a build. Any ideas?
Here is the code I'm using to build the mesh:
public void BuildPitModel(){
Color32 pitColor = Camera.main.backgroundColor;
for(int i = 0;i<model.colors.Length;i++){
model.colors[i] = pitColor;
model.colors32[i].a = 255;
}
//pitModel = model;
pitModel = new Mesh();
pitModel.name = model.name+" pit2";
pitModel.vertices = model.vertices;
pitModel.triangles = model.triangles;
pitModel.uv = model.uv;
pitModel.normals = model.normals;
pitModel.colors32 = model.colors32;
pitModel.tangents = model.tangents;
Color32[] cols = pitModel.colors32;
for(int i = 0;i<cols.Length;i++){
float lerp = pitModel.vertices[i].y+0.5f;
Color32 col2 = pitColor;
col2.a=255;
cols[i] = Color32.Lerp(pitColor, col2, lerp);
}
pitModel.colors32=cols;
}
This is the code I'm using to set the mesh:
Mesh usedMesh;
if(pit)
usedMesh = thePiece.pitModel;
else
usedMesh = thePiece.model;
MeshFilter meshFilt = theChild.GetComponent<MeshFilter>();
if(meshFilt==null)
meshFilt = theChild.AddComponent("MeshFilter") as MeshFilter;
meshFilt.mesh = usedMesh;
Your answer
Follow this Question
Related Questions
How do I change the colour of a part of a mesh between vertices? 2 Answers
Changing colour in a mesh? 0 Answers
random colors for each triangle face 1 Answer
How to make colored voxels?!? 1 Answer
Multi-Colored Cube 1 Answer