1
0
mirror of synced 2024-11-13 18:50:50 +01:00
Switch-Toolbox/Switch_Toolbox_Library/FileFormats/ANIM.cs

372 lines
14 KiB
C#
Raw Normal View History

Add files for the new one. Rework UI from scratch with proper themes and custom controls. MDI windows are now used for workspaces, comparing docs, and multiple usages. Tabs organise multiple workspaces and you can keep mdi windows maximized if you want to only use tabs. Themes currently include dark and white theme but plan to have XML files with list of color and styles Alot of things optimized. UI is very fast and snappy now Dae rigging fixed. Dae bones can be imported. Dae with textures can be imported and exported to a folder Custom sampler editor for sampler data. Texture refs, shader options, params, render info, and basically all material data can be added/removed and edited User data editor Update opengl framework by JuPaHe64 to the newest. Includes an origintation cube, multiple models in a scene, and many improvements Skeleton can be viewed GFPAK with some fixes in saving NUTEXB has proper mip map viewing PTCL Editor (Wii U and Switch). Can edit colors ( Wii U) and view textures. Also EFFN files in smash ultimate can be previewed Files can be associated with the program and opened with on clicking them ASTC textures can be viewed UVs can be viewed. Includes wrap modes and also translating and scaling for some basic edits Textures use a new editor. It includes channel viewing and some new editing options Fixed black textures on some wii u bfres Fixed saving sarcs in sarcs Shortcut keys have been added in. CTRL + S can save the active file in the currently used window Fix more issues with bfres crashing File - New includes BNTX for creating new bntx files from scatch Raw shader binaries can be extracted from bnsh and bfsha. Yuzu and Ryujinx can decompile these Sharc files can have source data previewed and shader programs in XML Aamp v1 and v2 data can be previewed. v1 can be edited and saved atm, v2 will be at a later update Byaml uses it's own editor instead of a seperate window for easy saving within sarcs Archives have a hex viewer Dae exporting greatly improved and can export rigged meshes Scene, shader param, srt, color, and texture pattern animations can all be previewed (in a list) Memory usage is greatly improved Narc (Nitro Archives) can be viewed and extracted. Fixed importing TGA images Support importing ASTC textures for bntx Added in PBR lighting for bfres from my implimentaion in forge Added gradient background for viewport. This can be edited in the settings Added skybox background option for viewport. Can load cubemaps Added grid with customizable cells for viewport. DDS decompression no longer requires Direct X tex. Zlib decompression has been improved for opening files that use it Rigid bones are properly ordered on importing a mesh. May fix some exploding issues. Endianness for KCL can be toggled for saving. Will be set to what it was using orignally Tangents can be filled with a constant value. Will allow them to not cause seams nor flat lighting however normal maps may not work as good Vertex buffers can be added and removed. Also re encoded Parameters now use drop down panels with values for easier editing Reworked the bone editor. Everything for a bone can be fully edited now besides the index, billboard index and parent index which get set automatically Fixed animation scaling for skeletal animations finally! Textures can be loaded in a tab now with thumbnail displaying for easy real time edits while previewing in the viewport Fixed support for audio files to be big endian in BARS Textures for switch now use their own folder. You can easily add textures to this and add textures to bfres that have no bntx. If there are no textures then the bfres will automatically not have one on save. Animations are split into multiple sub sections for switch's material animation for easier access Bfres for wii u has better binary exporting and is fully compatiable with Wexos Toolbox (to and from) Every section can be added in as new for both wii u and switch. Every section can be renamed properly and mostly everything can be edited. (Key frame editing and a more in depth curve editor later) Added option to copy UV channel Bone weights can be previewed Tons of fixes for the switch bfres library with more games working. Splatoon 2 (more work now), BOTW, Kirby Star Allies, and more! Fixed 3.3 Wii U bfres from not opening Wii U Sharcfb files can have shader program data previewed (XML) And possibly alot more things i missed! All this is still experimental but will improve over the next few weeks
2019-03-23 17:55:09 +01:00
using System;
using System.IO;
using System.Collections.Generic;
using OpenTK;
using System.Windows.Forms;
namespace Switch_Toolbox.Library.Animations
{
public class ANIM
{
private class AnimKey{
public float input, output;
public string intan, outtan;
public float t1 = 0, w1 = 1;
}
private class AnimData{
public string type, input, output, preInfinity, postInfinity;
public bool weighted = false;
public List<AnimKey> keys = new List<AnimKey>();
public float getValue(int frame){
AnimKey f1 = null, f2 = null;
for (int i = 0; i < keys.Count-1; i++) {
if ((keys [i].input-1 <= frame && keys [i + 1].input-1 >= frame)) {
f1 = keys [i];
f2 = keys [i + 1];
break;
}
}
if (f1 == null) {
if (keys.Count <= 1) {
return keys [0].output;
} else {
f1 = keys [keys.Count - 2];
f2 = keys [keys.Count - 1];
}
}
return Animation.Hermite (frame+1, f1.input, f2.input, weighted ? f1.t1 : 0, weighted ? f2.t1 : 0, f1.output, f2.output);
}
}
private class AnimBone{
public string name;
public List<AnimData> atts = new List<AnimData>();
}
public static Animation read(string filename, STSkeleton vbn){
StreamReader reader = File.OpenText(filename);
string line;
bool isHeader = true;
string angularUnit, linearUnit, timeUnit;
int startTime = 0;
int endTime = 0;
List<AnimBone> bones = new List<AnimBone>();
Animation.KeyNode current = null;
Animation.KeyFrame att = new Animation.KeyFrame();
bool inKeys = false;
string type = "";
Animation a = new Animation(filename);
while ((line = reader.ReadLine()) != null) {
string[] args = line.Replace (";", "").TrimStart().Split (' ');
if (isHeader) {
if (args [0].Equals ("anim"))
isHeader = false;
else if (args [0].Equals ("angularUnit"))
angularUnit = args [1];
else if (args [0].Equals ("endTime"))
endTime = (int)Math.Ceiling(float.Parse (args [1]));
else if (args [0].Equals ("startTime"))
startTime = (int)Math.Ceiling(float.Parse (args [1]));
}
if (!isHeader) {
if (inKeys) {
if(args[0].Equals("}")){
inKeys = false;
continue;
}
Animation.KeyFrame k = new Animation.KeyFrame ();
//att.keys.Add (k);
if (type.Contains("translate"))
{
if (type.Contains("X")) current.XPOS.Keys.Add(k);
if (type.Contains("Y")) current.YPOS.Keys.Add(k);
if (type.Contains("Z")) current.ZPOS.Keys.Add(k);
}
if (type.Contains("rotate"))
{
if (type.Contains("X")) current.XROT.Keys.Add(k);
if (type.Contains("Y")) current.YROT.Keys.Add(k);
if (type.Contains("Z")) current.ZROT.Keys.Add(k);
}
if (type.Contains("scale"))
{
if (type.Contains("X")) current.XSCA.Keys.Add(k);
if (type.Contains("Y")) current.YSCA.Keys.Add(k);
if (type.Contains("Z")) current.ZSCA.Keys.Add(k);
}
k.Frame = float.Parse (args [0])-1;
k.Value = float.Parse (args [1]);
if (type.Contains("rotate"))
{
k.Value *= (float)(Math.PI / 180f);
}
//k.intan = (args [2]);
//k.outtan = (args [3]);
if (args.Length > 7 && att.Weighted)
{
k.In = float.Parse(args[7]) * (float)(Math.PI / 180f);
k.Out = float.Parse(args[8]) * (float)(Math.PI / 180f);
}
}
if (args [0].Equals ("anim")) {
inKeys = false;
if (args.Length == 5) {
//TODO: finish this type
// can be name of attribute
}
if (args.Length == 7) {
// see of the bone of this attribute exists
current = null;
foreach (Animation.KeyNode b in a.Bones)
if (b.Text.Equals (args [3])) {
current = b;
break;
}
if (current == null) {
current = new Animation.KeyNode (args[3]);
current.RotType = Animation.RotationType.EULER;
a.Bones.Add (current);
}
current.Text = args [3];
att = new Animation.KeyFrame();
att.InterType = InterpolationType.HERMITE;
type = args [2];
//current.Nodes.Add (att);
// row child attribute aren't needed here
}
}
/*if (args [0].Equals ("input"))
att.input = args [1];
if (args [0].Equals ("output"))
att.output = args [1];
if (args [0].Equals ("preInfinity"))
att.preInfinity = args [1];
if (args [0].Equals ("postInfinity"))
att.postInfinity = args [1];*/
if (args[0].Equals("weighted"))
att.Weighted = args[1].Equals("1");
// begining keys section
if (args [0].Contains ("keys")) {
inKeys = true;
}
}
}
a.FrameCount = endTime-1;
reader.Close();
return a;
}
public static void CreateANIM(string fname, Animation a, STSkeleton vbn)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@fname))
{
file.WriteLine("animVersion 1.1;");
file.WriteLine("mayaVersion 2014 x64;\ntimeUnit ntscf;\nlinearUnit cm;\nangularUnit deg;\nstartTime 1;\nendTime " + (a.FrameCount+1) + ";");
a.SetFrame(a.FrameCount - 1); //from last frame
for (int li = 0; li < a.FrameCount; ++li) //go through each frame with nextFrame
a.NextFrame(vbn);
a.NextFrame(vbn); //go on first frame
int i = 0;
// writing node attributes
foreach (STBone b in vbn.getBoneTreeOrder())
{
i = vbn.boneIndex(b.Text);
if (a.HasBone(b.Text))
{
// write the bone attributes
// count the attributes
Animation.KeyNode n = a.GetBone(b.Text);
int ac = 0;
if (n.XPOS.HasAnimation())
{
file.WriteLine("anim translate.translateX translateX " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.XPOS, n, a.Size(), "translateX");
file.WriteLine("}");
}
if (n.YPOS.HasAnimation())
{
file.WriteLine("anim translate.translateY translateY " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.YPOS, n, a.Size(), "translateY");
file.WriteLine("}");
}
if (n.ZPOS.HasAnimation())
{
file.WriteLine("anim translate.translateZ translateZ " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.ZPOS, n, a.Size(), "translateZ");
file.WriteLine("}");
}
if (n.XROT.HasAnimation())
{
file.WriteLine("anim rotate.rotateX rotateX " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.XROT, n, a.Size(), "rotateX");
file.WriteLine("}");
}
if (n.YROT.HasAnimation())
{
file.WriteLine("anim rotate.rotateY rotateY " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.YROT, n, a.Size(), "rotateY");
file.WriteLine("}");
}
if (n.ZROT.HasAnimation())
{
file.WriteLine("anim rotate.rotateZ rotateZ " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.ZROT, n, a.Size(), "rotateZ");
file.WriteLine("}");
}
if (n.XSCA.HasAnimation())
{
file.WriteLine("anim scale.scaleX scaleX " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.XSCA, n, a.Size(), "scaleX");
file.WriteLine("}");
}
if (n.YSCA.HasAnimation())
{
file.WriteLine("anim scale.scaleY scaleY " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.YSCA, n, a.Size(), "scaleY");
file.WriteLine("}");
}
if (n.ZSCA.HasAnimation())
{
file.WriteLine("anim scale.scaleZ scaleZ " + b.Text + " 0 0 " + (ac++) + ";");
writeKey(file, n.ZSCA, n, a.Size(), "scaleZ");
file.WriteLine("}");
}
}
else
{
file.WriteLine("anim " + b.Text + " 0 0 0;");
}
}
}
}
private static void writeKey(StreamWriter file, Animation.KeyGroup keys, Animation.KeyNode rt, int size, string type)
{
file.WriteLine("animData {\n input time;\n output linear;\n weighted 1;\n preInfinity constant;\n postInfinity constant;\n keys {");
if (((Animation.KeyFrame)keys.Keys[0]).InterType == InterpolationType.CONSTANT)
size = 1;
int f = 1;
foreach (Animation.KeyFrame key in keys.Keys)
{
float v = 0;
switch (type)
{
case "translateX":
v = key.Value;
break;
case "translateY":
v = key.Value;
break;
case "translateZ":
v = key.Value;
break;
case "rotateX":
if (rt.RotType == Animation.RotationType.EULER)
v = key.Value * (float)(180f / Math.PI);
if (rt.RotType == Animation.RotationType.QUATERNION)
{
Quaternion q = new Quaternion(rt.XROT.GetValue(key.Frame), rt.YROT.GetValue(key.Frame), rt.ZROT.GetValue(key.Frame), rt.WROT.GetValue(key.Frame));
v = quattoeul(q).X * (float)(180f / Math.PI);
}
break;
case "rotateY":
if (rt.RotType == Animation.RotationType.EULER)
v = key.Value * (float)(180f / Math.PI);
if (rt.RotType == Animation.RotationType.QUATERNION)
{
Quaternion q = new Quaternion(rt.XROT.GetValue(key.Frame), rt.YROT.GetValue(key.Frame), rt.ZROT.GetValue(key.Frame), rt.WROT.GetValue(key.Frame));
v = quattoeul(q).Y * (float)(180f / Math.PI);
}
break;
case "rotateZ":
if (rt.RotType == Animation.RotationType.EULER)
v = key.Value * (float)(180f / Math.PI);
if (rt.RotType == Animation.RotationType.QUATERNION)
{
Quaternion q = new Quaternion(rt.XROT.GetValue(key.Frame), rt.YROT.GetValue(key.Frame), rt.ZROT.GetValue(key.Frame), rt.WROT.GetValue(key.Frame));
v = quattoeul(q).Z * (float)(180f / Math.PI);
}
break;
case "scaleX":
v = key.Value;
break;
case "scaleY":
v = key.Value;
break;
case "scaleZ":
v = key.Value;
break;
}
file.WriteLine(" " + (key.Frame + 1) + " {0:N6} fixed fixed 1 1 0 " + key.In * (float)(180f/Math.PI) + " 1 " + key.In * (float)(180f / Math.PI) + " 1;", v);
}
file.WriteLine(" }");
}
public static Vector3 quattoeul(Quaternion q){
float sqw = q.W * q.W;
float sqx = q.X * q.X;
float sqy = q.Y * q.Y;
float sqz = q.Z * q.Z;
float normal = (float)Math.Sqrt (sqw + sqx + sqy + sqz);
float pole_result = (q.X * q.Z) + (q.Y * q.W);
if (pole_result > (0.5 * normal)){
float ry = (float)Math.PI / 2;
float rz = 0;
float rx = 2 * (float)Math.Atan2(q.X, q.W);
return new Vector3(rx, ry, rz);
}
if (pole_result < (-0.5 * normal)){
float ry = (float)Math.PI/2;
float rz = 0;
float rx = -2 * (float)Math.Atan2(q.X, q.W);
return new Vector3(rx, ry, rz);
}
float r11 = 2*(q.X*q.Y + q.W*q.Z);
float r12 = sqw + sqx - sqy - sqz;
float r21 = -2*(q.X*q.Z - q.W*q.Y);
float r31 = 2*(q.Y*q.Z + q.W*q.X);
float r32 = sqw - sqx - sqy + sqz;
float frx = (float)Math.Atan2( r31, r32 );
float fry = (float)Math.Asin ( r21 );
float frz = (float)Math.Atan2( r11, r12 );
return new Vector3(frx, fry, frz);
}
}
}