Пример #1
0
    public static GameObject buildSceneMeshObject(GameObject root, MB2_MeshCombiner mom, Mesh m, bool createNewChild = false)
    {
        GameObject          mesh;
        MeshFilter          mf  = null;
        MeshRenderer        mr  = null;
        SkinnedMeshRenderer smr = null;
        Transform           mt  = null;

        if (!createNewChild)
        {
            foreach (Transform t in root.transform)
            {
                if (t.name.EndsWith("-mesh"))
                {
                    mt = t;
                }
            }
        }
        if (mt == null)
        {
            mesh = new GameObject(mom.name + "-mesh");
        }
        else
        {
            mesh = mt.gameObject;
        }
        if (mom.renderType == MB_RenderType.skinnedMeshRenderer)
        {
            smr = mesh.GetComponent <SkinnedMeshRenderer>();
            if (smr == null)
            {
                smr = mesh.AddComponent <SkinnedMeshRenderer>();
            }
        }
        else
        {
            mf = mesh.GetComponent <MeshFilter>();
            if (mf == null)
            {
                mf = mesh.AddComponent <MeshFilter>();
            }
            mr = mesh.GetComponent <MeshRenderer>();
            if (mr == null)
            {
                mr = mesh.AddComponent <MeshRenderer>();
            }
        }
        if (mom.textureBakeResults.doMultiMaterial)
        {
            Material[] sharedMats = new Material[mom.textureBakeResults.resultMaterials.Length];
            for (int i = 0; i < sharedMats.Length; i++)
            {
                sharedMats[i] = mom.textureBakeResults.resultMaterials[i].combinedMaterial;
            }
            if (mom.renderType == MB_RenderType.skinnedMeshRenderer)
            {
                smr.materials           = sharedMats;
                smr.bones               = mom.GetBones();
                smr.updateWhenOffscreen = true;                 //todo see if can remove
                //todo update bounds
            }
            else
            {
                mr.sharedMaterials = sharedMats;
            }
        }
        else
        {
            if (mom.renderType == MB_RenderType.skinnedMeshRenderer)
            {
                smr.material = mom.textureBakeResults.resultMaterial;
                smr.bones    = mom.GetBones();
            }
            else
            {
                mr.material = mom.textureBakeResults.resultMaterial;
            }
        }
        if (mom.renderType == MB_RenderType.skinnedMeshRenderer)
        {
            smr.sharedMesh    = m;
            smr.lightmapIndex = mom.GetLightmapIndex();
        }
        else
        {
            mf.mesh          = m;
            mr.lightmapIndex = mom.GetLightmapIndex();
        }
        if (mom.lightmapOption == MB2_LightmapOptions.preserve_current_lightmapping || mom.lightmapOption == MB2_LightmapOptions.generate_new_UV2_layout)
        {
            mesh.isStatic = true;
        }

        List <GameObject> gos = mom.GetObjectsInCombined();

        if (gos.Count > 0)
        {
            bool   tagsAreSame   = true;
            bool   layersAreSame = true;
            string tag           = gos[0].tag;
            int    layer         = gos[0].layer;
            for (int i = 0; i < gos.Count; i++)
            {
                if (!gos[i].tag.Equals(tag))
                {
                    tagsAreSame = false;
                }
                if (gos[i].layer != layer)
                {
                    layersAreSame = false;
                }
            }
            if (tagsAreSame)
            {
                root.tag = tag;
                mesh.tag = tag;
            }
            if (layersAreSame)
            {
                root.layer = layer;
                mesh.layer = layer;
            }
        }
        mesh.transform.parent = root.transform;
        return(mesh);
    }
Пример #2
0
 public int GetLightmapIndex()
 {
     return(meshCombiner.GetLightmapIndex());
 }