示例#1
0
        public override bool Load()
        {
            using (var strm = FileManager.Instance.Provider.OpenFile(mFileName))
            {
                var reader = new BinaryReader(strm);
                mHeader = reader.Read <M2Header>();

                if ((mHeader.GlobalFlags & 0x08) != 0)
                {
                    mRemapBlend = true;
                    var nBlendMaps   = reader.Read <int>();
                    var ofsBlendMaps = reader.Read <int>();
                    strm.Position = ofsBlendMaps;
                    mBlendMap     = reader.ReadArray <ushort>(nBlendMaps);
                }

                strm.Position = mHeader.OfsName;
                if (mHeader.LenName > 0)
                {
                    mModelName = Encoding.ASCII.GetString(reader.ReadBytes(mHeader.LenName - 1));
                }

                BoundingBox    = new BoundingBox(mHeader.BoundingBoxMin, mHeader.BoundingBoxMax);
                BoundingSphere = new BoundingSphere(Vector3.Zero, mHeader.BoundingRadius);

                GlobalSequences = ReadArrayOf <uint>(reader, mHeader.OfsGlobalSequences, mHeader.NGlobalSequences);
                Vertices        = ReadArrayOf <M2Vertex>(reader, mHeader.OfsVertices, mHeader.NVertices);

                for (var i = 0; i < Vertices.Length; ++i)
                {
                    var p = Vertices[i].position;
                    Vertices[i].position = new Vector3(p.X, -p.Y, p.Z);
                    Vertices[i].normal   = new Vector3(Vertices[i].normal.X, -Vertices[i].normal.Y, Vertices[i].normal.Z);
                }

                var textures = ReadArrayOf <M2Texture>(reader, mHeader.OfsTextures, mHeader.NTextures);
                mTextures = new Graphics.Texture[textures.Length];
                for (var i = 0; i < textures.Length; ++i)
                {
                    var tex = textures[i];
                    if (tex.type == 0 && tex.lenName > 0)
                    {
                        var texName =
                            Encoding.ASCII.GetString(ReadArrayOf <byte>(reader, tex.ofsName, tex.lenName - 1)).Trim();
                        mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture(texName);
                    }
                    else
                    {
                        mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture("default_texture");
                    }
                }

                LoadSkins(reader);
                LoadAnimations(reader);
            }

            return(true);
        }
示例#2
0
文件: M2File.cs 项目: veserine/Neo
        public override bool Load()
        {
            using (var strm = FileManager.Instance.Provider.OpenFile(mFileName))
            {
                var reader = new BinaryReader(strm);
                mHeader = reader.Read <M2Header>();

                BoundingRadius = mHeader.VertexRadius;

                if ((mHeader.GlobalFlags & 0x08) != 0)
                {
                    mRemapBlend = true;
                    var nBlendMaps   = reader.Read <int>();
                    var ofsBlendMaps = reader.Read <int>();
                    strm.Position = ofsBlendMaps;
                    mBlendMap     = reader.ReadArray <ushort>(nBlendMaps);
                }

                strm.Position = mHeader.OfsName;
                if (mHeader.LenName > 0)
                {
                    mModelName = Encoding.ASCII.GetString(reader.ReadBytes(mHeader.LenName - 1));
                }

                GlobalSequences = ReadArrayOf <uint>(reader, mHeader.OfsGlobalSequences, mHeader.NGlobalSequences);
                Vertices        = ReadArrayOf <M2Vertex>(reader, mHeader.OfsVertices, mHeader.NVertices);

                var minPos = new Vector3(float.MaxValue);
                var maxPos = new Vector3(float.MinValue);
                for (var i = 0; i < Vertices.Length; ++i)
                {
                    var p = Vertices[i].position;
                    p = new Vector3(p.X, -p.Y, p.Z);
                    Vertices[i].position = p;
                    Vertices[i].normal   = new Vector3(Vertices[i].normal.X, -Vertices[i].normal.Y, Vertices[i].normal.Z);
                    if (p.X < minPos.X)
                    {
                        minPos.X = p.X;
                    }
                    if (p.Y < minPos.Y)
                    {
                        minPos.Y = p.Y;
                    }
                    if (p.Z < minPos.Z)
                    {
                        minPos.Z = p.Z;
                    }
                    if (p.X > maxPos.X)
                    {
                        maxPos.X = p.X;
                    }
                    if (p.Y > maxPos.Y)
                    {
                        maxPos.Y = p.Y;
                    }
                    if (p.Z > maxPos.Z)
                    {
                        maxPos.Z = p.Z;
                    }
                }

                BoundingBox = new BoundingBox(minPos, maxPos);

                LoadCreatureVariations();

                var textures = ReadArrayOf <M2Texture>(reader, mHeader.OfsTextures, mHeader.NTextures);
                mTextures    = new Graphics.Texture[textures.Length];
                TextureInfos = new TextureInfo[textures.Length];
                for (var i = 0; i < textures.Length; ++i)
                {
                    var tex = textures[i];
                    if (tex.type == 0 && tex.lenName > 0)
                    {
                        var texName = Encoding.ASCII.GetString(ReadArrayOf <byte>(reader, tex.ofsName, tex.lenName - 1)).Trim();
                        mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture(texName);
                    }
                    else
                    {
                        switch ((TextureType)tex.type)
                        {
                        //case TextureType.Skin:
                        //    string skinTexName = $"{mDirectoryParts[1]}{mDirectoryParts[2]}NakedTorsoSkin00_{DisplayOptions.SkinId.ToString("00")}.blp";
                        //    skinTexName = Path.Combine(Path.GetDirectoryName(mFileName), skinTexName);
                        //    mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture(skinTexName);
                        //    break;

                        ////case TextureType.ObjectSkin:
                        ////    break;

                        //case TextureType.CharacterHair:
                        //    string hairTexName = $"{mDirectoryParts[1]}NakedTorsoSkin00_{DisplayOptions.SkinId.ToString("00")}.blp";
                        //    skinTexName = Path.Combine(Path.GetDirectoryName(mFileName), hairTexName);
                        //    mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture(skinTexName);
                        //    break;

                        case TextureType.MonsterSkin1:
                            if (DisplayOptions.TextureVariationFiles.Count > DisplayOptions.TextureVariation)
                            {
                                if (!string.IsNullOrEmpty(DisplayOptions.TextureVariationFiles[DisplayOptions.TextureVariation].Item1))
                                {
                                    mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture(DisplayOptions.TextureVariationFiles[DisplayOptions.TextureVariation].Item1);
                                }
                            }
                            break;

                        case TextureType.MonsterSkin2:
                            if (DisplayOptions.TextureVariationFiles.Count > DisplayOptions.TextureVariation)
                            {
                                if (!string.IsNullOrEmpty(DisplayOptions.TextureVariationFiles[DisplayOptions.TextureVariation].Item2))
                                {
                                    mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture(DisplayOptions.TextureVariationFiles[DisplayOptions.TextureVariation].Item2);
                                }
                            }
                            break;

                        case TextureType.MonsterSkin3:
                            if (DisplayOptions.TextureVariationFiles.Count > DisplayOptions.TextureVariation)
                            {
                                if (!string.IsNullOrEmpty(DisplayOptions.TextureVariationFiles[DisplayOptions.TextureVariation].Item3))
                                {
                                    mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture(DisplayOptions.TextureVariationFiles[DisplayOptions.TextureVariation].Item3);
                                }
                            }
                            break;

                        default:
                            mTextures[i] = Scene.Texture.TextureManager.Instance.GetTexture("default_texture");
                            break;
                        }
                    }

                    Graphics.Texture.SamplerFlagType samplerFlags;

                    if (tex.flags == 3)
                    {
                        samplerFlags = Graphics.Texture.SamplerFlagType.WrapBoth;
                    }
                    else if (tex.flags == 2)
                    {
                        samplerFlags = Graphics.Texture.SamplerFlagType.WrapV;
                    }
                    else if (tex.flags == 1)
                    {
                        samplerFlags = Graphics.Texture.SamplerFlagType.WrapU;
                    }
                    else
                    {
                        samplerFlags = Graphics.Texture.SamplerFlagType.ClampBoth;
                    }

                    TextureInfos[i] = new TextureInfo
                    {
                        Texture      = mTextures[i],
                        TextureType  = (TextureType)tex.type,
                        SamplerFlags = samplerFlags
                    };
                }

                LoadSkins(reader);
                LoadAnimations(reader);
            }

            return(true);
        }