void UpdateMesh()
    {
        for (int i = 0; i < transform.childCount; i++)
        {
            Destroy(transform.GetChild(i).gameObject);
        }

        List <Mesh>   subsetMesh  = new List <Mesh> ();
        List <string> subsetNames = new List <string> ();

        if (Util.FileExist(path))
        {
            BC2Mesh    bc2mesh = null;
            GameObject go      = Instantiate(Util.GetMapload().empty, Vector3.zero, Quaternion.identity) as GameObject;


            bc2mesh = MeshDataImporter.LoadMeshRaw(path, uvOffset, bigFloat, inverted);


            int subsetInt = 0;
            subsetMesh  = bc2mesh.subMesh;
            subsetNames = bc2mesh.subMeshNames;
            foreach (Mesh sub in subsetMesh)
            {
                GameObject   subGO = (GameObject)Instantiate(Util.GetMapload().empty, Vector3.zero, Quaternion.identity);
                MeshRenderer mr    = subGO.AddComponent <MeshRenderer>();
                MeshFilter   mf    = subGO.AddComponent <MeshFilter>();
                MeshCollider mc    = subGO.AddComponent <MeshCollider> ();

                mr.material = new Material(Util.GetMapload().materialwhite);

                if (texture != "")
                {
                    mr.material.mainTexture = Util.LoadiTexture(texture);
                }
                mr.material.name = subsetNames[subsetInt];
                mf.mesh          = sub;
                mf.mesh.RecalculateNormals();
                subGO.name             = subsetNames[subsetInt];
                subGO.transform.parent = go.transform;
                mc.sharedMesh          = mf.mesh;
                subsetInt++;
            }
            if (bc2mesh.inverted)
            {
                Vector3 localScale = go.transform.localScale;
                localScale.x           *= -1;
                go.transform.localScale = localScale;
            }
        }
    }
Exemplo n.º 2
0
    public GameObject loadMesh(string meshpath, string refMeshName)
    {
        GameObject go = Instantiate(emptyGO, Vector3.zero, Quaternion.identity) as GameObject;


        if (Util.FileExist(meshpath))
        {
            BC2Mesh bc2mesh = null;

            List <Mesh>   subsetMesh  = new List <Mesh> ();
            List <string> subsetNames = new List <string> ();
            int           subsetInt   = 0;

            if (!(instantiatedMeshDictionary.TryGetValue(meshpath, out bc2mesh)))
            {
                bc2mesh = MeshDataImporter.LoadMesh(meshpath);
                instantiatedMeshDictionary.Add(meshpath, bc2mesh);
            }



            subsetMesh  = bc2mesh.subMesh;
            subsetNames = bc2mesh.subMeshNames;
            foreach (Mesh sub in subsetMesh)
            {
                GameObject   subGO = (GameObject)Instantiate(emptyGO, Vector3.zero, Quaternion.identity);
                MeshRenderer mr    = subGO.AddComponent <MeshRenderer>();
                MeshFilter   mf    = subGO.AddComponent <MeshFilter>();
                mr.material.name = subsetNames[subsetInt];
                mf.mesh          = sub;
                mf.mesh.RecalculateNormals();
                subGO.name             = subsetNames[subsetInt];
                subGO.transform.parent = go.transform;

                subsetInt++;
            }
            if (bc2mesh.inverted)
            {
                Vector3 localScale = go.transform.localScale;
                localScale.x           *= -1;
                go.transform.localScale = localScale;
            }
        }
        return(go);
    }
Exemplo n.º 3
0
    // Horrible!

    //public IEnumerator GenerateItem(Inst inst) {
    void GenerateItem(Inst inst)
    {
        Vector3          pos          = Util.GetPosition(inst);
        Quaternion       rot          = Util.GetRotation(inst);
        string           name         = "Unknown";
        string           mesh         = inst.type + " | " + inst.guid;
        List <Partition> partitions   = new List <Partition>();
        string           shaderName   = "";
        string           shaderPath   = "";
        string           refMeshClean = "";

        //This part is just trying to get the actual model name. It goes through different partitions and blueprints in order to get an accurate model name.
        //Normally, it's fine to just do name + _lod0_data, but sometimes we have objects that reference non-existant objects, such as container_large_blue.
        //While container_large exists, _blue is just referencing an other instance, and thus an other material for said container.
        //It's mostly not an issue.
        if (inst.type == "Entity.ReferenceObjectData" && Util.GetField("ReferencedObject", inst) != null && (Util.GetField("ReferencedObject", inst).reference != null && Util.GetField("ReferencedObject", inst).reference != "null"))
        {
            name = Util.GetField("ReferencedObject", inst).reference;

            string cleanName = Util.ClearGUIDString(name);
            string refGuid   = Util.GetGuid(name);


            Partition refPartition = Util.LoadPartition(cleanName);
            partitions.Add(refPartition);

            if (refPartition != null && name != "null")
            {
                Inst   bluePrint = Util.GetInst(refGuid, refPartition);
                string refObject = "";
                if (bluePrint != null)
                {
                    refObject = Util.GetField("Object", bluePrint).reference;
                }


                if (Util.GetInst(refObject, refPartition) != null)
                {
                    Inst staticModelEntityData = Util.GetInst(refObject, refPartition);
                    if (staticModelEntityData != null)
                    {
                        if (Util.GetField("Mesh", staticModelEntityData) != null)
                        {
                            string refMesh = Util.GetField("Mesh", staticModelEntityData).reference;
                            refMeshClean = Util.ClearGUIDString(refMesh);

                            string refMeshGuid = Util.GetGuid(refMesh);

                            Partition meshPartition = Util.LoadPartition(refMeshClean);
                            if (meshPartition != null)
                            {
                                List <Inst> shaderInstances = Util.GetTypes("Render.MeshShaderSetAsset", meshPartition);
                                foreach (Inst shaderInstance in shaderInstances)
                                {
                                    foreach (Complex complex in Util.GetArray("SurfaceShaders", shaderInstance).complex)
                                    {
                                        List <string> textureNames = new List <string>();
                                        foreach (Field field in complex.field)
                                        {
                                            if (field.name == "Name")
                                            {
                                                shaderName = Util.ClearGUIDString(field.value);
                                            }

                                            if (field.name == "Shader")
                                            {
                                                shaderPath = Util.ClearGUIDString(field.reference);
                                            }
                                        }

                                        materialManager.RegisterShader(refMeshClean, shaderPath, shaderName);
                                    }
                                }
                                Inst rigidMeshAsset = Util.GetInst(refMeshGuid, meshPartition);
                                if (rigidMeshAsset != null)
                                {
                                    string refMeshMesh = Util.GetField("Name", rigidMeshAsset).value;

                                    mesh = refMeshMesh + "_lod0_data";
                                }
                            }
                        }
                    }
                }
            }
        }

        string        meshpath    = "Resources/" + mesh + ".meshdata";
        GameObject    go          = null;
        List <Mesh>   subsetMesh  = new List <Mesh> ();
        List <string> subsetNames = new List <string> ();

        if (Util.FileExist(meshpath))
        {
            BC2Mesh bc2mesh = null;
            go = Instantiate(empty, Vector3.zero, Quaternion.identity) as GameObject;

            if (!(instantiatedMeshDictionary.TryGetValue(mesh, out bc2mesh)))
            {
                bc2mesh = MeshDataImporter.LoadMesh(meshpath);


                foreach (string s in subsetNames)
                {
                    Debug.Log(s);
                }
                instantiatedMeshDictionary.Add(mesh, bc2mesh);
            }
            int subsetInt = 0;
            subsetMesh  = bc2mesh.subMesh;
            subsetNames = bc2mesh.subMeshNames;
            foreach (Mesh sub in subsetMesh)
            {
                GameObject   subGO = (GameObject)Instantiate(empty, Vector3.zero, Quaternion.identity);
                MeshRenderer mr    = subGO.AddComponent <MeshRenderer>();
                MeshFilter   mf    = subGO.AddComponent <MeshFilter>();
                MeshCollider mc    = subGO.AddComponent <MeshCollider> ();

                mr.material = materialManager.GetMaterial(refMeshClean, shaderPath, subsetNames[subsetInt]);

                mf.mesh = sub;
                mf.mesh.RecalculateNormals();
                subGO.name             = subsetNames[subsetInt];
                subGO.transform.parent = go.transform;
                mc.sharedMesh          = mf.mesh;
                subsetInt++;
            }
            if (bc2mesh.inverted)
            {
                Vector3 localScale = go.transform.localScale;
                localScale.x           *= -1;
                go.transform.localScale = localScale;
            }
        }
        else
        {
            go = Instantiate(placeholder.gameObject, pos, rot) as GameObject;
        }

        go.name = Util.ClearGUID(inst);

        //go.AddComponent<MeshRenderer>().material.color = new Color (UnityEngine.Random.Range (0.1f, 1.0f), UnityEngine.Random.Range (0.1f, 1.0f), UnityEngine.Random.Range (0.1f, 1.0f));
        GameObject parent = transform.GetComponent <MapItems>().SelectParent(inst.type);

        go.transform.parent = parent.transform;


        Matrix4x4  matrix  = Util.GenerateMatrix4x4(inst);
        Quaternion newQuat = MatrixHelper.QuatFromMatrix(matrix);

        //Quaternion newQuat = go.transform.localRotation;
        //newQuat.x *= -1;
        //newQuat.z *= -1f;
        go.transform.localRotation = newQuat;
        go.transform.position      = pos;
        if (inst.type == "Entity.ReferenceObjectData")
        {
            Vector3 scale = go.transform.localScale;
            scale.x *= -1;             // the meshimporter is inverted. This is a workaround.
            go.transform.localScale = scale;

            if (Regex.IsMatch(go.name.ToLower(), "invisiblewall"))
            {
                go.GetComponentInChildren <MeshRenderer>().material = new Material(materialInvisibleWall);
            }
        }
        BC2Instance instance = go.AddComponent <BC2Instance>();

        instance.instance   = inst;
        instance.id         = i;
        instance.mapLoad    = this;
        instance.partitions = partitions;
        instantiatedGameObjects.Add(go.gameObject);
        instantiatedDictionary.Add(inst.guid.ToUpper(), go.gameObject);
        i++;
        //	yield return null;
    }