private ZSCPart ReadPart() { uint transFlags = ZSC_TRANSITION_NONE; ZSCPart part = new ZSCPart { MeshID = bh.ReadWord(), MaterialID = bh.ReadWord() }; while (true) { byte IDFlag = br.ReadByte(); if (IDFlag == 0) { break; } byte size = br.ReadByte(); switch (IDFlag) { case ZSC_PART_POSITION: // $01 - Size $0C (12) part.Position = bh.ReadVector3f() * 0.01f; transFlags = transFlags | ZSC_TRANSITION_TRANSLATE; break; case ZSC_PART_ROTATION: // $02 - Size $10 (16) part.Rotation = bh.ReadQuaternion(); transFlags = transFlags | ZSC_TRANSITION_ROTATE; break; case ZSC_PART_SCALE: // $03 - Size $0C (12) part.Scale = bh.ReadVector3f(); transFlags = transFlags | ZSC_TRANSITION_SCALE; break; case ZSC_PART_AXISROTATION: // $04 - Size $0C (12) part.AxisRotation = bh.ReadQuaternion(); break; case ZSC_PART_BONEINDEX: // $05 - Size $02 (02) part.BoneID = (int)bh.ReadWord(); break; case ZSC_PART_DUMMYINDEX: // $06 - Size $02 (02) part.DummyID = (int)bh.ReadWord(); break; case ZSC_PART_PARENT: // $07 - Size $02 (02) part.ParentID = (int)bh.ReadWord(); break; case ZSC_PART_COLLISION: // $1D - Size $02 (02) part.CollisionType = bh.ReadWord(); break; case ZSC_PART_ZMOPATH: // $1E (30) byte[] bs = br.ReadBytes(size); part.ZMOPath = koreanEncoding.GetString(bs); break; case ZSC_PART_RANGEMODE: // $1F - Size $02 (02) part.RangeMode = bh.ReadWord(); break; case ZSC_PART_LIGHTMAPMODE: // $20 - Size $02 (02) part.UseLightmap = bh.ReadWord(); break; default: br.ReadBytes(size); break; } // switch case } ; part.TransformMatrix = Matrix4.IDENTITY; if ((transFlags & ZSC_TRANSITION_ROTATE) != 0) { part.TransformMatrix = new Matrix4(part.Rotation.ToRotationMatrix()); } if ((transFlags & ZSC_TRANSITION_SCALE) != 0) { part.TransformMatrix.SetScale(part.Scale); } if ((transFlags & ZSC_TRANSITION_TRANSLATE) != 0) { part.TransformMatrix.SetTrans(part.Position); } return(part); } // Read PART
public bool Load(string FileName) { Encoding koreanEncoding = Encoding.GetEncoding("EUC-KR"); try { FileStream fileStream = File.OpenRead(FileName); BinaryReader br = new BinaryReader(fileStream, koreanEncoding); bh = new BinaryHelper(br); try { FormatString = koreanEncoding.GetString(br.ReadBytes(7)); BonesCount = br.ReadUInt32(); if (BonesCount > 0) { for (int i = 0; i < BonesCount; i++) { RoseBone bone = new RoseBone(br.ReadInt32(), bh.ReadZString(), bh.ReadVector3f() /** scaleFactor*/, bh.ReadQuaternion()); bone.ID = i; bone.TransformMatrix = new Matrix4(); bone.TransformMatrix.MakeTrans(bone.Position); Matrix4 rotationMatrix = new Matrix4(bone.Rotation); bone.TransformMatrix = (rotationMatrix * bone.TransformMatrix); bone.InverseMatrix = bone.TransformMatrix.Inverse(); Bone.Add(bone); } // for } // if num_bones > 0 DummiesCount = br.ReadUInt32(); if (DummiesCount > 0) { for (int i = 0; i < DummiesCount; i++) { RoseBone dummy = null; if (FormatString.Equals("ZMD0003")) { // dummies are read different then bones; dummy = new RoseBone(bh.ReadZString(), br.ReadInt32(), bh.ReadVector3f() /* * scaleFactor*/, bh.ReadQuaternion()); } // if if (FormatString.Equals("ZMD0002")) { dummy = new RoseBone(bh.ReadZString(), br.ReadInt32(), bh.ReadVector3f() /* * scaleFactor*/); } // if dummy.ID = (int)BonesCount + i; dummy.TransformMatrix = new Matrix4(); dummy.TransformMatrix.MakeTrans(dummy.Position); Matrix4 rotationMatrix = new Matrix4(dummy.Rotation); dummy.TransformMatrix = (rotationMatrix * dummy.TransformMatrix); dummy.InverseMatrix = dummy.TransformMatrix.Inverse(); Dummy.Add(dummy); } // for } // if dummies } // try finally { br.Close(); fileStream.Close(); } } // try open file catch (Exception) { return(false); } // catch open file TransformChildren(0); return(true); } // Load
public bool Load(string FileName, ZMD zmd) { Encoding koreanEncoding = Encoding.GetEncoding("EUC-KR"); try { FileStream fileStream = File.OpenRead(FileName); BinaryReader br = new BinaryReader(fileStream, koreanEncoding); bh = new BinaryHelper(br); Quaternion rotYneg90 = new Quaternion(new Degree(-90f), Vector3.UnitY); try { AnimationName = Path.GetFileNameWithoutExtension(FileName); FormatString = koreanEncoding.GetString(br.ReadBytes(7)); br.ReadByte(); FPS = br.ReadInt32(); Frames = br.ReadInt32(); Channels = br.ReadInt32(); Track = new List <ZMOTrack>(); Channel = new List <ZMOChannel>(); // Read each track description for (int i = 0; i < Channels; i++) { int TracType = br.ReadInt32(); int BoneID = br.ReadInt32(); Channel.Add(new ZMOChannel((ZMOTrack.TrackType)TracType, BoneID)); } // Read tracks data for (int frameIDX = 0; frameIDX < Frames; frameIDX++) { for (int channelIDX = 0; channelIDX < Channels; channelIDX++) { int BoneID = Channel[channelIDX].BoneID; ZMOTrack track = new ZMOTrack(Channel[channelIDX].Type, Channel[channelIDX].BoneID, frameIDX); if (Channel[channelIDX].Type == ZMOTrack.TrackType.POSITION || Channel[channelIDX].Type == ZMOTrack.TrackType.NORMAL) { //read vector track.Position = bh.ReadVector3f() / 100f; if (zmd != null) { zmd.Bone[BoneID].AddAnimationAt(frameIDX, AnimationName, new BoneFrame() { Position = track.Position }, ZMOTrack.TrackType.POSITION); } } // position if (Channel[channelIDX].Type == ZMOTrack.TrackType.ROTATION) { //read quat track.Rotation = bh.ReadQuaternion(); if (zmd != null) { zmd.Bone[BoneID].AddAnimationAt(frameIDX, AnimationName, new BoneFrame() { Rotation = zmd.Bone[BoneID].Rotation * track.Rotation.Inverse() }, ZMOTrack.TrackType.ROTATION); } } // rotation if (Channel[channelIDX].Type == ZMOTrack.TrackType.ALPHA || Channel[channelIDX].Type == ZMOTrack.TrackType.TEXTUREANIM) { //read float track.Value = br.ReadSingle(); } // alpha if (Channel[channelIDX].Type == ZMOTrack.TrackType.SCALE) { track.Value = br.ReadSingle(); if (zmd != null) { zmd.Bone[BoneID].AddAnimationAt(frameIDX, AnimationName, new BoneFrame() { Scale = Vector3.One * track.Value }, ZMOTrack.TrackType.SCALE); } } if (Channel[channelIDX].Type == ZMOTrack.TrackType.UV1 || Channel[channelIDX].Type == ZMOTrack.TrackType.UV2 || Channel[channelIDX].Type == ZMOTrack.TrackType.UV3 || Channel[channelIDX].Type == ZMOTrack.TrackType.UV4) { //read vector track.UV = bh.ReadVector2f(); } // UV Track.Add(track); } // bones } // frames } // try finally { br.Close(); fileStream.Close(); } } // try open file catch (Exception) { return(false); } // catch open file return(true); } // Load
public bool Load(string FileName) { //Quaternion rotYneg90 = new Quaternion(new Degree(-90f), Vector3.UnitY); //rotYneg90 = rotYneg90.Inverse(); Encoding koreanEncoding = Encoding.GetEncoding("EUC-KR"); try { FileStream fileStream = File.OpenRead(FileName); BinaryReader br = new BinaryReader(fileStream, koreanEncoding); bh = new BinaryHelper(br); try { FormatString = koreanEncoding.GetString(br.ReadBytes(7)); BonesCount = br.ReadUInt32(); if (BonesCount > 0) { for (int i = 0; i < BonesCount; i++) { RoseBone bone = new RoseBone(br.ReadInt32(), bh.ReadZString(), bh.ReadVector3f() * scaleFactor, bh.ReadQuaternion()); bone.ID = i; bone.TransformMatrix = new Matrix4(bone.Rotation); bone.TransformMatrix.SetTrans(bone.Position); if (i != 0) { Bone[bone.ParentID].ChildID.Add(i); bone.TransformMatrix *= Bone[bone.ParentID].TransformMatrix; } Bone.Add(bone); } // for } // if num_bones > 0 DummiesCount = br.ReadUInt32(); if (DummiesCount > 0) { for (int i = 0; i < DummiesCount; i++) { RoseBone dummy = null; if (FormatString.Equals("ZMD0003")) { // dummies are read different then bones; dummy = new RoseBone(bh.ReadZString(), br.ReadInt32(), bh.ReadVector3f() * scaleFactor, bh.ReadQuaternion()); } // if if (FormatString.Equals("ZMD0002")) { dummy = new RoseBone(bh.ReadZString(), br.ReadInt32(), bh.ReadVector3f() * scaleFactor); } // if dummy.ID = (int)BonesCount + i; dummy.TransformMatrix = new Matrix4(dummy.Rotation); dummy.TransformMatrix.SetTrans(dummy.Position); if (dummy.ParentID < BonesCount) { dummy.TransformMatrix *= Bone[dummy.ParentID].TransformMatrix; } else { dummy.TransformMatrix *= Dummy[dummy.ParentID].TransformMatrix; } Dummy.Add(dummy); } // for } // if dummies } // try finally { br.Close(); fileStream.Close(); } } // try open file catch (Exception) { return(false); } // catch open file return(true); } // Load
public bool Load(string FileName, ZMD zmd) { Encoding koreanEncoding = Encoding.GetEncoding("EUC-KR"); try { FileStream fileStream = File.OpenRead(FileName); BinaryReader br = new BinaryReader(fileStream, koreanEncoding); bh = new BinaryHelper(br); try { FormatString = koreanEncoding.GetString(br.ReadBytes(7)); br.ReadByte(); FPS = br.ReadInt32(); Frames = br.ReadInt32(); Channels = br.ReadInt32(); Track = new List <ZMOTrack>(); Channel = new List <ZMOChannel>(); // Read each track description for (int i = 0; i < Channels; i++) { int TracType = br.ReadInt32(); int BoneID = br.ReadInt32(); Channel.Add(new ZMOChannel((ZMOTrack.TrackType)TracType, BoneID)); } // set frames number for each bone foreach (RoseBone bone in zmd.Bone) { bone.InitFrames(Frames); } // Read tracks data for (int frameIDX = 0; frameIDX < Frames; frameIDX++) { for (int channelIDX = 0; channelIDX < Channels; channelIDX++) { int BoneID = Channel[channelIDX].BoneID; ZMOTrack track = new ZMOTrack(Channel[channelIDX].Type, Channel[channelIDX].BoneID, frameIDX); if (Channel[channelIDX].Type == ZMOTrack.TrackType.POSITION || Channel[channelIDX].Type == ZMOTrack.TrackType.NORMAL) { //read vector track.Position = bh.ReadVector3f(); if (zmd != null) { zmd.Bone[BoneID].Frame[frameIDX].Position = track.Position; } } // position if (Channel[channelIDX].Type == ZMOTrack.TrackType.ROTATION) { //read quat Quaternion q = bh.ReadQuaternion(); track.Rotation = q; if (zmd != null) { zmd.Bone[BoneID].Frame[frameIDX].Rotation = track.Rotation; } } // rotation if (Channel[channelIDX].Type == ZMOTrack.TrackType.ALPHA || Channel[channelIDX].Type == ZMOTrack.TrackType.TEXTUREANIM) { //read float track.Value = br.ReadSingle(); } // alpha if (Channel[channelIDX].Type == ZMOTrack.TrackType.SCALE) { track.Value = br.ReadSingle(); if (zmd != null) { zmd.Bone[BoneID].Frame[frameIDX].Scale = new Vector3(track.Value, track.Value, track.Value); } } if (Channel[channelIDX].Type == ZMOTrack.TrackType.UV1 || Channel[channelIDX].Type == ZMOTrack.TrackType.UV2 || Channel[channelIDX].Type == ZMOTrack.TrackType.UV3 || Channel[channelIDX].Type == ZMOTrack.TrackType.UV4) { //read vector track.UV = bh.ReadVector2f(); } // UV Track.Add(track); } // bones } // frames } // try finally { br.Close(); fileStream.Close(); } } // try open file catch (Exception) { return(false); } // catch open file return(true); } // Load