I have one question, I have the script to edit all children in the object, from second object. Both objects have the same number of children, of course, the same material … but after running I got error “Array index is out of range.” It’s strange, because if I set the same object for regular and smoothed, here’s no error as well if objects have only one child … I tried foreach instead of for, but it did not help … Any ideas?
public GameObject regularMesh;
public GameObject smoothedMesh;
private float ScaleAndBias(float normal)
{
return (normal * 0.5f + 0.5f);
}
void Start (){
MeshFilter[] allChildren = new MeshFilter[regularMesh.transform.childCount];
allChildren = regularMesh.GetComponentsInChildren<MeshFilter>();
MeshFilter[] allChildren2 = new MeshFilter[regularMesh.transform.childCount];
allChildren2 = smoothedMesh.GetComponentsInChildren<MeshFilter>();
for (int m = 0; m < allChildren.Length; m++){
Mesh m1 = allChildren[m].mesh;
Mesh m2 = allChildren2[m].mesh;
GameObject go = new GameObject(allChildren[m].transform.name);
go.transform.parent = this.transform;
MeshFilter meshFilter;
meshFilter = go.AddComponent<MeshFilter>();
MeshRenderer meshRenderer;
meshRenderer = go.AddComponent<MeshRenderer>();
meshRenderer.materials = allChildren[m].renderer.materials;
Mesh newMesh = new Mesh();
meshFilter.mesh = newMesh;
Color[] newColors = new Color[m1.vertexCount];
for (int i = 0; i < m1.vertexCount; i++)
{
newColors <em>= new Color(ScaleAndBias(m2.normals<em>.x), ScaleAndBias(m2.normals_.y), ScaleAndBias(m2.normals*.z));*_</em></em>
* }*
* newMesh.vertices = m1.vertices;*
* newMesh.triangles = m1.triangles;*
* newMesh.normals = m1.normals;*
* newMesh.uv = m1.uv;*
* newMesh.colors = newColors;*
* }*
* //ObjExporter.MeshToFile (meshFilter, Application.dataPath + “//Test.obj”, true);*
* }*