1
0
mirror of synced 2024-11-12 02:00:50 +01:00

Add setting to limit vertex skin to better help prevent crashes

This commit is contained in:
KillzXGaming 2019-05-27 20:02:57 -04:00
parent 33d662d61a
commit f787c59627
10 changed files with 211 additions and 26 deletions

View File

@ -511,11 +511,7 @@ namespace Bfres.Structs
//Function addes shapes, vertices and meshes
public void AddOjects(string FileName, ResFile resFileNX, ResU.ResFile resFileU, bool Replace = true)
{
int totalSkinCountLimiter = 0;
bool IsWiiU = (resFileU != null);
if (shapes.Count > 0)
totalSkinCountLimiter = shapes[0].VertexSkinCount;
int MatStartIndex = materials.Count;
string ext = System.IO.Path.GetExtension(FileName);
@ -606,10 +602,24 @@ namespace Bfres.Structs
}
BfresModelImportSettings csvsettings = new BfresModelImportSettings();
csvsettings.DisableMaterialEdits();
csvsettings.SkinCountLimit = totalSkinCountLimiter;
csvsettings.SetModelAttributes(csvModel.objects[0]);
if (csvsettings.ShowDialog() == DialogResult.OK)
{
if (csvsettings.LimitSkinCount) {
for (int i = 0; i < csvModel.objects.Count; i++)
{
List<FSHP> Matches = shapes.Where(p => String.Equals(p.Name,
csvModel.objects[i].ObjectName, StringComparison.CurrentCulture)).ToList();
if (Matches != null)
{
//Match the skin count setting if names match
//Only one match should be found as shapes can't have duped names
csvModel.objects[i].VertexSkinCount = ((FSHP)Matches[0]).VertexSkinCount;
}
}
}
if (Replace)
{
shapes.Clear();
@ -618,11 +628,12 @@ namespace Bfres.Structs
Cursor.Current = Cursors.WaitCursor;
bool ForceSkinInfluence = false;
int ForceSkinInfluenceMax = 4;
bool ForceSkinInfluence = csvsettings.LimitSkinCount;
foreach (STGenericObject obj in csvModel.objects)
{
int ForceSkinInfluenceMax = obj.VertexSkinCount;
FSHP shape = new FSHP();
Nodes["FshpFolder"].Nodes.Add(shape);
shapes.Add(shape);
@ -640,13 +651,14 @@ namespace Bfres.Structs
shape.ApplyImportSettings(csvsettings, GetMaterial(shape.MaterialIndex));
shape.BoneIndices = shape.GetIndices(Skeleton);
shape.VertexSkinCount = obj.GetMaxSkinInfluenceCount();
if (!csvsettings.LimitSkinCount)
shape.VertexSkinCount = obj.GetMaxSkinInfluenceCount();
if (shape.VertexSkinCount == 1)
{
int boneIndex = shape.BoneIndices[0];
shape.BoneIndex = boneIndex;
}
Console.WriteLine($"VertexSkinCount {shape.VertexSkinCount}");
shape.OptmizeAttributeFormats();
shape.SaveShape(IsWiiU);
@ -708,6 +720,22 @@ namespace Bfres.Structs
bool UseMats = settings.ExternalMaterialPath != string.Empty;
if (settings.LimitSkinCount)
{
for (int i = 0; i < assimp.objects.Count; i++)
{
List<FSHP> Matches = shapes.Where(p => String.Equals(p.Name,
assimp.objects[i].ObjectName, StringComparison.CurrentCulture)).ToList();
if (Matches != null)
{
//Match the skin count setting if names match
//Only one match should be found as shapes can't have duped names
assimp.objects[i].VertexSkinCount = ((FSHP)Matches[0]).VertexSkinCount;
}
}
}
if (Replace)
{
shapes.Clear();
@ -935,13 +963,14 @@ namespace Bfres.Structs
Console.WriteLine("Processing Data. Object count " + assimp.objects.Count);
bool ForceSkinInfluence = false;
int ForceSkinInfluenceMax = 4;
bool ForceSkinInfluence = settings.LimitSkinCount;
int curShp = 0;
foreach (STGenericObject obj in assimp.objects)
{
int ForceSkinInfluenceMax = obj.VertexSkinCount;
if (obj.ObjectName == "")
obj.ObjectName = $"Mesh {curShp}";
@ -1004,7 +1033,8 @@ namespace Bfres.Structs
progressBar.Task = $"Generating Max Skin Influence. Mesh: {obj.ObjectName}";
progressBar.Refresh();
shape.VertexSkinCount = obj.GetMaxSkinInfluenceCount();
if (settings.LimitSkinCount)
shape.VertexSkinCount = obj.GetMaxSkinInfluenceCount();
if (shape.VertexSkinCount == 1)
{

View File

@ -279,6 +279,34 @@ namespace Bfres.Structs
{
FromChr0(FileName, resFileU != null);
}
else if (ext == ".dae")
{
// FromAssimp(FileName, resFileU != null);
}
else if (ext == ".fbx")
{
// FromAssimp(FileName, resFileU != null);
}
}
private void FromAssimp(string FileName, bool IsWiiU)
{
var anims = AssimpData.ImportAnimations(FileName);
for (int i = 0; i < anims.Length; i++)
{
if (IsWiiU)
{
SkeletalAnimU = ConvertSwitchToWiiU(FromGeneric(anims[i]));
LoadAnim(SkeletalAnimU);
}
else
{
SkeletalAnim = FromGeneric(anims[i]);
LoadAnim(SkeletalAnim);
}
break;
}
}
private void UpdateAnimation(SkeletalAnim ska)

View File

@ -87,6 +87,7 @@
this.tabPage2 = new System.Windows.Forms.TabPage();
this.tabPageAdvanced = new System.Windows.Forms.TabPage();
this.stPanel1 = new Switch_Toolbox.Library.Forms.STPanel();
this.ogSkinCountChkBox = new Switch_Toolbox.Library.Forms.STCheckBox();
this.contentContainer.SuspendLayout();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
@ -418,7 +419,7 @@
// chkBoxFlipUvsY
//
this.chkBoxFlipUvsY.AutoSize = true;
this.chkBoxFlipUvsY.Location = new System.Drawing.Point(3, 39);
this.chkBoxFlipUvsY.Location = new System.Drawing.Point(3, 62);
this.chkBoxFlipUvsY.Name = "chkBoxFlipUvsY";
this.chkBoxFlipUvsY.Size = new System.Drawing.Size(65, 17);
this.chkBoxFlipUvsY.TabIndex = 4;
@ -429,7 +430,7 @@
// chkBoxImportBones
//
this.chkBoxImportBones.AutoSize = true;
this.chkBoxImportBones.Location = new System.Drawing.Point(3, 62);
this.chkBoxImportBones.Location = new System.Drawing.Point(3, 85);
this.chkBoxImportBones.Name = "chkBoxImportBones";
this.chkBoxImportBones.Size = new System.Drawing.Size(88, 17);
this.chkBoxImportBones.TabIndex = 10;
@ -440,6 +441,7 @@
//
// panel8
//
this.panel8.Controls.Add(this.ogSkinCountChkBox);
this.panel8.Controls.Add(this.chkPlaceHolderTextures);
this.panel8.Controls.Add(this.chkBoxImportMat);
this.panel8.Controls.Add(this.chkBoxParamDefaults);
@ -469,7 +471,7 @@
// chkPlaceHolderTextures
//
this.chkPlaceHolderTextures.AutoSize = true;
this.chkPlaceHolderTextures.Location = new System.Drawing.Point(3, 204);
this.chkPlaceHolderTextures.Location = new System.Drawing.Point(3, 227);
this.chkPlaceHolderTextures.Name = "chkPlaceHolderTextures";
this.chkPlaceHolderTextures.Size = new System.Drawing.Size(160, 17);
this.chkPlaceHolderTextures.TabIndex = 32;
@ -483,7 +485,7 @@
this.chkBoxImportMat.AutoSize = true;
this.chkBoxImportMat.Checked = true;
this.chkBoxImportMat.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkBoxImportMat.Location = new System.Drawing.Point(3, 240);
this.chkBoxImportMat.Location = new System.Drawing.Point(3, 263);
this.chkBoxImportMat.Name = "chkBoxImportMat";
this.chkBoxImportMat.Size = new System.Drawing.Size(88, 17);
this.chkBoxImportMat.TabIndex = 31;
@ -495,7 +497,7 @@
// chkBoxParamDefaults
//
this.chkBoxParamDefaults.AutoSize = true;
this.chkBoxParamDefaults.Location = new System.Drawing.Point(3, 16);
this.chkBoxParamDefaults.Location = new System.Drawing.Point(3, 39);
this.chkBoxParamDefaults.Name = "chkBoxParamDefaults";
this.chkBoxParamDefaults.Size = new System.Drawing.Size(117, 17);
this.chkBoxParamDefaults.TabIndex = 30;
@ -506,7 +508,7 @@
// chkBoxRotNegative90Y
//
this.chkBoxRotNegative90Y.AutoSize = true;
this.chkBoxRotNegative90Y.Location = new System.Drawing.Point(3, 108);
this.chkBoxRotNegative90Y.Location = new System.Drawing.Point(3, 131);
this.chkBoxRotNegative90Y.Name = "chkBoxRotNegative90Y";
this.chkBoxRotNegative90Y.Size = new System.Drawing.Size(117, 17);
this.chkBoxRotNegative90Y.TabIndex = 29;
@ -517,7 +519,7 @@
// textBoxMaterialPath
//
this.textBoxMaterialPath.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxMaterialPath.Location = new System.Drawing.Point(3, 263);
this.textBoxMaterialPath.Location = new System.Drawing.Point(3, 286);
this.textBoxMaterialPath.Name = "textBoxMaterialPath";
this.textBoxMaterialPath.ReadOnly = true;
this.textBoxMaterialPath.Size = new System.Drawing.Size(162, 20);
@ -606,7 +608,7 @@
// chkBoxRecalcNormals
//
this.chkBoxRecalcNormals.AutoSize = true;
this.chkBoxRecalcNormals.Location = new System.Drawing.Point(3, 154);
this.chkBoxRecalcNormals.Location = new System.Drawing.Point(3, 177);
this.chkBoxRecalcNormals.Name = "chkBoxRecalcNormals";
this.chkBoxRecalcNormals.Size = new System.Drawing.Size(124, 17);
this.chkBoxRecalcNormals.TabIndex = 18;
@ -633,7 +635,7 @@
this.chkBoxTransformMatrix.AutoSize = true;
this.chkBoxTransformMatrix.Checked = true;
this.chkBoxTransformMatrix.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkBoxTransformMatrix.Location = new System.Drawing.Point(3, 131);
this.chkBoxTransformMatrix.Location = new System.Drawing.Point(3, 154);
this.chkBoxTransformMatrix.Name = "chkBoxTransformMatrix";
this.chkBoxTransformMatrix.Size = new System.Drawing.Size(104, 17);
this.chkBoxTransformMatrix.TabIndex = 14;
@ -645,7 +647,7 @@
// chkBoxRot90Y
//
this.chkBoxRot90Y.AutoSize = true;
this.chkBoxRot90Y.Location = new System.Drawing.Point(3, 85);
this.chkBoxRot90Y.Location = new System.Drawing.Point(3, 108);
this.chkBoxRot90Y.Name = "chkBoxRot90Y";
this.chkBoxRot90Y.Size = new System.Drawing.Size(114, 17);
this.chkBoxRot90Y.TabIndex = 13;
@ -657,7 +659,7 @@
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
this.pictureBox1.Location = new System.Drawing.Point(3, 180);
this.pictureBox1.Location = new System.Drawing.Point(3, 203);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(17, 17);
this.pictureBox1.TabIndex = 12;
@ -666,7 +668,7 @@
// checkBox9
//
this.checkBox9.AutoSize = true;
this.checkBox9.Location = new System.Drawing.Point(25, 181);
this.checkBox9.Location = new System.Drawing.Point(25, 204);
this.checkBox9.Name = "checkBox9";
this.checkBox9.Size = new System.Drawing.Size(130, 17);
this.checkBox9.TabIndex = 11;
@ -764,6 +766,17 @@
this.stPanel1.Size = new System.Drawing.Size(186, 65);
this.stPanel1.TabIndex = 17;
//
// ogSkinCountChkBox
//
this.ogSkinCountChkBox.AutoSize = true;
this.ogSkinCountChkBox.Location = new System.Drawing.Point(3, 16);
this.ogSkinCountChkBox.Name = "ogSkinCountChkBox";
this.ogSkinCountChkBox.Size = new System.Drawing.Size(234, 17);
this.ogSkinCountChkBox.TabIndex = 33;
this.ogSkinCountChkBox.Text = "Keep Original Skin Count (can help crashes)";
this.ogSkinCountChkBox.UseVisualStyleBackColor = true;
this.ogSkinCountChkBox.CheckedChanged += new System.EventHandler(this.ogSkinCountChkBox_CheckedChanged);
//
// BfresModelImportSettings
//
this.ClientSize = new System.Drawing.Size(547, 398);
@ -859,5 +872,6 @@
private Switch_Toolbox.Library.Forms.STCheckBox chkBoxImportMat;
private Switch_Toolbox.Library.Forms.STPanel stPanel1;
private Switch_Toolbox.Library.Forms.STCheckBox chkPlaceHolderTextures;
private Switch_Toolbox.Library.Forms.STCheckBox ogSkinCountChkBox;
}
}

View File

@ -50,6 +50,7 @@ namespace FirstPlugin
public string ExternalMaterialPath;
public bool SetDefaultParamData;
public int SkinCountLimit;
public bool LimitSkinCount;
public bool GeneratePlaceholderTextures = true;
@ -345,5 +346,10 @@ namespace FirstPlugin
{
GeneratePlaceholderTextures = chkPlaceHolderTextures.Checked;
}
private void ogSkinCountChkBox_CheckedChanged(object sender, EventArgs e)
{
LimitSkinCount = ogSkinCountChkBox.Checked;
}
}
}

View File

@ -98,6 +98,21 @@ namespace Switch_Toolbox.Library
}
}
}
public static Animations.Animation[] ImportAnimations(string FileName)
{
AssimpContext Importer = new AssimpContext();
STConsole.WriteLine($"Loading File {FileName}", Color.FromArgb(0, 255, 0));
var scene = Importer.ImportFile(FileName);
List<Animations.Animation> Anims = new List<Animations.Animation>();
foreach (var anim in scene.Animations)
Anims.Add(CreateGenericAnimation(anim));
return Anims.ToArray();
}
private void BuildNode(Node parent, ref Matrix4x4 rootTransform)
{
Matrix4x4 world = rootTransform;
@ -158,11 +173,14 @@ namespace Switch_Toolbox.Library
}
}
public Animations.Animation CreateGenericAnimation(Assimp.Animation animation)
public static Animations.Animation CreateGenericAnimation(Assimp.Animation animation)
{
Animations.Animation STanim = new Animations.Animation();
STanim.Text = animation.Name;
STanim.FrameCount = (int)animation.DurationInTicks;
float TicksPerSecond = animation.TicksPerSecond != 0 ? (float)animation.TicksPerSecond : 25.0f;
float Duriation = (float)animation.DurationInTicks;
STanim.FrameCount = (int)(Duriation * 30);
//Load node animations
if (animation.HasNodeAnimations)
@ -171,6 +189,95 @@ namespace Switch_Toolbox.Library
for (int i = 0; i < _channels.Length; i++)
{
_channels[i] = new NodeAnimationChannel();
var boneAnim = new Animations.Animation.KeyNode(_channels[i].NodeName);
boneAnim.RotType = Animations.Animation.RotationType.EULER;
STanim.Bones.Add(boneAnim);
STConsole.WriteLine($"Creating Bone Anims {boneAnim.Text} ");
for (int frame = 0; frame < STanim.FrameCount; i++)
{
if (_channels[i].HasPositionKeys)
{
for (int key = 0; key < _channels[i].PositionKeyCount; key++)
{
if (frame == _channels[i].PositionKeys[key].Time)
{
boneAnim.XPOS.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = _channels[i].PositionKeys[key].Value.X,
Frame = frame,
});
boneAnim.YPOS.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = _channels[i].PositionKeys[key].Value.Y,
Frame = frame,
});
boneAnim.ZPOS.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = _channels[i].PositionKeys[key].Value.Z,
Frame = frame,
});
}
}
}
if (_channels[i].HasRotationKeys)
{
for (int key = 0; key < _channels[i].RotationKeyCount; key++)
{
if (frame == _channels[i].RotationKeys[key].Time)
{
var quat = _channels[i].RotationKeys[key].Value;
var euler = STMath.ToEulerAngles(quat.X, quat.Y, quat.Z, quat.W);
boneAnim.XROT.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = euler.X,
Frame = frame,
});
boneAnim.YROT.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = euler.Y,
Frame = frame,
});
boneAnim.ZROT.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = euler.Z,
Frame = frame,
});
boneAnim.WROT.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = 1,
Frame = frame,
});
}
}
}
if (_channels[i].HasScalingKeys)
{
for (int key = 0; key < _channels[i].ScalingKeyCount; key++)
{
if (frame == _channels[i].ScalingKeys[key].Time)
{
boneAnim.XSCA.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = _channels[i].ScalingKeys[key].Value.X,
Frame = frame,
});
boneAnim.YSCA.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = _channels[i].ScalingKeys[key].Value.Y,
Frame = frame,
});
boneAnim.ZSCA.Keys.Add(new Animations.Animation.KeyFrame()
{
Value = _channels[i].ScalingKeys[key].Value.Z,
Frame = frame,
});
}
}
}
}
}
}