private void initialize(BinaryReader i_reader) { DataReader reader = new DataReader(i_reader); PMD_Header pPMDHeader = new PMD_Header(); pPMDHeader.read(reader); if (!pPMDHeader.szMagic.Equals("PMD", StringComparison.CurrentCultureIgnoreCase)) { throw new MmdException(); } this._name = pPMDHeader.szName; // ----------------------------------------------------- // 頂点数取得 this._number_of_vertex = reader.readInt();// if (this._number_of_vertex < 0) { throw new MmdException(); } // 頂点配列をコピー this._position_array = MmdVector3.createArray(this._number_of_vertex); this._normal_array = MmdVector3.createArray(this._number_of_vertex); this._texture_uv = MmdTexUV.createArray(this._number_of_vertex); this._skin_info_array = new PmdSkinInfo[this._number_of_vertex]; PMD_Vertex tmp_pmd_vertex = new PMD_Vertex(); for (int i = 0; i < _number_of_vertex; i++) { tmp_pmd_vertex.read(reader); _position_array[i].setValue(tmp_pmd_vertex.vec3Pos); _normal_array[i].setValue(tmp_pmd_vertex.vec3Normal); _texture_uv[i].setValue(tmp_pmd_vertex.uvTex); this._skin_info_array[i] = new PmdSkinInfo(); this._skin_info_array[i].fWeight = tmp_pmd_vertex.cbWeight / 100.0f; this._skin_info_array[i].unBoneNo[0] = tmp_pmd_vertex.unBoneNo[0]; this._skin_info_array[i].unBoneNo[1] = tmp_pmd_vertex.unBoneNo[1]; } // ----------------------------------------------------- // 頂点インデックス数取得 short[] indices_array = createIndicesArray(reader); // ----------------------------------------------------- // マテリアル数取得 int number_of_materials = reader.readInt(); // マテリアル配列をコピー this._materials = new PmdMaterial[number_of_materials]; PMD_Material tmp_pmd_material = new PMD_Material(); int indices_ptr = 0; for (int i = 0; i < number_of_materials; i++) { tmp_pmd_material.read(reader); this._materials[i] = new PmdMaterial(); this._materials[i].unknown = tmp_pmd_material.unknown; this._materials[i].indices = new short[tmp_pmd_material.ulNumIndices]; System.Array.Copy(indices_array, indices_ptr, this._materials[i].indices, 0, tmp_pmd_material.ulNumIndices); indices_ptr += tmp_pmd_material.ulNumIndices; this._materials[i].col4Diffuse.setValue(tmp_pmd_material.col4Diffuse); this._materials[i].col4Specular.r = tmp_pmd_material.col3Specular.r; this._materials[i].col4Specular.g = tmp_pmd_material.col3Specular.g; this._materials[i].col4Specular.b = tmp_pmd_material.col3Specular.b; this._materials[i].col4Specular.a = 1.0f; this._materials[i].col4Ambient.r = tmp_pmd_material.col3Ambient.r; this._materials[i].col4Ambient.g = tmp_pmd_material.col3Ambient.g; this._materials[i].col4Ambient.b = tmp_pmd_material.col3Ambient.b; this._materials[i].col4Ambient.a = 1.0f; this._materials[i].fShininess = tmp_pmd_material.fShininess; this._materials[i].texture_name = tmp_pmd_material.szTextureFileName; if (this._materials[i].texture_name.Length < 1) { this._materials[i].texture_name = null; } } //Boneの読み出し this.m_pBoneArray = createBoneArray(reader); //IK配列の読み出し this.m_pIKArray = createIKArray(reader, this.m_pBoneArray); //Face配列の読み出し this.m_pFaceArray = createFaceArray(reader); return; }
public void ReadPmdFile(BinaryReader i_reader) { DataReader reader = new DataReader(i_reader); // ----------------------------------------------------- // ヘッダー情報取得 pmd_header = new PMD_Header(); pmd_header.read(reader); if (!pmd_header.szMagic.Equals("PMD", StringComparison.CurrentCultureIgnoreCase)) { throw new MmdException(); } // ----------------------------------------------------- // 頂点数取得 number_of_vertex = reader.readInt();// if (number_of_vertex < 0) { throw new MmdException(); } // 頂点配列取得 pmd_vertex = new PMD_Vertex[number_of_vertex]; for (int i = 0; i < number_of_vertex; i++) { pmd_vertex[i] = new PMD_Vertex(); pmd_vertex[i].read(reader); } // ----------------------------------------------------- // 頂点インデックス数取得 number_of_indices = reader.readInt(); // 頂点インデックス配列取得 indices_array = new short[number_of_indices]; for (int i = 0; i < number_of_indices; i++) { indices_array[i] = reader.readShort(); } // ----------------------------------------------------- // マテリアル数取得 number_of_materials = reader.readInt(); // マテリアル配列取得 pmd_material = new PMD_Material[number_of_materials]; for (int i = 0; i < number_of_materials; i++) { pmd_material[i] = new PMD_Material(); pmd_material[i].read(reader); } // ----------------------------------------------------- // Bone数取得 number_of_bone = reader.readShort(); // Bone配列取得 pmd_bone = new PMD_Bone[number_of_bone]; for (int i = 0; i < number_of_bone; i++) { pmd_bone[i] = new PMD_Bone(); pmd_bone[i].read(reader); } // ボーンIDを名前に置き換え // Bone foreach (PMD_Bone bone in pmd_bone) { if (bone.nParentNo <= -1) { bone.ParentName = null; } else { bone.ParentName = pmd_bone[bone.nParentNo].szName; } if (bone.nChildNo <= 0) { bone.ChildName = null; } else { bone.ChildName = pmd_bone[bone.nChildNo].szName; } if (bone.unIKTarget <= 0) { bone.IKTargetName = null; } else { bone.IKTargetName = pmd_bone[bone.unIKTarget].szName; } } // Vertex foreach (PMD_Vertex vertex in pmd_vertex) { if (vertex.unBoneNo[0] <= -1) { vertex.unBoneName[0] = null; } else { vertex.unBoneName[0] = pmd_bone[vertex.unBoneNo[0]].szName; } if (vertex.unBoneNo[1] <= -1) { vertex.unBoneName[1] = null; } else { vertex.unBoneName[1] = pmd_bone[vertex.unBoneNo[1]].szName; } } // ----------------------------------------------------- // IK配列数取得 number_of_ik = reader.readShort(); // IK配列取得 pmd_ik = new PMD_IK[number_of_ik]; if (number_of_ik > 0) { for (int i = 0; i < number_of_ik; i++) { pmd_ik[i] = new PMD_IK(); pmd_ik[i].read(reader); } } // ボーンIDを名前に置き換え foreach (PMD_IK ik in pmd_ik) { if (ik.nTargetNo <= -1) { ik.nTargetName = null; } else { ik.nTargetName = pmd_bone[ik.nTargetNo].szName; } if (ik.nEffNo <= -1) { ik.nEffName = null; } else { ik.nEffName = pmd_bone[ik.nEffNo].szName; } for (int i = 0; i < ik.cbNumLink; i++) { if (ik.punLinkNo[i] <= -1) { ik.punLinkName[i] = null; } else { ik.punLinkName[i] = pmd_bone[ik.punLinkNo[i]].szName; } } } // ----------------------------------------------------- // Face数取得 number_of_face = reader.readShort(); // Face配列取得 pmd_face = new PMD_FACE[number_of_face]; if (number_of_face > 0) { for (int i = 0; i < number_of_face; i++) { pmd_face[i] = new PMD_FACE(); pmd_face[i].read(reader); } } // ----------------------------------------------------- // 表情枠 // ----------------------------------------------------- // 表情枠に表示する表情数 skin_disp_count = reader.readByte(); // 表情番号 skin_index = new int[skin_disp_count]; if (skin_disp_count > 0) { for (int i = 0; i < skin_disp_count; i++) { skin_index[i] = reader.readShort(); } } // ----------------------------------------------------- // ボーン枠 // ----------------------------------------------------- // ボーン枠用の枠名数 bone_disp_name_count = reader.readByte(); // 枠名(50Bytes/枠) disp_name = new string[bone_disp_name_count]; if (bone_disp_name_count > 0) { for (int i = 0; i < bone_disp_name_count; i++) { disp_name[i] = reader.readAscii(50); } } // ----------------------------------------------------- // ボーン枠に表示するボーン // ----------------------------------------------------- // ボーン枠に表示するボーン数 bone_disp_count = reader.readInt(); // 枠用ボーンデータ (3Bytes/bone) bone_disp = new PMD_BoneDisp[bone_disp_count]; if (bone_disp_count > 0) { for (int i = 0; i < bone_disp_count; i++) { bone_disp[i] = new PMD_BoneDisp(); bone_disp[i].read(reader); } } // ボーンIDを名前に置き換え for (int i = 0; i < bone_disp.Length; i++) { bone_disp[i].bone_name = pmd_bone[bone_disp[i].bone_index].szName; } // ----------------------------------------------------- // 英名対応(0:英名対応なし, 1:英名対応あり) // ----------------------------------------------------- english_name_compatibility = reader.readByte(); if (english_name_compatibility == 1) { model_name_eg = reader.readAscii(20); // モデル名(英語) comment_eg = reader.readAscii(256); // コメント(英語) // ----------------------------------------------------- // ボーン名(英語) // ----------------------------------------------------- bone_name_eg = new string[number_of_bone]; if (number_of_bone > 0) { for (int i = 0; i < number_of_bone; i++) { bone_name_eg[i] = reader.readAscii(20); } } // ----------------------------------------------------- // 表情名(英語) // ----------------------------------------------------- skin_name_eg = new string[number_of_face - 1]; if (number_of_face - 1 > 0) { for (int i = 0; i < number_of_face - 1; i++) { skin_name_eg[i] = reader.readAscii(20); } } // ----------------------------------------------------- // 枠名(英語) // ----------------------------------------------------- disp_name_eg = new string[bone_disp_name_count]; if (bone_disp_name_count > 0) { for (int i = 0; i < bone_disp_name_count; i++) { disp_name_eg[i] = reader.readAscii(50); } } } // ----------------------------------------------------- // トゥーンテクスチャファイル名 // ----------------------------------------------------- toon_file_name = new string[10]; for (int i = 0; i < 10; i++) { toon_file_name[i] = reader.readAscii(100); } // ----------------------------------------------------- // 剛体 // ----------------------------------------------------- // 剛体数 rigidbody_count = reader.readInt(); // 剛体データ(83Bytes/rigidbody) rigidbody = new PMD_RigidBody[rigidbody_count]; if (rigidbody_count > 0) { for (int i = 0; i < rigidbody_count; i++) { rigidbody[i] = new PMD_RigidBody(); rigidbody[i].read(reader); } } // ----------------------------------------------------- // ジョイント // ----------------------------------------------------- // ジョイント数 joint_count = reader.readInt(); // ジョイントデータ(124Bytes/joint) joint = new PMD_Joint[joint_count]; if (joint_count > 0) { for (int i = 0; i < joint_count; i++) { joint[i] = new PMD_Joint(); joint[i].read(reader); } } return; }