如何将第二种材质设置为组合网格中的第二个子网格?

时间:2015-03-16 17:09:02

标签: unity3d unityscript

我有这个代码(最初不是我的发明)工作正常,但我遇到了问题。我似乎无法用另一种材料渲染第二个子网格。

我无法弄清楚问题在于[渲染器]部分或第二个“子网格”可能永远不会合并。

@script RequireComponent(MeshFilter)
@script RequireComponent(MeshRenderer)
private var matarray : Material[] = new Material[2];
var mat : Material;
var mat2 : Material;

function Start () {
    matarray[0] = mat;
    matarray[1] = mat2;

    for (var child in transform)
        child.position += transform.position;
    transform.position = Vector3.zero;
    transform.rotation = Quaternion.identity;

    var meshFilters = GetComponentsInChildren(MeshFilter);
    var combine : CombineInstance[] = new CombineInstance[meshFilters.Length-1];
    var index = 0;
    for (var i = 0; i < meshFilters.Length; i++) {
        if (meshFilters[i].sharedMesh == null)
            continue;
        combine[index].mesh = meshFilters[i].sharedMesh;
        combine[index++].transform = meshFilters[i].transform.localToWorldMatrix;
        renderer.sharedMaterials = matarray;
        meshFilters[i].active = false;
    }
    GetComponent(MeshFilter).mesh = new Mesh();
    GetComponent(MeshFilter).mesh.CombineMeshes(combine);
    renderer.active = true;

    //here seems to be the trouble.
    renderer.sharedMaterials = matarray;
}

或者我尝试过,

renderer.sharedMaterials[1] = matarray[1];

总之,我遇到了麻烦。我必须保存几个绘制调用,除非得到修复,否则看起来不太好。

0 个答案:

没有答案