private Vertex GetVertex(ByteReader io) { Vertex vertex; vertex.Position=io.GetVector3(); vertex.Normal=io.GetVector3(); vertex.UV=io.GetVector2(); vertex.DeformType=(VERTEX_DEFORM)io.GetByte(); vertex.BoneIndices=new int[4]; vertex.BoneWeights=new float[4]; switch(vertex.DeformType) { case VERTEX_DEFORM.BDEF1: vertex.BoneIndices[0]=GetBoneIndex(io); break; case VERTEX_DEFORM.BDEF2: vertex.BoneIndices[0]=GetBoneIndex(io); vertex.BoneIndices[1]=GetBoneIndex(io); vertex.BoneWeights[0]=io.GetFloat(); break; case VERTEX_DEFORM.BDEF4: vertex.BoneIndices[0]=GetBoneIndex(io); vertex.BoneIndices[1]=GetBoneIndex(io); vertex.BoneIndices[2]=GetBoneIndex(io); vertex.BoneIndices[3]=GetBoneIndex(io); vertex.BoneWeights[0]=io.GetFloat(); vertex.BoneWeights[1]=io.GetFloat(); vertex.BoneWeights[2]=io.GetFloat(); vertex.BoneWeights[3]=io.GetFloat(); break; case VERTEX_DEFORM.SDEF: throw new PmxException("not implemented"); } vertex.EdgeFactor=io.GetFloat(); return vertex; }
private Bone GetBone(ByteReader io) { var bone=new Bone(); bone.Name=GetText(io); bone.EnglishName=GetText(io); bone.Position=io.GetVector3(); bone.ParentIndex=GetBoneIndex(io); bone.Layer=io.GetInt(); bone.Flags=(BONEFLAG)io.GetUShort(); if(bone.HasFlag(BONEFLAG.HAS_TAILBONE)){ bone.TailBoneIndex=GetBoneIndex(io); } else{ bone.TailOffset=io.GetVector3(); } if(bone.HasFlag(BONEFLAG.ROTATION_EFFECTED) || bone.HasFlag(BONEFLAG.TRANSLATION_EFFECTED)){ bone.EffectIndex=GetBoneIndex(io); bone.EffectFactor=io.GetFloat(); } if(bone.HasFlag(BONEFLAG.HAS_FIXEDAXIS)){ bone.FixedAxis=io.GetVector3(); } if(bone.HasFlag(BONEFLAG.HAS_LOCALAXIS)){ bone.LocalAxisX=io.GetVector3(); bone.LocalAxisZ=io.GetVector3(); } if(bone.HasFlag(BONEFLAG.DEFORM_EXTERNAL_PARENT)){ bone.ExternalParentKey=io.GetInt(); } if(bone.HasFlag(BONEFLAG.HAS_IK)){ var ik=new IKSolver(); bone.IKSolver=ik; ik.TargetIndex=GetBoneIndex(io); ik.Iterations=io.GetInt(); ik.UnitRadian=io.GetFloat(); int Count=io.GetInt(); ik.Chains=Enumerable.Range(1, Count).Select(_ =>{ var link=new IKLink(); link.BoneIndex=GetBoneIndex(io); link.IsLimited=io.GetByte()==0 ? false : true; if(link.IsLimited){ link.MinEulerRadians=io.GetVector3(); link.MaxEulerRadians=io.GetVector3(); } return link; }).ToArray(); } return bone; }
private Material GetMaterial(ByteReader io) { var material=new Material(); material.Name=GetText(io); material.EnglishName=GetText(io); material.Diffuse=io.GetVector4(); material.Specular=io.GetVector3(); material.SpecularFactor=io.GetFloat(); material.Ambient=io.GetVector3(); material.Flag=io.GetByte(); material.EdgeColor=io.GetVector4(); material.EdgeSize=io.GetFloat(); material.TextureIndex=GetTextureIndex(io); material.SphereTextureIndex=GetTextureIndex(io); material.SphereMode=(SPHERE_MODE)io.GetByte(); material.UseSharedToon=io.GetByte()==0 ? false : true; material.ToonTextureIndex=GetTextureIndex(io); material.Memo=GetText(io); material.IndexCount=io.GetInt(); return material; }
public static Model loadFromPath(string path) { var io=new ByteReader(File.ReadAllBytes(path)); // pmx header var magic=io.GetAscii(4); if(magic!="PMX "){ throw new PmxException("invalid magic"); } var version=io.GetFloat(); if(version!=2.0f){ throw new PmxException("invalid version"); } // flags int flags=io.GetByte(); if(flags!=8){ throw new PmxException("invalid byte"); } TEXT_ENCODING encoding=(TEXT_ENCODING)io.GetByte(); byte additional_uv=io.GetByte(); byte vertex_index_bytes=io.GetByte(); byte texture_index_bytes=io.GetByte(); byte material_index_bytes=io.GetByte(); byte bone_index_bytes=io.GetByte(); byte morph_index_bytes=io.GetByte(); byte rigidbody_index_bytes=io.GetByte(); var loader=new Loader( GetTextFunc(encoding), GetIndexFunc(vertex_index_bytes), GetIndexFunc(texture_index_bytes), GetIndexFunc(bone_index_bytes) ); return loader.load(io); }