public void Read(FileData f) { textures.Clear(); BFRES b = new BFRES(); temp = f.Pos(); f.Skip(8); //Magic int Version = f.ReadInt(); int ByteOrderMark = f.ReadShort(); int FormatRevision = f.ReadShort(); Text = f.ReadString(f.ReadInt() + temp, -1); f.Skip(2); int strOffset = f.ReadShort(); int relocOffset = f.ReadInt(); int FileSize = f.ReadInt(); target = f.ReadString(f.Pos(), 4); //NX f.Skip(4); int TexturesCount = f.ReadInt(); int InfoPtrsOffset = f.ReadInt(); int DataBlockOffset = f.ReadInt(); int DictOffset = f.ReadInt(); int strDictSize = f.ReadInt(); Text = Text + ".bntx"; BNTXFile = f.GetSection(temp, FileSize); for (int i = 0; i < TexturesCount; i++) { f.Seek(InfoPtrsOffset + temp + i * 8); BRTIOffset = f.ReadInt(); f.Seek(BRTIOffset + temp); // textures.Add(new BRTI(f)); BRTI texture = new BRTI(); texture.Read(f, this); textures.Add(texture); } Nodes.AddRange(textures.ToArray()); }
public BfresMeshEditor(BFRES.Mesh p, BFRES.FMDL_Model mdl, BFRES b) { InitializeComponent(); button1.Enabled = false; textBox1.Text = p.Text; VertCountlabel1.Text = VertCountlabel1.Text + " " + p.vertices.Count.ToString(); PolyCountlabel3.Text = PolyCountlabel3.Text + " " + p.lodMeshes[p.DisplayLODIndex].displayFaceSize.ToString(); skinCountLabel.Text = skinCountLabel.Text + " " + p.VertexSkinCount.ToString(); label3.Text = label3.Text + " " + mdl.skeleton.bones[p.boneIndx].ToString(); mesh = p; model = mdl; bfres = b; foreach (var lod in p.lodMeshes) { LODcomboBox3.Items.Add(lod); } LODcomboBox3.SelectedIndex = p.DisplayLODIndex; List <string> attributes = new List <string>(); foreach (Syroot.NintenTools.NSW.Bfres.GFX.AttribFormat attr in Enum.GetValues(typeof(Syroot.NintenTools.NSW.Bfres.GFX.AttribFormat))) { attributes.Add(attr.ToString()); } attributes.Sort(); foreach (string att in attributes) { comboBox2.Items.Add(att); } int height = 2; foreach (BFRES.Mesh.VertexAttribute att in p.vertexAttributes) { comboBox1.Items.Add(att); } }
public static void WiiU2Switch(string FileName, int CurModel, BFRES b) { ResFile TargetWiiUBFRES = new ResFile(FileName); int CurMdl = 0; foreach (Model mdl in TargetWiiUBFRES.Models.Values) { int CurBn = 0; foreach (Syroot.NintenTools.Bfres.Bone bn in mdl.Skeleton.Bones.Values) { Bone bone = b.models[CurMdl].skeleton.bones[CurBn]; bone.scale[0] = bn.Scale.X; bone.scale[1] = bn.Scale.Y; bone.scale[2] = bn.Scale.Z; bone.rotation[0] = bn.Rotation.X; bone.rotation[1] = bn.Rotation.Y; bone.rotation[2] = bn.Rotation.Z; bone.rotation[3] = bn.Rotation.W; bone.position[0] = bn.Position.X; bone.position[1] = bn.Position.Y; bone.position[2] = bn.Position.Z; CurBn++; } b.models[CurMdl].skeleton.reset(); int CurShape = 0; foreach (Shape shp in mdl.Shapes.Values) { Mesh poly = b.models[CurMdl].poly[CurShape]; //Create a buffer instance which stores all the buffer data // VertexBufferHelperAttrib uv1 = helper["_u1"]; int TotalCount = poly.vertices.Count; int LODCount = 0; uint FaceCount = FaceCount = shp.Meshes[LODCount].IndexCount; uint[] indicesArray = shp.Meshes[LODCount].GetIndices().ToArray(); int TotalFaceCount = poly.lodMeshes[poly.DisplayLODIndex].faces.Count; poly.lodMeshes[poly.DisplayLODIndex].faces.Clear(); for (int face = 0; face < FaceCount; face++) { poly.lodMeshes[poly.DisplayLODIndex].faces.Add((int)indicesArray[face] + (int)shp.Meshes[LODCount].FirstVertex); } if (TotalFaceCount != poly.lodMeshes[poly.DisplayLODIndex].faces.Count) { MessageBox.Show("Error F"); } poly.vertices.Clear(); //Create a buffer instance which stores all the buffer data VertexBufferHelper helper = new VertexBufferHelper(mdl.VertexBuffers[shp.VertexBufferIndex], TargetWiiUBFRES.ByteOrder); //Set each array first from the lib if exist. Then add the data all in one loop Syroot.Maths.Vector4F[] vec4Positions = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4Normals = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4uv0 = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4uv1 = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4uv2 = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4c0 = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4t0 = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4b0 = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4w0 = new Syroot.Maths.Vector4F[0]; Syroot.Maths.Vector4F[] vec4i0 = new Syroot.Maths.Vector4F[0]; foreach (VertexAttrib att in mdl.VertexBuffers[shp.VertexBufferIndex].Attributes.Values) { Mesh.VertexAttribute attr = new Mesh.VertexAttribute(); attr.Name = att.Name; // attr.Format = att.Format; if (att.Name == "_p0") { vec4Positions = WiiUAttributeData(att, helper, "_p0"); } if (att.Name == "_n0") { vec4Normals = WiiUAttributeData(att, helper, "_n0"); } if (att.Name == "_u0") { vec4uv0 = WiiUAttributeData(att, helper, "_u0"); } if (att.Name == "_u1") { vec4uv1 = WiiUAttributeData(att, helper, "_u1"); } if (att.Name == "_u2") { vec4uv2 = WiiUAttributeData(att, helper, "_u2"); } if (att.Name == "_c0") { vec4c0 = WiiUAttributeData(att, helper, "_c0"); } if (att.Name == "_t0") { vec4t0 = WiiUAttributeData(att, helper, "_t0"); } if (att.Name == "_b0") { vec4b0 = WiiUAttributeData(att, helper, "_b0"); } if (att.Name == "_w0") { vec4w0 = WiiUAttributeData(att, helper, "_w0"); } if (att.Name == "_i0") { vec4i0 = WiiUAttributeData(att, helper, "_i0"); } poly.vertexAttributes.Add(attr); } for (int i = 0; i < vec4Positions.Length; i++) { Vertex v = new Vertex(); if (vec4Positions.Length > 0) { v.pos = new Vector3(vec4Positions[i].X, vec4Positions[i].Y, vec4Positions[i].Z); } if (vec4Normals.Length > 0) { v.nrm = new Vector3(vec4Normals[i].X, vec4Normals[i].Y, vec4Normals[i].Z); } if (vec4uv0.Length > 0) { v.uv0 = new Vector2(vec4uv0[i].X, vec4uv0[i].Y); } if (vec4uv1.Length > 0) { v.uv1 = new Vector2(vec4uv1[i].X, vec4uv1[i].Y); } if (vec4uv2.Length > 0) { v.uv2 = new Vector2(vec4uv2[i].X, vec4uv2[i].Y); } if (vec4w0.Length > 0) { v.boneWeights.Add(vec4w0[i].X); v.boneWeights.Add(vec4w0[i].Y); v.boneWeights.Add(vec4w0[i].Z); v.boneWeights.Add(vec4w0[i].W); } if (vec4i0.Length > 0) { v.boneIds.Add((int)vec4i0[i].X); v.boneIds.Add((int)vec4i0[i].Y); v.boneIds.Add((int)vec4i0[i].Z); v.boneIds.Add((int)vec4i0[i].W); } if (vec4t0.Length > 0) { v.tan = new Vector4(vec4t0[i].X, vec4t0[i].Y, vec4t0[i].Z, vec4t0[i].W); } if (vec4b0.Length > 0) { v.bitan = new Vector4(vec4b0[i].X, vec4b0[i].Y, vec4b0[i].Z, vec4b0[i].W); } if (vec4c0.Length > 0) { v.col = new Vector4(vec4c0[i].X, vec4c0[i].Y, vec4c0[i].Z, vec4c0[i].W); } poly.vertices.Add(v); } CurShape++; } CurMdl++; } b.UpdateRenderMeshes(); }
public static void BFRES2OBJ(string fname, BFRES bfres) { using (StreamWriter f = new StreamWriter(fname)) { int FaceShift = 1; int CurMesh = 0; foreach (BFRES.FMDL_Model mdl in bfres.models) { foreach (BFRES.Mesh m in mdl.poly) { List <Vector3> VerticesN = new List <Vector3>(); //a lot of normals are often shared List <string> ExportTextures = new List <string>(); f.WriteLine($"o {m.Text}"); foreach (BFRES.Vertex vtx in m.vertices) { f.WriteLine($"v {vtx.pos.X} {vtx.pos.Y} {vtx.pos.Z}"); f.WriteLine($"vn {vtx.nrm.X} {vtx.nrm.Y} {vtx.nrm.Z}"); VerticesN.Add(vtx.nrm); } Vector2 Scale = new Vector2(1); Vector2 Position = new Vector2(0); if (m.material.matparam.ContainsKey("gsys_bake_st0")) { Vector4 uvShift = m.material.matparam["gsys_bake_st0"].Value_float4; Scale = uvShift.Xy; Position = uvShift.Zw; } if (Scale != new Vector2(1) || Position != new Vector2(0)) { foreach (BFRES.Vertex vtx in m.vertices) { Vector2 st = (vtx.uv1 * Scale) + Position; st = new Vector2(st.X, 1 - st.Y); f.WriteLine($"vt {st.X} {st.Y}"); } } else { foreach (BFRES.Vertex vtx in m.vertices) { f.WriteLine($"vt {vtx.uv0.X * Scale.X + Position.X} {vtx.uv0.Y * Scale.Y + Position.Y}"); } } f.WriteLine($"usemtl {m.material.Name}"); if (!ExportTextures.Contains(m.material.textures[0].Name)) { ExportTextures.Add(m.material.textures[0].Name); } f.WriteLine($"s off"); var vert = m.CreateDisplayVertices(); for (int i = 0; i < m.display.Length; i++) { int[] verts = new int[3] { (int)m.display[i++], (int)m.display[i++], (int)m.display[i] }; int[] normals = new int[3] { VerticesN.IndexOf(m.vertices[verts[0]].nrm), VerticesN.IndexOf(m.vertices[verts[1]].nrm), VerticesN.IndexOf(m.vertices[verts[2]].nrm) }; f.WriteLine($"f {verts[0] + FaceShift}/{verts[0] + FaceShift}/{normals[0] + FaceShift} {verts[1] + FaceShift}/{verts[1] + FaceShift}/{normals[1] + FaceShift} {verts[2] + FaceShift}/{verts[2] + FaceShift}/{normals[2] + FaceShift}"); } FaceShift += vert.Count; Console.WriteLine(m.Text); Console.WriteLine(m.vertices.Count); Console.WriteLine(m.display.Length); CurMesh++; } } } }