2019-07-11 21:22:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-07-16 21:35:21 +00:00
|
|
|
|
using Toolbox;
|
2019-07-11 21:22:59 +00:00
|
|
|
|
using System.Windows.Forms;
|
2019-07-16 21:35:21 +00:00
|
|
|
|
using Toolbox.Library;
|
|
|
|
|
using Toolbox.Library.IO;
|
|
|
|
|
using Toolbox.Library.Forms;
|
|
|
|
|
using Toolbox.Library.Rendering;
|
2019-07-11 21:22:59 +00:00
|
|
|
|
using SuperBMDLib;
|
|
|
|
|
using System.Drawing;
|
2019-07-11 23:38:29 +00:00
|
|
|
|
using SuperBMDLib.Rigging;
|
|
|
|
|
using SuperBMDLib.Geometry.Enums;
|
|
|
|
|
using SuperBMDLib.Util;
|
2019-07-12 21:31:00 +00:00
|
|
|
|
using OpenTK;
|
2019-07-11 21:22:59 +00:00
|
|
|
|
|
|
|
|
|
namespace FirstPlugin
|
|
|
|
|
{
|
2019-07-11 23:38:29 +00:00
|
|
|
|
public class BMD : TreeNodeFile, IFileFormat, IContextMenuNode, ITextureContainer
|
2019-07-11 21:22:59 +00:00
|
|
|
|
{
|
|
|
|
|
public FileType FileType { get; set; } = FileType.Layout;
|
|
|
|
|
|
|
|
|
|
public bool CanSave { get; set; }
|
|
|
|
|
public string[] Description { get; set; } = new string[] { "Gamecube/Wii Binary Model (BMD/BDL)" };
|
|
|
|
|
public string[] Extension { get; set; } = new string[] { "*.bmd", "*.bdl" };
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
|
public IFileInfo IFileInfo { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool Identify(System.IO.Stream stream)
|
|
|
|
|
{
|
2019-07-16 21:35:21 +00:00
|
|
|
|
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
|
2019-07-11 21:22:59 +00:00
|
|
|
|
{
|
|
|
|
|
reader.SetByteOrder(true);
|
|
|
|
|
bool IsBMD = reader.ReadUInt32() == 0x4A334432;
|
|
|
|
|
reader.Position = 0;
|
|
|
|
|
|
|
|
|
|
return IsBMD;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Type[] Types
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
List<Type> types = new List<Type>();
|
|
|
|
|
return types.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-11 23:38:29 +00:00
|
|
|
|
|
|
|
|
|
Viewport viewport
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var editor = LibraryGUI.GetObjectEditor();
|
|
|
|
|
return editor.GetViewport();
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var editor = LibraryGUI.GetObjectEditor();
|
|
|
|
|
editor.LoadViewport(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, STGenericTexture> Textures { get; set; }
|
|
|
|
|
|
|
|
|
|
bool DrawablesLoaded = false;
|
|
|
|
|
public override void OnClick(TreeView treeView)
|
|
|
|
|
{
|
|
|
|
|
if (Runtime.UseOpenGL && !Runtime.UseLegacyGL)
|
|
|
|
|
{
|
|
|
|
|
if (viewport == null)
|
|
|
|
|
{
|
|
|
|
|
viewport = new Viewport(ObjectEditor.GetDrawableContainers());
|
|
|
|
|
viewport.Dock = DockStyle.Fill;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!DrawablesLoaded)
|
|
|
|
|
{
|
|
|
|
|
ObjectEditor.AddContainer(DrawableContainer);
|
|
|
|
|
DrawablesLoaded = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
viewport.ReloadDrawables(DrawableContainer);
|
|
|
|
|
LibraryGUI.LoadEditor(viewport);
|
|
|
|
|
|
|
|
|
|
viewport.Text = Text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BMD_Renderer Renderer;
|
|
|
|
|
|
|
|
|
|
public DrawableContainer DrawableContainer = new DrawableContainer();
|
|
|
|
|
|
2019-07-11 21:22:59 +00:00
|
|
|
|
public Model BMDFile;
|
2019-07-12 23:44:17 +00:00
|
|
|
|
public STSkeleton Skeleton;
|
2019-07-11 21:22:59 +00:00
|
|
|
|
private TreeNode TextureFolder;
|
|
|
|
|
private TreeNode ShapeFolder;
|
2019-07-12 00:23:47 +00:00
|
|
|
|
private TreeNode MaterialFolder;
|
2019-07-12 19:28:14 +00:00
|
|
|
|
private TreeNode SkeletonFolder;
|
2019-07-11 21:22:59 +00:00
|
|
|
|
|
|
|
|
|
public void Load(System.IO.Stream stream)
|
|
|
|
|
{
|
|
|
|
|
Text = FileName;
|
|
|
|
|
CanSave = true;
|
|
|
|
|
|
2019-07-11 23:38:29 +00:00
|
|
|
|
//Set renderer
|
|
|
|
|
Renderer = new BMD_Renderer();
|
2019-07-12 23:44:17 +00:00
|
|
|
|
Skeleton = new STSkeleton();
|
|
|
|
|
|
2019-07-11 23:38:29 +00:00
|
|
|
|
DrawableContainer.Name = FileName;
|
|
|
|
|
DrawableContainer.Drawables.Add(Renderer);
|
2019-07-12 23:44:17 +00:00
|
|
|
|
DrawableContainer.Drawables.Add(Skeleton);
|
|
|
|
|
|
2019-07-11 23:38:29 +00:00
|
|
|
|
Textures = new Dictionary<string, STGenericTexture>();
|
|
|
|
|
|
|
|
|
|
BMD_Renderer.TextureContainers.Add(this);
|
|
|
|
|
|
2019-07-12 23:44:17 +00:00
|
|
|
|
BMDFile = Model.Load(stream);
|
|
|
|
|
LoadBMD(BMDFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadBMD(Model model)
|
|
|
|
|
{
|
|
|
|
|
Nodes.Clear();
|
|
|
|
|
|
2019-07-12 00:23:47 +00:00
|
|
|
|
ShapeFolder = new TreeNode("Shapes");
|
2019-07-12 19:28:14 +00:00
|
|
|
|
SkeletonFolder = new TreeNode("Skeleton");
|
2019-07-12 00:23:47 +00:00
|
|
|
|
MaterialFolder = new TreeNode("Materials");
|
2019-07-11 21:22:59 +00:00
|
|
|
|
TextureFolder = new TreeNode("Textures");
|
|
|
|
|
Nodes.Add(ShapeFolder);
|
2019-07-12 00:23:47 +00:00
|
|
|
|
Nodes.Add(MaterialFolder);
|
2019-07-12 19:28:14 +00:00
|
|
|
|
Nodes.Add(SkeletonFolder);
|
2019-07-11 21:22:59 +00:00
|
|
|
|
Nodes.Add(TextureFolder);
|
|
|
|
|
|
2019-07-12 23:44:17 +00:00
|
|
|
|
BMDFile = model;
|
|
|
|
|
|
|
|
|
|
FillSkeleton(BMDFile.Scenegraph, Skeleton, BMDFile.Joints.FlatSkeleton);
|
2019-07-12 19:28:14 +00:00
|
|
|
|
|
2019-07-12 23:44:17 +00:00
|
|
|
|
foreach (var bone in Skeleton.bones)
|
2019-07-12 19:28:14 +00:00
|
|
|
|
{
|
|
|
|
|
if (bone.Parent == null)
|
|
|
|
|
SkeletonFolder.Nodes.Add(bone);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 21:22:59 +00:00
|
|
|
|
for (int i = 0; i < BMDFile.Shapes.Shapes.Count; i++)
|
|
|
|
|
{
|
2019-07-13 00:06:31 +00:00
|
|
|
|
var curShape = BMDFile.Shapes.Shapes[i];
|
2019-07-12 19:28:14 +00:00
|
|
|
|
|
2019-07-12 00:56:11 +00:00
|
|
|
|
var mat = new BMDMaterialWrapper(BMDFile.Materials.GetMaterial(i), BMDFile);
|
2019-07-12 00:23:47 +00:00
|
|
|
|
MaterialFolder.Nodes.Add(mat);
|
|
|
|
|
|
2019-07-12 19:28:14 +00:00
|
|
|
|
var shpWrapper = new BMDShapeWrapper(curShape, BMDFile, mat);
|
2019-07-11 21:22:59 +00:00
|
|
|
|
shpWrapper.Text = $"Shape {i}";
|
|
|
|
|
ShapeFolder.Nodes.Add(shpWrapper);
|
2019-07-11 23:38:29 +00:00
|
|
|
|
Renderer.Meshes.Add(shpWrapper);
|
|
|
|
|
|
|
|
|
|
var polyGroup = new STGenericPolygonGroup();
|
|
|
|
|
shpWrapper.PolygonGroups.Add(polyGroup);
|
|
|
|
|
|
|
|
|
|
var VertexAttributes = BMDFile.VertexData.Attributes;
|
|
|
|
|
|
|
|
|
|
int vertexID = 0;
|
2019-07-15 01:08:47 +00:00
|
|
|
|
int packetID = 0;
|
2019-07-15 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
foreach (var att in curShape.Descriptor.Attributes)
|
|
|
|
|
shpWrapper.Nodes.Add($"Attribute {att.Key} {att.Value.Item1}");
|
|
|
|
|
|
2019-07-11 23:38:29 +00:00
|
|
|
|
foreach (SuperBMDLib.Geometry.Packet pack in curShape.Packets)
|
|
|
|
|
{
|
2019-07-15 19:41:02 +00:00
|
|
|
|
int primID = 0;
|
2019-07-11 23:38:29 +00:00
|
|
|
|
foreach (SuperBMDLib.Geometry.Primitive prim in pack.Primitives)
|
|
|
|
|
{
|
|
|
|
|
List<SuperBMDLib.Geometry.Vertex> triVertices = J3DUtility.PrimitiveToTriangles(prim);
|
|
|
|
|
for (int triIndex = 0; triIndex < triVertices.Count; triIndex += 3)
|
|
|
|
|
{
|
|
|
|
|
polyGroup.faces.AddRange(new int[] { vertexID + 2, vertexID + 1, vertexID });
|
|
|
|
|
|
|
|
|
|
for (int triVertIndex = 0; triVertIndex < 3; triVertIndex++)
|
|
|
|
|
{
|
|
|
|
|
SuperBMDLib.Geometry.Vertex vert = triVertices[triIndex + triVertIndex];
|
|
|
|
|
|
|
|
|
|
Vertex vertex = new Vertex();
|
|
|
|
|
vertex.pos = VertexAttributes.Positions[(int)vert.GetAttributeIndex(GXVertexAttribute.Position)];
|
|
|
|
|
shpWrapper.vertices.Add(vertex);
|
|
|
|
|
|
|
|
|
|
if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Normal))
|
|
|
|
|
vertex.nrm = VertexAttributes.Normals[(int)vert.NormalIndex];
|
|
|
|
|
if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Color0))
|
|
|
|
|
{
|
|
|
|
|
var color0 = VertexAttributes.Color_0[(int)vert.Color0Index];
|
|
|
|
|
vertex.col = new OpenTK.Vector4(color0.R, color0.G, color0.B, color0.A);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 21:31:00 +00:00
|
|
|
|
for (int j = 0; j < vert.VertexWeight.WeightCount; j++)
|
|
|
|
|
{
|
|
|
|
|
vertex.boneWeights.Add(vert.VertexWeight.Weights[j]);
|
|
|
|
|
vertex.boneIds.Add(vert.VertexWeight.BoneIndices[j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vert.VertexWeight.WeightCount == 1)
|
|
|
|
|
{
|
|
|
|
|
if (BMDFile.SkinningEnvelopes.InverseBindMatrices.Count > vert.VertexWeight.BoneIndices[0])
|
|
|
|
|
{
|
|
|
|
|
Matrix4 test = BMDFile.SkinningEnvelopes.InverseBindMatrices[vert.VertexWeight.BoneIndices[0]].Inverted();
|
|
|
|
|
test.Transpose();
|
|
|
|
|
vertex.pos = OpenTK.Vector3.TransformPosition(vertex.pos, test);
|
2019-07-12 21:33:48 +00:00
|
|
|
|
vertex.nrm = OpenTK.Vector3.TransformNormal(vertex.nrm, test);
|
2019-07-12 21:31:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
vertex.pos = OpenTK.Vector3.TransformPosition(vertex.pos, BMDFile.Joints.FlatSkeleton[vert.VertexWeight.BoneIndices[0]].TransformationMatrix);
|
2019-07-12 21:33:48 +00:00
|
|
|
|
vertex.nrm = OpenTK.Vector3.TransformNormal(vertex.nrm, BMDFile.Joints.FlatSkeleton[vert.VertexWeight.BoneIndices[0]].TransformationMatrix);
|
2019-07-12 21:31:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:38:29 +00:00
|
|
|
|
for (int texCoordNum = 0; texCoordNum < 8; texCoordNum++)
|
|
|
|
|
{
|
|
|
|
|
if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Tex0 + texCoordNum))
|
|
|
|
|
{
|
|
|
|
|
switch (texCoordNum)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
vertex.uv0 = VertexAttributes.TexCoord_0[(int)vert.TexCoord0Index];
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
vertex.uv1 = VertexAttributes.TexCoord_0[(int)vert.TexCoord0Index];
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
vertex.uv2 = VertexAttributes.TexCoord_0[(int)vert.TexCoord0Index];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vertexID++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-15 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
primID++;
|
2019-07-11 23:38:29 +00:00
|
|
|
|
}
|
2019-07-15 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
packetID++;
|
2019-07-11 23:38:29 +00:00
|
|
|
|
}
|
2019-07-11 21:22:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 19:28:14 +00:00
|
|
|
|
CorrectMaterialIndices(Renderer.Meshes, BMDFile.Scenegraph, BMDFile.Materials);
|
|
|
|
|
|
2019-07-11 21:22:59 +00:00
|
|
|
|
for (int i = 0; i < BMDFile.Textures.Textures.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var texWrapper = new BMDTextureWrapper(BMDFile.Textures.Textures[i]);
|
|
|
|
|
TextureFolder.Nodes.Add(texWrapper);
|
2019-07-12 21:53:57 +00:00
|
|
|
|
Renderer.TextureList.Add(texWrapper);
|
2019-07-11 21:22:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 19:28:14 +00:00
|
|
|
|
public void CorrectMaterialIndices(List<GenericRenderedObject> Meshes, SuperBMDLib.BMD.INF1 INF1, SuperBMDLib.BMD.MAT3 materials)
|
|
|
|
|
{
|
|
|
|
|
foreach (SuperBMDLib.Scenegraph.SceneNode node in INF1.FlatNodes)
|
|
|
|
|
{
|
|
|
|
|
if (node.Type == SuperBMDLib.Scenegraph.Enums.NodeType.Shape)
|
|
|
|
|
{
|
|
|
|
|
if (node.Index < Meshes.Count)
|
|
|
|
|
{
|
|
|
|
|
int matIndex = node.Parent.Index;
|
|
|
|
|
((BMDShapeWrapper)Meshes[node.Index]).SetMaterial((STGenericMaterial)MaterialFolder.Nodes[materials.m_RemapIndices[matIndex]]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FillSkeleton(SuperBMDLib.BMD.INF1 INF1, STSkeleton skeleton, List<SuperBMDLib.Rigging.Bone> flatSkeleton)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 1; i < INF1.FlatNodes.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
SuperBMDLib.Scenegraph.SceneNode curNode = INF1.FlatNodes[i];
|
|
|
|
|
|
|
|
|
|
if (curNode.Type == SuperBMDLib.Scenegraph.Enums.NodeType.Joint)
|
|
|
|
|
{
|
|
|
|
|
var Bone = flatSkeleton[curNode.Index];
|
|
|
|
|
|
|
|
|
|
var stBone = new STBone(skeleton);
|
|
|
|
|
stBone.Text = Bone.Name;
|
|
|
|
|
stBone.FromTransform(Bone.TransformationMatrix);
|
|
|
|
|
|
|
|
|
|
if (Bone.Parent != null)
|
|
|
|
|
stBone.parentIndex = flatSkeleton.IndexOf(Bone.Parent);
|
|
|
|
|
else
|
|
|
|
|
stBone.parentIndex = -1;
|
|
|
|
|
|
|
|
|
|
skeleton.bones.Add(stBone);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 21:22:59 +00:00
|
|
|
|
public ToolStripItem[] GetContextMenuItems()
|
|
|
|
|
{
|
|
|
|
|
List<ToolStripItem> Items = new List<ToolStripItem>();
|
|
|
|
|
Items.Add(new STToolStipMenuItem("Save", null, SaveAction, Keys.Control | Keys.S));
|
2019-07-12 23:44:17 +00:00
|
|
|
|
Items.Add(new STToolStripSeparator());
|
|
|
|
|
Items.Add(new STToolStipMenuItem("Export", null, ExportAction, Keys.Control | Keys.E));
|
|
|
|
|
Items.Add(new STToolStipMenuItem("Replace", null, ReplaceAction, Keys.Control | Keys.R));
|
2019-07-11 21:22:59 +00:00
|
|
|
|
return Items.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 23:44:17 +00:00
|
|
|
|
private void ExportAction(object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
SaveFileDialog sfd = new SaveFileDialog();
|
|
|
|
|
sfd.Filter = "Collada DAE |*.dae;";
|
|
|
|
|
if (sfd.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
BMDFile.ExportAssImp(sfd.FileName, "dae", new ExportSettings());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReplaceAction(object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
|
|
|
ofd.Filter = "Collada DAE |*.dae;";
|
|
|
|
|
if (ofd.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2019-07-13 00:01:22 +00:00
|
|
|
|
BMDModelImportSettings settings = new BMDModelImportSettings();
|
|
|
|
|
if (settings.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
Arguments arguments = new Arguments();
|
|
|
|
|
arguments.input_path = ofd.FileName;
|
|
|
|
|
arguments.texheaders_path = settings.TexturePath;
|
|
|
|
|
arguments.materials_path = settings.MaterialPath;
|
|
|
|
|
|
|
|
|
|
var model = Model.Load(arguments);
|
|
|
|
|
LoadBMD(model);
|
|
|
|
|
}
|
2019-07-12 23:44:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 21:22:59 +00:00
|
|
|
|
private void SaveAction(object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
SaveFileDialog sfd = new SaveFileDialog();
|
|
|
|
|
sfd.Filter = Utils.GetAllFilters(this);
|
|
|
|
|
sfd.FileName = FileName;
|
|
|
|
|
|
|
|
|
|
if (sfd.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
STFileSaver.SaveFileFormat(this, sfd.FileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unload()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] Save()
|
|
|
|
|
{
|
|
|
|
|
var mem = new System.IO.MemoryStream();
|
|
|
|
|
BMDFile.ExportBMD(mem);
|
|
|
|
|
return mem.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|