BrawlCrate v0.41
Wii File Editor
Loading...
Searching...
No Matches
Public Types | Static Public Member Functions | Static Public Attributes | Properties | List of all members
BrawlLib.Modeling.PMDModel Class Reference

Public Types

enum  CoordinateType
 

Static Public Member Functions

static MDL0Node ImportModel (string filepath)
 
static void Export (MDL0Node model, string filename)
 
static void Read (BinaryReader reader, CoordinateType coordinate)
 
static void Write (BinaryWriter writer)
 
static unsafe void PMD2MDL0 (MDL0Node model)
 
static void AssignParent (MDL0BoneNode pBone, ModelBone child, MDL0BoneNode cBone, ModelBone parent)
 
static void MDL02PMD (MDL0Node model)
 

Static Public Attributes

static float _version = 1.0f
 
static ModelHeader _header
 
static ModelVertex[] _vertices
 
static ushort[] _faceIndices
 
static ModelMaterial[] _materials
 
static ModelBone[] _bones
 
static ModelIK[] _IKs
 
static ModelSkin[] _skins
 
static ushort[] _skinIndex
 
static ModelBoneDispName[] _boneDispNames
 
static ModelBoneDisp[] _boneDisps
 
static bool _expansion
 
static bool _toonExpansion
 
static string[] _toonFileNames
 
const int _numToonFileName = 10
 
static bool _physicsExpansion
 
static ModelRigidBody[] _rigidBodies
 
static ModelJoint[] _joints
 
static CoordinateType _coordinate
 

Properties

static float CoordZ [get]
 
static MDL0ShaderNode TexSpaShader [get]
 
static MDL0ShaderNode ColorSpaShader [get]
 
static MDL0ShaderNode TexShader [get]
 
static MDL0ShaderNode ColorShader [get]
 

Member Enumeration Documentation

◆ CoordinateType

144 {
145 //MMD standard coordinate system
146 RightHanded = 1,
147
148 //XNA standard coordinate system
149 LeftHanded = -1
150 }

Member Function Documentation

◆ AssignParent()

static void BrawlLib.Modeling.PMDModel.AssignParent ( MDL0BoneNode  pBone,
ModelBone  child,
MDL0BoneNode  cBone,
ModelBone  parent 
)
inlinestatic
954 {
955 if (pBone._entryIndex == child._parentBoneIndex)
956 {
957 //Link child to its parent
958 (cBone._parent = pBone)._children.Add(cBone);
959
960 //Convert the world point into a local point relative to the bone's parent
961 Matrix m = Matrix.TranslationMatrix(child._wPos) *
962 Matrix.Invert(Matrix.TranslationMatrix(parent._wPos));
963
964 //Derive to state and recalc bind matrices
965 cBone._bindState = m.Derive();
966 cBone.RecalcBindState(false, false);
967 }
968 else //Parent not found, continue searching children.
969 {
970 foreach (MDL0BoneNode pMatch in pBone._children)
971 {
972 AssignParent(pMatch, child, cBone, parent);
973 }
974 }
975 }
static void AssignParent(MDL0BoneNode pBone, ModelBone child, MDL0BoneNode cBone, ModelBone parent)
Definition: PMDModel.cs:953
Definition: MDL0BoneNode.cs:17
void RecalcBindState(bool updateMesh, bool moveMeshWithBone, bool updateAssetLists=true)
Definition: MDL0BoneNode.cs:888
int _entryIndex
Definition: MDL0GroupNode.cs:22
List< ResourceNode > _children
Definition: ResourceNode.cs:143
Definition: Matrix.cs:14
static Matrix TranslationMatrix(Vector3 v)
Definition: Matrix.cs:353
Matrix Invert()
Definition: Matrix.cs:1474
FrameState Derive()
Definition: Matrix.cs:1010

◆ Export()

static void BrawlLib.Modeling.PMDModel.Export ( MDL0Node  model,
string  filename 
)
inlinestatic
65 {
66 filename = Path.GetFullPath(filename);
67 using (FileStream fs = new FileStream(filename, FileMode.Create))
68 {
69 BinaryWriter writer = new BinaryWriter(fs);
70 writer.Write(encoder.GetBytes("Pmd"));
71 writer.Write(1.0f); //Version
72 MDL02PMD(model);
73 Write(writer);
74 }
75 }
static void Write(BinaryWriter writer)
Definition: PMDModel.cs:338
static void MDL02PMD(MDL0Node model)
Definition: PMDModel.cs:1223

◆ ImportModel()

static MDL0Node BrawlLib.Modeling.PMDModel.ImportModel ( string  filepath)
inlinestatic
23 {
24 filepath = Path.GetFullPath(filepath);
25 if (!File.Exists(filepath))
26 {
27 throw new FileNotFoundException("PMD model file " + filepath + " not found.");
28 }
29
30 MDL0Node model = null;
31 using (FileStream fs = new FileStream(filepath, FileMode.Open))
32 {
33 BinaryReader reader = new BinaryReader(fs);
34 string magic = encoder.GetString(reader.ReadBytes(3));
35 if (magic != "Pmd")
36 {
37 throw new FileLoadException("Model format not recognized.");
38 }
39
40 float version = BitConverter.ToSingle(reader.ReadBytes(4), 0);
41 if (version == 1.0f)
42 {
43 model = new MDL0Node();
44 }
45 else
46 {
47 throw new Exception("Version " + version + " models are not supported.");
48 }
49
50 if (model != null)
51 {
52 Read(reader, CoordinateType.LeftHanded); //Will flip model backwards if right handed
53 PMD2MDL0(model);
54 }
55 }
56
57 return model;
58 }
static void Read(BinaryReader reader, CoordinateType coordinate)
Definition: PMDModel.cs:182
static unsafe void PMD2MDL0(MDL0Node model)
Definition: PMDModel.cs:579
CoordinateType
Definition: PMDModel.cs:144
Definition: MDL0Node.cs:24

◆ MDL02PMD()

static void BrawlLib.Modeling.PMDModel.MDL02PMD ( MDL0Node  model)
inlinestatic
1224 {
1225 _header = new ModelHeader
1226 {
1227 _modelName = model.Name,
1228
1229 //To do: Add the ability to change the comment
1230 _comment = "MDL0 model converted to PMD by BrawlCrate."
1231 };
1232
1233 foreach (MDL0MaterialNode m in model._matList)
1234 {
1235 ModelMaterial mat = new ModelMaterial
1236 {
1237 _textureFileName = m.Children[0].Name
1238 };
1239 }
1240
1241 _bones = new ModelBone[model._linker.BoneCache.Length];
1242 for (int i = 0; i < model._linker.BoneCache.Length; i++)
1243 {
1244 ModelBone bone = new ModelBone();
1245 MDL0BoneNode mBone = model._linker.BoneCache[i];
1246
1247 Vector3 wPos = mBone._bindMatrix.GetPoint();
1248 bone._wPos[0] = wPos._x;
1249 bone._wPos[1] = wPos._y;
1250 bone._wPos[2] = wPos._z;
1251
1252 bone._boneName = mBone.Name;
1253
1254 bone._boneType = 0;
1255 bone._parentBoneIndex = (ushort) model._linker.BoneCache.ToList<ResourceNode>().IndexOf(mBone.Parent);
1256
1257 _bones[i] = bone;
1258 }
1259 }
static ModelBone[] _bones
Definition: PMDModel.cs:161
static ModelHeader _header
Definition: PMDModel.cs:157
Matrix _bindMatrix
Definition: MDL0BoneNode.cs:56
Definition: MDL0MaterialNode.cs:22
ModelLinker _linker
Definition: MDL0Node.cs:45
List< ResourceNode > _matList
Definition: MDL0Node.cs:304
Definition: ResourceNode.cs:129
ResourceNode Parent
Definition: ResourceNode.cs:245
virtual string Name
Definition: ResourceNode.cs:223
List< ResourceNode > Children
Definition: ResourceNode.cs:262
MDL0BoneNode[] BoneCache
Definition: ModelLinker.cs:141
string Name
Definition: IBoneNode.cs:11
Vector3 GetPoint()
Definition: Matrix.cs:321
Definition: Vector3.cs:40
float _y
Definition: Vector3.cs:41
float _z
Definition: Vector3.cs:41
float _x
Definition: Vector3.cs:41

◆ PMD2MDL0()

static unsafe void BrawlLib.Modeling.PMDModel.PMD2MDL0 ( MDL0Node  model)
inlinestatic
580 {
581 Collada.Collada._importOptions = Properties.Settings.Default.ColladaImportOptions;
582 Collada.Collada._importOptions._forceCCW = true;
583 Collada.Collada._importOptions._fltVerts = true;
584 Collada.Collada._importOptions._fltNrms = true;
585 Collada.Collada._importOptions._fltUVs = true;
586 Collada.Collada.CurrentModel = model;
587
588 model.BeginImport();
589 model._version = 9;
590
591 List<MDL0BoneNode> BoneCache = new List<MDL0BoneNode>();
592
593 int index = 0;
594 if (!string.IsNullOrWhiteSpace(_header._modelNameEnglish))
595 {
596 model.Name = _header._modelNameEnglish;
597 }
598 else
599 {
600 model.Name = _header._modelName;
601 }
602
603 if (!string.IsNullOrWhiteSpace(_header._commentEnglish))
604 {
605 MessageBox.Show(_header._commentEnglish);
606 }
607 else
608 {
609 MessageBox.Show(_header._comment);
610 }
611
612 ModelBone parent = null;
613 foreach (ModelBone b in _bones)
614 {
615 MDL0BoneNode bone = new MDL0BoneNode();
616
617 if (!string.IsNullOrWhiteSpace(b._boneNameEnglish))
618 {
619 bone._name = b._boneNameEnglish;
620 }
621 else
622 {
623 bone._name = b._boneName;
624 }
625
626 bone._entryIndex = index++;
627
628 if (b._parentBoneIndex != ushort.MaxValue)
629 {
630 parent = _bones[b._parentBoneIndex];
631 foreach (MDL0BoneNode v in model._boneGroup._children)
632 {
633 AssignParent(v, b, bone, parent);
634 }
635 }
636 else
637 {
638 bone.Parent = model._boneGroup;
639 bone._bindState._scale = new Vector3(1.0f);
640 bone._bindState._translate = new Vector3(b._wPos[0], b._wPos[1], b._wPos[2]);
641 bone._bindState.CalcTransforms();
642 bone.RecalcBindState(false, false);
643 }
644
645 BoneCache.Add(bone);
646 }
647
648 MDL0ShaderNode texSpa = null, tex = null, colorSpa = null, color = null;
649
650 index = 0;
651 foreach (ModelMaterial m in _materials)
652 {
654 {
655 Name = "Material" + index++
656 };
657
658 MDL0MaterialRefNode texRef = null;
659 MDL0MaterialRefNode spaRef = null;
660
661 if (!string.IsNullOrEmpty(m._textureFileName))
662 {
663 if (m._textureFileName.Contains('*'))
664 {
665 string[] names = m._textureFileName.Split('*');
666 if (!string.IsNullOrEmpty(names[0]))
667 {
668 texRef = new MDL0MaterialRefNode
669 {
670 Name = names[0].Substring(0, names[0].IndexOf('.'))
671 };
672 }
673
674 if (!string.IsNullOrEmpty(names[1]))
675 {
676 spaRef = new MDL0MaterialRefNode
677 {
678 Name = names[1].Substring(0, names[1].IndexOf('.')),
679 MapMode = MappingMethod.EnvCamera,
680 UWrapMode = MatWrapMode.Clamp,
681 VWrapMode = MatWrapMode.Clamp,
682 Projection = TexProjection.STQ,
683 InputForm = TexInputForm.ABC1,
684 Coordinates = TexSourceRow.Normals,
685 Normalize = true
686 };
687 }
688 }
689 else
690 {
691 texRef = new MDL0MaterialRefNode
692 {
693 Name = m._textureFileName.Substring(0, m._textureFileName.IndexOf('.')),
694 Coordinates = TexSourceRow.TexCoord0
695 };
696 }
697 }
698
699 if (texRef != null)
700 {
701 (texRef._texture = model.FindOrCreateTexture(texRef.Name))._references.Add(texRef);
702 texRef._parent = mn;
703 mn._children.Add(texRef);
704 }
705
706 if (spaRef != null)
707 {
708 (spaRef._texture = model.FindOrCreateTexture(spaRef.Name))._references.Add(spaRef);
709 spaRef._parent = mn;
710 mn._children.Add(spaRef);
711 }
712
713 mn._chan1._matColor = new RGBAPixel((byte) (m._diffuseColor[0] * 255),
714 (byte) (m._diffuseColor[1] * 255), (byte) (m._diffuseColor[2] * 255), 255);
715 mn._chan1.ColorMaterialSource = GXColorSrc.Register;
716
717 if (texRef != null && spaRef != null)
718 {
719 if (texSpa == null)
720 {
722 n._parent = model._shadGroup;
723 model._shadList.Add(n);
724 texSpa = n;
725 }
726
727 mn.ShaderNode = texSpa;
728 }
729 else if (texRef != null)
730 {
731 if (tex == null)
732 {
734 n._parent = model._shadGroup;
735 model._shadList.Add(n);
736 tex = n;
737 }
738
739 mn.ShaderNode = tex;
740 }
741 else if (spaRef != null)
742 {
743 if (colorSpa == null)
744 {
746 n._parent = model._shadGroup;
747 model._shadList.Add(n);
748 colorSpa = n;
749 }
750
751 mn.ShaderNode = colorSpa;
752 }
753 else
754 {
755 if (color == null)
756 {
758 n._parent = model._shadGroup;
759 model._shadList.Add(n);
760 color = n;
761 }
762
763 mn.ShaderNode = color;
764 }
765
766 mn._parent = model._matGroup;
767 model._matList.Add(mn);
768 }
769
770 model._numTriangles = 0;
771 model._numFacepoints = 0;
772
773 int x = 0;
774 int offset = 0;
775 foreach (ModelMaterial m in _materials)
776 {
777 PrimitiveManager manager = new PrimitiveManager {_pointCount = (int) m._faceVertCount};
779 {
780 _manager = manager,
781 _drawCalls = new System.ComponentModel.BindingList<DrawCall>()
782 };
783 p._drawCalls.Add(new DrawCall(p)
784 {
785 MaterialNode = (MDL0MaterialNode) model._matList[x]
786 });
787 p._manager._vertices = new List<Vertex3>();
788 p.Name = "polygon" + x++;
789 p._parent = model._objGroup;
790
791 model._numTriangles += p._numFaces = manager._faceCount = manager._pointCount / 3;
792 model._numFacepoints += p._numFacepoints = manager._pointCount;
793
794 p._manager._indices = new UnsafeBuffer((int) m._faceVertCount * 2);
795 p._manager._faceData[0] = new UnsafeBuffer((int) m._faceVertCount * 12);
796 p._manager._faceData[1] = new UnsafeBuffer((int) m._faceVertCount * 12);
797 p._manager._faceData[4] = new UnsafeBuffer((int) m._faceVertCount * 8);
798
799 p._manager._dirty[0] = true;
800 p._manager._dirty[1] = true;
801 p._manager._dirty[4] = true;
802
803 ushort* Indices = (ushort*) p._manager._indices.Address;
804 Vector3* Vertices = (Vector3*) p._manager._faceData[0].Address;
805 Vector3* Normals = (Vector3*) p._manager._faceData[1].Address;
806 Vector2* UVs = (Vector2*) p._manager._faceData[4].Address;
807
808 manager._triangles = new GLPrimitive((int) m._faceVertCount, BeginMode.Triangles);
809 uint[] pTriarr = manager._triangles._indices;
810 uint pTri = 0;
811
812 index = 0;
813 List<int> usedVertices = new List<int>();
814 List<int> vertexIndices = new List<int>();
815 for (int s = offset, l = 0; l < (int) m._faceVertCount; l++, s++)
816 {
817 ushort i = _faceIndices[s];
818 ModelVertex mv = _vertices[i];
819 ushort j = 0;
820 if (!usedVertices.Contains(i))
821 {
822 Influence inf;
823 BoneWeight weight1 = null, weight2 = null;
824
825 float weight = (mv._boneWeight / 100.0f).Clamp(0.0f, 1.0f);
826
827 if (weight > 0.0f && weight < 1.0f)
828 {
829 weight1 = new BoneWeight(BoneCache[mv._boneIndex[0]], weight);
830 weight2 = new BoneWeight(BoneCache[mv._boneIndex[1]], 1.0f - weight);
831 }
832 else if (weight == 0.0f)
833 {
834 weight1 = new BoneWeight(BoneCache[mv._boneIndex[1]]);
835 }
836 else
837 {
838 weight1 = new BoneWeight(BoneCache[mv._boneIndex[0]]);
839 }
840
841 if (weight2 != null)
842 {
843 inf = new Influence(new List<BoneWeight> {weight1, weight2});
844 }
845 else
846 {
847 inf = new Influence(new List<BoneWeight> {weight1});
848 }
849
850 Vector3 t = new Vector3();
851 Vertex3 v;
852 t._x = mv._position[0];
853 t._y = mv._position[1];
854 t._z = mv._position[2];
855 if (inf.Weights.Count > 1)
856 {
857 inf = model._influences.FindOrCreate(inf);
858 inf.CalcMatrix();
859 v = new Vertex3(t, inf);
860 }
861 else
862 {
863 MDL0BoneNode bone = inf.Weights[0].Bone as MDL0BoneNode;
864 v = new Vertex3(t * bone._inverseBindMatrix, bone);
865 }
866
867 p._manager._vertices.Add(v);
868 vertexIndices.Add(usedVertices.Count);
869 usedVertices.Add(i);
870 j = (ushort) (usedVertices.Count - 1);
871 }
872 else
873 {
874 j = (ushort) vertexIndices[usedVertices.IndexOf(i)];
875 }
876
877 *Indices++ = j;
878 pTriarr[pTri++] = (uint) l;
879 *Vertices++ = p._manager._vertices[j].Position;
880 *Normals++ = mv._normal;
881 *UVs++ = mv._texCoord;
882 }
883
884 model._objList.Add(p);
885 offset += (int) m._faceVertCount;
886 }
887
888 //Check each polygon to see if it can be rigged to a single influence
889 if (model._objList != null && model._objList.Count != 0)
890 {
891 foreach (MDL0ObjectNode p in model._objList)
892 {
893 IMatrixNode node = null;
894 bool singlebind = true;
895
896 foreach (Vertex3 v in p._manager._vertices)
897 {
898 if (v.MatrixNode != null)
899 {
900 if (node == null)
901 {
902 node = v.MatrixNode;
903 }
904
905 if (v.MatrixNode != node)
906 {
907 singlebind = false;
908 break;
909 }
910 }
911 }
912
913 if (singlebind && p._matrixNode == null)
914 {
915 //Increase reference count ahead of time for rebuild
916 //p._manager._vertices[0].MatrixNode.ReferenceCount++;
917 p._manager._vertices[0].MatrixNode?.Users.Add(p);
918
919 foreach (Vertex3 v in p._manager._vertices)
920 {
921 //v.MatrixNode.ReferenceCount--;
922 v.MatrixNode?.Users.Remove(v);
923 }
924
925 p._nodeId = -2; //Continued on polygon rebuild
926 }
927 }
928 }
929
930 //foreach (MDL0ObjectNode p in model._objList)
931 // foreach (MDL0MaterialNode m in model._matList)
932 // {
933 // MDL0MaterialNode m2 = p.OpaMaterialNode;
934
935 // if (m2 == null || m2.ShaderNode != m.ShaderNode || m2.Children.Count != m.Children.Count)
936 // continue;
937
938 // for (int i = 0; i < m.Children.Count; i++)
939 // if (m2.Children[i].Name != m.Children[i].Name)
940 // continue;
941
942 // p.OpaMaterialNode = m;
943 // break;
944 // }
945 //for (int i = 0; i < model._matList.Count; i++)
946 // if (((MDL0MaterialNode)model._matList[i])._objects.Count == 0)
947 // model._matList.RemoveAt(i--);
948
949 model.FinishImport();
950 Collada.Collada.CurrentModel = null;
951 }
Definition: UnsafeBuffer.cs:7
string _commentEnglish
Definition: PMDModel.cs:1275
string _modelNameEnglish
Definition: PMDModel.cs:1274
string _modelName
Definition: PMDModel.cs:1272
string _comment
Definition: PMDModel.cs:1273
static MDL0ShaderNode ColorShader
Definition: PMDModel.cs:1174
static MDL0ShaderNode ColorSpaShader
Definition: PMDModel.cs:1052
static ushort[] _faceIndices
Definition: PMDModel.cs:159
static ModelVertex[] _vertices
Definition: PMDModel.cs:158
static MDL0ShaderNode TexShader
Definition: PMDModel.cs:1125
static MDL0ShaderNode TexSpaShader
Definition: PMDModel.cs:978
static ModelMaterial[] _materials
Definition: PMDModel.cs:160
Definition: MDL0ObjectNode.cs:2040
FrameState _bindState
Definition: MDL0BoneNode.cs:55
Matrix _inverseBindMatrix
Definition: MDL0BoneNode.cs:56
Definition: MDL0MaterialRefNode.cs:17
override string Name
Definition: MDL0MaterialRefNode.cs:41
List< ResourceNode > _objList
Definition: MDL0Node.cs:306
InfluenceManager _influences
Definition: MDL0Node.cs:50
MDL0GroupNode _matGroup
Definition: MDL0Node.cs:290
MDL0GroupNode _shadGroup
Definition: MDL0Node.cs:291
void FinishImport(Collada form=null)
Definition: MDL0Node.cs:2605
MDL0GroupNode _boneGroup
Definition: MDL0Node.cs:289
MDL0GroupNode _objGroup
Definition: MDL0Node.cs:292
List< ResourceNode > _shadList
Definition: MDL0Node.cs:305
void BeginImport()
Definition: MDL0Node.cs:2599
MDL0TextureNode FindOrCreateTexture(string name)
Definition: MDL0Node.cs:706
Definition: MDL0ObjectNode.cs:21
BindingList< DrawCall > _drawCalls
Definition: MDL0ObjectNode.cs:626
PrimitiveManager _manager
Definition: MDL0ObjectNode.cs:710
Definition: MDL0ShaderNode.cs:11
Definition: InfluenceManager.cs:381
Definition: InfluenceManager.cs:96
void CalcMatrix()
Definition: InfluenceManager.cs:271
List< BoneWeight > Weights
Don't modify this array!
Definition: InfluenceManager.cs:111
Influence FindOrCreate(Influence inf)
Definition: InfluenceManager.cs:20
GXColorSrc
Definition: MDL0MaterialNode.cs:2882
MatWrapMode
Definition: MDL0MaterialRefNode.cs:1001
MappingMethod
Definition: MDL0MaterialRefNode.cs:1024
TexInputForm
Definition: Enum.cs:560
TexSourceRow
Definition: Enum.cs:574
TexProjection
Definition: Enum.cs:554
Definition: PixelTypes.cs:327
Definition: Vector2.cs:10

◆ Read()

static void BrawlLib.Modeling.PMDModel.Read ( BinaryReader  reader,
CoordinateType  coordinate 
)
inlinestatic
183 {
184 _coordinate = coordinate;
185
186 _header = new ModelHeader();
187 _header.Read(reader);
188
189 //Read Vertices
190 uint num_vertex = BitConverter.ToUInt32(reader.ReadBytes(4), 0);
191 _vertices = new ModelVertex[num_vertex];
192 for (uint i = 0; i < num_vertex; i++)
193 {
194 _vertices[i] = new ModelVertex();
195 _vertices[i].Read(reader, CoordZ);
196 }
197
198 //Read Primitives
199 uint face_vert_count = BitConverter.ToUInt32(reader.ReadBytes(4), 0);
200 _faceIndices = new ushort[face_vert_count];
201 for (uint i = 0; i < face_vert_count; i++)
202 {
203 _faceIndices[i] = BitConverter.ToUInt16(reader.ReadBytes(2), 0);
204 }
205
206 //Read Materials
207 uint material_count = BitConverter.ToUInt32(reader.ReadBytes(4), 0);
208 _materials = new ModelMaterial[material_count];
209 for (uint i = 0; i < material_count; i++)
210 {
211 _materials[i] = new ModelMaterial();
212 _materials[i].Read(reader);
213 }
214
215 //Read Bones
216 ushort bone_count = BitConverter.ToUInt16(reader.ReadBytes(2), 0);
217 _bones = new ModelBone[bone_count];
218 for (ushort i = 0; i < bone_count; i++)
219 {
220 _bones[i] = new ModelBone();
221 _bones[i].Read(reader, CoordZ);
222 }
223
224 //Read IK Bones
225 ushort ik_count = BitConverter.ToUInt16(reader.ReadBytes(2), 0);
226 _IKs = new ModelIK[ik_count];
227 for (ushort i = 0; i < ik_count; i++)
228 {
229 _IKs[i] = new ModelIK();
230 _IKs[i].Read(reader);
231 }
232
233 //Read Face Morphs
234 ushort skin_count = BitConverter.ToUInt16(reader.ReadBytes(2), 0);
235 _skins = new ModelSkin[skin_count];
236 for (ushort i = 0; i < skin_count; i++)
237 {
238 _skins[i] = new ModelSkin();
239 _skins[i].Read(reader, CoordZ);
240 }
241
242 //Read face morph indices
243 byte skin_disp_count = reader.ReadByte();
244 _skinIndex = new ushort[skin_disp_count];
245 for (byte i = 0; i < _skinIndex.Length; i++)
246 {
247 _skinIndex[i] = BitConverter.ToUInt16(reader.ReadBytes(2), 0);
248 }
249
250 //Read bone morph names
251 byte bone_disp_name_count = reader.ReadByte();
252 _boneDispNames = new ModelBoneDispName[bone_disp_name_count];
253 for (byte i = 0; i < _boneDispNames.Length; i++)
254 {
255 _boneDispNames[i] = new ModelBoneDispName();
256 _boneDispNames[i].Read(reader);
257 }
258
259 //Read bone morphs
260 uint bone_disp_count = BitConverter.ToUInt32(reader.ReadBytes(4), 0);
261 _boneDisps = new ModelBoneDisp[bone_disp_count];
262 for (uint i = 0; i < _boneDisps.Length; i++)
263 {
264 _boneDisps[i] = new ModelBoneDisp();
265 _boneDisps[i].Read(reader);
266 }
267
268 //Read English strings, if there are any.
269 try
270 {
271 _expansion = reader.ReadByte() != 0;
272 if (_expansion)
273 {
274 _header.ReadExpansion(reader);
275 for (ushort i = 0; i < bone_count; i++)
276 {
277 _bones[i].ReadExpansion(reader);
278 }
279
280 for (ushort i = 0; i < skin_count; i++)
281 {
282 if (_skins[i]._skinType != 0)
283 {
284 _skins[i].ReadExpansion(reader);
285 }
286 }
287
288 for (byte i = 0; i < _boneDispNames.Length; i++)
289 {
290 _boneDispNames[i].ReadExpansion(reader);
291 }
292
293 if (reader.BaseStream.Position >= reader.BaseStream.Length)
294 {
295 _toonExpansion = false;
296 }
297 else
298 {
299 _toonExpansion = true;
300 _toonFileNames = new string[_numToonFileName];
301 for (int i = 0; i < _toonFileNames.Length; i++)
302 {
303 _toonFileNames[i] = GetString(reader.ReadBytes(100));
304 }
305 }
306
307 if (reader.BaseStream.Position >= reader.BaseStream.Length)
308 {
309 _physicsExpansion = false;
310 }
311 else
312 {
313 _physicsExpansion = true;
314 uint rididbody_count = BitConverter.ToUInt32(reader.ReadBytes(4), 0);
315 _rigidBodies = new ModelRigidBody[rididbody_count];
316 for (uint i = 0; i < rididbody_count; i++)
317 {
318 _rigidBodies[i] = new ModelRigidBody();
319 _rigidBodies[i].ReadExpansion(reader, CoordZ);
320 }
321
322 uint joint_count = BitConverter.ToUInt32(reader.ReadBytes(4), 0);
323 _joints = new ModelJoint[joint_count];
324 for (uint i = 0; i < joint_count; i++)
325 {
326 _joints[i] = new ModelJoint();
327 _joints[i].ReadExpansion(reader, CoordZ);
328 }
329 }
330 }
331 }
332 catch
333 {
334 Console.WriteLine("This file does not contain English strings.");
335 }
336 }
const int _numToonFileName
Definition: PMDModel.cs:170
static string[] _toonFileNames
Definition: PMDModel.cs:169
static ModelBoneDispName[] _boneDispNames
Definition: PMDModel.cs:165
static ModelIK[] _IKs
Definition: PMDModel.cs:162
static ushort[] _skinIndex
Definition: PMDModel.cs:164
static ModelJoint[] _joints
Definition: PMDModel.cs:173
static ModelBoneDisp[] _boneDisps
Definition: PMDModel.cs:166
static ModelRigidBody[] _rigidBodies
Definition: PMDModel.cs:172
static CoordinateType _coordinate
Definition: PMDModel.cs:174
static bool _toonExpansion
Definition: PMDModel.cs:168
static bool _physicsExpansion
Definition: PMDModel.cs:171
static bool _expansion
Definition: PMDModel.cs:167
static ModelSkin[] _skins
Definition: PMDModel.cs:163
static float CoordZ
Definition: PMDModel.cs:176

◆ Write()

static void BrawlLib.Modeling.PMDModel.Write ( BinaryWriter  writer)
inlinestatic
339 {
340 _header?.Write(writer);
341
342 if (_vertices == null)
343 {
344 writer.Write((uint) 0);
345 }
346 else
347 {
348 writer.Write((uint) _vertices.LongLength);
349 for (uint i = 0; i < _vertices.LongLength; i++)
350 {
351 if (_vertices[i] == null)
352 {
353 throw new ArgumentNullException("Vertexes[" + i + "] is null!");
354 }
355
356 _vertices[i].Write(writer, CoordZ);
357 }
358 }
359
360 if (_faceIndices == null)
361 {
362 writer.Write((uint) 0);
363 }
364 else
365 {
366 writer.Write((uint) _faceIndices.LongLength);
367 for (uint i = 0; i < _faceIndices.LongLength; i++)
368 {
369 writer.Write(_faceIndices[i]);
370 }
371 }
372
373 if (_materials == null)
374 {
375 writer.Write((uint) 0);
376 }
377 else
378 {
379 writer.Write((uint) _materials.LongLength);
380 for (uint i = 0; i < _materials.LongLength; i++)
381 {
382 if (_materials[i] == null)
383 {
384 throw new ArgumentNullException("Materials[" + i + "] is null!");
385 }
386
387 _materials[i].Write(writer);
388 }
389 }
390
391 if (_bones == null)
392 {
393 writer.Write((ushort) 0);
394 }
395 else
396 {
397 writer.Write((ushort) _bones.Length);
398 for (ushort i = 0; i < _bones.Length; i++)
399 {
400 if (_bones[i] == null)
401 {
402 throw new ArgumentNullException("Bones[" + i + "] is null!");
403 }
404
405 _bones[i].Write(writer, CoordZ);
406 }
407 }
408
409 if (_IKs == null)
410 {
411 writer.Write((ushort) 0);
412 }
413 else
414 {
415 writer.Write((ushort) _IKs.Length);
416 for (ushort i = 0; i < _IKs.Length; i++)
417 {
418 if (_IKs[i] == null)
419 {
420 throw new ArgumentNullException("IKs[" + i + "] is null!");
421 }
422
423 _IKs[i].Write(writer);
424 }
425 }
426
427 if (_skins == null)
428 {
429 writer.Write((ushort) 0);
430 }
431 else
432 {
433 writer.Write((ushort) _skins.Length);
434 for (ushort i = 0; i < _skins.Length; i++)
435 {
436 if (_skins[i] == null)
437 {
438 throw new ArgumentNullException("Skins[" + i + "] is null!");
439 }
440
441 _skins[i].Write(writer, CoordZ);
442 }
443 }
444
445 if (_skinIndex == null)
446 {
447 writer.Write((byte) 0);
448 }
449 else
450 {
451 writer.Write((byte) _skinIndex.Length);
452
453 for (byte i = 0; i < _skinIndex.Length; i++)
454 {
455 writer.Write(_skinIndex[i]);
456 }
457 }
458
459 if (_boneDispNames == null)
460 {
461 writer.Write((byte) 0);
462 }
463 else
464 {
465 writer.Write((byte) _boneDispNames.Length);
466 for (byte i = 0; i < _boneDispNames.Length; i++)
467 {
468 if (_boneDispNames[i] == null)
469 {
470 throw new ArgumentNullException("BoneDispNames[" + i + "] is null!");
471 }
472
473 _boneDispNames[i].Write(writer);
474 }
475 }
476
477 if (_boneDisps == null)
478 {
479 writer.Write((uint) 0);
480 }
481 else
482 {
483 writer.Write((uint) _boneDisps.Length);
484 for (uint i = 0; i < _boneDisps.Length; i++)
485 {
486 if (_boneDisps[i] == null)
487 {
488 throw new ArgumentNullException("BoneDisps[" + i + "] is null!");
489 }
490
491 _boneDisps[i].Write(writer);
492 }
493 }
494
495 writer.Write((byte) (_expansion ? 1 : 0));
496 if (_expansion)
497 {
498 _header.WriteExpansion(writer);
499 if (_bones != null)
500 {
501 for (ushort i = 0; i < _bones.Length; i++)
502 {
503 _bones[i].WriteExpansion(writer);
504 }
505 }
506
507 if (_skins != null)
508 {
509 for (ushort i = 0; i < _skins.Length; i++)
510 {
511 if (_skins[i]._skinType != 0)
512 {
513 _skins[i].WriteExpansion(writer);
514 }
515 }
516 }
517
518 if (_boneDispNames != null)
519 {
520 for (byte i = 0; i < _boneDispNames.Length; i++)
521 {
522 _boneDispNames[i].WriteExpansion(writer);
523 }
524 }
525
526 if (_toonExpansion)
527 {
528 for (int i = 0; i < _toonFileNames.Length; i++)
529 {
530 writer.Write(GetBytes(_toonFileNames[i], 100));
531 }
532
534 {
535 if (_rigidBodies == null)
536 {
537 writer.Write((uint) 0);
538 }
539 else
540 {
541 writer.Write((uint) _rigidBodies.LongLength);
542 for (long i = 0; i < _rigidBodies.LongLength; i++)
543 {
544 if (_rigidBodies[i] == null)
545 {
546 throw new ArgumentNullException("RididBodies[" + i + "] is null!");
547 }
548
549 _rigidBodies[i].WriteExpansion(writer, CoordZ);
550 }
551 }
552
553 if (_joints == null)
554 {
555 writer.Write((uint) 0);
556 }
557 else
558 {
559 writer.Write((uint) _joints.LongLength);
560 for (long i = 0; i < _joints.LongLength; i++)
561 {
562 if (_joints[i] == null)
563 {
564 throw new ArgumentNullException("Joints[" + i + "] is null!");
565 }
566
567 _joints[i].WriteExpansion(writer, CoordZ);
568 }
569 }
570 }
571 }
572 }
573 }

Member Data Documentation

◆ _boneDispNames

ModelBoneDispName [] BrawlLib.Modeling.PMDModel._boneDispNames
static

◆ _boneDisps

ModelBoneDisp [] BrawlLib.Modeling.PMDModel._boneDisps
static

◆ _bones

ModelBone [] BrawlLib.Modeling.PMDModel._bones
static

◆ _coordinate

CoordinateType BrawlLib.Modeling.PMDModel._coordinate
static

◆ _expansion

bool BrawlLib.Modeling.PMDModel._expansion
static

◆ _faceIndices

ushort [] BrawlLib.Modeling.PMDModel._faceIndices
static

◆ _header

ModelHeader BrawlLib.Modeling.PMDModel._header
static

◆ _IKs

ModelIK [] BrawlLib.Modeling.PMDModel._IKs
static

◆ _joints

ModelJoint [] BrawlLib.Modeling.PMDModel._joints
static

◆ _materials

ModelMaterial [] BrawlLib.Modeling.PMDModel._materials
static

◆ _numToonFileName

const int BrawlLib.Modeling.PMDModel._numToonFileName = 10
static

◆ _physicsExpansion

bool BrawlLib.Modeling.PMDModel._physicsExpansion
static

◆ _rigidBodies

ModelRigidBody [] BrawlLib.Modeling.PMDModel._rigidBodies
static

◆ _skinIndex

ushort [] BrawlLib.Modeling.PMDModel._skinIndex
static

◆ _skins

ModelSkin [] BrawlLib.Modeling.PMDModel._skins
static

◆ _toonExpansion

bool BrawlLib.Modeling.PMDModel._toonExpansion
static

◆ _toonFileNames

string [] BrawlLib.Modeling.PMDModel._toonFileNames
static

◆ _version

float BrawlLib.Modeling.PMDModel._version = 1.0f
static

◆ _vertices

ModelVertex [] BrawlLib.Modeling.PMDModel._vertices
static

Property Documentation

◆ ColorShader

MDL0ShaderNode BrawlLib.Modeling.PMDModel.ColorShader
staticget
1174 {
1175 get
1176 {
1177 MDL0ShaderNode shader = new MDL0ShaderNode();
1178
1180 shader.AddChild(s);
1181
1182 s._colorEnv = 0x28FFFA;
1183 s._alphaEnv = 0x8FFD0;
1184 s.ConstantColorSelection = TevKColorSel.ConstantColor0_RGB;
1185 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1186 s.TextureMapID = TexMapID.TexMap7;
1187 s.TextureCoordID = TexCoordID.TexCoord7;
1188 s.TextureEnabled = false;
1189 s.RasterColor = ColorSelChan.LightChannel0;
1190
1191 s = new MDL0TEVStageNode();
1192 shader.AddChild(s);
1193
1194 s._colorEnv = 0x8FEB0;
1195 s._alphaEnv = 0x81FF0;
1196 s.ConstantColorSelection = TevKColorSel.ConstantColor1_RGB;
1197 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1198 s.TextureMapID = TexMapID.TexMap7;
1199 s.TextureCoordID = TexCoordID.TexCoord7;
1200 s.TextureEnabled = false;
1201 s.RasterColor = ColorSelChan.LightChannel0;
1202
1203 s = new MDL0TEVStageNode();
1204 shader.AddChild(s);
1205
1206 s._colorEnv = 0x806EF;
1207 s._alphaEnv = 0x81FF0;
1208 s.ConstantColorSelection = TevKColorSel.ConstantColor0_RGB;
1209 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1210 s.TextureMapID = TexMapID.TexMap7;
1211 s.TextureCoordID = TexCoordID.TexCoord7;
1212 s.TextureEnabled = false;
1213 s.RasterColor = ColorSelChan.Zero;
1214
1215 return shader;
1216 }
1217 }
override void AddChild(ResourceNode child, bool change)
Definition: MDL0ShaderNode.cs:345
TexCoordID
Definition: Enum.cs:327
TevKAlphaSel
Definition: Enum.cs:342
ColorSelChan
Definition: Enum.cs:303
TexMapID
Definition: Enum.cs:286
TevKColorSel
Definition: Enum.cs:376

◆ ColorSpaShader

MDL0ShaderNode BrawlLib.Modeling.PMDModel.ColorSpaShader
staticget
1052 {
1053 get
1054 {
1055 MDL0ShaderNode shader = new MDL0ShaderNode
1056 {
1057 TextureRef0 = true
1058 };
1059
1061 shader.AddChild(s);
1062
1063 s._colorEnv = 0x8FFFA;
1064 s._alphaEnv = 0x8FFD0;
1065 s.ConstantColorSelection = TevKColorSel.ConstantColor3_RGB;
1066 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor3_Alpha;
1067 s.TextureMapID = TexMapID.TexMap7;
1068 s.TextureCoordID = TexCoordID.TexCoord7;
1069 s.TextureEnabled = false;
1070 s.RasterColor = ColorSelChan.LightChannel0;
1071
1072 s = new MDL0TEVStageNode();
1073 shader.AddChild(s);
1074
1075 s._colorEnv = 0x88E80;
1076 s._alphaEnv = 0x8FF80;
1077 s.ConstantColorSelection = TevKColorSel.Constant1_4;
1078 s.ConstantAlphaSelection = TevKAlphaSel.Constant1_4;
1079 s.TextureMapID = TexMapID.TexMap0;
1080 s.TextureCoordID = TexCoordID.TexCoord0;
1081 s.TextureEnabled = true;
1082 s.RasterColor = ColorSelChan.Zero;
1083
1084 s = new MDL0TEVStageNode();
1085 shader.AddChild(s);
1086
1087 s._colorEnv = 0x28F0AF;
1088 s._alphaEnv = 0x8FF80;
1089 s.ConstantColorSelection = TevKColorSel.ConstantColor0_RGB;
1090 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1091 s.TextureMapID = TexMapID.TexMap7;
1092 s.TextureCoordID = TexCoordID.TexCoord7;
1093 s.TextureEnabled = false;
1094 s.RasterColor = ColorSelChan.LightChannel0;
1095
1096 s = new MDL0TEVStageNode();
1097 shader.AddChild(s);
1098
1099 s._colorEnv = 0x8FEB0;
1100 s._alphaEnv = 0x81FF0;
1101 s.ConstantColorSelection = TevKColorSel.ConstantColor1_RGB;
1102 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1103 s.TextureMapID = TexMapID.TexMap7;
1104 s.TextureCoordID = TexCoordID.TexCoord7;
1105 s.TextureEnabled = false;
1106 s.RasterColor = ColorSelChan.LightChannel0;
1107
1108 s = new MDL0TEVStageNode();
1109 shader.AddChild(s);
1110
1111 s._colorEnv = 0x806EF;
1112 s._alphaEnv = 0x81FF0;
1113 s.ConstantColorSelection = TevKColorSel.ConstantColor0_RGB;
1114 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1115 s.TextureMapID = TexMapID.TexMap7;
1116 s.TextureCoordID = TexCoordID.TexCoord7;
1117 s.TextureEnabled = true;
1118 s.RasterColor = ColorSelChan.Zero;
1119
1120 return shader;
1121 }
1122 }

◆ CoordZ

float BrawlLib.Modeling.PMDModel.CoordZ
staticget

◆ TexShader

MDL0ShaderNode BrawlLib.Modeling.PMDModel.TexShader
staticget
1125 {
1126 get
1127 {
1128 MDL0ShaderNode shader = new MDL0ShaderNode
1129 {
1130 TextureRef0 = true
1131 };
1132
1134 shader.AddChild(s);
1135
1136 s._colorEnv = 0x28FFF8;
1137 s._alphaEnv = 0x8FFC0;
1138 s.ConstantColorSelection = TevKColorSel.ConstantColor0_RGB;
1139 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1140 s.TextureMapID = TexMapID.TexMap0;
1141 s.TextureCoordID = TexCoordID.TexCoord0;
1142 s.TextureEnabled = true;
1143 s.RasterColor = ColorSelChan.Zero;
1144
1145 s = new MDL0TEVStageNode();
1146 shader.AddChild(s);
1147
1148 s._colorEnv = 0x8FEB0;
1149 s._alphaEnv = 0x81FF0;
1150 s.ConstantColorSelection = TevKColorSel.ConstantColor1_RGB;
1151 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1152 s.TextureMapID = TexMapID.TexMap7;
1153 s.TextureCoordID = TexCoordID.TexCoord7;
1154 s.TextureEnabled = false;
1155 s.RasterColor = ColorSelChan.LightChannel0;
1156
1157 s = new MDL0TEVStageNode();
1158 shader.AddChild(s);
1159
1160 s._colorEnv = 0x806EF;
1161 s._alphaEnv = 0x81FF0;
1162 s.ConstantColorSelection = TevKColorSel.ConstantColor0_RGB;
1163 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1164 s.TextureMapID = TexMapID.TexMap7;
1165 s.TextureCoordID = TexCoordID.TexCoord7;
1166 s.TextureEnabled = false;
1167 s.RasterColor = ColorSelChan.Zero;
1168
1169 return shader;
1170 }
1171 }

◆ TexSpaShader

MDL0ShaderNode BrawlLib.Modeling.PMDModel.TexSpaShader
staticget
978 {
979 get
980 {
981 MDL0ShaderNode shader = new MDL0ShaderNode
982 {
983 TextureRef0 = true,
984 TextureRef1 = true
985 };
986
988 shader.AddChild(s);
989
990 s._colorEnv = 0x8FFF8;
991 s._alphaEnv = 0x8FFC0;
992 s.ConstantColorSelection = TevKColorSel.ConstantColor3_RGB;
993 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor3_Alpha;
994 s.TextureMapID = TexMapID.TexMap0;
995 s.TextureCoordID = TexCoordID.TexCoord0;
996 s.TextureEnabled = true;
997 s.RasterColor = ColorSelChan.Zero;
998
999 s = new MDL0TEVStageNode();
1000 shader.AddChild(s);
1001
1002 s._colorEnv = 0x88E80;
1003 s._alphaEnv = 0x8FF80;
1004 s.ConstantColorSelection = TevKColorSel.Constant1_4;
1005 s.ConstantAlphaSelection = TevKAlphaSel.Constant1_4;
1006 s.TextureMapID = TexMapID.TexMap1;
1007 s.TextureCoordID = TexCoordID.TexCoord1;
1008 s.TextureEnabled = true;
1009 s.RasterColor = ColorSelChan.Zero;
1010
1011 s = new MDL0TEVStageNode();
1012 shader.AddChild(s);
1013
1014 s._colorEnv = 0x28F0AF;
1015 s._alphaEnv = 0x8FF80;
1016 s.ConstantColorSelection = TevKColorSel.ConstantColor0_RGB;
1017 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1018 s.TextureMapID = TexMapID.TexMap7;
1019 s.TextureCoordID = TexCoordID.TexCoord7;
1020 s.TextureEnabled = false;
1021 s.RasterColor = ColorSelChan.LightChannel0;
1022
1023 s = new MDL0TEVStageNode();
1024 shader.AddChild(s);
1025
1026 s._colorEnv = 0x8FEB0;
1027 s._alphaEnv = 0x81FF0;
1028 s.ConstantColorSelection = TevKColorSel.ConstantColor1_RGB;
1029 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1030 s.TextureMapID = TexMapID.TexMap7;
1031 s.TextureCoordID = TexCoordID.TexCoord7;
1032 s.TextureEnabled = false;
1033 s.RasterColor = ColorSelChan.LightChannel0;
1034
1035 s = new MDL0TEVStageNode();
1036 shader.AddChild(s);
1037
1038 s._colorEnv = 0x806EF;
1039 s._alphaEnv = 0x81FF0;
1040 s.ConstantColorSelection = TevKColorSel.ConstantColor0_RGB;
1041 s.ConstantAlphaSelection = TevKAlphaSel.ConstantColor0_Alpha;
1042 s.TextureMapID = TexMapID.TexMap7;
1043 s.TextureCoordID = TexCoordID.TexCoord7;
1044 s.TextureEnabled = true;
1045 s.RasterColor = ColorSelChan.Zero;
1046
1047 return shader;
1048 }
1049 }

The documentation for this class was generated from the following file: