Beispiel #1
0
    public GMBFile Load(string file)
    {
        string file_no_ext = file;

        file += ".gmb";
        if (GmbFile.ContainsKey(file))
        {
            return(GmbFile[file]);
        }
        GMBFile gmb = null;

        if (CombatData.Ins.Chapter != null)
        {
            string path = CombatData.Ins.Chapter.GetResPath(FileExt.Gmc, file_no_ext);
            if (!string.IsNullOrEmpty(path))
            {
                gmb = new GMBFile();
                gmb = gmb.Load(path);
                if (gmb != null)
                {
                    GmbFile.Add(file, gmb);
                    return(gmb);
                }
            }
        }
        gmb = new GMBFile();
        GMBFile gmbOK = gmb.Load(file);

        if (gmbOK != null)
        {
            GmbFile.Add(file, gmbOK);
        }
        return(gmbOK);
    }
Beispiel #2
0
    //给一个面序号,得到面的3个顶点
    static List <Vector3> GetVertices(int faceid, GMBFile gmb)
    {
        List <Vector3> vec       = new List <Vector3>();
        int            faceIndex = faceid;

        if (faceid < gmb.FacesCount)
        {
            for (int i = 0; i < gmb.mesh.Length; i++)
            {
                if (faceIndex - gmb.mesh[i].Faces.Length < 0)
                {
                    float x0 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 1], 0];
                    float y0 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 1], 0];
                    float z0 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 1], 0];
                    float x1 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 3], 2];
                    float y1 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 3], 2];
                    float z1 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 3], 2];
                    float x2 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 2], 1];
                    float y2 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 2], 1];
                    float z2 = gmb.mesh[i].Vertices[(int)gmb.mesh[i].Faces[faceIndex, 2], 1];
                    vec.Add(new Vector3(x0, y0, z0));
                    vec.Add(new Vector3(x1, y1, z1));
                    vec.Add(new Vector3(x2, y2, z2));
                    return(vec);
                }
                //new List<int> { (int)mesh[i].Faces[j, 1], (int)mesh[i].Faces[j, 3], (int)mesh[i].Faces[j, 2] };
                else
                {
                    faceIndex -= gmb.mesh[i].Faces.Length;
                }
            }
        }
        return(vec);
    }
    public GMBFile Load(TextAsset asset)
    {
        if (GmbFile.ContainsKey(asset.name))
        {
            return(GmbFile[asset.name]);
        }
        GMBFile gmb   = new GMBFile();
        GMBFile gmbOK = gmb.Load(asset);

        if (gmbOK != null)
        {
            GmbFile.Add(asset.name, gmbOK);
        }
        return(gmbOK);
    }
    public GMBFile Load(string file)
    {
        file += ".gmb";
        if (GmbFile.ContainsKey(file))
        {
            return(GmbFile[file]);
        }
        GMBFile gmb   = new GMBFile();
        GMBFile gmbOK = gmb.Load(file);

        if (gmbOK != null)
        {
            GmbFile.Add(file, gmbOK);
        }
        return(gmbOK);
    }
Beispiel #5
0
    public static void GenerateCobMesh(Transform parent, GMBFile gmb)
    {
        GameObject obj = new GameObject("hellBox");

        obj.transform.position   = Vector3.zero;
        obj.transform.rotation   = Quaternion.identity;
        obj.transform.localScale = Vector3.one;
        obj.transform.SetParent(parent);
        obj.layer = parent.gameObject.layer;
        BoxCollider boc = obj.AddComponent <BoxCollider>();
        //右手转换左手
        Vector3 lefttop     = new Vector3(pos[3], pos[5], pos[4]);
        Vector3 rightbottom = new Vector3(pos[6], pos[8], pos[7]);

        boc.center = (lefttop + rightbottom) / 2;
        boc.size   = lefttop - rightbottom;

        //for (int i = 0; i < content.Count; i++)
        //{
        //    GameObject objsub = new GameObject("submesh" + i);
        //    objsub.transform.position = Vector3.zero;
        //    objsub.transform.rotation = Quaternion.identity;
        //    objsub.transform.localScale = Vector3.one;
        //    objsub.transform.SetParent(parent);
        //    objsub.layer = parent.gameObject.layer;
        //    MeshCollider mec = objsub.AddComponent<MeshCollider>();
        //    MeshFilter mf = objsub.AddComponent<MeshFilter>();
        //    MeshRenderer mr = objsub.AddComponent<MeshRenderer>();

        //    Mesh mesh = new Mesh();
        //    List<Vector3> vec = new List<Vector3>();
        //    List<int> triangles = new List<int>();
        //    //得到低模的顶点缓冲区,索引缓冲区,然后应用到新网格上,使用这个网格做碰撞.
        //    for (int j = 0; j < content[i].Length; j++)
        //    {
        //        //得到每个面的顶点索引序号.
        //        List<int> vecIndexFace = GetVerticesIndex(content[i][j], gmb);
        //    }
        //    mesh.SetVertices(vec);
        //    mesh.SetTriangles();
        //}
    }
Beispiel #6
0
    //由于Obj格式子对象根据对象名排序,又由于子对象顺序与动画有关系
    //所以子物件把名称改为顺序,再导出Obj
    public void LoadItemAndExportModel(Transform parent, string despath)
    {
        string fullSavePath = ItemSavePath + despath + "/";

        if (!System.IO.Directory.Exists(fullSavePath))
        {
            System.IO.Directory.CreateDirectory(fullSavePath);
        }
        DesFile des = Main.Ins.DesLoader.Load(despath);
        GMBFile gmb = Main.Ins.GMBLoader.Load(despath);

        if (des == null || gmb == null)
        {
            return;
        }
        string shaders = "Custom/MeteorVertexL";

        Material[] mat = new Material[gmb.TexturesCount];
        for (int x = 0; x < gmb.TexturesCount; x++)
        {
            string materialName = string.Format("{0:D2}_{1:D2}", despath, x);
            mat[x] = new Material(Resources.Load <Shader>("Mobile-Diffuse"));
            string tex = gmb.TexturesNames[x];
            int    del = tex.LastIndexOf('.');
            if (del != -1)
            {
                tex = tex.Substring(0, del);
            }
            Texture texture = Resources.Load <Texture>(tex);
            //这里的贴图可能是一个ifl文件,这种先不考虑,手动修改
            if (texture == null)
            {
                Debug.LogError("texture miss:" + tex);
            }
            mat[x].SetTexture("_MainTex", texture);
            mat[x].name = materialName;
            //UnityEditor.AssetDatabase.CreateAsset(mat[x], fullSavePath + mat[x].name + ".mat");
            //UnityEditor.AssetDatabase.Refresh();
        }

        bool saveMesh = false;

        if (parent == null)
        {
            saveMesh = true;
            GameObject root = new GameObject(despath);
            root.Identity(transform);
            parent = root.transform;
        }
        while (true)
        {
            for (int i = 0; i < Mathf.Min(des.SceneItems.Count, gmb.SceneObjectsCount); i++)
            {
                GameObject objMesh = new GameObject();
                objMesh.name = i.ToString();
                objMesh.transform.localRotation = Quaternion.identity;
                objMesh.transform.localPosition = Vector3.zero;
                objMesh.transform.localScale    = Vector3.one;
                int  j = i;
                Mesh w = new Mesh();
                //前者子网格编号,后者 索引缓冲区
                Dictionary <int, List <int> > tr = new Dictionary <int, List <int> >();
                List <Vector3> ve  = new List <Vector3>();
                List <Vector2> uv  = new List <Vector2>();
                List <Vector3> nor = new List <Vector3>();
                List <Color>   col = new List <Color>();
                for (int k = 0; k < gmb.mesh[j].faces.Count; k++)
                {
                    int key = gmb.mesh[j].faces[k].material;
                    if (tr.ContainsKey(key))
                    {
                        tr[key].Add(gmb.mesh[j].faces[k].triangle[0]);
                        tr[key].Add(gmb.mesh[j].faces[k].triangle[1]);
                        tr[key].Add(gmb.mesh[j].faces[k].triangle[2]);
                    }
                    else
                    {
                        tr.Add(key, new List <int>());
                        tr[key].Add(gmb.mesh[j].faces[k].triangle[0]);
                        tr[key].Add(gmb.mesh[j].faces[k].triangle[1]);
                        tr[key].Add(gmb.mesh[j].faces[k].triangle[2]);
                    }
                }
                for (int k = 0; k < gmb.mesh[j].vertices.Count; k++)
                {
                    Vector3    vecLocalToWorld = des.SceneItems[i].pos;
                    Quaternion quatToWorld     = des.SceneItems[i].quat;
                    Vector3    vecWorldToLocal = new Vector3(-vecLocalToWorld.x, -vecLocalToWorld.y, -vecLocalToWorld.z);
                    Vector3    vec             = gmb.mesh[j].vertices[k].pos + vecWorldToLocal;
                    vec = Quaternion.Inverse(quatToWorld) * vec;
                    ve.Add(vec);
                    uv.Add(gmb.mesh[j].vertices[k].uv);
                    col.Add(gmb.mesh[j].vertices[k].color);
                    nor.Add(gmb.mesh[j].vertices[k].normal);
                }
                w.SetVertices(ve);
                w.uv           = uv.ToArray();
                w.subMeshCount = tr.Count;
                int ss = 0;
                //查看这个网格使用了哪些材质,然后把这几个材质取出来
                List <Material> targetMat = new List <Material>();
                foreach (var each in tr)
                {
                    w.SetIndices(each.Value.ToArray(), MeshTopology.Triangles, ss++);
                    if (each.Key >= 0 && each.Key < gmb.shader.Length)
                    {
                        int materialIndex = gmb.shader[each.Key].TextureArg0;
                        if (materialIndex >= 0 && materialIndex < mat.Length)
                        {
                            targetMat.Add(mat[materialIndex]);
                        }
                        else
                        {
                            //即使没有贴图,也存在材质
                            string   materialName = string.Format("{0:D2}_{1:D2}", despath, materialIndex);
                            Material defaults     = Resources.Load <Material>(materialName);
                            if (defaults == null)
                            {
                                defaults      = new Material(Shader.Find("Custom/MeteorVertexL"));
                                defaults.name = materialName;
                                //UnityEditor.AssetDatabase.CreateAsset(defaults, fullSavePath + "/Resources/" + defaults.name + ".mat");
                                //UnityEditor.AssetDatabase.Refresh();
                            }
                            targetMat.Add(defaults);
                        }
                    }
                }
                MeshRenderer mr = objMesh.AddComponent <MeshRenderer>();
                MeshFilter   mf = objMesh.AddComponent <MeshFilter>();
                mf.sharedMesh         = w;
                mf.sharedMesh.colors  = col.ToArray();
                mf.sharedMesh.normals = nor.ToArray();
                mf.sharedMesh.RecalculateBounds();
                mf.sharedMesh.RecalculateNormals();
                mr.materials = targetMat.ToArray();
                string vis = "";
                if (des.SceneItems[i].ContainsKey("visible", out vis))
                {
                    if (vis == "0")
                    {
                        mr.enabled = false;
                        mr.gameObject.AddComponent <MeshCollider>();
                    }
                }
                string block = "";
                if (des.SceneItems[i].ContainsKey("blockplayer", out block))
                {
                    if (block == "no")
                    {
                    }
                    else
                    {
                        mr.gameObject.AddComponent <MeshCollider>();
                    }
                }
                else
                {
                    mr.gameObject.AddComponent <MeshCollider>();
                }

                string v = "";
                if (des.SceneItems[i].ContainsKey("model", out v))
                {
                    if (!string.IsNullOrEmpty(v))
                    {
                        LoadItemAndExportModel(objMesh.transform, v);
                    }
                }
                objMesh.transform.SetParent(parent);
                objMesh.transform.localRotation = des.SceneItems[i].quat;
                objMesh.transform.localPosition = des.SceneItems[i].pos;
                //MeshFilter mfSave = objMesh.GetComponent<MeshFilter>();
                //if (mfSave != null)
                //{
                //MeshToFile(mfSave, fullSavePath, des.SceneItems[i].name);
                //UnityEditor.AssetDatabase.CreateAsset(objMesh.GetComponent<MeshFilter>().sharedMesh, fullSavePath + "/" + des.SceneItems[i].name + ".asset");
                //UnityEditor.AssetDatabase.Refresh();
                //}
                //yield return 0;
            }

            if (saveMesh)
            {
                MeshFilter[] mfs = parent.GetComponentsInChildren <MeshFilter>();
                if (mfs.Length != 0)
                {
                    MeshesToFile(mfs, fullSavePath, despath, null);
                }
            }

            return;
        }
    }
Beispiel #7
0
    //GMB文件合并了网格,应该是不用设置位置和旋转了
    public void GenerateWeaponModel(string DesFile, GMBFile fModel, DesFile fIns, float scale = 1.0f, string textureOverrite = "")
    {
        if (string.IsNullOrEmpty(DesFile) || fModel == null || fIns == null)
        {
            return;
        }
        string    matIden = DesFile;
        Transform WR      = null;

        if (R != null)
        {
            DestroyImmediate(R);
        }
        R = new GameObject().transform;
        R.gameObject.layer = LayerMask.NameToLayer("Flight");
        R.SetParent(WeaponRoot);
        R.localRotation = Quaternion.identity;
        R.localPosition = Vector3.zero;
        R.localScale    = Vector3.one;
        R.name          = matIden;
        WR = R;

        //武器挂点必须在缩放正确后才能到指定的位置
        WR.localScale = Vector3.one;
        Material[] mat = new Material[fModel.TexturesCount];
        Dictionary <int, string> iflMaterials = new Dictionary <int, string>();

        for (int x = 0; x < fModel.TexturesCount; x++)
        {
            mat[x] = null;
            string tex = "";
            if (!string.IsNullOrEmpty(textureOverrite))
            {
                tex = textureOverrite;
            }
            else
            {
                tex = fModel.TexturesNames[x];
            }
            //若不是以ifl结尾的材质,那么要分割文件后缀,若是ifl类型的,则直接是xx.ifl.bytes类型的数据,是OK的
            bool iflMaterial = true;
            if (!tex.ToLower().EndsWith(".ifl"))
            {
                iflMaterial = false;
                int del = tex.LastIndexOf('.');
                if (del != -1)
                {
                    tex = tex.Substring(0, del);
                }
            }

            if (iflMaterial)
            {
                if (!iflMaterials.ContainsKey(x))
                {
                    iflMaterials.Add(x, tex);//记录x号材质是ifl类型的材质,以后会用到,这个序号的材质并且动态更换这个材质的贴图的
                }
            }
            else
            {
                string weaponIden = string.Format("{0}_{1:D2}{2}", matIden, x, textureOverrite);//还要考虑重载贴图也要生成对应的新材质
                mat[x] = Resources.Load <Material>(weaponIden);
                if (mat[x] == null)
                {
                    Texture texture = Resources.Load <Texture>(tex);
                    if (texture == null)
                    {
                        Debug.LogError("texture miss on load gmb:" + tex + " texture name:" + tex);
                    }
                    mat[x] = new Material(ShaderUtil.Find("AlphaTexture"));
                    mat[x].SetTexture("_MainTex", texture);
                    mat[x].name = weaponIden;
                    //if (!System.IO.Directory.Exists("Assets/Materials/" + "Weapons" + "/resources/"))
                    //    System.IO.Directory.CreateDirectory("Assets/Materials/" + "Weapons" + "/resources/");
                    //AssetDatabase.CreateAsset(mat[x], "Assets/Materials/" + "Weapons" + "/resources/" + mat[x].name + ".mat");
                    //AssetDatabase.Refresh();
                }
            }
        }

        for (int i = 0; i < fIns.SceneItems.Count; i++)
        {
            GameObject objMesh         = new GameObject();
            bool       addIflComponent = false;
            int        iflParam        = -1;
            int        iflMatIndex     = 0;
            objMesh.name = fIns.SceneItems[i].name;
            objMesh.transform.localRotation = Quaternion.identity;
            objMesh.transform.localPosition = Vector3.zero;
            objMesh.transform.localScale    = Vector3.one;
            bool realObject = false;//是不是正常物体,虚拟体无需设置材质球之类
            if (i < fModel.SceneObjectsCount)
            {
                //for (int j = 0; j < fModel.SceneObjectsCount; j++)
                //{
                //if (fModel.mesh[j].name == objMesh.name)
                {
                    realObject = true;
                    Mesh w = new Mesh();
                    //前者子网格编号,后者 索引缓冲区
                    Dictionary <int, List <int> > tr = new Dictionary <int, List <int> >();
                    List <Vector3> ve  = new List <Vector3>();
                    List <Vector2> uv  = new List <Vector2>();
                    List <Vector3> nor = new List <Vector3>();
                    List <Color>   col = new List <Color>();
                    for (int k = 0; k < fModel.mesh[i].faces.Count; k++)
                    {
                        int key = fModel.mesh[i].faces[k].material;
                        if (tr.ContainsKey(key))
                        {
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[0]);
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[1]);
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[2]);
                        }
                        else
                        {
                            tr.Add(key, new List <int>());
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[0]);
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[1]);
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[2]);
                        }
                    }
                    for (int k = 0; k < fModel.mesh[i].vertices.Count; k++)
                    {
                        ve.Add(fModel.mesh[i].vertices[k].pos);
                        uv.Add(fModel.mesh[i].vertices[k].uv);
                        col.Add(fModel.mesh[i].vertices[k].color);
                        nor.Add(fModel.mesh[i].vertices[k].normal);
                    }
                    w.SetVertices(ve);
                    w.uv           = uv.ToArray();
                    w.subMeshCount = tr.Count;
                    int             ss        = 0;
                    List <Material> targetMat = new List <Material>();
                    foreach (var each in tr)
                    {
                        w.SetIndices(each.Value.ToArray(), MeshTopology.Triangles, ss++);
                        if (each.Key >= 0 && each.Key < fModel.shader.Length)
                        {
                            int materialIndex = fModel.shader[each.Key].TextureArg0;
                            if (materialIndex >= 0 && materialIndex < mat.Length)
                            {
                                if (mat[materialIndex] == null)
                                {
                                    targetMat.Add(new Material(Shader.Find("Unlit/Transparent")));
                                    addIflComponent = true;
                                    iflParam        = materialIndex;
                                    iflMatIndex     = targetMat.Count - 1;
                                }
                                else
                                {
                                    targetMat.Add(mat[materialIndex]);
                                }
                            }
                            else
                            {
                                //占位材质,代表原文件里用到了序号<0的材质(即空材质),这里使用默认材质代替,一般武器加载不会触发这里,但是有极个别情况
                                Material defaults = new Material(Shader.Find("Unlit/Texture"));
                                defaults.name = string.Format("{0}_{1:D2}", objMesh.name, materialIndex);
                                targetMat.Add(defaults);
                            }
                        }
                    }
                    MeshRenderer mr = objMesh.AddComponent <MeshRenderer>();
                    MeshFilter   mf = objMesh.AddComponent <MeshFilter>();
                    mf.mesh         = w;
                    mf.mesh.colors  = col.ToArray();
                    mf.mesh.normals = nor.ToArray();
                    mf.mesh.RecalculateBounds();
                    mf.mesh.RecalculateNormals();

                    mr.materials = targetMat.ToArray();
                    string vis = "";
                    if (fIns.SceneItems[i].ContainsKey("visible", out vis))
                    {
                        if (vis == "0")
                        {
                            mr.enabled = false;
                            BoxCollider box = mr.gameObject.AddComponent <BoxCollider>();
                            box.enabled = false;
                            //weaponDamage.Add(box);
                        }
                    }
                    else
                    if (fIns.SceneItems[i].name.EndsWith("Box"))
                    {
                        BoxCollider box = mr.gameObject.AddComponent <BoxCollider>();
                        box.enabled = false;
                        //weaponDamage.Add(box);
                    }
                }
            }

            objMesh.transform.SetParent(WR);
            objMesh.layer = WR.gameObject.layer;
            if (addIflComponent && iflMaterials.ContainsKey(iflParam))
            {
                IFLLoader iflL = objMesh.AddComponent <IFLLoader>();
                iflL.fileNameReadOnly = iflMaterials[iflParam];
                iflL.IFLFile          = Resources.Load <TextAsset>(iflMaterials[iflParam]);
                iflL.matIndex         = iflMatIndex;
                iflL.LoadIFL();
            }

            if (!realObject)
            {
                objMesh.transform.localRotation = fIns.SceneItems[i].quat;
                objMesh.transform.localPosition = fIns.SceneItems[i].pos;
            }
            else
            {
                objMesh.transform.localRotation = Quaternion.identity;
                objMesh.transform.localScale    = Vector3.one;
                objMesh.transform.localPosition = Vector3.zero;
            }
        }

        WR.localScale = Vector3.one * scale;
    }
Beispiel #8
0
    public void LoadWeapon()
    {
        InventoryItem item = Weapon;

        if (item.Info().MainType == (int)EquipType.Weapon)
        {
            float      scale          = 1.0f;
            WeaponBase weaponProperty = WeaponMng.Instance.GetItem(item.Info().UnitId);
            string     weaponR        = "";
            weaponR = weaponProperty.WeaponR;

            if (!string.IsNullOrEmpty(weaponR))
            {
                GameObject weaponPrefab = Resources.Load <GameObject>(weaponR);
                if (weaponPrefab == null)
                {
                    GMCFile fGmcL = GMCLoader.Instance.Load(weaponR);
                    DesFile fDesL = DesLoader.Instance.Load(weaponR);

                    if (fGmcL != null && fDesL != null)
                    {
                        GenerateWeaponModel(weaponR, fGmcL, fDesL, scale, weaponProperty.TextureL);
                    }
                    else if (fGmcL == null && fDesL != null)
                    {
                        GMBFile fGmbL = GMBLoader.Instance.Load(weaponR);
                        GenerateWeaponModel(weaponR, fGmbL, fDesL, scale, weaponProperty.TextureL);
                    }
                }
                else
                {
                    if (R != null)
                    {
                        DestroyImmediate(R);
                    }
                    GameObject objWeapon = GameObject.Instantiate(weaponPrefab);
                    objWeapon.layer = LayerMask.NameToLayer("Flight");
                    R = objWeapon.transform;
                    //L = new GameObject().transform;
                    R.SetParent(WeaponRoot);
                    R.localPosition = Vector3.zero;
                    //这种导入来的模型,需要Y轴旋转180,与原系统的物件坐标系存在一些问题
                    R.localRotation = new Quaternion(0, 1, 0, 0);
                    R.name          = weaponR;
                    R.localScale    = Vector3.one;

                    //每个武器只能有一个碰撞盒
                    BoxCollider box = R.GetComponentInChildren <BoxCollider>();
                    if (box != null)
                    {
                        box.enabled = false;
                        //box.gameObject.tag = "Flight";
                        box.gameObject.layer = LayerMask.NameToLayer("Flight");
                        //weaponDamage.Add(box);
                    }
                    else
                    {
                        Debug.LogError("新增武器上找不到碰撞盒[除了暗器火枪飞轮外都应该有碰撞盒]");
                    }
                }
            }
        }
    }
Beispiel #9
0
    //适用于非武器类型的任意模型的显示,包括地图都可以用此方法读,且支持IFL材质,创建虚拟体,只匹配序号,不匹配名称
    public static void ShowMeteorObject(string file, Transform parent, bool gen = false)
    {
        if (file.EndsWith(".des"))
        {
            file = file.Substring(0, file.Length - 4);
        }
        DesFile fIns   = Main.Ins.DesLoader.Load(file);
        GMBFile gmb    = Main.Ins.GMBLoader.Load(file);
        GMCFile fModel = Main.Ins.GMCLoader.Load(file);

        //保存材质球
        if (Application.isEditor)
        {
            if (!System.IO.Directory.Exists("Assets/Materials/Weapons/resources/"))
            {
                System.IO.Directory.CreateDirectory("Assets/Materials/Weapons/resources/");
            }
        }

        //看des文件是否为空
        //优先GMB
        if (gmb != null)
        {
            //还未处理IFL序列图动态加载。
            Material[] mat = new Material[gmb.TexturesCount];
            for (int i = 0; i < mat.Length; i++)
            {
                mat[i] = null;
            }
            Dictionary <int, string> iflMat = new Dictionary <int, string>();//IFL材质特殊处理,IFL材质均为mobile/particle/additive
            for (int x = 0; x < gmb.TexturesCount; x++)
            {
                string tex = gmb.TexturesNames[x];
                if (tex.ToLower().EndsWith(".ifl"))
                {
                    if (!iflMat.ContainsKey(x))
                    {
                        iflMat.Add(x, tex);
                    }
                    continue;
                }

                string iden = string.Format("{0}_{1:D2}", file, x);
                mat[x] = Resources.Load <Material>(iden);
                if (mat[x] == null)
                {
                    mat[x] = new Material(ShaderMng.Find("AlphaTexture"));
                    int del = tex.LastIndexOf('.');
                    if (del != -1)
                    {
                        tex = tex.Substring(0, del);
                    }
                    Texture texture = Resources.Load <Texture>(tex);
                    if (texture == null)
                    {
                        Debug.LogError("texture miss on load gmb:" + file + " texture name:" + tex);
                    }
                    mat[x].SetTexture("_MainTex", texture);
                    mat[x].name = iden;
                    if (gen)
                    {
                        //UnityEditor.AssetDatabase.CreateAsset(mat[x], "Assets/Materials/Weapons/resources/" + mat[x].name + ".mat");
                        //UnityEditor.AssetDatabase.Refresh();
                    }
                }
            }

            for (int i = 0; i < fIns.SceneItems.Count; i++)
            {
                GameObject objMesh         = new GameObject();
                bool       addIflComponent = false;
                int        iflParam        = -1;
                int        iflMatIndex     = 0;
                objMesh.name = fIns.SceneItems[i].name;
                objMesh.transform.localRotation = Quaternion.identity;
                objMesh.transform.localPosition = Vector3.zero;
                objMesh.transform.localScale    = Vector3.one;
                objMesh.layer = parent.gameObject.layer;
                //bool realObject = false;//是不是正常物体,虚拟体无需设置材质球之类
                if (i < gmb.SceneObjectsCount)
                {
                    //for (int j = 0; j < gmb.SceneObjectsCount; j++)
                    //{
                    //    if (gmb.mesh[j].name == objMesh.name)
                    //    {
                    //realObject = true;
                    Mesh w = new Mesh();
                    //前者子网格编号,后者 索引缓冲区
                    Dictionary <int, List <int> > tr = new Dictionary <int, List <int> >();
                    List <Vector3> ve  = new List <Vector3>();
                    List <Vector2> uv  = new List <Vector2>();
                    List <Vector3> nor = new List <Vector3>();
                    List <Color>   col = new List <Color>();
                    for (int k = 0; k < gmb.mesh[i].faces.Count; k++)
                    {
                        int key = gmb.mesh[i].faces[k].material;
                        if (tr.ContainsKey(key))
                        {
                            tr[key].Add(gmb.mesh[i].faces[k].triangle[0]);
                            tr[key].Add(gmb.mesh[i].faces[k].triangle[1]);
                            tr[key].Add(gmb.mesh[i].faces[k].triangle[2]);
                        }
                        else
                        {
                            tr.Add(key, new List <int>());
                            tr[key].Add(gmb.mesh[i].faces[k].triangle[0]);
                            tr[key].Add(gmb.mesh[i].faces[k].triangle[1]);
                            tr[key].Add(gmb.mesh[i].faces[k].triangle[2]);
                        }
                    }
                    for (int k = 0; k < gmb.mesh[i].vertices.Count; k++)
                    {
                        //ve.Add(gmb.mesh[i].vertices[k].pos);
                        Vector3 vec = gmb.mesh[i].vertices[k].pos - (Vector3)fIns.SceneItems[i].pos;
                        vec = Quaternion.Inverse(fIns.SceneItems[i].quat) * vec;
                        //ve.Add(fModel.mesh[i].vertices[k].pos);
                        ve.Add(vec);
                        uv.Add(gmb.mesh[i].vertices[k].uv);
                        col.Add(gmb.mesh[i].vertices[k].color);
                        nor.Add(gmb.mesh[i].vertices[k].normal);
                    }
                    w.SetVertices(ve);
                    w.uv           = uv.ToArray();
                    w.subMeshCount = tr.Count;
                    int             ss        = 0;
                    List <Material> targetMat = new List <Material>();
                    foreach (var each in tr)
                    {
                        w.SetIndices(each.Value.ToArray(), MeshTopology.Triangles, ss++);
                        if (each.Key >= 0 && each.Key < gmb.shader.Length)
                        {
                            int materialIndex = gmb.shader[each.Key].TextureArg0;
                            if (materialIndex >= 0 && materialIndex < mat.Length)
                            {
                                if (mat[materialIndex] == null)
                                {
                                    targetMat.Add(new Material(Shader.Find("Unlit/Transparent")));
                                    addIflComponent = true;
                                    iflParam        = materialIndex;
                                    iflMatIndex     = targetMat.Count - 1;
                                }
                                else
                                {
                                    targetMat.Add(mat[materialIndex]);
                                }
                            }
                            else
                            {
                                Material defaults = Resources.Load <Material>(string.Format("{0}_{1:D2}", file, materialIndex));
                                if (defaults == null)
                                {
                                    defaults = new Material(Shader.Find("Unlit/Texture"));
                                }
                                defaults.name = string.Format("{0}_{1:D2}", file, materialIndex);
                                if (gen)
                                {
                                    //UnityEditor.AssetDatabase.CreateAsset(defaults, "Assets/Materials/Weapons/resources/" + defaults.name + ".mat");
                                    //UnityEditor.AssetDatabase.Refresh();
                                }
                                targetMat.Add(defaults);
                            }
                        }
                    }
                    MeshRenderer mr = objMesh.AddComponent <MeshRenderer>();
                    MeshFilter   mf = objMesh.AddComponent <MeshFilter>();
                    mf.mesh         = w;
                    mf.mesh.colors  = col.ToArray();
                    mf.mesh.normals = nor.ToArray();
                    mf.mesh.RecalculateBounds();
                    mf.mesh.RecalculateNormals();

                    mr.materials = targetMat.ToArray();
                    string vis = "";
                    if (fIns.SceneItems[i].ContainsKey("visible", out vis))
                    {
                        if (vis == "0")
                        {
                            mr.enabled = false;
                            //BoxCollider co = mr.gameObject.AddComponent<BoxCollider>();
                        }
                    }
                    string block = "";
                    if (fIns.SceneItems[i].ContainsKey("blockplayer", out block))
                    {
                        if (block == "no")
                        {
                            MeshCollider co = mr.gameObject.GetComponent <MeshCollider>();
                            if (co == null)
                            {
                                co = mr.gameObject.AddComponent <MeshCollider>();
                            }
                            if (co != null)
                            {
                                co.enabled   = false;
                                co.convex    = false;
                                co.isTrigger = false;
                            }
                        }
                    }
                    else
                    {
                        Collider coexist = mr.gameObject.GetComponent <Collider>();
                        if (coexist == null)
                        {
                            MeshCollider co = mr.gameObject.AddComponent <MeshCollider>();
                            co.isTrigger = false;
                        }
                    }
                }
                //}

                objMesh.transform.SetParent(parent);

                if (addIflComponent && iflMat.ContainsKey(iflParam))
                {
                    IFLLoader iflL = objMesh.AddComponent <IFLLoader>();
                    iflL.fileNameReadOnly  = iflMat[iflParam];
                    iflL.IFLFile           = Resources.Load <TextAsset>(iflMat[iflParam]);
                    iflL.matIndex          = iflMatIndex;
                    iflL.useSharedMaterial = false;
                    iflL.LoadIFL();
                }

                //如果是板凳桌子,加上双面网格,避免一些BUG
                if (parent.name.ToLower().Contains("chair") || parent.name.ToLower().Contains("desk"))
                {
                    objMesh.AddComponent <DoubleSideMeshCollider>();
                }
                objMesh.transform.localRotation = fIns.SceneItems[i].quat;
                objMesh.transform.localScale    = Vector3.one;
                objMesh.transform.localPosition = fIns.SceneItems[i].pos;
            }
        }
        else if (fModel != null)
        {
            //Debug.LogError("error !!!!!!!!!!!!!!!!!!!!!!!!! not support gmc file any more");

            Material[] mat = new Material[fModel.TexturesCount];
            for (int i = 0; i < mat.Length; i++)
            {
                mat[i] = null;
            }
            Dictionary <int, string> iflMat = new Dictionary <int, string>();//IFL材质特殊处理,IFL材质均为mobile/particle/additive

            for (int x = 0; x < fModel.TexturesCount; x++)
            {
                string tex = fModel.TexturesNames[x];
                if (tex.ToLower().EndsWith(".ifl"))
                {
                    if (!iflMat.ContainsKey(x))
                    {
                        iflMat.Add(x, tex);
                    }
                    continue;
                }

                mat[x] = Resources.Load <Material>(string.Format("{0}_{1:D2}", file, x));
                if (mat[x] == null)
                {
                    mat[x] = new Material(ShaderMng.Find("AlphaTexture"));
                    int del = tex.LastIndexOf('.');
                    if (del != -1)
                    {
                        tex = tex.Substring(0, del);
                    }
                    Texture texture = Resources.Load <Texture>(tex);
                    if (texture == null)
                    {
                        Debug.LogError("texture miss on load gmc:" + file + " texture name:" + tex);
                    }
                    mat[x].SetTexture("_MainTex", texture);
                    mat[x].name = string.Format("{0}_{1:D2}", file, x);
                    if (gen)
                    {
                        //UnityEditor.AssetDatabase.CreateAsset(mat[x], "Assets/Materials/Weapons/resources/" + mat[x].name + ".mat");
                        //UnityEditor.AssetDatabase.Refresh();
                    }
                }
                //mat[x] = Resources.Load<Material>(string.Format("{0}_{1:D2}", file, x));
                //if (mat[x] == null)
                //{
                //    mat[x] = new Material(ShaderUtil.Find("UnlitAlphaTexture"));
                //    string tex = fModel.TexturesNames[x];
                //    int del = tex.LastIndexOf('.');
                //    if (del != -1)
                //        tex = tex.Substring(0, del);
                //    Texture texture = Resources.Load<Texture>(tex);
                //    if (texture == null)
                //        Debug.LogError("texture miss on load gmc:" + file + " texture name:" + tex);
                //    mat[x].SetTexture("_MainTex", texture);
                //    mat[x].name = string.Format("{0}_{1:D2}", file, x);
                //    //AssetDatabase.CreateAsset(mat[x], "Assets/Materials/" + file + "/resources/" + mat[x].name + ".mat");
                //    //AssetDatabase.Refresh();
                //}
            }
            for (int i = 0; i < fIns.SceneItems.Count; i++)
            {
                GameObject objMesh         = new GameObject();
                bool       addIflComponent = false;
                int        iflParam        = -1;
                int        iflMatIndex     = 0;
                objMesh.name = fIns.SceneItems[i].name;
                objMesh.transform.localRotation = Quaternion.identity;
                objMesh.transform.localPosition = Vector3.zero;
                objMesh.transform.localScale    = Vector3.one;
                objMesh.layer = parent.gameObject.layer;
                //bool realObject = false;//是不是正常物体,虚拟体无需设置材质球之类
                if (i < fModel.SceneObjectsCount)
                {
                    //for (int j = 0; j < fModel.SceneObjectsCount; j++)
                    //{
                    //if (fModel.mesh[j].name == objMesh.name)
                    {
                        //realObject = true;
                        Mesh w = new Mesh();
                        //前者子网格编号,后者 索引缓冲区
                        Dictionary <int, List <int> > tr = new Dictionary <int, List <int> >();
                        List <Vector3> ve  = new List <Vector3>();
                        List <Vector2> uv  = new List <Vector2>();
                        List <Vector3> nor = new List <Vector3>();
                        List <Color>   col = new List <Color>();
                        for (int k = 0; k < fModel.mesh[i].faces.Count; k++)
                        {
                            int key = fModel.mesh[i].faces[k].material;
                            if (tr.ContainsKey(key))
                            {
                                tr[key].Add(fModel.mesh[i].faces[k].triangle[0]);
                                tr[key].Add(fModel.mesh[i].faces[k].triangle[1]);
                                tr[key].Add(fModel.mesh[i].faces[k].triangle[2]);
                            }
                            else
                            {
                                tr.Add(key, new List <int>());
                                tr[key].Add(fModel.mesh[i].faces[k].triangle[0]);
                                tr[key].Add(fModel.mesh[i].faces[k].triangle[1]);
                                tr[key].Add(fModel.mesh[i].faces[k].triangle[2]);
                            }
                        }
                        for (int k = 0; k < fModel.mesh[i].vertices.Count; k++)
                        {
                            Vector3 vec = fModel.mesh[i].vertices[k].pos - (Vector3)fIns.SceneItems[i].pos;
                            vec = Quaternion.Inverse(fIns.SceneItems[i].quat) * vec;
                            //ve.Add(fModel.mesh[i].vertices[k].pos);
                            ve.Add(vec);
                            uv.Add(fModel.mesh[i].vertices[k].uv);
                            col.Add(fModel.mesh[i].vertices[k].color);
                            nor.Add(fModel.mesh[i].vertices[k].normal);
                        }
                        w.SetVertices(ve);
                        w.uv           = uv.ToArray();
                        w.subMeshCount = tr.Count;
                        int             ss        = 0;
                        List <Material> targetMat = new List <Material>();
                        foreach (var each in tr)
                        {
                            w.SetIndices(each.Value.ToArray(), MeshTopology.Triangles, ss++);
                            if (each.Key >= 0 && each.Key < fModel.shader.Length)
                            {
                                int materialIndex = fModel.shader[each.Key].TextureArg0;
                                if (materialIndex >= 0 && materialIndex < mat.Length)
                                {
                                    if (mat[materialIndex] == null)
                                    {
                                        targetMat.Add(new Material(Shader.Find("Unlit/Transparent")));
                                        addIflComponent = true;
                                        iflParam        = materialIndex;
                                        iflMatIndex     = targetMat.Count - 1;
                                    }
                                    else
                                    {
                                        targetMat.Add(mat[materialIndex]);
                                    }
                                }
                                else
                                {
                                    //即使没有贴图,也存在材质
                                    Material defaults = Resources.Load <Material>(string.Format("{0}_{1:D2}", file, materialIndex));
                                    if (defaults == null)
                                    {
                                        defaults = new Material(Shader.Find("Unlit/Texture"));
                                    }
                                    defaults.name = string.Format("{0}_{1:D2}", file, materialIndex);
                                    if (gen)
                                    {
                                        //UnityEditor.AssetDatabase.CreateAsset(defaults, "Assets/Materials/Weapons/resources/" + defaults.name + ".mat");
                                        //UnityEditor.AssetDatabase.Refresh();
                                    }
                                    targetMat.Add(defaults);
                                }
                            }
                        }
                        MeshRenderer mr = objMesh.AddComponent <MeshRenderer>();
                        MeshFilter   mf = objMesh.AddComponent <MeshFilter>();
                        mf.mesh         = w;
                        mf.mesh.colors  = col.ToArray();
                        mf.mesh.normals = nor.ToArray();
                        mf.mesh.RecalculateBounds();
                        mf.mesh.RecalculateNormals();

                        mr.materials = targetMat.ToArray();
                        string vis = "";
                        if (fIns.SceneItems[i].ContainsKey("visible", out vis))
                        {
                            if (vis == "0")
                            {
                                mr.enabled = false;
                                //BoxCollider co = mr.gameObject.AddComponent<BoxCollider>();
                            }
                        }
                        string block = "";
                        if (fIns.SceneItems[i].ContainsKey("blockplayer", out block))
                        {
                            if (block == "no")
                            {
                                Collider co = mr.gameObject.GetComponent <MeshCollider>();
                                if (co == null)
                                {
                                    co = mr.gameObject.AddComponent <MeshCollider>();
                                }
                                if (co != null)
                                {
                                    MeshCollider mec = co as MeshCollider;
                                    if (mec != null)
                                    {
                                        mec.enabled   = false;
                                        mec.convex    = false;//unity bug
                                        mec.isTrigger = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            Collider coexist = mr.gameObject.GetComponent <Collider>();
                            if (coexist == null)
                            {
                                MeshCollider co = mr.gameObject.AddComponent <MeshCollider>();
                                co.isTrigger = false;
                            }
                        }
                    }
                }

                objMesh.transform.SetParent(parent);
                if (addIflComponent && iflMat.ContainsKey(iflParam))
                {
                    IFLLoader iflL = objMesh.AddComponent <IFLLoader>();
                    iflL.fileNameReadOnly  = iflMat[iflParam];
                    iflL.IFLFile           = Resources.Load <TextAsset>(iflMat[iflParam]);
                    iflL.matIndex          = iflMatIndex;
                    iflL.useSharedMaterial = false;
                    iflL.LoadIFL();
                }
                //如果是板凳桌子,加上双面网格,避免一些BUG
                if (parent.name.ToLower().Contains("chair") || parent.name.ToLower().Contains("desk"))
                {
                    objMesh.AddComponent <DoubleSideMeshCollider>();
                }
                objMesh.transform.localRotation = fIns.SceneItems[i].quat;
                objMesh.transform.localScale    = Vector3.one;
                objMesh.transform.localPosition = fIns.SceneItems[i].pos;
            }
        }
        else
        {
            //一个预设
            GameObject prefab = Resources.Load(file) as GameObject;
            if (prefab != null)
            {
                GameObject obj = GameObject.Instantiate(prefab, parent);
                obj.transform.localScale    = Vector3.one;
                obj.transform.localPosition = Vector3.zero;
                obj.transform.rotation      = Quaternion.identity;
                BoxCollider co = obj.GetComponentInChildren <BoxCollider>();
                co.isTrigger = true;
                int k = obj.transform.childCount;
                while (obj.transform.childCount != 0)
                {
                    Transform tri = obj.transform.GetChild(0);
                    tri.SetParent(parent);
                }
                GameObject.Destroy(obj);
                WsGlobal.SetObjectLayer(parent.gameObject, parent.gameObject.layer);
            }
        }
    }
    //把背包里的物品,装备到身上.
    public void EquipWeapon(InventoryItem item, bool ModelLayer = false)
    {
        if (Weapon == null && item != null)
        {
            if (item.Info().MainType == (int)EquipType.Weapon)
            {
                float scale = 1.0f;
                if (item.Info().Size != 0)//size为0的时候
                {
                    scale = (item.extra == null ? item.Info().Size : item.Info().Size *(1 + item.extra.SizePercent / 100.0f));
                }
                else
                {
                    scale = (item.extra == null ? 1 : (1 + item.extra.SizePercent / 100.0f));
                }
                WeaponDatas.WeaponDatas weaponProperty = U3D.GetWeaponProperty(item.Info().UnitId);


                string weaponL = "";
                string weaponR = "";
                PoseType = item.WeaponPos;
                if (item.WeaponPos == 0)
                {
                    weaponL = weaponProperty.WeaponL;
                    weaponR = weaponProperty.WeaponR;
                }
                else if (item.WeaponPos == 1)
                {
                    weaponL = weaponProperty.PosAWL;
                    weaponR = weaponProperty.PosAWR;
                }
                else if (item.WeaponPos == 2)
                {
                    weaponL = weaponProperty.PosBWL;
                    weaponR = weaponProperty.PosBWR;
                }

                //Debug.Log("加载武器:" + " 左手:" + weaponL + "右手:" + weaponR);
                if (!string.IsNullOrEmpty(weaponL))
                {
                    GameObject weaponPrefab = Resources.Load <GameObject>(weaponL);
                    if (weaponPrefab == null)
                    {
                        GMCFile fGmcL = Main.Ins.GMCLoader.Load(weaponL);
                        DesFile fDesL = Main.Ins.DesLoader.Load(weaponL);

                        if (fGmcL != null && fDesL != null)
                        {
                            GenerateWeaponModel(weaponL, fGmcL, fDesL, false, scale, weaponProperty.TextureL, ModelLayer);
                        }
                        else if (fGmcL == null && fDesL != null)
                        {
                            GMBFile fGmbL = Main.Ins.GMBLoader.Load(weaponL);
                            GenerateWeaponModel(weaponL, fGmbL, fDesL, false, scale, weaponProperty.TextureL, ModelLayer);
                        }
                    }
                    else
                    {
                        if (L != null)
                        {
                            DestroyImmediate(L);
                        }
                        GameObject objWeapon = GameObject.Instantiate(weaponPrefab);
                        objWeapon.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
                        L = objWeapon.transform;
                        //L = new GameObject().transform;
                        L.SetParent(LP);
                        L.localPosition = Vector3.zero;
                        //这种导入来的模型,需要Y轴旋转180,与原系统的物件坐标系存在一些问题
                        L.localRotation = new Quaternion(0, 1, 0, 0);
                        L.name          = weaponL;
                        L.localScale    = Vector3.one;
                        //WE = L;
                        //武器挂点必须在缩放正确后才能到指定的位置
                        WeaponTrail wt = L.gameObject.AddComponent <WeaponTrail>();
                        //右手
                        //GameObject ctrlRs = Global.ldaControlX("d_wpnRS", L.gameObject);
                        //if (ctrlRs != null)
                        //    wt.AddTransform(ctrlRs.transform);
                        //GameObject ctrlRe = Global.ldaControlX("d_wpnRE", L.gameObject);
                        //if (ctrlRe != null)
                        //    wt.AddTransform(ctrlRe.transform);
                        //左手
                        GameObject ctrlLs = NodeHelper.Find("d_wpnLS", L.gameObject);
                        if (ctrlLs != null)
                        {
                            wt.AddTransform(ctrlLs.transform);
                        }
                        GameObject ctrlLe = NodeHelper.Find("d_wpnLE", L.gameObject);
                        if (ctrlLe != null)
                        {
                            wt.AddTransform(ctrlLe.transform);
                        }

                        //每个武器只能有一个碰撞盒
                        BoxCollider box = L.GetComponentInChildren <BoxCollider>();
                        if (box != null)
                        {
                            box.enabled          = false;
                            box.gameObject.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
                            weaponDamage.Add(box);
                        }
                        else
                        {
                            Debug.LogError("新增武器上找不到碰撞盒[除了暗器火枪飞轮外都应该有碰撞盒]");
                        }
                        if (owner != null)
                        {
                            wt.Init(owner);
                            trail.Add(wt);
                        }
                        else
                        {
                            GameObject.Destroy(wt);
                            wt = null;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(weaponR))
                {
                    GameObject weaponPrefab = Resources.Load <GameObject>(weaponR);
                    if (weaponPrefab == null)
                    {
                        GMCFile fGmcR = Main.Ins.GMCLoader.Load(weaponR);
                        DesFile fDesR = Main.Ins.DesLoader.Load(weaponR);
                        if (fGmcR != null && fDesR != null)
                        {
                            GenerateWeaponModel(weaponR, fGmcR, fDesR, true, scale, weaponProperty.TextureR, ModelLayer);
                        }
                        else if (fGmcR == null && fDesR != null)
                        {
                            GMBFile fGmbR = Main.Ins.GMBLoader.Load(weaponProperty.WeaponR);
                            GenerateWeaponModel(weaponR, fGmbR, fDesR, true, scale, weaponProperty.TextureR, ModelLayer);
                        }
                    }
                    else
                    {
                        if (R != null)
                        {
                            DestroyImmediate(R);
                        }
                        GameObject objWeapon = GameObject.Instantiate(weaponPrefab);
                        objWeapon.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
                        R = objWeapon.transform;
                        R.SetParent(RP);
                        R.localPosition = Vector3.zero;
                        R.localRotation = new Quaternion(0, 1, 0, 0);
                        R.name          = weaponR;
                        R.localScale    = Vector3.one;

                        //武器挂点必须在缩放正确后才能到指定的位置
                        WeaponTrail wt = R.gameObject.AddComponent <WeaponTrail>();
                        //右手
                        GameObject ctrlRs = NodeHelper.Find("d_wpnRS", R.gameObject);
                        if (ctrlRs != null)
                        {
                            wt.AddTransform(ctrlRs.transform);
                        }
                        GameObject ctrlRe = NodeHelper.Find("d_wpnRE", R.gameObject);
                        if (ctrlRe != null)
                        {
                            wt.AddTransform(ctrlRe.transform);
                        }
                        BoxCollider box = R.GetComponentInChildren <BoxCollider>();
                        if (box != null)
                        {
                            box.enabled          = false;
                            box.gameObject.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
                            weaponDamage.Add(box);
                        }
                        else
                        {
                            Debug.LogError("新增武器上找不到碰撞盒[除了暗器火枪飞轮外都应该有碰撞盒]");
                        }

                        if (owner != null)
                        {
                            wt.Init(owner);
                            trail.Add(wt);
                        }
                        else
                        {
                            GameObject.Destroy(wt);
                        }
                    }
                }
            }
            Weapon = item;
        }
        else
        {
            Debug.LogError("if you want equip you must call unequip first if the weapon exist");
        }

        if (owner != null && owner.Stealth)
        {
            MeshRenderer[] mrl = LP.gameObject.GetComponentsInChildren <MeshRenderer>();
            for (int i = 0; i < mrl.Length; i++)
            {
                if (!mrl[i].enabled)
                {
                    continue;
                }
                for (int j = 0; j < mrl[i].materials.Length; j++)
                {
                    mrl[i].materials[j].SetFloat("_Alpha", 0.2f);
                }
            }

            MeshRenderer[] mrr = RP.gameObject.GetComponentsInChildren <MeshRenderer>();
            for (int i = 0; i < mrr.Length; i++)
            {
                if (!mrr[i].enabled)
                {
                    continue;
                }
                for (int j = 0; j < mrr[i].materials.Length; j++)
                {
                    mrr[i].materials[j].SetFloat("_Alpha", 0.2f);
                }
            }
        }
    }
Beispiel #11
0
    public void LoadMap(int level)
    {
        LevelData lev = DataMgr.Ins.GetLevelData(level);
        DesFile   des = DesLoader.Ins.Load(lev.sceneItems);
        GMBFile   gmb = GMBLoader.Ins.Load(lev.sceneItems);

        if (lev == null || des == null || gmb == null)
        {
            Debug.LogError("can not find");
            return;
        }
        LoadDesMap(lev.sceneItems);
        return;

        //return;
        bool generateFile = true;

        if (!System.IO.Directory.Exists("Assets/Materials/" + level + "/"))
        {
            System.IO.Directory.CreateDirectory("Assets/Materials/" + level + "/");
            //generateFile = true;
        }

        //Material[] mat = new Material[gmb.ShaderCount];
        //for (int x = 0; x < gmb.ShaderCount; x++)
        //{
        //    //mat[x] = new Material
        //    //gmb.shader[x].
        //}
        string shaders = "Unlit/Texture";//因为场景如果都不接受光照,那么会显得没什么氛围

        shaders = "Mobile/Diffuse";
        Material[] mat = new Material[gmb.TexturesCount];
        for (int x = 0; x < gmb.TexturesCount; x++)
        {
            mat[x] = new Material(Shader.Find(shaders));
            string tex = gmb.TexturesNames[x];
            int    del = tex.LastIndexOf('.');
            if (del != -1)
            {
                tex = tex.Substring(0, del);
            }
            Texture texture = Resources.Load <Texture>(tex);
            //这里的贴图可能是一个ifl文件,这种先不考虑,手动修改
            if (texture == null)
            {
                Debug.LogError("texture miss:" + tex);
            }
            mat[x].SetTexture("_MainTex", texture);
            mat[x].name = string.Format("{0:D2}_{1:D2}", level, x);
            if (generateFile)
            {
                //AssetDatabase.CreateAsset(mat[x], "Assets/Materials/" + level + "/" + mat[x].name + ".mat");
                //AssetDatabase.Refresh();
            }
        }

        while (true)
        {
            for (int i = 0; i < Mathf.Min(des.SceneItems.Count, gmb.SceneObjectsCount); i++)
            {
                //if (des.SceneItems[i].name != "ian1633")
                //    continue;

                GameObject objMesh = new GameObject();
                objMesh.name = des.SceneItems[i].name;
                objMesh.transform.localRotation = Quaternion.identity;
                objMesh.transform.localPosition = Vector3.zero;
                objMesh.transform.localScale    = Vector3.one;
                bool realObject = false;//是不是正常物体,虚拟体无需设置材质球之类
                for (int j = 0; j < gmb.SceneObjectsCount; j++)
                {
                    if (gmb.mesh[j].name == objMesh.name && j == i)
                    {
                        realObject = true;
                        Mesh w = new Mesh();
                        //前者子网格编号,后者 索引缓冲区
                        SortedDictionary <int, List <int> > tr = new SortedDictionary <int, List <int> >();
                        List <Vector3> ve  = new List <Vector3>();
                        List <Vector2> uv  = new List <Vector2>();
                        List <Vector3> nor = new List <Vector3>();
                        List <Color>   col = new List <Color>();
                        for (int k = 0; k < gmb.mesh[j].faces.Count; k++)
                        {
                            int key = gmb.mesh[j].faces[k].material;
                            if (tr.ContainsKey(key))
                            {
                                tr[key].Add(gmb.mesh[j].faces[k].triangle[0]);
                                tr[key].Add(gmb.mesh[j].faces[k].triangle[1]);
                                tr[key].Add(gmb.mesh[j].faces[k].triangle[2]);
                            }
                            else
                            {
                                tr.Add(key, new List <int>());
                                tr[key].Add(gmb.mesh[j].faces[k].triangle[0]);
                                tr[key].Add(gmb.mesh[j].faces[k].triangle[1]);
                                tr[key].Add(gmb.mesh[j].faces[k].triangle[2]);
                            }
                        }
                        for (int k = 0; k < gmb.mesh[j].vertices.Count; k++)
                        {
                            Vector3    vecLocalToWorld = des.SceneItems[i].pos;
                            Quaternion quatToWorld     = des.SceneItems[i].quat;

//                            Object ian1633
//{
//                                Position: -894.278 - 2506.653 51.167
//  Quaternion: 0.000 0.000 0.000 1.000
//  TextureAnimation: 0 0.000 0.000
//  Custom:
//                                {
//                                }
//                            }
//                            */
//                           Vector3 vecLocalToWorld = new Vector3(-894.278f, -2506.653f, 51.167f);
//                            Quaternion quatToWorld = new Quaternion(0f, 0f, 0f, 1f);

//                            Vector3 vecWorldToLocal = new Vector3(-vecLocalToWorld.x, -vecLocalToWorld.z, -vecLocalToWorld.y);
//                            Quaternion quat = new Quaternion(-quatToWorld.y, -quatToWorld.w, -quatToWorld.z, quatToWorld.x);
                            Vector3 vecWorldToLocal = new Vector3(-vecLocalToWorld.x, -vecLocalToWorld.y, -vecLocalToWorld.z);
                            Vector3 vec             = gmb.mesh[j].vertices[k].pos + vecWorldToLocal;
                            vec = Quaternion.Inverse(quatToWorld) * vec;
                            //这个是世界坐标,要变换到自身坐标系来。
                            //ve.Add(gmb.mesh[j].vertices[k].pos);
                            ve.Add(vec);
                            uv.Add(gmb.mesh[j].vertices[k].uv);
                            col.Add(gmb.mesh[j].vertices[k].color);
                            nor.Add(gmb.mesh[j].vertices[k].normal);
                        }
                        w.SetVertices(ve);
                        w.uv           = uv.ToArray();
                        w.subMeshCount = tr.Count;
                        int ss = 0;
                        //查看这个网格使用了哪些材质,然后把这几个材质取出来
                        List <Material> targetMat = new List <Material>();
                        foreach (var each in tr)
                        {
                            w.SetIndices(each.Value.ToArray(), MeshTopology.Triangles, ss++);
                            if (each.Key >= 0 && each.Key < gmb.shader.Length)
                            {
                                int materialIndex = gmb.shader[each.Key].TextureArg0;
                                if (materialIndex >= 0 && materialIndex < mat.Length)
                                {
                                    targetMat.Add(mat[materialIndex]);
                                }
                                else
                                {
                                    //即使没有贴图,也存在材质
                                    Material defaults = new Material(Shader.Find("Mobile/Diffuse"));
                                    defaults.name = string.Format("{0:D2}_{1:D2}", level, materialIndex);
                                    if (generateFile)
                                    {
                                        //AssetDatabase.CreateAsset(defaults, "Assets/Materials/" + level + "/" + defaults.name + ".mat");
                                        //AssetDatabase.Refresh();
                                    }
                                    targetMat.Add(defaults);
                                }
                            }
                        }
                        MeshRenderer mr = objMesh.AddComponent <MeshRenderer>();
                        MeshFilter   mf = objMesh.AddComponent <MeshFilter>();
                        mf.sharedMesh         = w;
                        mf.sharedMesh.colors  = col.ToArray();
                        mf.sharedMesh.normals = nor.ToArray();
                        mf.sharedMesh.RecalculateBounds();
                        mf.sharedMesh.RecalculateNormals();
                        mr.materials = targetMat.ToArray();
                        string vis = "";
                        if (des.SceneItems[i].ContainsKey("visible", out vis))
                        {
                            if (vis == "0")
                            {
                                mr.enabled = false;
                                mr.gameObject.AddComponent <MeshCollider>();
                            }
                        }

                        /*
                         * animation=sin
                         * deformsize=5
                         * deformfreq=0.25
                         * deformrange=5.0
                         */
                        string block = "";
                        if (des.SceneItems[i].ContainsKey("blockplayer", out block))
                        {
                            if (block == "no")
                            {
                            }
                            else
                            {
                                mr.gameObject.AddComponent <MeshCollider>();
                            }
                        }
                        else
                        {
                            mr.gameObject.AddComponent <MeshCollider>();
                        }
                        break;
                    }
                }

                objMesh.transform.SetParent(transform);
                objMesh.transform.localRotation = des.SceneItems[i].quat;
                objMesh.transform.localPosition = des.SceneItems[i].pos;
                //if (objMesh.GetComponent<MeshFilter>() != null && generateFile)
                //{
                //    AssetDatabase.CreateAsset(objMesh.GetComponent<MeshFilter>().sharedMesh, "Assets/Materials/" + level + "/" + des.SceneItems[i].name + ".asset");
                //    AssetDatabase.Refresh();
                //}
                //yield return 0;
            }
            return;
            //yield break;
        }
    }