public override void WriteToStream(BinaryWriter bw, MMDExportSettings exportSettings) { if (exportSettings.Format == MMDExportSettings.ModelFormat.PMX) { base.WriteToStream(bw, exportSettings); PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.Bone2, exportSettings)); bw.Write(this.Bone1Weight); } else { int b1i = PMXBone.CheckIndexInModel(this.Bone1, exportSettings); int b2i = PMXBone.CheckIndexInModel(this.Bone2, exportSettings); List <KeyValuePair <int, float> > sortKeys = new List <KeyValuePair <int, float> >(); if (b1i >= 0) { sortKeys.Add(new KeyValuePair <int, float>(b1i, this.Bone1Weight)); } if (b2i >= 0) { sortKeys.Add(new KeyValuePair <int, float>(b2i, 1.0f - this.Bone1Weight)); } this.ExportToPMDBase(sortKeys, bw); } }
public override void WriteToStream(BinaryWriter bw, MMDExportSettings exportSettings) { if (exportSettings.Format == MMDExportSettings.ModelFormat.PMX) { PMXParser.WriteString(bw, exportSettings.TextEncoding, this.NameJP); PMXParser.WriteString(bw, exportSettings.TextEncoding, this.NameEN); PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.Bone, exportSettings, true)); } else { PMDParser.WriteString(bw, 20, exportSettings.TextEncoding, this.NameEN); PMXParser.WriteIndex(bw, 2, PMXBone.CheckIndexInModel(this.Bone, exportSettings, true)); } bw.Write((byte)this.CollissionGroup); this.NoCollissionGroups.WriteToStream(bw, exportSettings); bw.Write((byte)(int)this.Shape); this._shapeSize.WriteToStream(bw); if (exportSettings.Format == MMDExportSettings.ModelFormat.PMD && this.Bone != null) { //PMD location fix PMXVector3 pos = new PMXVector3(this.Position.X, this.Position.Y, this.Position.Z); pos -= this.Bone.Position; pos.WriteToStream(bw); } else { this.Position.WriteToStream(bw); } this.Rotation.WriteToStream(bw); bw.Write(this.Mass); bw.Write(this.LinearDamping); bw.Write(this.AngularDamping); bw.Write(this.Repulsion); bw.Write(this.Friction); bw.Write((byte)(int)this.Type); }
public override void WriteToStream(BinaryWriter bw, MMDExportSettings exportSettings) { if (exportSettings.Format == MMDExportSettings.ModelFormat.PMX) { //PMX IK PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.Bone, exportSettings, true)); if (this.HasLimits) { bw.Write((byte)1); this.Minimum.WriteToStream(bw); this.Maximum.WriteToStream(bw); } else { bw.Write((byte)0); } } else { //PMD IK PMXParser.WriteIndex(bw, 2, PMXBone.CheckIndexInModel(this.Bone, exportSettings, true)); } }
public override void WriteToStream(BinaryWriter bw, MMDExportSettings exportSettings) { if (exportSettings.Format == MMDExportSettings.ModelFormat.PMX) { //PMX IK PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.Target, exportSettings, true)); bw.Write((Int32)this.Loop); bw.Write(this.RadianLimit); bw.Write((Int32)this.IKLinks.Count); } else { //PMD PMXParser.WriteIndex(bw, 2, PMXBone.CheckIndexInModel(this.Target, exportSettings, true)); bw.Write((byte)this.IKLinks.Count); bw.Write((UInt16)this.Loop); bw.Write(this.RadianLimit); } foreach (PMXIKLink link in this.IKLinks) { link.WriteToStream(bw, exportSettings); } }
/// <summary> /// Saves to a stream as PMD. /// </summary> /// <param name="fs"></param> public void SaveToStreamPMD(Stream stream) { List <string> requiredToons = new List <string>(); //List of textures to export string[] defaultToons = new string[] { "toon01.bmp", "toon02.bmp", "toon03.bmp", "toon04.bmp", "toon05.bmp", "toon06.bmp", "toon07.bmp", "toon08.bmp", "toon09.bmp", "toon10.bmp" }; int triangleCount = 0; foreach (PMXMaterial mat in this.Materials) { //this.AddToListIfRequired(requiredToons, mat.DiffuseTexture); if (mat.StandardToon) { this.AddToListIfRequired(requiredToons, defaultToons[mat.StandardToonIndex]); } else { this.AddToListIfRequired(requiredToons, mat.NonStandardToonTexture); } triangleCount += mat.Triangles.Count; } string[] toonFiles = new string[10]; for (int i = 0; i < 10; i++) { if (i < requiredToons.Count) { toonFiles[i] = requiredToons[i]; } else { toonFiles[i] = defaultToons[i]; } } MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); byte[] magic = Encoding.ASCII.GetBytes("Pmd"); ms.Write(magic, 0, 3); Random rnd = new Random(); int randomNumber = rnd.Next(); MMDExportSettings settings = new MMDExportSettings(MMDExportSettings.ModelFormat.PMD); settings.Model = this; settings.ExportHash = randomNumber; settings.TextEncoding = Encoding.GetEncoding(932); float version = 1.0f; bw.Write(version); PMDParser.WriteString(bw, 20, settings.TextEncoding, this.NameJP); PMDParser.WriteString(bw, 256, settings.TextEncoding, this.DescriptionJP); //Vertices bw.Write((int)this.Vertices.Count); int vtxIndex = 0; foreach (PMXVertex v in this.Vertices) { v.AddIndexForExport(settings, vtxIndex); v.WriteToStream(bw, settings); vtxIndex++; } //Triangles bw.Write((Int32)(triangleCount * 3)); foreach (PMXMaterial mat in this.Materials) { foreach (PMXTriangle t in mat.Triangles) { t.WriteToStream(bw, settings); } } //Materials bw.Write((Int32)this.Materials.Count); foreach (PMXMaterial mat in this.Materials) { mat.WriteToStream(bw, settings, toonFiles, defaultToons); } //Bones bw.Write((Int16)this.Bones.Count); List <PMXBone> ikBonesList = new List <PMXBone>(); foreach (PMXBone bn in this.Bones) { if (bn.IK != null) { ikBonesList.Add(bn); } } PMXBone[] ikBones = ikBonesList.ToArray(); foreach (PMXBone bn in this.Bones) { bn.WriteToStream(bw, settings, ikBones); } //PMD IKs bw.Write((Int16)ikBones.Length); foreach (PMXBone bn in ikBones) { PMXParser.WriteIndex(bw, 2, PMXBone.CheckIndexInModel(bn, settings, true)); bn.IK.WriteToStream(bw, settings); } //PMD Morphs List <PMXMorph> exportableMorphs = new List <PMXMorph>(); List <PMXVertex> baseVertices = new List <PMXVertex>(); foreach (PMXMorph mrph in this.Morphs) { bool exportable = true; foreach (PMXMorphOffsetBase mofb in mrph.Offsets) { if (!(mofb is PMXMorphOffsetVertex)) { exportable = false; } } if (exportable) { exportableMorphs.Add(mrph); foreach (PMXMorphOffsetBase mofb in mrph.Offsets) { PMXMorphOffsetVertex mofv = (PMXMorphOffsetVertex)mofb; if (!baseVertices.Contains(mofv.Vertex)) { baseVertices.Add(mofv.Vertex); } } } } settings.BaseMorphVertices = baseVertices; bw.Write((Int16)(exportableMorphs.Count + 1)); PMDParser.WriteString(bw, 20, settings.TextEncoding, "base"); bw.Write((Int32)baseVertices.Count); bw.Write((byte)0); foreach (PMXVertex vtx in baseVertices) { PMXParser.WriteIndex(bw, 4, PMXVertex.CheckIndexInModel(vtx, settings)); vtx.Position.WriteToStream(bw); } foreach (PMXMorph mrph in exportableMorphs) { mrph.WriteToStream(bw, settings); } //Display groups - kinda insanely set up for PMD //PMD doesn't have a root slot //Expression display slots List <PMXMorph> morphs = new List <PMXMorph>(); //List of facial expressions foreach (PMXDisplaySlot ds in this.DisplaySlots) { foreach (PMXBasePart reference in ds.References) { if (reference is PMXMorph) { morphs.Add((PMXMorph)reference); } } } bw.Write((byte)morphs.Count); foreach (PMXMorph mrph in morphs) { int morphId = this.Morphs.IndexOf(mrph) + 1; bw.Write((UInt16)morphId); } //Bone display slots bw.Write((byte)(this.DisplaySlots.Count - 2)); for (int dsidx = 2; dsidx < this.DisplaySlots.Count; dsidx++) { PMDParser.WriteString(bw, 50, settings.TextEncoding, this.DisplaySlots[dsidx].NameJP); } //We've got the names - now let's put the bones in uint totalBoneRefCount = 0; for (int dsidx = 2; dsidx < this.DisplaySlots.Count; dsidx++) { PMXDisplaySlot ds = this.DisplaySlots[dsidx]; foreach (PMXBasePart reference in ds.References) { if (reference is PMXBone) { totalBoneRefCount++; } } } bw.Write((uint)totalBoneRefCount); for (int dsidx = 2; dsidx < this.DisplaySlots.Count; dsidx++) { PMXDisplaySlot ds = this.DisplaySlots[dsidx]; foreach (PMXBasePart reference in ds.References) { if (reference is PMXBone) { PMXBone bref = (PMXBone)reference; bw.Write((UInt16)this.Bones.IndexOf(bref)); bw.Write((byte)(dsidx - 1)); } } } //Always write english names bw.Write((byte)1); PMDParser.WriteString(bw, 20, settings.TextEncoding, this.NameEN); PMDParser.WriteString(bw, 256, settings.TextEncoding, this.DescriptionEN); foreach (PMXBone bn in this.Bones) { PMDParser.WriteString(bw, 20, settings.TextEncoding, bn.NameEN); } foreach (PMXMorph mrph in exportableMorphs) { PMDParser.WriteString(bw, 20, settings.TextEncoding, mrph.NameEN); } for (int i = 2; i < this.DisplaySlots.Count; i++) { PMDParser.WriteString(bw, 50, settings.TextEncoding, this.DisplaySlots[i].NameEN); } //Toon files foreach (string toon in toonFiles) { PMDParser.WriteString(bw, 100, settings.TextEncoding, toon); } //Rigid bodies bw.Write((uint)this.RigidBodies.Count); foreach (PMXRigidBody rb in this.RigidBodies) { rb.WriteToStream(bw, settings); } //Joints bw.Write((uint)this.Joints.Count); foreach (PMXJoint jt in this.Joints) { jt.WriteToStream(bw, settings); } //Vertex restore foreach (PMXVertex v in this.Vertices) { v.RemoveIndexForExport(settings); } long length = ms.Position; ms.Seek(0, SeekOrigin.Begin); ms.CopyTo(stream); ms.Close(); ms = null; }
public override void WriteToStream(BinaryWriter bw, MMDExportSettings exportSettings) { PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.Bone, exportSettings, false)); this.Translation.WriteToStream(bw); this.Rotation.WriteToStream(bw); }
public void WriteToStream(BinaryWriter bw, MMDExportSettings exportSettings, PMXBone[] ikBones) { if (exportSettings.Format == MMDExportSettings.ModelFormat.PMX) { //PMX export short flags = 0x0000; flags |= (short)(this.HasChildBone ? PMXBone.BONE_TAILPOS_IS_BONE : 0x0000); flags |= (short)(this.Rotatable ? PMXBone.BONE_CAN_ROTATE : 0x0000); flags |= (short)(this.Translatable ? PMXBone.BONE_CAN_TRANSLATE : 0x0000); flags |= (short)(this.Visible ? PMXBone.BONE_IS_VISIBLE : 0x0000); flags |= (short)(this.Operating ? PMXBone.BONE_CAN_MANIPULATE : 0x0000); flags |= (short)((this.ExternalModificationType == BoneExternalModificationType.Both || this.ExternalModificationType == BoneExternalModificationType.Rotation) ? PMXBone.BONE_IS_EXTERNAL_ROTATION : 0x0000); flags |= (short)((this.ExternalModificationType == BoneExternalModificationType.Both || this.ExternalModificationType == BoneExternalModificationType.Translation) ? PMXBone.BONE_IS_EXTERNAL_TRANSLATION : 0x0000); flags |= (short)(this.FixedAxis ? PMXBone.BONE_HAS_FIXED_AXIS : 0x0000); flags |= (short)(this.LocalCoordinates ? PMXBone.BONE_HAS_LOCAL_COORDINATE : 0x0000); flags |= (short)(this.HasExternalParent ? PMXBone.BONE_IS_EXTERNAL_PARENT_DEFORM : 0x0000); flags |= (short)(this.TransformPhysicsFirst ? PMXBone.BONE_IS_AFTER_PHYSICS_DEFORM : 0x0000); flags |= (short)((this.IK != null) ? PMXBone.BONE_IS_IK : 0x0000); PMXParser.WriteString(bw, exportSettings.TextEncoding, this.NameJP); PMXParser.WriteString(bw, exportSettings.TextEncoding, this.NameEN); this.Position.WriteToStream(bw); PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.Parent, exportSettings, true)); bw.Write((Int32)this.Layer); bw.Write((Int16)flags); if (this.HasChildBone) { PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.ChildBone, exportSettings, true)); } else { this.ChildVector.WriteToStream(bw); } if (this.ExternalModificationType != BoneExternalModificationType.None) { PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.ExternalBone, exportSettings, true)); bw.Write(this.ExternalBoneEffect); } if (this.FixedAxis) { this.AxisLimit.WriteToStream(bw); } if (this.LocalCoordinates) { this.LocalCoordinatesX.WriteToStream(bw); this.LocalCoordinatesZ.WriteToStream(bw); } if (this.HasExternalParent) { bw.Write((Int32)this.ExternalParentKey); } if (this.IK != null) { this.IK.WriteToStream(bw, exportSettings); } } else { //PMD format PMDParser.WriteString(bw, 20, exportSettings.TextEncoding, this.NameJP); bw.Write((Int16)PMXBone.CheckIndexInModel(this.Parent, exportSettings, true)); if (this.HasChildBone) { bw.Write((Int16)PMXBone.CheckIndexInModel(this.ChildBone, exportSettings, true)); } else { bw.Write((Int16)(-1)); } byte type = PMD_BONE_TYPE_ROTATE; short ikIndex = 0; if (this.Translatable) { type = PMD_BONE_TYPE_ROTATE_MOVE; } if (this.IK != null) { type = PMD_BONE_TYPE_IK; } if (this.IK != null) { bool isIkTarget = false; foreach (PMXBone ikBone in ikBones) { foreach (PMXIKLink link in ikBone.IK.IKLinks) { if (link.Bone == this) { ikIndex = (Int16)PMXBone.CheckIndexInModel(ikBone, exportSettings, true); isIkTarget = true; break; } } if (isIkTarget) { break; } } if (isIkTarget) { type = (this.Visible ? PMD_BONE_TYPE_IK_CHILD:PMD_BONE_TYPE_IK_TARGET); } else if (!this.Visible) { type = PMD_BONE_TYPE_INVISIBLE; } else if (this.FixedAxis) { type = (this.Visible ? PMD_BONE_TYPE_TWIST : PMD_BONE_TYPE_TWIST_INVISIBLE); } else if (this.ExternalModificationType != BoneExternalModificationType.Both) { type = PMD_BONE_TYPE_EXTERNAL_ROTATOR; ikIndex = (Int16)PMXBone.CheckIndexInModel(this.ExternalBone, exportSettings, true); } } bw.Write(type); bw.Write(ikIndex); this.Position.WriteToStream(bw); } }
public override void WriteToStream(BinaryWriter bw, MMDExportSettings exportSettings) { PMXParser.WriteString(bw, exportSettings.TextEncoding, this.NameJP); PMXParser.WriteString(bw, exportSettings.TextEncoding, this.NameEN); bool isImportant = (this.Model.DisplaySlots.IndexOf(this) <= 1); //Root and EXP Displays if (isImportant) { bw.Write((byte)1); } else { bw.Write((byte)0); } bw.Write((Int32)this.References.Count); foreach (PMXBasePart rfr in this.References) { if (rfr is PMXBone) { bw.Write((byte)PMXDisplaySlot.REF_IDENTIFY_BONE); PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel((PMXBone)rfr, exportSettings, false)); } else if (rfr is PMXMorph) { bw.Write((byte)PMXDisplaySlot.REF_IDENTIFY_MORPH); PMXParser.WriteIndex(bw, exportSettings.BitSettings.MorphIndexLength, PMXMorph.CheckIndexInModel((PMXMorph)rfr, exportSettings, false)); } else { throw new InvalidDataException("Invalid reference in display slots. Only bones and morphs are supported!"); } } }