Skip to content

Commit

Permalink
section colors + no empty morph target
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Feb 26, 2023
1 parent ab13eff commit edd3a47
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 23 deletions.
18 changes: 17 additions & 1 deletion FModel/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Numerics;
using System.Reflection;
using CUE4Parse.UE4.Objects.Core.Misc;

namespace FModel;
Expand Down Expand Up @@ -27,4 +28,19 @@ public static class Constants
public const string _VAL_LIVE_TRIGGER = "valorant-live.manifest";

public const string _NO_PRESET_TRIGGER = "Hand Made";

public static int PALETTE_LENGTH => COLOR_PALETTE.Length;
public static readonly Vector3[] COLOR_PALETTE =
{
new (0.231f, 0.231f, 0.231f), // Dark gray
new (0.376f, 0.490f, 0.545f), // Teal
new (0.957f, 0.263f, 0.212f), // Red
new (0.196f, 0.804f, 0.196f), // Green
new (0.957f, 0.647f, 0.212f), // Orange
new (0.612f, 0.153f, 0.690f), // Purple
new (0.129f, 0.588f, 0.953f), // Blue
new (1.000f, 0.920f, 0.424f), // Yellow
new (0.824f, 0.412f, 0.118f), // Brown
new (0.612f, 0.800f, 0.922f) // Light blue
};
}
12 changes: 8 additions & 4 deletions FModel/Resources/default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ uniform Light uLights[MAX_LIGHT_COUNT];
uniform int uNumLights;
uniform int uUvCount;
uniform bool uHasVertexColors;
uniform vec3 uSectionColor;
uniform bool bVertexColors[6];
uniform vec3 uViewPos;

Expand Down Expand Up @@ -211,19 +212,23 @@ vec3 CalcSpotLight(int layer, vec3 normals, Light light)

void main()
{
if (bVertexColors[2] && uHasVertexColors)
if (bVertexColors[1])
{
FragColor = vec4(uSectionColor, 1.0);
}
else if (bVertexColors[2] && uHasVertexColors)
{
FragColor = fColor;
}
else if (bVertexColors[3])
{
int layer = LayerToIndex();
vec3 normals = ComputeNormals(layer);
FragColor = vec4(normals, 1);
FragColor = vec4(normals, 1.0);
}
else if (bVertexColors[4])
{
FragColor = vec4(fTexCoords, 0, 1);
FragColor = vec4(fTexCoords, 0.0, 1.0);
}
else
{
Expand Down Expand Up @@ -257,7 +262,6 @@ void main()
result += uParameters.Emissive[layer].Color.rgb * emissive.rgb * uParameters.EmissiveMult;
}

if (!bVertexColors[1])
{
result += CalcLight(layer, normals, uViewPos, vec3(0.75), 1.0, false);

Expand Down
2 changes: 2 additions & 0 deletions FModel/ViewModels/CUE4ParseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,8 @@ public void ExtractAndScroll(CancellationToken cancellationToken, string fullPat
case USkeleton when UserSettings.Default.SaveSkeletonAsMesh && HasFlag(bulk, EBulkType.Meshes):
// case UMaterialInstance when HasFlag(bulk, EBulkType.Materials): // read the fucking json
case UAnimSequence when HasFlag(bulk, EBulkType.Animations):
case UAnimMontage when HasFlag(bulk, EBulkType.Animations):
case UAnimComposite when HasFlag(bulk, EBulkType.Animations):
{
SaveExport(export, HasFlag(bulk, EBulkType.Auto));
return true;
Expand Down
27 changes: 18 additions & 9 deletions FModel/Views/Snooper/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public class Model : IDisposable
public bool HasSockets => Sockets.Count > 0;
public readonly List<Socket> Sockets;

public bool HasMorphTargets => Morphs.Length > 0;
public readonly Morph[] Morphs;
public bool HasMorphTargets => Morphs.Count > 0;
public readonly List<Morph> Morphs;

private string _attachedTo = string.Empty;
private readonly List<string> _attachedFor = new ();
Expand Down Expand Up @@ -114,7 +114,7 @@ protected Model(UObject export)
UvCount = 1;
Box = new FBox(new FVector(-2f), new FVector(2f));
Sockets = new List<Socket>();
Morphs = Array.Empty<Morph>();
Morphs = new List<Morph>();
Transforms = new List<Transform>();
}

Expand Down Expand Up @@ -150,10 +150,13 @@ public Model(USkeletalMesh export, CSkeletalMesh skeletalMesh, Transform transfo
Sockets.Add(new Socket(socket));
}

Morphs = new Morph[export.MorphTargets.Length];
for (var i = 0; i < Morphs.Length; i++)
for (var i = 0; i < export.MorphTargets.Length; i++)
{
Morphs[i] = new Morph(Vertices, VertexSize, export.MorphTargets[i].Load<UMorphTarget>());
if (!export.MorphTargets[i].TryLoad(out UMorphTarget morphTarget) ||
morphTarget.MorphLODModels.Length < 1 || morphTarget.MorphLODModels[0].Vertices.Length < 1)
continue;

Morphs.Add(new Morph(Vertices, VertexSize, morphTarget));
}
}

Expand Down Expand Up @@ -355,7 +358,7 @@ public void Setup(Options options)
if (HasSkeleton) Skeleton.Setup();
if (HasMorphTargets)
{
for (uint morph = 0; morph < Morphs.Length; morph++)
for (int morph = 0; morph < Morphs.Count; morph++)
{
Morphs[morph].Setup();
if (morph == 0)
Expand Down Expand Up @@ -398,7 +401,12 @@ public void Render(Shader shader, bool outline = false)
foreach (var section in Sections)
{
if (!section.Show) continue;
if (!outline) Materials[section.MaterialIndex].Render(shader);
if (!outline)
{
shader.SetUniform("uSectionColor", section.Color);
Materials[section.MaterialIndex].Render(shader);
}

GL.DrawElementsInstanced(PrimitiveType.Triangles, section.FacesCount, DrawElementsType.UnsignedInt, section.FirstFaceIndexPtr, TransformsCount);
}
_vao.Unbind();
Expand Down Expand Up @@ -443,10 +451,11 @@ public void Dispose()
}
Sockets.Clear();
if (HasMorphTargets) _morphVbo.Dispose();
for (var morph = 0; morph < Morphs.Length; morph++)
for (var morph = 0; morph < Morphs.Count; morph++)
{
Morphs[morph]?.Dispose();
}
Morphs.Clear();

GL.DeleteProgram(_handle);
}
Expand Down
3 changes: 3 additions & 0 deletions FModel/Views/Snooper/Models/Section.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Numerics;
using FModel.Views.Snooper.Shading;

namespace FModel.Views.Snooper.Models;
Expand All @@ -9,6 +10,7 @@ public class Section
public readonly int FacesCount;
public readonly int FirstFaceIndex;
public readonly IntPtr FirstFaceIndexPtr;
public readonly Vector3 Color;

public bool Show;

Expand All @@ -18,6 +20,7 @@ public Section(int index, int facesCount, int firstFaceIndex)
FacesCount = facesCount;
FirstFaceIndex = firstFaceIndex;
FirstFaceIndexPtr = new IntPtr(FirstFaceIndex * sizeof(uint));
Color = Constants.COLOR_PALETTE[index % Constants.PALETTE_LENGTH];
Show = true;
}

Expand Down
17 changes: 12 additions & 5 deletions FModel/Views/Snooper/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@

namespace FModel.Views.Snooper;

public enum VertexColor
{
Default,
Sections,
Colors,
Normals,
TextureCoordinates
}

public class Renderer : IDisposable
{
private readonly Skybox _skybox;
Expand All @@ -40,7 +49,7 @@ public class Renderer : IDisposable
public bool ShowGrid;
public bool ShowLights;
public bool AnimateWithRotationOnly;
public int VertexColor;
public VertexColor Color;

public Camera CameraOp { get; }
public PickingTexture Picking { get; }
Expand All @@ -58,7 +67,7 @@ public Renderer(int width, int height)
ShowSkybox = UserSettings.Default.ShowSkybox;
ShowGrid = UserSettings.Default.ShowGrid;
AnimateWithRotationOnly = UserSettings.Default.AnimateWithRotationOnly;
VertexColor = 0; // default
Color = VertexColor.Default;
}

public void Load(CancellationToken cancellationToken, UObject export)
Expand Down Expand Up @@ -89,7 +98,6 @@ public void Swap(UMaterialInstance unrealMaterial)

model.Materials[section.MaterialIndex].SwapMaterial(unrealMaterial);
Application.Current.Dispatcher.Invoke(() => model.Materials[section.MaterialIndex].Setup(Options, model.UvCount));
Options.SwapMaterial(false);
}

public void Animate(UObject anim) => Animate(anim, Options.SelectedModel);
Expand Down Expand Up @@ -192,7 +200,6 @@ private void Animate(UObject anim, FGuid guid)

Options.Tracker.IsPaused = false;
Options.Tracker.SafeSetMaxElapsedTime(maxElapsedTime);
Options.AnimateMesh(false);
}

public void Setup()
Expand All @@ -218,7 +225,7 @@ public void Render(float deltaSeconds)

_shader.Render(viewMatrix, CameraOp.Position, projMatrix);
for (int i = 0; i < 5; i++)
_shader.SetUniform($"bVertexColors[{i}]", i == VertexColor);
_shader.SetUniform($"bVertexColors[{i}]", i == (int) Color);

// update animations
if (Options.Animations.Count > 0) Options.Tracker.Update(deltaSeconds);
Expand Down
1 change: 1 addition & 0 deletions FModel/Views/Snooper/Shading/TextureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void FixChannels(string game, Texture texture)
case "HK_PROJECT":
case "COSMICSHAKE":
case "PHOENIX":
case "ATOMICHEART":
{
texture.SwizzleMask = new []
{
Expand Down
12 changes: 9 additions & 3 deletions FModel/Views/Snooper/SnimGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ private void DrawWorld(Snooper s)
ImGui.PopID();Layout("Animate With Rotation Only");ImGui.PushID(4);
ImGui.Checkbox("", ref s.Renderer.AnimateWithRotationOnly);
ImGui.PopID();Layout("Vertex Colors");ImGui.PushID(5);
ImGui.Combo("vertex_colors", ref s.Renderer.VertexColor,
"Default\0Diffuse Only\0Colors\0Normals\0Texture Coordinates\0");
var c = (int) s.Renderer.Color;
ImGui.Combo("vertex_colors", ref c,
"Default\0Sections\0Colors\0Normals\0Texture Coordinates\0");
s.Renderer.Color = (VertexColor) c;
ImGui.PopID();

ImGui.EndTable();
Expand Down Expand Up @@ -549,6 +551,10 @@ private void DrawDetails(Snooper s)
{
ImGui.TableSetBgColor(ImGuiTableBgTarget.RowBg0, ImGui.GetColorU32(new Vector4(1, 0, 0, .5f)));
}
else if (s.Renderer.Color == VertexColor.Sections)
{
ImGui.TableSetBgColor(ImGuiTableBgTarget.RowBg0, ImGui.GetColorU32(new Vector4(section.Color, 0.5f)));
}
ImGui.Text(section.MaterialIndex.ToString("D"));
ImGui.TableNextColumn();
Expand Down Expand Up @@ -607,7 +613,7 @@ private void DrawDetails(Snooper s)
if (ImGui.BeginListBox("", box))
{
for (int i = 0; i < model.Morphs.Length; i++)
for (int i = 0; i < model.Morphs.Count; i++)
{
ImGui.PushID(i);
if (ImGui.Selectable(model.Morphs[i].Name, s.Renderer.Options.SelectedMorph == i))
Expand Down
2 changes: 2 additions & 0 deletions FModel/Views/Snooper/Snooper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public unsafe void WindowShouldFreeze(bool value)

public override void Run()
{
Renderer.Options.SwapMaterial(false);
Renderer.Options.AnimateMesh(false);
Application.Current.Dispatcher.Invoke(delegate
{
WindowShouldClose(false, false);
Expand Down

0 comments on commit edd3a47

Please sign in to comment.