Skip to content

Commit

Permalink
Support combined uvs
Browse files Browse the repository at this point in the history
  • Loading branch information
KillzXGaming committed Oct 12, 2024
1 parent 6a542fa commit e5c1680
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ static void SetVertexBuffer(IOMesh mesh, ResFile resFile, Shape shape, Model mod
var bitangents = TryGetValues(helper, "_b0");
var weights0 = TryGetValues(helper, "_w0");
var indices0 = TryGetValues(helper, "_i0");
var texCoordsCombined1 = TryGetValues(helper, "_g3d_02_u0_u1");
var texCoordsCombined2 = TryGetValues(helper, "_g3d_02_u2_u1");

//Get the position attribute and use the length for the vertex count
for (int v = 0; v < positions.Length; v++)
Expand All @@ -97,6 +99,13 @@ static void SetVertexBuffer(IOMesh mesh, ResFile resFile, Shape shape, Model mod
for (int i = 0; i < texCoords.Length; i++)
vertex.SetUV(texCoords[i][v].X, 1 - texCoords[i][v].Y, i);
}

if (texCoordsCombined1.Length > 0)
{
vertex.SetUV(texCoordsCombined1[v].X, 1 - texCoordsCombined1[v].Y, 0);
vertex.SetUV(texCoordsCombined1[v].Z, 1 - texCoordsCombined1[v].W, 1);
}

if (colors.Length > 0)
{
for (int i = 0; i < colors.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,21 @@ private static VertexBuffer GenerateVertexBuffer(ResFile resFile, IOMesh mesh, S
if (i >= TexCoords.Length)
continue;

TexCoords[i][v] = new Vector4F(
vertex.UVs[i].X,
1 - vertex.UVs[i].Y,
0, 0);
if (settings.CombineUVs && i % 2 == 0 && i < vertex.UVs.Count - 1)
{
TexCoords[i][v] = new Vector4F(
vertex.UVs[i].X,
1 - vertex.UVs[i].Y,
vertex.UVs[i + 1].X,
1 - vertex.UVs[i + 1].Y);
}
else
{
TexCoords[i][v] = new Vector4F(
vertex.UVs[i].X,
1 - vertex.UVs[i].Y,
0, 0);
}
}

for (int i = 0; i < vertex.Colors?.Count; i++)
Expand Down Expand Up @@ -984,12 +995,37 @@ private static VertexBuffer GenerateVertexBuffer(ResFile resFile, IOMesh mesh, S

if (settings.UseTexCoord[i])
{
attributes.Add(new VertexBufferHelperAttrib()
if (settings.CombineUVs)
{
Name = $"_u{i}",
Data = TexCoords[i],
Format = format,
});
//Combine and only use even coords
if (i % 2 == 0 && i < TexCoords.Length - 1)
{
attributes.Add(new VertexBufferHelperAttrib()
{
Name = $"_u{i}",
Data = TexCoords[i],
Format = format,
});
}
else if (i == TexCoords.Length - 1) //else check if last element
{
attributes.Add(new VertexBufferHelperAttrib()
{
Name = $"_u{i}",
Data = TexCoords[i],
Format = format,
});
}
}
else
{
attributes.Add(new VertexBufferHelperAttrib()
{
Name = $"_u{i}",
Data = TexCoords[i],
Format = format,
});
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions Plugins/CafeLibrary/Bfres/Imgui/ModelImportDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public void Setup(FMDL fmdl, IOScene scene, ModelImportSettings settings)
Settings = settings;
PresetWindow.LoadPresets(fmdl.ResFile.IsPlatformSwitch);

//Check for combined
var combineUVs = fmdl.Model.Materials.Values.Any(x => x.ShaderAssign != null &&
x.ShaderAssign.AttribAssigns.ContainsKey("_g3d_02_u0_u1"));

string DefaultPreset = "";
bool isCourse = fmdl.ResFile.Name == "course_model.szs";
bool isSwitch = fmdl.ResFile.IsPlatformSwitch;
Expand All @@ -49,6 +53,7 @@ public void Setup(FMDL fmdl, IOScene scene, ModelImportSettings settings)
var meshSettings = new ModelImportSettings.MeshSettings();
meshSettings.MeshData = mesh;
meshSettings.Name = mesh.Name;
meshSettings.CombineUVs = combineUVs;
meshSettings.MaterialName = "";
meshSettings.SkinCount = mesh.Vertices.Max(x => x.Envelope.Weights.Count);
meshSettings.Normal.Enable = mesh.HasNormals;
Expand Down
2 changes: 2 additions & 0 deletions Plugins/CafeLibrary/Bfres/Imgui/ModelImportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class MeshSettings
public string ImportedMaterial;
public BfresLibrary.Material MaterialInstance;

public bool CombineUVs;

public bool UseCustomAttributeSettings;

public string PresetName { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions Plugins/CafeLibrary/Bfres/Loading/BfresGLLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public static byte[] LoadBufferData(ResFile resFile, Model mdl, Shape shape, Lis
{ "_c", 4 },
{ "_w", 4 },
{ "_i", 4 },
{ "_g3d_02_u0_u1", 4 },
};

public class VaoAttribute
Expand Down Expand Up @@ -219,6 +220,7 @@ public string UniformName
case "_c0": return GLConstants.VColor;
case "_t0": return GLConstants.VTangent;
case "_b0": return GLConstants.VBitangent;
case "_g3d_02_u0_u1": return GLConstants.VTexCoord0;
default:
return name;
}
Expand Down

0 comments on commit e5c1680

Please sign in to comment.