Example #1
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int linkCount;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX IK
                this.boneTargetIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
                this.Loop            = br.ReadInt32();
                this.RadianLimit     = br.ReadSingle();

                linkCount = br.ReadInt32();
            }
            else
            { //PMD IK
                this.boneTargetIndex = br.ReadUInt16();
                linkCount            = (int)br.ReadByte();

                this.Loop        = br.ReadUInt16();
                this.RadianLimit = br.ReadSingle();
            }

            for (int i = 0; i < linkCount; i++)
            {
                PMXIKLink link = new PMXIKLink(this.Model, this);
                link.LoadFromStream(br, importSettings);
                this.IKLinks.Add(link);
            }
        }
Example #2
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
            this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

            byte flag = br.ReadByte(); //Ignored will be automatically generated on export.

            int refCount = br.ReadInt32();

            for (int i = 0; i < refCount; i++)
            {
                byte refType = br.ReadByte();
                switch (refType)
                {
                case PMXDisplaySlot.REF_IDENTIFY_BONE:
                    int boneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
                    this.References.Add(this.Model.Bones[boneIndex]);
                    break;

                case PMXDisplaySlot.REF_IDENTIFY_MORPH:
                    int morphIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.MorphIndexLength);
                    this.References.Add(this.Model.Morphs[morphIndex]);
                    break;
                }
            }
        }
Example #3
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int mtIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.MaterialIndexLength);

            if (mtIndex < 0)
            {
                this.Material = null;
            }
            else
            {
                this.Material = this.Model.Materials[mtIndex];
            }

            this.Type = (MaterialMorphOffsetType)(int)br.ReadByte();

            this.Diffuse  = PMXColorRGBA.LoadFromStreamStatic(br);
            this.Specular = PMXColorRGBA.LoadFromStreamStatic(br);
            this.Ambient  = PMXColorRGB.LoadFromStreamStatic(br);

            this.EdgeColor = PMXColorRGBA.LoadFromStreamStatic(br);
            this.EdgeSize  = br.ReadSingle();

            this.TextureFactor       = PMXColorRGBA.LoadFromStreamStatic(br);
            this.SphereTextureFactor = PMXColorRGBA.LoadFromStreamStatic(br);
            this.ToonTextureFactor   = PMXColorRGBA.LoadFromStreamStatic(br);
        }
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int vtxIndex;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX
                vtxIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.VertexIndexLength);
            }
            else
            { //PMD
                vtxIndex = br.ReadInt32();

                if (importSettings.BaseMorph != null)
                {
                    if (vtxIndex < importSettings.BaseMorph.Offsets.Count)
                    {
                        PMXMorphOffsetVertex mov = (PMXMorphOffsetVertex)importSettings.BaseMorph.Offsets[vtxIndex];
                        vtxIndex = this.Model.Vertices.IndexOf(mov.Vertex);
                    }
                }
            }

            this.Vertex      = this.Model.Vertices[vtxIndex];
            this.Translation = PMXVector3.LoadFromStreamStatic(br);
        }
Example #5
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int vtxIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.VertexIndexLength);

            this.Vertex         = this.Model.Vertices[vtxIndex];
            this.UVTranslation  = PMXVector2.LoadFromStreamStatic(br);
            this.UVTranslation2 = PMXVector2.LoadFromStreamStatic(br);
        }
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int boneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);

            this.Bone        = this.Model.Bones[boneIndex];
            this.Translation = PMXVector3.LoadFromStreamStatic(br);
            this.Rotation    = PMXQuaternion.LoadFromStreamStatic(br);
        }
Example #7
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            base.LoadFromStream(br, importSettings);

            this.C  = PMXVector3.LoadFromStreamStatic(br);
            this.R0 = PMXVector3.LoadFromStreamStatic(br);
            this.R1 = PMXVector3.LoadFromStreamStatic(br);
        }
 public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
 {
     this.bone1Index  = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
     this.bone2Index  = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
     this.bone3Index  = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
     this.bone4Index  = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
     this.Bone1Weight = br.ReadSingle();
     this.Bone2Weight = br.ReadSingle();
     this.Bone3Weight = br.ReadSingle();
     this.Bone4Weight = br.ReadSingle();
 }
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            byte[] buffer = new byte[2];
            br.BaseStream.Read(buffer, 0, 2);

            for (int i = 0; i < 2; i++)
            {
                buffer[i] = (byte)(~((int)buffer[i]) & 0xFF);
            }

            this._noCollissionGroup = new BitArray(buffer);
        }
Example #10
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            this.Position = PMXVector3.LoadFromStreamStatic(br);
            this.Normals  = PMXVector3.LoadFromStreamStatic(br);
            this.UV       = PMXVector2.LoadFromStreamStatic(br);


            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX format
                for (int i = 0; i < importSettings.ExtendedUV; i++)
                {
                    this.AddedUVs.Add(PMXQuaternion.LoadFromStreamStatic(br));
                }

                byte deformType = br.ReadByte();

                switch (deformType)
                {
                case PMXBaseDeform.DEFORM_IDENTIFY_BDEF1:
                    this.Deform = new PMXVertexDeformBDEF1(this.Model, this);
                    break;

                case PMXBaseDeform.DEFORM_IDENTIFY_BDEF2:
                    this.Deform = new PMXVertexDeformBDEF2(this.Model, this);
                    break;

                case PMXBaseDeform.DEFORM_IDENTIFY_BDEF4:
                    this.Deform = new PMXVertexDeformBDEF4(this.Model, this);
                    break;

                case PMXBaseDeform.DEFORM_IDENTIFY_SDEF:
                    this.Deform = new PMXVertexDeformSDEF(this.Model, this);
                    break;

                case PMXBaseDeform.DEFORM_IDENTIFY_QDEF:
                default:
                    this.Deform = new PMXVertexDeformQDEF(this.Model, this);
                    break;
                }
                this.Deform.LoadFromStream(br, importSettings);

                this.OutlineMagnification = br.ReadSingle();
            }
            else
            { //PMD format
                this.Deform = PMXVertexDeformBDEF2.DeformFromPMDFile(this.Model, this, br);

                this.OutlineMagnification = ((br.ReadByte() == 0) ? 1.0f : 0.0f);
            }
        }
Example #11
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int boneIndex;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            {
                this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
                this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

                boneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
            }
            else
            {
                this.NameJP = PMDParser.ReadString(br, 20, importSettings.TextEncoding);
                this.NameEN = this.NameJP;

                boneIndex = br.ReadInt16();
            }

            if (boneIndex < 0)
            {
                this.Bone = null;
            }
            else
            {
                this.Bone = this.Model.Bones[boneIndex];
            }

            this.CollissionGroup = br.ReadByte();
            this.NoCollissionGroups.LoadFromStream(br, importSettings);

            this.Shape      = (BodyShape)(int)br.ReadByte();
            this._shapeSize = PMXVector3.LoadFromStreamStatic(br);

            this.Position = PMXVector3.LoadFromStreamStatic(br);
            this.Rotation = PMXVector3.LoadFromStreamStatic(br);

            this.Mass           = br.ReadSingle();
            this.LinearDamping  = br.ReadSingle();
            this.AngularDamping = br.ReadSingle();
            this.Repulsion      = br.ReadSingle();
            this.Friction       = br.ReadSingle();

            this.Type = (BodyType)(int)br.ReadByte();

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMD && this.Bone != null)
            { //PMD location fix
                this.Position += this.Bone.Position;
            }
        }
Example #12
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX format
                this.boneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);

                this.HasLimits = (br.ReadByte() == 1);
                if (this.HasLimits)
                {
                    this.Minimum = PMXVector3.LoadFromStreamStatic(br);
                    this.Maximum = PMXVector3.LoadFromStreamStatic(br);
                }
            }
            else
            {
                this.boneIndex = br.ReadUInt16();
            }
        }
Example #13
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int index1, index2, index3;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX
                index1 = PMXParser.ReadIndex(br, importSettings.BitSettings.VertexIndexLength);
                index2 = PMXParser.ReadIndex(br, importSettings.BitSettings.VertexIndexLength);
                index3 = PMXParser.ReadIndex(br, importSettings.BitSettings.VertexIndexLength);
            }
            else
            { //PMD
                index1 = (int)br.ReadUInt16();
                index2 = (int)br.ReadUInt16();
                index3 = (int)br.ReadUInt16();
            }

            this.Vertex1 = this.Model.Vertices[index1];
            this.Vertex2 = this.Model.Vertices[index2];
            this.Vertex3 = this.Model.Vertices[index3];
        }
Example #14
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int rigidBodyIndexA, rigidBodyIndexB;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            {
                this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
                this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

                this.Type = (JointType)(int)br.ReadByte();

                rigidBodyIndexA = PMXParser.ReadIndex(br, importSettings.BitSettings.RigidBodyIndexLength);
                rigidBodyIndexB = PMXParser.ReadIndex(br, importSettings.BitSettings.RigidBodyIndexLength);
            }
            else
            {
                this.NameJP = PMDParser.ReadString(br, 20, importSettings.TextEncoding);
                this.NameEN = this.NameJP;

                this.Type = JointType.SpringSixDOF;

                rigidBodyIndexA = br.ReadInt32();
                rigidBodyIndexB = br.ReadInt32();
            }


            this.RigidBodyA = this.Model.RigidBodies[rigidBodyIndexA];
            this.RigidBodyB = this.Model.RigidBodies[rigidBodyIndexB];

            this.Position                  = PMXVector3.LoadFromStreamStatic(br);
            this.Rotation                  = PMXVector3.LoadFromStreamStatic(br);
            this.TranslationLimitMin       = PMXVector3.LoadFromStreamStatic(br);
            this.TranslationLimitMax       = PMXVector3.LoadFromStreamStatic(br);
            this.RotationLimitMin          = PMXVector3.LoadFromStreamStatic(br);
            this.RotationLimitMax          = PMXVector3.LoadFromStreamStatic(br);
            this.SpringConstantTranslation = PMXVector3.LoadFromStreamStatic(br);
            this.SpringConstantRotation    = PMXVector3.LoadFromStreamStatic(br);
        }
Example #15
0
 /// <summary>
 /// Reads this part from an existing model file.
 /// </summary>
 /// <param name="br">Reader for the model file</param>
 /// <param name="importSettings">Settings for the import</param>
 public abstract void LoadFromStream(BinaryReader br, MMDImportSettings importSettings);
Example #16
0
 public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
 {
     this.morphTargetIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.MorphIndexLength);
     this.Strength         = br.ReadSingle();
 }
Example #17
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int  offsets;
            byte morphType;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX format
                this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
                this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

                this.Panel = (PanelType)(int)br.ReadByte();
                morphType  = br.ReadByte();

                offsets = br.ReadInt32();
            }
            else
            { //PMD format
                this.NameJP = PMDParser.ReadString(br, 20, importSettings.TextEncoding);

                offsets    = br.ReadInt32();
                this.Panel = (PanelType)(int)br.ReadByte();

                morphType = PMXMorph.MORPH_IDENTIFY_VERTEX;
            }

            for (int i = 0; i < offsets; i++)
            {
                PMXMorphOffsetBase mb = null;

                switch (morphType)
                {
                case PMXMorph.MORPH_IDENTIFY_GROUP:
                    mb = new PMXMorphOffsetGroup(this.Model, this);
                    break;

                case PMXMorph.MORPH_IDENTIFY_VERTEX:
                    mb = new PMXMorphOffsetVertex(this.Model, this);
                    break;

                case PMXMorph.MORPH_IDENTIFY_BONE:
                    mb = new PMXMorphOffsetBone(this.Model, this);
                    break;

                case PMXMorph.MORPH_IDENTIFY_UV:
                    mb = new PMXMorphOffsetUV(this.Model, this, PMXMorphOffsetUV.UVAddIndexType.UV);
                    break;

                case PMXMorph.MORPH_IDENTIFY_UV_EXTENDED1:
                    mb = new PMXMorphOffsetUV(this.Model, this, PMXMorphOffsetUV.UVAddIndexType.AddUV1);
                    break;

                case PMXMorph.MORPH_IDENTIFY_UV_EXTENDED2:
                    mb = new PMXMorphOffsetUV(this.Model, this, PMXMorphOffsetUV.UVAddIndexType.AddUV2);
                    break;

                case PMXMorph.MORPH_IDENTIFY_UV_EXTENDED3:
                    mb = new PMXMorphOffsetUV(this.Model, this, PMXMorphOffsetUV.UVAddIndexType.AddUV3);
                    break;

                case PMXMorph.MORPH_IDENTIFY_UV_EXTENDED4:
                    mb = new PMXMorphOffsetUV(this.Model, this, PMXMorphOffsetUV.UVAddIndexType.AddUV4);
                    break;

                case PMXMorph.MORPH_IDENTIFY_MATERIAL:
                    mb = new PMXMorphOffsetMaterial(this.Model, this);
                    break;

                default:
                    throw new InvalidDataException("Unknown morph type " + morphType);
                }
                mb.LoadFromStream(br, importSettings);
                this.Offsets.Add(mb);
            }
        }
Example #18
0
        public static PMXModel LoadFromPMXFile(string pmxFile, PMXModelDescriptor modelDescriptor)
        {
            FileStream fs = new FileStream(pmxFile, FileMode.Open, FileAccess.Read, FileShare.Read);

            byte[] buffer = new byte[3];
            fs.Read(buffer, 0, 3);
            string head = Encoding.ASCII.GetString(buffer);

            fs.Seek(1, SeekOrigin.Current);

            if (head != "PMX")
            {
                throw new Exception("Not a PMX file!");
            }

            BinaryReader br = new BinaryReader(fs);

            float PMXVersion = br.ReadSingle();

            if (PMXVersion != 2.0f && PMXVersion != 2.1f)
            {
                throw new Exception("Unsupported PMX Version!");
            }

            byte flags = br.ReadByte();

            if (flags != 8)
            {
                throw new Exception("Invalid PMX bytes version!");
            }

            MMDImportSettings  settings = new MMDImportSettings(MMDImportSettings.ModelFormat.PMX);
            List <PMXBasePart> allParts = new List <PMXBasePart>();

            byte text_encoding = br.ReadByte();

            settings.TextEncoding =
                (text_encoding == 1 ? Encoding.UTF8 :
                 (text_encoding == 0 ? Encoding.Unicode : Encoding.GetEncoding(932)));

            settings.ExtendedUV      = br.ReadByte();
            settings.ClassDescriptor = modelDescriptor;

            PMXModel md = new PMXModel();

            settings.BitSettings.VertexIndexLength    = br.ReadByte();
            settings.BitSettings.TextureIndexLength   = br.ReadByte();
            settings.BitSettings.MaterialIndexLength  = br.ReadByte();
            settings.BitSettings.BoneIndexLength      = br.ReadByte();
            settings.BitSettings.MorphIndexLength     = br.ReadByte();
            settings.BitSettings.RigidBodyIndexLength = br.ReadByte();

            md.NameJP        = PMXParser.ReadString(br, settings.TextEncoding);
            md.NameEN        = PMXParser.ReadString(br, settings.TextEncoding);
            md.DescriptionJP = PMXParser.ReadString(br, settings.TextEncoding);
            md.DescriptionEN = PMXParser.ReadString(br, settings.TextEncoding);


            //Vertices
            uint vertexCount = br.ReadUInt32();

            for (int i = 0; i < vertexCount; i++)
            {
                PMXVertex v = (PMXVertex)Activator.CreateInstance(modelDescriptor.VertexType.StoredType, new object[] { md });
                v.LoadFromStream(br, settings);
                md.Vertices.Add(v);
                allParts.Add(v);
            }

            //Triangles
            uint vertexRefCount = br.ReadUInt32();

            if (vertexRefCount % 3 != 0)
            {
                throw new Exception("Invalid triangle count!");
            }

            uint triangleCount = vertexRefCount / 3;
            List <PMXTriangle> importTriangles = new List <PMXTriangle>();

            for (int i = 0; i < triangleCount; i++)
            {
                PMXTriangle t = (PMXTriangle)Activator.CreateInstance(modelDescriptor.TriangleType.StoredType, new object[] { md });
                t.LoadFromStream(br, settings);
                importTriangles.Add(t);
                allParts.Add(t);
            }

            //Textures
            uint textureCount = br.ReadUInt32();

            List <string> importTextures = new List <string>();

            for (int i = 0; i < textureCount; i++)
            {
                string tex = PMXParser.ReadString(br, settings.TextEncoding);
                importTextures.Add(tex);
            }
            string[] textures = importTextures.ToArray();

            //Materials
            uint materialCount = br.ReadUInt32();

            for (int i = 0; i < materialCount; i++)
            {
                PMXMaterial mt = (PMXMaterial)Activator.CreateInstance(modelDescriptor.MaterialType.StoredType, new object[] { md });
                mt.LoadFromStream(br, settings, textures, importTriangles);
                md.Materials.Add(mt);
                allParts.Add(mt);
            }

            if (importTriangles.Count > 0)
            {
                throw new InvalidDataException("Model materials don't cover all triangles!");
            }

            //Bones
            uint boneCount = br.ReadUInt32();

            for (int i = 0; i < boneCount; i++)
            {
                PMXBone bn = (PMXBone)Activator.CreateInstance(modelDescriptor.BoneType.StoredType, new object[] { md });
                bn.LoadFromStream(br, settings);
                md.Bones.Add(bn);
                allParts.Add(bn);
            }

            //Morphs
            uint morphCount = br.ReadUInt32();

            for (int i = 0; i < morphCount; i++)
            {
                PMXMorph mrph = (PMXMorph)Activator.CreateInstance(modelDescriptor.MorphType.StoredType, new object[] { md });
                mrph.LoadFromStream(br, settings);
                md.Morphs.Add(mrph);
                allParts.Add(mrph);
            }

            //Display frames
            md.DisplaySlots.Clear();
            uint displayCount = br.ReadUInt32();

            for (int i = 0; i < displayCount; i++)
            {
                PMXDisplaySlot ds = new PMXDisplaySlot(md);
                ds.LoadFromStream(br, settings);
                md.DisplaySlots.Add(ds);
                allParts.Add(ds);
            }

            //Rigid bodies
            uint rigidBodyCount = br.ReadUInt32();

            for (int i = 0; i < rigidBodyCount; i++)
            {
                PMXRigidBody rb = (PMXRigidBody)Activator.CreateInstance(modelDescriptor.RigidBodyType.StoredType, new object[] { md });
                rb.LoadFromStream(br, settings);
                md.RigidBodies.Add(rb);
                allParts.Add(rb);
            }

            //Joints
            uint jointsCount = br.ReadUInt32();

            for (int i = 0; i < jointsCount; i++)
            {
                PMXJoint jt = (PMXJoint)Activator.CreateInstance(modelDescriptor.JointType.StoredType, new object[] { md });
                jt.LoadFromStream(br, settings);
                md.Joints.Add(jt);
                allParts.Add(jt);
            }

            br.BaseStream.Close();

            br = null;
            fs = null;

            foreach (PMXBasePart part in allParts)
            {
                part.FinaliseAfterImport();
            }

            return(md);
        }
Example #19
0
        /// <summary>
        /// Loads a model from a PMD file and converts it to PMX internally.
        /// </summary>
        /// <param name="pmdFile">PMD file name</param>
        /// <param name="modelDescriptor">Classes to use during import</param>
        /// <returns></returns>
        public static PMXModel LoadFromPMDFile(string pmdFile, PMXModelDescriptor modelDescriptor)
        {
            FileStream fs = new FileStream(pmdFile, FileMode.Open, FileAccess.Read, FileShare.Read);

            byte[] buffer = new byte[3];
            fs.Read(buffer, 0, 3);
            string head = Encoding.ASCII.GetString(buffer);

            if (head != "Pmd")
            {
                throw new Exception("Not a PMD file!");
            }

            BinaryReader br = new BinaryReader(fs);

            float PMDVersion = br.ReadSingle();

            if (PMDVersion != 1.0f)
            {
                throw new Exception("Unsupported PMD Version!");
            }

            MMDImportSettings  settings = new MMDImportSettings(MMDImportSettings.ModelFormat.PMD);
            List <PMXBasePart> allParts = new List <PMXBasePart>();

            settings.ClassDescriptor = modelDescriptor;
            settings.TextEncoding    = Encoding.GetEncoding(932);
            settings.ExtendedUV      = 0;

            PMXModel md = new PMXModel();

            md.NameJP        = PMDParser.ReadString(br, 20, settings.TextEncoding);
            md.DescriptionJP = PMDParser.ReadString(br, 256, settings.TextEncoding);

            //Vertices
            uint vertexCount = br.ReadUInt32();

            for (int i = 0; i < vertexCount; i++)
            {
                PMXVertex v = (PMXVertex)Activator.CreateInstance(modelDescriptor.VertexType.StoredType, new object[] { md });
                v.LoadFromStream(br, settings);
                md.Vertices.Add(v);
                allParts.Add(v);
            }

            //Triangles
            uint vertexRefCount = br.ReadUInt32();

            if (vertexRefCount % 3 != 0)
            {
                throw new Exception("Invalid triangle count!");
            }

            uint triangleCount = vertexRefCount / 3;
            List <PMXTriangle> importTriangles = new List <PMXTriangle>();

            for (int i = 0; i < triangleCount; i++)
            {
                PMXTriangle t = (PMXTriangle)Activator.CreateInstance(modelDescriptor.TriangleType.StoredType, new object[] { md });
                t.LoadFromStream(br, settings);
                importTriangles.Add(t);
                allParts.Add(t);
            }

            //Materials
            uint materialCount = br.ReadUInt32();

            for (int i = 0; i < materialCount; i++)
            {
                PMXMaterial mt = (PMXMaterial)Activator.CreateInstance(modelDescriptor.MaterialType.StoredType, new object[] { md });
                mt.LoadFromStream(br, settings, null, importTriangles);
                md.Materials.Add(mt);

                mt.NameJP = "Material" + (i + 1).ToString(); //Initialise default names
                mt.NameEN = "Material" + (i + 1).ToString(); //Initialise default names

                allParts.Add(mt);
            }

            //Bones
            uint boneCount = (uint)br.ReadUInt16();

            for (int i = 0; i < boneCount; i++)
            {
                PMXBone bn = (PMXBone)Activator.CreateInstance(modelDescriptor.BoneType.StoredType, new object[] { md });
                bn.LoadFromStream(br, settings);
                md.Bones.Add(bn);
                allParts.Add(bn);
            }

            //PMD IKs - will be handled internally
            uint ikCount = (uint)br.ReadUInt16();

            for (int i = 0; i < ikCount; i++)
            {
                int     boneId = br.ReadUInt16();
                PMXBone bn     = md.Bones[boneId];
                bn.IK = new PMXIK(md, bn);
                bn.IK.LoadFromStream(br, settings);
            }

            //Morphs
            uint mCount = (uint)br.ReadUInt16();

            for (int i = 0; i < mCount; i++)
            {
                PMXMorph mrph = (PMXMorph)Activator.CreateInstance(modelDescriptor.MorphType.StoredType, new object[] { md });
                mrph.LoadFromStream(br, settings);

                if (mrph.NameJP == "base")
                {
                    settings.BaseMorph = mrph;
                }

                md.Morphs.Add(mrph);
                allParts.Add(mrph);
            }

            //Display groups - kinda insanely set up for PMD

            //Initialising root slot manually
            md.DisplaySlots[0].References.Add(md.Bones[0]);
            allParts.Add(md.DisplaySlots[0]);

            //Exp slot is initialised differently (cause why not?)
            uint miCount = (uint)br.ReadByte();

            for (int i = 0; i < miCount; i++)
            {
                int morphId = br.ReadUInt16();
                md.DisplaySlots[1].References.Add(md.Morphs[morphId]);
            }
            allParts.Add(md.DisplaySlots[1]);

            //Display slots.. guess.. work completely different as well - first of all: Let's gather the names!
            uint nameCount = (uint)br.ReadByte();

            for (int i = 0; i < nameCount; i++)
            {
                PMXDisplaySlot ds = new PMXDisplaySlot(md);
                ds.NameJP = PMDParser.ReadString(br, 50, settings.TextEncoding);
                md.DisplaySlots.Add(ds);
                allParts.Add(ds);
            }

            //We've got the names - now let's put the bones in
            uint boneIndexCount = (uint)br.ReadUInt32();

            for (int i = 0; i < boneIndexCount; i++)
            {
                ushort bI   = br.ReadUInt16();
                byte   slot = br.ReadByte();

                md.DisplaySlots[slot + 1].References.Add(md.Bones[bI]);
            }

            //Those were the display slots!

            //Does the model have English names?
            bool hasEnglishNames = false;

            if (br.BaseStream.Position < br.BaseStream.Length) //Not EOF yet
            {
                hasEnglishNames = (br.ReadByte() == 1);
            }

            if (hasEnglishNames) //Read English names
            {
                md.NameEN        = PMDParser.ReadString(br, 20, settings.TextEncoding);
                md.DescriptionEN = PMDParser.ReadString(br, 256, settings.TextEncoding);
                foreach (PMXBone bn in md.Bones)
                {
                    bn.NameEN = PMDParser.ReadString(br, 20, settings.TextEncoding);
                }
                bool firstMorph = true;
                foreach (PMXMorph mrph in md.Morphs)
                {
                    if (firstMorph && mrph.NameJP == "base")
                    {
                        continue;
                    }
                    mrph.NameEN = PMDParser.ReadString(br, 20, settings.TextEncoding);
                    firstMorph  = false;
                }
                for (int i = 2; i < md.DisplaySlots.Count; i++)
                {
                    md.DisplaySlots[i].NameEN = PMDParser.ReadString(br, 50, settings.TextEncoding);
                }
            }
            else //At least initialise them by using JP names
            {
                md.NameEN        = md.NameJP;
                md.DescriptionEN = md.DescriptionJP;
                foreach (PMXBone bn in md.Bones)
                {
                    bn.NameEN = bn.NameJP;
                }
                foreach (PMXMorph mrph in md.Morphs)
                {
                    mrph.NameEN = mrph.NameJP;
                }
                for (int i = 2; i < md.DisplaySlots.Count; i++)
                {
                    md.DisplaySlots[i].NameEN = md.DisplaySlots[i].NameJP;
                }
            }

            //Are there special toon textures?
            string[] defaultToons = new string[]
            {
                "toon01.bmp", "toon02.bmp", "toon03.bmp", "toon04.bmp", "toon05.bmp",
                "toon06.bmp", "toon07.bmp", "toon08.bmp", "toon09.bmp", "toon10.bmp"
            };
            string[] thisModelToons = new string[10];

            if (br.BaseStream.Position < br.BaseStream.Length) //Not EOF yet
            {
                for (int i = 0; i < 10; i++)
                {
                    thisModelToons[i] = PMDParser.ReadString(br, 100, settings.TextEncoding);
                }
            }
            else
            {
                Array.Copy(defaultToons, thisModelToons, 10);
            }

            //Does the PMD file have physics?
            if (br.BaseStream.Position < br.BaseStream.Length) //Not EOF yet
            {
                //Rigid bodies
                uint rigidBodyCount = br.ReadUInt32();

                for (int i = 0; i < rigidBodyCount; i++)
                {
                    PMXRigidBody rb = (PMXRigidBody)Activator.CreateInstance(modelDescriptor.RigidBodyType.StoredType, new object[] { md });
                    rb.LoadFromStream(br, settings);
                    md.RigidBodies.Add(rb);
                    allParts.Add(rb);
                }

                //Joints
                uint jointsCount = br.ReadUInt32();

                for (int i = 0; i < jointsCount; i++)
                {
                    PMXJoint jt = (PMXJoint)Activator.CreateInstance(modelDescriptor.JointType.StoredType, new object[] { md });
                    jt.LoadFromStream(br, settings);
                    md.Joints.Add(jt);
                    allParts.Add(jt);
                }
            }

            foreach (PMXBasePart part in allParts)
            {
                part.FinaliseAfterImport();
            }

            foreach (PMXMaterial mt in md.Materials)
            {
                mt.AssignToonForPMD(defaultToons, thisModelToons);
            }

            foreach (PMXBone bn in md.Bones)
            {
                bn.ParsePMDTwist();
                bn.CreateLocalCoodinateAxisForPMD();
                bn.UpdatePMDIKs();
            }

            if (md.Morphs[0].NameJP == "base")
            {
                md.Morphs.RemoveAt(0);
            }

            br = null;
            fs.Close();
            fs = null;

            return(md);
        }
Example #20
0
 public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
 {
     this.LoadFromStream(br, importSettings, null, null);
 }
Example #21
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int ignoreIndex;

            this.LoadFromStream(br, importSettings, out ignoreIndex);
        }
Example #22
0
        public void LoadFromStream(BinaryReader br, MMDImportSettings importSettings, out int pmdIKIndex)
        {
            pmdIKIndex = -1;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX
                this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
                this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

                this.Position = PMXVector3.LoadFromStreamStatic(br);

                this.parentIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);

                this.Layer = br.ReadInt32();

                short flags = br.ReadInt16();

                this.HasChildBone = ((flags & PMXBone.BONE_TAILPOS_IS_BONE) != 0);
                if (this.HasChildBone)
                {
                    this.childBoneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
                }
                else
                {
                    this.ChildVector = PMXVector3.LoadFromStreamStatic(br);
                }

                this.Rotatable    = ((flags & PMXBone.BONE_CAN_ROTATE) != 0);
                this.Translatable = ((flags & PMXBone.BONE_CAN_TRANSLATE) != 0);
                this.Visible      = ((flags & PMXBone.BONE_IS_VISIBLE) != 0);
                this.Operating    = ((flags & PMXBone.BONE_CAN_MANIPULATE) != 0);

                bool extRotation    = ((flags & PMXBone.BONE_IS_EXTERNAL_ROTATION) != 0);
                bool extTranslation = ((flags & PMXBone.BONE_IS_EXTERNAL_TRANSLATION) != 0);

                int rotFlag = 0;
                if (extRotation)
                {
                    rotFlag |= 1;
                }
                if (extTranslation)
                {
                    rotFlag |= 2;
                }
                this.ExternalModificationType = (BoneExternalModificationType)rotFlag;
                if (this.ExternalModificationType != BoneExternalModificationType.None)
                {
                    this.externalBoneIndex  = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
                    this.ExternalBoneEffect = br.ReadSingle();
                }

                this.FixedAxis = ((flags & PMXBone.BONE_HAS_FIXED_AXIS) != 0);
                if (this.FixedAxis)
                {
                    this.AxisLimit = PMXVector3.LoadFromStreamStatic(br);
                }

                this.LocalCoordinates = ((flags & PMXBone.BONE_HAS_LOCAL_COORDINATE) != 0);
                if (this.LocalCoordinates)
                {
                    this.LocalCoordinatesX = PMXVector3.LoadFromStreamStatic(br);
                    this.LocalCoordinatesZ = PMXVector3.LoadFromStreamStatic(br);
                }

                this.HasExternalParent = ((flags & PMXBone.BONE_IS_EXTERNAL_PARENT_DEFORM) != 0);
                if (this.HasExternalParent)
                {
                    this.ExternalParentKey = br.ReadInt32();
                }

                this.TransformPhysicsFirst = ((flags & PMXBone.BONE_IS_AFTER_PHYSICS_DEFORM) != 0);

                bool isIKBone = ((flags & PMXBone.BONE_IS_IK) != 0);
                if (isIKBone)
                {
                    PMXIK ikData = new PMXIK(this.Model, this);
                    ikData.LoadFromStream(br, importSettings);
                    this.IK = ikData;
                }
                else
                {
                    this.IK = null;
                }
            }

            else
            { //PMD
                this.NameJP      = PMDParser.ReadString(br, 20, importSettings.TextEncoding);
                this.parentIndex = br.ReadInt16();

                this.HasChildBone   = true;
                this.childBoneIndex = br.ReadUInt16();

                byte type = br.ReadByte();

                ushort ikIndex = br.ReadUInt16();

                this.Position = PMXVector3.LoadFromStreamStatic(br);

                switch (type)
                {
                case PMD_BONE_TYPE_ROTATE:
                    //Default
                    break;

                case PMD_BONE_TYPE_ROTATE_MOVE:
                    this.Translatable = true;
                    break;

                case PMD_BONE_TYPE_IK:
                    //IK parameters will be initialised later
                    this.Translatable = true;
                    break;

                case PMD_BONE_TYPE_IK_CHILD:
                    //PMX doesn't even bother about these
                    break;

                case PMD_BONE_TYPE_EXTERNAL_ROTATOR:
                    this.ExternalModificationType = BoneExternalModificationType.Rotation;
                    this.externalBoneIndex        = (int)ikIndex;
                    break;

                case PMD_BONE_TYPE_IK_TARGET:
                    //PMX doesn't bother either
                    this.Visible = false;
                    break;

                case PMD_BONE_TYPE_INVISIBLE:
                    this.Visible = false;
                    break;

                case PMD_BONE_TYPE_TWIST:
                    //PMX handles these differently
                    this._isPMDTwist = true;
                    break;

                case PMD_BONE_TYPE_TWIST_INVISIBLE:
                    //PMX handles these differently
                    this._isPMDTwist = true;
                    this.Visible     = false;
                    break;
                }
            }
        }
Example #23
0
        public void LoadFromStream(BinaryReader br, MMDImportSettings importSettings, string[] textures, List <PMXTriangle> triangles)
        {
            if (textures == null)
            {
                textures = new string[0];
            }

            int triangleVerticesCount;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX format
                this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
                this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

                this.Diffuse        = PMXColorRGB.LoadFromStreamStatic(br);
                this.Alpha          = br.ReadSingle();
                this.Specular       = PMXColorRGB.LoadFromStreamStatic(br);
                this.SpecularFactor = br.ReadSingle();
                this.Ambient        = PMXColorRGB.LoadFromStreamStatic(br);

                byte flags = br.ReadByte();
                //Flag parsing
                //1st bit = double sided
                this.DoubleSided = ((flags & PMXMaterial.MATERIAL_DOUBLE_SIDED) != 0);
                //2nd bit = ground shadow
                this.GroundShadow = ((flags & PMXMaterial.MATERIAL_GROUND_SHADOW) != 0);
                //3rd bit - self shadow
                this.SelfShadow = ((flags & PMXMaterial.MATERIAL_SELF_SHADOW) != 0);
                //4th bit - self shadow+
                this.SelfShadowPlus = ((flags & PMXMaterial.MATERIAL_SELF_SHADOW_PLUS) != 0);
                //5th bit - has edge line
                this.EdgeEnabled = ((flags & PMXMaterial.MATERIAL_EDGE_ENABLED) != 0);
                //6th bit - shine with vertex colour
                this.VertexColor = ((flags & PMXMaterial.MATERIAL_VERTEX_COLOR) != 0);

                //7th and 8bit - Tri, Point or Line shadow
                int shadowType = ((flags & 0xC0) >> 6);
                this.GroundShadowType = (PMXGroundShadowType)shadowType;

                this.EdgeColor = PMXColorRGBA.LoadFromStreamStatic(br);
                this.EdgeSize  = br.ReadSingle();

                int diffIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.TextureIndexLength);
                this.DiffuseTexture = this.GetTextureFromIndex(diffIndex, textures);

                int sphereIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.TextureIndexLength);
                this.SphereTexture = this.GetTextureFromIndex(sphereIndex, textures);

                this.SphereMode = (PMXSphereMode)(int)(br.ReadByte());

                this.StandardToon = (br.ReadByte() == 1);
                if (this.StandardToon)
                {
                    this.StandardToonIndex = br.ReadSByte();
                }
                else
                {
                    int toonTexIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.TextureIndexLength);
                    this.NonStandardToonTexture = this.GetTextureFromIndex(toonTexIndex, textures);
                }

                this.Comment          = PMXParser.ReadString(br, importSettings.TextEncoding);
                triangleVerticesCount = br.ReadInt32();
            }
            else
            { //PMD format
                this.Diffuse        = PMXColorRGB.LoadFromStreamStatic(br);
                this.Alpha          = br.ReadSingle();
                this.SpecularFactor = br.ReadSingle();
                this.Specular       = PMXColorRGB.LoadFromStreamStatic(br);
                this.Ambient        = PMXColorRGB.LoadFromStreamStatic(br);

                this._pmdToonIndex = br.ReadSByte();

                byte flags = br.ReadByte();

                this.EdgeEnabled  = (flags == 1);
                this.GroundShadow = this.EdgeEnabled;

                triangleVerticesCount = br.ReadInt32();

                string textureFile = PMDParser.ReadString(br, 20, importSettings.TextEncoding);

                if (textureFile != null)
                {
                    string[] textureRefs = textureFile.Split(new char[] { '*' }, 2, StringSplitOptions.RemoveEmptyEntries);

                    this.DiffuseTexture = textureRefs[0];

                    if (textureRefs.Length > 1)
                    {
                        this.SphereTexture = textureRefs[1];
                    }
                }
            }



            if (triangleVerticesCount % 3 != 0)
            {
                throw new InvalidDataException("Invalid triangle format!");
            }

            if (triangles != null)
            {
                int triangleCount = triangleVerticesCount / 3;

                if (triangleCount > triangles.Count)
                {
                    throw new InvalidDataException("Model doesn't have enough triangles!");
                }

                this.Triangles = triangles.GetRange(0, triangleCount);
                triangles.RemoveRange(0, triangleCount);
            }
        }
Example #24
0
 public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
 {
     this.bone1Index = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
 }