Skip to content

Commit

Permalink
switch to ImTool
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Sep 3, 2022
1 parent ac124e8 commit eabf6d9
Show file tree
Hide file tree
Showing 11 changed files with 773 additions and 104 deletions.
496 changes: 496 additions & 0 deletions FModel/Extensions/ImGuiControllerExtensions.cs

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion FModel/FModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
<None Remove="Resources\skybox.vert" />
<None Remove="Resources\framebuffer.frag" />
<None Remove="Resources\framebuffer.vert" />
<None Remove="Resources\imgui.frag" />
<None Remove="Resources\imgui.vert" />
</ItemGroup>

<ItemGroup>
Expand All @@ -118,6 +120,8 @@
<EmbeddedResource Include="Resources\skybox.vert" />
<EmbeddedResource Include="Resources\framebuffer.frag" />
<EmbeddedResource Include="Resources\framebuffer.vert" />
<EmbeddedResource Include="Resources\imgui.frag" />
<EmbeddedResource Include="Resources\imgui.vert" />
</ItemGroup>

<ItemGroup>
Expand All @@ -129,6 +133,7 @@
<PackageReference Include="DiscordRichPresence" Version="1.0.175" />
<PackageReference Include="EpicManifestParser" Version="1.2.70-temp" />
<PackageReference Include="HelixToolkit.SharpDX.Core.Wpf" Version="2.21.0" />
<PackageReference Include="ImTool" Version="1.3.6" />
<PackageReference Include="K4os.Compression.LZ4.Streams" Version="1.2.16" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NVorbis" Version="0.10.4" />
Expand All @@ -138,8 +143,8 @@
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Silk.NET.Input" Version="2.16.0" />
<PackageReference Include="Silk.NET.Input.Extensions" Version="2.16.0" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.16.0" />
<PackageReference Include="Silk.NET.OpenGL.Extensions.ImGui" Version="2.16.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SkiaSharp.HarfBuzz" Version="2.88.0" />
<PackageReference Include="SkiaSharp.Svg" Version="1.60.0" />
Expand Down
16 changes: 16 additions & 0 deletions FModel/Framework/ImGuiFontConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace FModel.Framework;

public readonly struct ImGuiFontConfig
{
public ImGuiFontConfig(string fontPath, int fontSize)
{
if (fontSize <= 0) throw new ArgumentOutOfRangeException(nameof(fontSize));
FontPath = fontPath ?? throw new ArgumentNullException(nameof(fontPath));
FontSize = fontSize;
}

public string FontPath { get; }
public int FontSize { get; }
}
2 changes: 1 addition & 1 deletion FModel/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private async void OnLoaded(object sender, RoutedEventArgs e)
#if DEBUG
await _threadWorkerView.Begin(_ =>
_applicationView.CUE4Parse.Extract(
"FortniteGame/Content/Environments/Props/Winter/Meshes/SM_ChristmasTree_Llama.uasset"));
"FortniteGame/Content/Characters/Player/Male/Medium/Bodies/M_MED_StaminaCat/Meshes/M_MED_StaminaCat.uasset"));
#endif
}

Expand Down
13 changes: 13 additions & 0 deletions FModel/Resources/imgui.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 330

layout (location = 0) out vec4 Out_Color;

in vec2 Frag_UV;
in vec4 Frag_Color;

uniform sampler2D Texture;

void main()
{
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
}
17 changes: 17 additions & 0 deletions FModel/Resources/imgui.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#version 330

layout (location = 0) in vec2 Position;
layout (location = 1) in vec2 UV;
layout (location = 2) in vec4 Color;

uniform mat4 ProjMtx;

out vec2 Frag_UV;
out vec4 Frag_Color;

void main()
{
Frag_UV = UV;
Frag_Color = Color;
gl_Position = ProjMtx * vec4(Position.xy,0,1);
}
4 changes: 2 additions & 2 deletions FModel/Views/Snooper/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Model : IDisposable
public readonly float[] Vertices;
public readonly Section[] Sections;

public Transform Transforms = Transform.Identity;
public readonly Transform Transforms = Transform.Identity;
public readonly string[] TransformsLabels = {
"X Location", "Y", "Z",
"X Rotation", "Y", "Z",
Expand Down Expand Up @@ -127,7 +127,7 @@ public void Bind(Camera camera)

for (int section = 0; section < Sections.Length; section++)
{
Sections[section].Bind(section, _shader);
Sections[section].Bind(_shader);
}
}

Expand Down
119 changes: 39 additions & 80 deletions FModel/Views/Snooper/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using CUE4Parse_Conversion.Textures;
using FModel.Services;
using FModel.Settings;
using ImGuiNET;
using Silk.NET.OpenGL;

namespace FModel.Views.Snooper;
Expand All @@ -16,16 +15,6 @@ public class Section : IDisposable
private uint _handle;
private GL _gl;

private Texture _diffuseMap;
private Texture _normalMap;
private Texture _specularMap;
private Texture _emissionMap;

private bool _hasSpecularMap;
private bool _hasDiffuseColor;
private Vector4 _diffuseColor = Vector4.Zero;
private Vector4 _emissionColor = Vector4.Zero;

private Vector3 _ambientLight;
private Vector3 _diffuseLight;
private Vector3 _specularLight;
Expand All @@ -38,9 +27,14 @@ public class Section : IDisposable
public readonly int FirstFaceIndex;
public readonly CMaterialParams Parameters;

private bool _show = true;
private bool _wireframe;
private bool _selected;
public bool Show;
public bool Wireframe;
public readonly Texture[] Textures;
public readonly string[] TexturesLabels;
public Vector4 DiffuseColor;
public Vector4 EmissionColor;
public bool HasSpecularMap;
public bool HasDiffuseColor;

public Section(string name, int index, uint facesCount, int firstFaceIndex, CMeshSection section)
{
Expand All @@ -55,6 +49,12 @@ public Section(string name, int index, uint facesCount, int firstFaceIndex, CMes
unrealMaterial.GetParams(Parameters);
}

Show = true;
Textures = new Texture[4];
TexturesLabels = new[] { "Diffuse", "Normal", "Specular", "Emissive" };
DiffuseColor = Vector4.Zero;
EmissionColor = Vector4.Zero;

_game = ApplicationService.ApplicationView.CUE4Parse.Game;
}

Expand All @@ -66,34 +66,34 @@ public void Setup(GL gl)

if (Parameters.IsNull)
{
_diffuseColor = new Vector4(1, 0, 0, 1);
DiffuseColor = new Vector4(1, 0, 0, 1);
}
else
{
var platform = UserSettings.Default.OverridedPlatform;
if (!Parameters.HasTopDiffuseTexture && Parameters.DiffuseColor is { A: > 0 } diffuseColor)
{
_diffuseColor = new Vector4(diffuseColor.R, diffuseColor.G, diffuseColor.B, diffuseColor.A);
DiffuseColor = new Vector4(diffuseColor.R, diffuseColor.G, diffuseColor.B, diffuseColor.A);
}
else if (Parameters.Diffuse is UTexture2D { IsVirtual: false } diffuse)
{
var mip = diffuse.GetFirstMip();
TextureDecoder.DecodeTexture(mip, diffuse.Format, diffuse.isNormalMap, platform, out var data, out _);
_diffuseMap = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
Textures[0] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
}

if (Parameters.Normal is UTexture2D { IsVirtual: false } normal)
{
var mip = normal.GetFirstMip();
TextureDecoder.DecodeTexture(mip, normal.Format, normal.isNormalMap, platform, out var data, out _);
_normalMap = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
Textures[1] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
}

if (Parameters.Specular is UTexture2D { IsVirtual: false } specular)
{
var mip = specular.GetFirstMip();
SwapSpecular(specular, mip, platform, out var data);
_specularMap = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
Textures[2] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
}

if (Parameters.HasTopEmissiveTexture &&
Expand All @@ -102,18 +102,18 @@ public void Setup(GL gl)
{
var mip = emissive.GetFirstMip();
TextureDecoder.DecodeTexture(mip, emissive.Format, emissive.isNormalMap, platform, out var data, out _);
_emissionMap = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
_emissionColor = new Vector4(emissiveColor.R, emissiveColor.G, emissiveColor.B, emissiveColor.A);
Textures[3] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
EmissionColor = new Vector4(emissiveColor.R, emissiveColor.G, emissiveColor.B, emissiveColor.A);
}
}

// diffuse light is based on normal map, so increase ambient if no normal map
_ambientLight = new Vector3(_normalMap == null ? 1.0f : 0.2f);
_ambientLight = new Vector3(Textures[1] == null ? 1.0f : 0.2f);
_diffuseLight = new Vector3(0.75f);
_specularLight = new Vector3(0.5f);
_hasSpecularMap = _specularMap != null;
_hasDiffuseColor = _diffuseColor != Vector4.Zero;
_show = !Parameters.IsNull && !Parameters.IsTransparent;
HasSpecularMap = Textures[2] != null;
HasDiffuseColor = DiffuseColor != Vector4.Zero;
Show = !Parameters.IsNull && !Parameters.IsTransparent;
}

/// <summary>
Expand Down Expand Up @@ -220,77 +220,36 @@ private void SwapSpecular(UTexture2D specular, FTexture2DMipMap mip, ETexturePla
}
}

public void Bind(int index, Shader shader)
public void Bind(Shader shader)
{
// ImGui.TableNextRow();
//
// ImGui.TableSetColumnIndex(0);
// ImGui.Text(Index.ToString());
// ImGui.TableSetColumnIndex(1);
// ImGui.Text(Name);
// if (ImGui.IsItemHovered())
// {
// ImGui.BeginTooltip();
// ImGui.Text($"Faces: {FacesCount} ({Math.Round(FacesCount / indices * 100f, 2)}%%)");
// ImGui.Text($"First Face: {FirstFaceIndex}");
// ImGui.Separator();
// if (_hasDiffuseColor)
// {
// ImGui.ColorEdit4("Diffuse Color", ref _diffuseColor, ImGuiColorEditFlags.NoInputs);
// }
// else
// {
// ImGui.Text($"Diffuse: ({Parameters.Diffuse?.ExportType}) {Parameters.Diffuse?.Name}");
// ImGui.Text($"Normal: ({Parameters.Normal?.ExportType}) {Parameters.Normal?.Name}");
// ImGui.Text($"Specular: ({Parameters.Specular?.ExportType}) {Parameters.Specular?.Name}");
// if (Parameters.HasTopEmissiveTexture)
// ImGui.Text($"Emissive: ({Parameters.Emissive?.ExportType}) {Parameters.Emissive?.Name}");
// ImGui.Separator();
// }
// ImGui.EndTooltip();
// }

// DrawImGui(index);

_diffuseMap?.Bind(TextureUnit.Texture0);
_normalMap?.Bind(TextureUnit.Texture1);
_specularMap?.Bind(TextureUnit.Texture2);
_emissionMap?.Bind(TextureUnit.Texture3);
for (var i = 0; i < Textures.Length; i++)
{
Textures[i]?.Bind(TextureUnit.Texture0 + i);
}

shader.SetUniform("material.useSpecularMap", _hasSpecularMap);
shader.SetUniform("material.useSpecularMap", HasSpecularMap);

shader.SetUniform("material.hasDiffuseColor", _hasDiffuseColor);
shader.SetUniform("material.diffuseColor", _diffuseColor);
shader.SetUniform("material.hasDiffuseColor", HasDiffuseColor);
shader.SetUniform("material.diffuseColor", DiffuseColor);

shader.SetUniform("material.emissionColor", _emissionColor);
shader.SetUniform("material.emissionColor", EmissionColor);

shader.SetUniform("material.shininess", Parameters.MetallicValue);

shader.SetUniform("light.ambient", _ambientLight);
shader.SetUniform("light.diffuse", _diffuseLight);
shader.SetUniform("light.specular", _specularLight);

_gl.PolygonMode(MaterialFace.Front, _wireframe ? PolygonMode.Line : PolygonMode.Fill);
if (_show) _gl.DrawArrays(PrimitiveType.Triangles, FirstFaceIndex, FacesCount);
_gl.PolygonMode(MaterialFace.Front, Wireframe ? PolygonMode.Line : PolygonMode.Fill);
if (Show) _gl.DrawArrays(PrimitiveType.Triangles, FirstFaceIndex, FacesCount);
}

public void Dispose()
{
_diffuseMap?.Dispose();
_normalMap?.Dispose();
_specularMap?.Dispose();
_emissionMap?.Dispose();
_gl.DeleteProgram(_handle);
}

private void DrawImGui(int index)
{
ImGui.PushID(index);
ImGui.Selectable(Name, ref _selected);
if (_selected)
for (var i = 0; i < Textures.Length; i++)
{

Textures[i]?.Dispose();
}
ImGui.PopID();
_gl.DeleteProgram(_handle);
}
}
35 changes: 33 additions & 2 deletions FModel/Views/Snooper/Shader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Reflection;
Expand All @@ -8,8 +9,10 @@ namespace FModel.Views.Snooper;

public class Shader : IDisposable
{
private uint _handle;
private GL _gl;
private readonly uint _handle;
private readonly GL _gl;
private readonly Dictionary<string, int> _uniformToLocation = new ();
private readonly Dictionary<string, int> _attribLocation = new ();

public Shader(GL gl) : this(gl, "default") {}

Expand Down Expand Up @@ -103,6 +106,34 @@ public void SetUniform(string name, Vector4 value)
_gl.Uniform4(location, value.X, value.Y, value.Z, value.W);
}

public int GetUniformLocation(string uniform)
{
if (!_uniformToLocation.TryGetValue(uniform, out int location))
{
location = _gl.GetUniformLocation(_handle, uniform);
_uniformToLocation.Add(uniform, location);
if (location == -1)
{
Serilog.Log.Debug($"The uniform '{uniform}' does not exist in the shader!");
}
}
return location;
}

public int GetAttribLocation(string attrib)
{
if (!_attribLocation.TryGetValue(attrib, out int location))
{
location = _gl.GetAttribLocation(_handle, attrib);
_attribLocation.Add(attrib, location);
if (location == -1)
{
Serilog.Log.Debug($"The attrib '{attrib}' does not exist in the shader!");
}
}
return location;
}

public void Dispose()
{
_gl.DeleteProgram(_handle);
Expand Down
Loading

0 comments on commit eabf6d9

Please sign in to comment.