public void LoadSubFiles() { foreach (Bar.Entry barEntry in BarFile) { try { switch (barEntry.Type) { case Bar.EntryType.Model: ModelFile = Mdlx.Read(barEntry.Stream); break; case Bar.EntryType.ModelTexture: TextureFile = ModelTexture.Read(barEntry.Stream); break; case Bar.EntryType.ModelCollision: CollisionFile = new ModelCollision(barEntry.Stream); break; default: break; } } catch (Exception e) { } } }
public MdlxParser(Mdlx mdlx) { if (IsEntity(mdlx)) { var builtModel = FromEntity(mdlx); Model = new Model { Segments = builtModel.textureIndexBasedModelDict.Values.Select(x => new Model.Segment { Vertices = x.Vertices.Select(vertex => new PositionColoredTextured { X = vertex.X, Y = vertex.Y, Z = vertex.Z, U = vertex.Tu, V = vertex.Tv, Color = vertex.Color }).ToArray() }).ToArray(), Parts = builtModel.parser.MeshDescriptors.Select(x => new Model.Part { Indices = x.Indices, SegmentIndex = x.SegmentIndex, TextureIndex = x.TextureIndex, IsOpaque = x.IsOpaque }).ToArray() }; } else if (IsMap(mdlx)) { MeshDescriptors = mdlx.MapModel.VifPackets .Select(vifPacket => Parse(vifPacket)) .ToList(); } }
public NewModelParser(Mdlx mdlx) { Mdlx = mdlx; Vertices = new List <CustomVertex.PositionColoredTextured>(); MeshDescriptors = new List <MeshDescriptor>(); Parse(mdlx.MapModel.VifPackets); }
public MdlxParser(Mdlx mdlx) { if (IsEntity(mdlx)) { var builtModel = FromEntity(mdlx); Model = new Model { Segments = builtModel.textureIndexBasedModelDict.Values.Select(x => new Model.Segment { Vertices = x.Vertices.Select(vertex => new PositionColoredTextured { X = vertex.X, Y = vertex.Y, Z = vertex.Z, U = vertex.Tu, V = vertex.Tv, Color = vertex.Color }).ToArray() }).ToArray(), Parts = builtModel.parser.MeshDescriptors.Select(x => new Model.Part { Indices = x.Indices, SegmentIndex = x.SegmentIndex, TextureIndex = x.TextureIndex }).ToArray() }; } else if (IsMap(mdlx)) { var myParser = new NewModelParser(mdlx); Model = new Model { Segments = new Model.Segment[] { new Model.Segment { Vertices = myParser.Vertices.Select(vertex => new PositionColoredTextured { X = vertex.X, Y = vertex.Y, Z = vertex.Z, U = vertex.Tu, V = vertex.Tv, Color = vertex.Color }).ToArray() } }, Parts = myParser.MeshDescriptors.Select(x => new Model.Part { Indices = x.Indices, SegmentIndex = x.SegmentIndex, TextureIndex = x.TextureIndex }).ToArray() }; } }
public void ReadRemapTable() => File.OpenRead(MapFileName).Using(stream => { var remapTable = Mdlx.Read(stream).MapModel.DmaChainIndexRemapTable; Assert.Equal(97, remapTable.Count); Assert.Equal(0, remapTable[0]); Assert.Equal(1, remapTable[1]); Assert.Equal(2, remapTable[2]); Assert.Equal(96, remapTable[96]); });
public void ReadAlb1t2Table() => File.OpenRead(MapFileName).Using(stream => { var alb1t2 = Mdlx.Read(stream).MapModel.alb1t2; Assert.Equal(97, alb1t2.Count); Assert.Equal(0, alb1t2[0]); Assert.Equal(1, alb1t2[1]); Assert.Equal(2, alb1t2[2]); Assert.Equal(96, alb1t2[96]); });
public static MeshGroup FromKH2(Mdlx model) { if (model == null) { return(null); } var modelParsed = new MdlxParser(model); return(LoadKH2New(modelParsed)); }
public ModelFile_Control(Mdlx ModelFile) { InitializeComponent(); modelControlModel = new ModelFile_VM(ModelFile); DataContext = modelControlModel; if (modelControlModel.ModelFile != null && modelControlModel.ModelFile.SubModels.Count > 0) { contentFrame.Content = new SubModel_Control(modelControlModel.ModelFile.SubModels[0]); } }
public void ShouldWriteBackTheExactSameFile() => File.OpenRead(FileName).Using(stream => { Helpers.AssertStream(stream, inStream => { var mdlx = Mdlx.Read(inStream); var outStream = new MemoryStream(); mdlx.Write(outStream); return(outStream); }); });
public MeshGroupModel(GraphicsDevice g, string name, Mdlx map, List <ModelTexture.Texture> texture, int index) { _graphics = g; _model = map; Name = name; Texture = texture; Index = index; IsVisible = true; InvalidateTextures(); Invalidate(); }
public void WriteMapBack() => File.OpenRead(MapFileName).Using(stream => { Helpers.AssertStream(stream, inStream => { var mdlx = Mdlx.Read(inStream); var outStream = new MemoryStream(); mdlx.Write(outStream); return(outStream); }); });
public static MeshGroup FromKH2(Mdlx model) { if (model == null) { return(null); } var modelParsed = new MdlxParser(model); return(modelParsed.MeshDescriptors != null? LoadKH2New(modelParsed) : LoadKH2Legacy(modelParsed)); }
public static MeshGroup FromKH2(GraphicsDevice graphics, Mdlx model, ModelTexture texture) { if (model == null || texture == null) { return(null); } var meshGroup = FromKH2(model); meshGroup.Textures = LoadTextures(graphics, texture).ToArray(); return(meshGroup); }
public static MeshGroup FromKH2(GraphicsDevice graphics, Mdlx model, ModelTexture texture) { if (model == null || texture == null) { return(null); } var modelParsed = new MdlxParser(model); return(modelParsed.MeshDescriptors != null? LoadNew(graphics, modelParsed, texture) : LoadLegacy(graphics, modelParsed, texture)); }
public void ReadRenderingGroup() => File.OpenRead(MapFileName).Using(stream => { var renderingGroup = Mdlx.Read(stream).MapModel.vifPacketRenderingGroup; Assert.Equal(9, renderingGroup.Count); Assert.Equal(20, renderingGroup[0].Length); Assert.Equal(1, renderingGroup[0][0]); Assert.Equal(91, renderingGroup[0][19]); Assert.Equal(3, renderingGroup[8].Length); Assert.Equal(30, renderingGroup[8][0]); Assert.Equal(68, renderingGroup[8][2]); });
public void ReadAlb2() => File.OpenRead(MapFileName).Using(stream => { var alb2 = Mdlx.Read(stream).MapModel.alb2; Assert.Equal(9, alb2.Count); Assert.Equal(20, alb2[0].Length); Assert.Equal(1, alb2[0][0]); Assert.Equal(91, alb2[0][19]); Assert.Equal(3, alb2[8].Length); Assert.Equal(30, alb2[8][0]); Assert.Equal(68, alb2[8][2]); });
public MeshGroup this[int objId] { get { if (_meshGroups.TryGetValue(objId, out var meshGroup)) { return(meshGroup); } var objEntryName = _objEntryLookupReversed[objId]; var modelPath = Path.Combine(_objPath, objEntryName); var modelFileName = modelPath + ".mdlx"; if (File.Exists(modelFileName)) { var mdlxEntries = File.OpenRead(modelFileName).Using(Bar.Read); var modelEntry = mdlxEntries.FirstOrDefault(x => x.Type == Bar.EntryType.Model); if (modelEntry != null) { var model = Mdlx.Read(modelEntry.Stream); ModelTexture textures = null; var textureEntry = mdlxEntries.FirstOrDefault(x => x.Type == Bar.EntryType.ModelTexture); if (textureEntry != null) { textures = ModelTexture.Read(textureEntry.Stream); } var modelMotion = MeshLoader.FromKH2(model); modelMotion.ApplyMotion(modelMotion.InitialPose); meshGroup = new MeshGroup { MeshDescriptors = modelMotion.MeshDescriptors, Textures = textures == null ? new IKingdomTexture[0] : textures.LoadTextures(_graphics).ToArray() }; } else { meshGroup = EmptyMeshGroup; } } else { meshGroup = EmptyMeshGroup; } _meshGroups[objId] = meshGroup; return(meshGroup); } }
internal static MemoryStream CreateMdlxHaving2Bones(float tx = 0) { var model = Mdlx.CreateModelFromScratch(); model.SubModels[0].BoneCount = 2; model.SubModels[0].Bones.Add( new Mdlx.Bone { Index = 0, Parent = -1, ScaleX = 1, ScaleY = 1, ScaleZ = 1, ScaleW = 1, } ); model.SubModels[0].Bones.Add( new Mdlx.Bone { Index = 1, Parent = 0, ScaleX = 1, ScaleY = 1, ScaleZ = 1, ScaleW = 1, TranslationX = tx, } ); var modelBin = new MemoryStream(); model.Write(modelBin); var mdlxFile = new MemoryStream(); Bar.Write( mdlxFile, new Bar.Entry[] { new Bar.Entry { Type = Bar.EntryType.Model, Stream = modelBin, Name = "p_ex", }, } ); return(mdlxFile); }
public void LoadMesh(GraphicsDevice graphics) { var objEntry = Kernel.ObjEntries.FirstOrDefault(x => x.ObjectId == ObjectId); if (objEntry == null) { Log.Warn("Object ID {0} not found.", ObjectId); return; } IsPlayer = objEntry.ObjectType == Objentry.Type.PLAYER; var modelName = $"obj/{objEntry.ModelName}.mdlx"; using var stream = Kernel.DataContent.FileOpen(modelName); var entries = Bar.Read(stream); _model = entries.ForEntry(x => x.Type == Bar.EntryType.Model, Mdlx.Read); Model = MeshLoader.FromKH2(_model); var texture = entries.ForEntry("tim_", Bar.EntryType.ModelTexture, ModelTexture.Read); Textures = texture.LoadTextures(graphics).ToArray(); ObjectCollisions = entries.ForEntry(x => x.Type == Bar.EntryType.ModelCollision && x.Stream.Length > 0, ObjectCollision.Read) ?? new List <ObjectCollision>(); try { var msetName = $"obj/{objEntry.AnimationName}"; if (Kernel.DataContent.FileExists(msetName)) { var msetEntries = Kernel.DataContent.FileOpen(msetName).Using(Bar.Read); Motion = new Kh2MotionEngine(msetEntries) { CurrentAnimationIndex = 0 }; } else { Motion = new Kh2MotionEngine(); Log.Warn("MSET {0} does not exist", objEntry.AnimationName); } } catch (System.NotImplementedException) { Motion = null; } }
public MdlxParser(Mdlx mdlx) { if (IsEntity(mdlx)) { InitialPose = BuildTPoseMatrices(mdlx.SubModels.First(), Matrix4x4.Identity); Bones = mdlx.SubModels.First().Bones; _parsedModel = new Kkdf2MdlxParser(mdlx.SubModels.First()); MeshDescriptors = _parsedModel.ProcessVerticesAndBuildModel(InitialPose); } else if (IsMap(mdlx)) { MeshDescriptors = mdlx.MapModel.VifPackets .Select(vifPacket => Parse(vifPacket)) .ToList(); } }
public MdlxParser(Mdlx mdlx) { if (IsEntity(mdlx)) { MeshDescriptors = new Kkdf2MdlxParser(mdlx.SubModels.First()) .ProcessVerticesAndBuildModel( MdlxMatrixUtil.BuildTPoseMatrices(mdlx.SubModels.First(), Matrix4x4.Identity) ); } else if (IsMap(mdlx)) { MeshDescriptors = mdlx.MapModel.VifPackets .Select(vifPacket => Parse(vifPacket)) .ToList(); } }
public void ShouldReadVifPackets() => File.OpenRead(FileName).Using(stream => { var dmaChain = Mdlx.Read(stream).SubModels[0].DmaChains; Assert.Equal(26, dmaChain[0].DmaVifs.Count); Assert.Equal(4, dmaChain.Count); Assert.Equal(58, dmaChain.Sum(x => x.DmaVifs.Count)); var dmaVif = dmaChain[0].DmaVifs[0]; Assert.Equal(0, dmaVif.TextureIndex); Assert.Equal(10, dmaVif.Alaxi.Length); Assert.Equal(53, dmaVif.Alaxi[0]); Assert.Equal(22, dmaVif.Alaxi[9]); Assert.Equal(1600, dmaVif.VifPacket.Length); Assert.Equal(1, dmaVif.VifPacket[0]); Assert.Equal(248, dmaVif.VifPacket[324]); });
public void ReadVifPackets() => File.OpenRead(MapFileName).Using(stream => { var alvifpkt = Mdlx.Read(stream).MapModel.VifPackets; Assert.Equal(97, alvifpkt.Count); var packet1 = alvifpkt[0]; Assert.Equal(1, packet1.TextureId); Assert.Equal(1184, packet1.VifPacket.Length); Assert.Equal(1, packet1.VifPacket[0]); Assert.Equal(0, packet1.VifPacket[10]); Assert.Equal(64, packet1.VifPacket[100]); Assert.Equal(76, packet1.VifPacket[1170]); var packet2 = alvifpkt[96]; Assert.Equal(2, packet2.TextureId); Assert.Equal(960, packet2.VifPacket.Length); Assert.Equal(1, packet2.VifPacket[0]); Assert.Equal(117, packet2.VifPacket[500]); });
private static Kkdf2MdlxBuiltModel FromEntity(Mdlx mdlx) { var parser = new Kddf2.Kkdf2MdlxParser(mdlx.SubModels.First()); var builtModel = parser .ProcessVerticesAndBuildModel( MdlxMatrixUtil.BuildTPoseMatrices(mdlx.SubModels.First(), Matrix.Identity) ); var ci = builtModel.textureIndexBasedModelDict.Values.Select((model, i) => new Kddf2.Kkdf2MdlxParser.CI { Indices = model.Vertices.Select((_, index) => index).ToArray(), TextureIndex = i, SegmentIndex = i }); parser.MeshDescriptors.AddRange(ci); return(builtModel); }
public void OpenMdlxFile(string filePath) { using var stream = File.Open(filePath, FileMode.Open); Bar binarc = Bar.Read(stream); ObservableCollection <Bar.Entry> testColl = new ObservableCollection <Bar.Entry>(binarc); testColl[0].Name = "Changed"; Debug.WriteLine("TEST: " + binarc[0].Name); Debug.WriteLine("TEST: " + testColl[0].Name); if (binarc != null) { Mdlx modelFile; ModelTexture textureFile; ModelCollision collisionFile; foreach (Bar.Entry barEntry in binarc) { switch (barEntry.Type) { case Bar.EntryType.Model: modelFile = Mdlx.Read(barEntry.Stream); break; case Bar.EntryType.ModelTexture: textureFile = ModelTexture.Read(barEntry.Stream); break; case Bar.EntryType.ModelCollision: collisionFile = new ModelCollision(barEntry.Stream); break; default: break; } } Debug.WriteLine("BREAKPOINT"); } }
public void ShouldReadBones() => File.OpenRead(FileName).Using(stream => { var bones = Mdlx.Read(stream).SubModels[0].Bones; Assert.Equal(228, bones.Count); var bone = bones[0]; Assert.Equal(0, bone.Index); Assert.Equal(-1, bone.Parent); Assert.Equal(0, bone.Unk08); Assert.Equal(3, bone.Unk0c); Assert.Equal(1, bone.ScaleX); Assert.Equal(0, bone.RotationX); Assert.Equal(0, bone.TranslationX); Assert.Equal(1, bone.ScaleY); Assert.Equal(4.71, bone.RotationY, 2); Assert.Equal(102.62, bone.TranslationY, 2); Assert.Equal(1, bone.ScaleZ); Assert.Equal(4.71, bone.RotationZ, 2); Assert.Equal(0, bone.TranslationZ); Assert.Equal(0, bone.ScaleW); Assert.Equal(0, bone.RotationW); Assert.Equal(0, bone.TranslationW); });
public MeshGroup this[int objId] { get { if (_meshGroups.TryGetValue(objId, out var meshGroup)) { return(meshGroup); } var objEntryName = _objEntryLookupReversed[objId]; var modelPath = Path.Combine(_objPath, objEntryName); var modelFileName = modelPath + ".mdlx"; if (File.Exists(modelFileName)) { var mdlxEntries = File.OpenRead(modelFileName).Using(Bar.Read); var modelEntry = mdlxEntries.FirstOrDefault(x => x.Type == Bar.EntryType.Model); if (modelEntry != null) { var model = Mdlx.Read(modelEntry.Stream); var textures = ModelTexture.Read(mdlxEntries.First(x => x.Type == Bar.EntryType.ModelTexture).Stream); meshGroup = MeshLoader.FromKH2(_graphics, model, textures); } else { meshGroup = EmptyMeshGroup; } } else { meshGroup = EmptyMeshGroup; } _meshGroups[objId] = meshGroup; return(meshGroup); } }
public List <Bar.Entry> GetBarEntries(Action <string, MemoryStream> trySaveTo = null) { var entries = new List <Bar.Entry>(); { var mapBin = new MemoryStream(); Mdlx.CreateFromMapModel(mapModel).Write(mapBin); mapBin.Position = 0; entries.Add( new Bar.Entry { Name = config.bar?.model?.name ?? "MAP", Type = Bar.EntryType.Model, Stream = mapBin, } ); trySaveTo?.Invoke(config.bar?.model?.toFile, mapBin); } { var texBin = new MemoryStream(); modelTex.Write(texBin); texBin.Position = 0; entries.Add( new Bar.Entry { Name = config.bar?.texture?.name ?? "MAP", Type = Bar.EntryType.ModelTexture, Stream = texBin, } ); trySaveTo?.Invoke(config.bar?.texture?.toFile, texBin); } { var doctBin = new MemoryStream(); collisionBuilder.doct.Write(doctBin); doctBin.Position = 0; entries.Add( new Bar.Entry { Name = config.bar?.doct?.name ?? "eh_1", Type = Bar.EntryType.MeshOcclusion, Stream = doctBin, } ); trySaveTo?.Invoke(config.bar?.doct?.toFile, doctBin); } { var coctBin = new MemoryStream(); collisionBuilder.coct.Write(coctBin); coctBin.Position = 0; entries.Add( new Bar.Entry { Name = config.bar?.coct?.name ?? "ID_e", Type = Bar.EntryType.MapCollision, Stream = coctBin, } ); trySaveTo?.Invoke(config.bar?.coct?.toFile, coctBin); } return(entries); }
public ModelFile_VM(Mdlx modelFile) { ModelFile = modelFile; subModelList = SubModelWrapper.getObservable(ModelFile.SubModels); }
public static IModelMotion FromKH2(Mdlx model) => model != null ? new MdlxParser(model) : null;