1
0
mirror of synced 2024-12-13 00:01:13 +01:00
Switch-Toolbox/Switch_FileFormatsMain/GUI/BFRES/Materials/SamplerEditor.cs
KillzXGaming d1f03b161f 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 12:55:09 -04:00

401 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using Switch_Toolbox.Library.Forms;
using Bfres.Structs;
using ResUGX2 = Syroot.NintenTools.Bfres.GX2;
using ResGFX = Syroot.NintenTools.NSW.Bfres.GFX;
using ResNX = Syroot.NintenTools.NSW.Bfres;
namespace FirstPlugin.Forms
{
public partial class SamplerEditor : STUserControl
{
FMAT material;
public ImageList textureImageList;
private Thread Thread;
public SamplerEditor()
{
InitializeComponent();
textureImageList = new ImageList()
{
ColorDepth = ColorDepth.Depth32Bit,
ImageSize = new Size(30, 30),
};
}
public void FillForm()
{
InitializeTextureListView(material);
}
private void textureRefListView_DoubleClick(object sender, EventArgs e)
{
if (textureRefListView.SelectedIndices.Count <= 0)
return;
int index = textureRefListView.SelectedIndices[0];
Texture_Selector tex = new Texture_Selector();
tex.LoadTexture(textureRefListView.SelectedItems[0].Text, material.GetResFileU() != null);
if (tex.ShowDialog() == DialogResult.OK)
{
material.TextureMaps[index].Name = tex.GetSelectedTexture();
InitializeTextureListView(material);
material.UpdateTextureMaps();
}
}
public void InitializeTextureListView(FMAT fmat)
{
material = fmat;
textureRefListView.Items.Clear();
textureRefListView.SmallImageList = textureImageList;
textureRefListView.FullRowSelect = true;
foreach (MatTexture tex in material.TextureMaps)
{
ListViewItem item = new ListViewItem();
item.Text = tex.Name;
item.SubItems.Add(tex.SamplerName);
if (material.shaderassign.samplers.ContainsValue(tex.SamplerName))
{
var FragSampler = material.shaderassign.samplers.FirstOrDefault(x => x.Value == tex.SamplerName).Key;
item.SubItems.Add(FragSampler.ToString());
}
textureRefListView.Items.Add(item);
}
textureImageList.Images.Clear();
int CurTex = 0;
if (PluginRuntime.bntxContainers.Count == 0 &&
PluginRuntime.ftexContainers.Count == 0)
{
foreach (ListViewItem item in textureRefListView.Items)
{
AddBlankTexture(item, item.Text, CurTex++);
}
}
return;
bool FoundTexture = false;
foreach (ListViewItem item in textureRefListView.Items)
{
foreach (BNTX bntx in PluginRuntime.bntxContainers)
{
if (bntx.Textures.ContainsKey(item.Text))
{
FoundTexture = true;
Thread Thread = new Thread((ThreadStart)(() =>
{
Bitmap temp = bntx.Textures[item.Text].GetBitmap();
textureRefListView.Invoke(new Action(() =>
{
textureImageList.Images.Add(item.Text, temp);
}));
temp.Dispose();
}));
Thread.Start();
item.ImageIndex = CurTex++;
}
}
foreach (BFRESGroupNode ftexCont in PluginRuntime.ftexContainers)
{
if (ftexCont.ResourceNodes.ContainsKey(item.Text))
{
FoundTexture = true;
FTEX tex = (FTEX)ftexCont.ResourceNodes[item.Text];
var renderedTex = tex.RenderableTex;
Bitmap temp = tex.GetBitmap();
textureImageList.Images.Add(tex.Text, temp);
item.ImageIndex = CurTex++;
var dummy = textureImageList.Handle;
temp.Dispose();
}
}
if (FoundTexture == false)
{
AddBlankTexture(item, item.Text, CurTex++);
}
}
}
private void AddBlankTexture(ListViewItem item, string Name, int ImageIndex)
{
Bitmap temp = new Bitmap(Properties.Resources.TextureError);
textureImageList.Images.Add(Name, temp);
item.ImageIndex = ImageIndex;
var dummy = textureImageList.Handle;
temp.Dispose();
}
private void Reset()
{
stPropertyGrid1.LoadProperty(null, null);
}
private string GetHint(string Sampler)
{
switch (Sampler)
{
case "_a0": return "albedo 0";
case "_a1": return "albedo 1";
case "_a2": return "albedo 2";
case "_a3": return "albedo 3";
case "_n0": return "normal 0";
case "_n1": return "normal 1";
case "_n2": return "normal 2";
case "_n3": return "normal 3";
default:
return "";
}
}
public void OnPropertyChanged() { }
private void textureRefListView_SelectedIndexChanged(object sender, EventArgs e)
{
//Do this only if none are selected so they don't flicker off/on on change
if (textureRefListView.SelectedIndices.Count <= 0)
{
btnEdit.Enabled = false;
samplerHintTB.Text = "";
samplerTB.Text = "";
btnRemove.Enabled = false;
}
Reset();
if (textureRefListView.SelectedIndices.Count > 0)
{
btnEdit.Enabled = true;
btnRemove.Enabled = true;
int index = textureRefListView.SelectedIndices[0];
MatTexture matTex = (MatTexture)material.TextureMaps[index];
string name = matTex.Name;
textureNameTB.Text = name;
if (matTex.SamplerName != null)
{
samplerTB.Text = matTex.SamplerName;
}
if (matTex.switchSampler != null)
{
stPropertyGrid1.LoadProperty(matTex.switchSampler, OnPropertyChanged);
}
if (matTex.wiiUSampler != null)
{
stPropertyGrid1.LoadProperty(matTex.wiiUSampler, OnPropertyChanged);
}
foreach (BNTX bntx in PluginRuntime.bntxContainers)
{
if (bntx.Textures.ContainsKey(name))
{
Thread = new Thread((ThreadStart)(() =>
{
textureBP.Image = Switch_Toolbox.Library.Imaging.GetLoadingImage();
textureBP.Image = bntx.Textures[name].GetBitmap();
}));
Thread.Start();
}
}
foreach (BFRESGroupNode ftexCont in PluginRuntime.ftexContainers)
{
if (ftexCont.ResourceNodes.ContainsKey(name))
{
Thread = new Thread((ThreadStart)(() =>
{
textureBP.Image = Switch_Toolbox.Library.Imaging.GetLoadingImage();
textureBP.Image = ((FTEX)ftexCont.ResourceNodes[name]).GetBitmap();
}));
Thread.Start();
}
}
}
}
private void LoadWiiUSamplerEnums()
{
}
private void LoadSwitchSamplerEnums()
{
}
private void SamplerEditor_SelectedIndexChanged(object sender, EventArgs e)
{
int index = textureRefListView.SelectedIndices[0];
MatTexture matTex = (MatTexture)material.TextureMaps[index];
}
private void lodMinUD_ValueChanged(object sender, EventArgs e)
{
}
private void lodMaxUD_ValueChanged(object sender, EventArgs e)
{
}
private void biasUD_ValueChanged(object sender, EventArgs e)
{
}
private void deptComparechkBox_CheckedChanged(object sender, EventArgs e)
{
}
int SamplerDupeIndex = 0;
private string GetSamplerName(string Sampler)
{
foreach (MatTexture tex in material.TextureMaps)
{
if (tex.SamplerName == Sampler)
{
var output = Regex.Replace(Sampler, @"[\d-]", string.Empty);
Sampler = $"{output}{SamplerDupeIndex++}";
return GetSamplerName(Sampler);
}
}
return Sampler;
}
private void btnAdd_Click(object sender, EventArgs e)
{
var tex = new MatTexture();
tex.SamplerName = GetSamplerName("_a0");
tex.FragShaderSampler = "_a0";
tex.Name = "Untitled";
tex.Type = Switch_Toolbox.Library.STGenericMatTexture.TextureType.Unknown;
tex.wrapModeS = 0;
tex.wrapModeT = 0;
tex.wrapModeW = 2;
if (material.GetResFileU() != null)
{
var texSampler = new ResUGX2.TexSampler();
texSampler.BorderType = ResUGX2.GX2TexBorderType.ClearBlack;
texSampler.ClampX = ResUGX2.GX2TexClamp.Wrap;
texSampler.ClampY = ResUGX2.GX2TexClamp.Wrap;
texSampler.ClampZ = ResUGX2.GX2TexClamp.Clamp;
texSampler.DepthCompareEnabled = false;
texSampler.DepthCompareFunc = ResUGX2.GX2CompareFunction.Never;
texSampler.MagFilter = ResUGX2.GX2TexXYFilterType.Point;
texSampler.MaxAnisotropicRatio = ResUGX2.GX2TexAnisoRatio.TwoToOne;
texSampler.MinFilter = ResUGX2.GX2TexXYFilterType.Point;
texSampler.MipFilter = ResUGX2.GX2TexMipFilterType.Linear;
texSampler.ZFilter = 0;
texSampler.MaxLod = 13;
texSampler.MinLod = 0;
texSampler.LodBias = 0;
tex.wiiUSampler = texSampler;
}
else
{
var texSampler = new ResNX.Sampler();
tex.switchSampler = texSampler;
}
material.TextureMaps.Add(tex);
var item = new ListViewItem();
item.Text = "Untitled";
item.SubItems.Add(tex.SamplerName);
item.SubItems.Add(tex.FragShaderSampler);
textureRefListView.Items.Add(item);
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (textureRefListView.SelectedIndices.Count > 0)
{
string FirstSelecteItem = textureRefListView.SelectedItems[0].Text;
DialogResult result;
if (textureRefListView.SelectedIndices.Count == 1)
result = MessageBox.Show($"Are you sure you want to remove texture {FirstSelecteItem}? This could potentially break things!",
"Texture Map Editor", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
else
result = MessageBox.Show("Are you sure you want to remove these textures? This could potentially break things!",
"Texture Map Editor", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (result == DialogResult.Yes)
{
foreach (ListViewItem tex in textureRefListView.SelectedItems)
{
textureRefListView.Items.Remove(tex);
int index = material.TextureMaps.IndexOf(material.TextureMaps.Where(p => p.Name == tex.Text).FirstOrDefault());
material.TextureMaps.RemoveAt(index);
}
}
}
}
private void samplerTB_TextChanged(object sender, EventArgs e)
{
if (textureRefListView.SelectedItems.Count <= 0)
return;
var index = textureRefListView.SelectedIndices[0];
samplerHintTB.Text = GetHint(samplerHintTB.Text);
bool IsDuped = false;
List<string> KeyCheck = new List<string>();
foreach (MatTexture item in material.TextureMaps)
{
if (!KeyCheck.Contains(item.SamplerName))
KeyCheck.Add(item.SamplerName);
else
{
IsDuped = true;
STErrorDialog.Show($"A sampler with the same name already exists! {item.SamplerName}",
this.Text, "");
}
}
if (!IsDuped)
{
material.TextureMaps[index].SamplerName = samplerTB.Text;
}
KeyCheck.Clear();
}
}
}