Skip to content

Commit

Permalink
imgui stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Sep 3, 2022
1 parent 97b03ed commit 872b01c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
3 changes: 3 additions & 0 deletions FModel/Creator/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ public static string GetFullPath(string partialPath)
return string.Empty;
}

public static string FixPath(string weirdPath) =>
_applicationView.CUE4Parse.Provider.FixPath(weirdPath, StringComparison.Ordinal);

public static void DrawCenteredMultilineText(SKCanvas c, string text, int maxCount, int size, int margin, SKTextAlign side, SKRect area, SKPaint paint)
{
var lineHeight = paint.TextSize * 1.2f;
Expand Down
10 changes: 5 additions & 5 deletions FModel/Views/Snooper/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ public void Setup(GL gl)
{
var mip = diffuse.GetFirstMip();
TextureDecoder.DecodeTexture(mip, diffuse.Format, diffuse.isNormalMap, platform, out var data, out _);
Textures[0] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
Textures[0] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY, diffuse);
}

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

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

if (Parameters.HasTopEmissiveTexture &&
Expand All @@ -102,7 +102,7 @@ public void Setup(GL gl)
{
var mip = emissive.GetFirstMip();
TextureDecoder.DecodeTexture(mip, emissive.Format, emissive.isNormalMap, platform, out var data, out _);
Textures[3] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
Textures[3] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY, emissive);
EmissionColor = new Vector4(emissiveColor.R, emissiveColor.G, emissiveColor.B, emissiveColor.A);
}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public void Bind(Shader shader)
shader.SetUniform("light.diffuse", _diffuseLight);
shader.SetUniform("light.specular", _specularLight);

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

Expand Down
34 changes: 31 additions & 3 deletions FModel/Views/Snooper/SnimGui.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Windows;
using FModel.Creator;
using ImGuiNET;
using Silk.NET.Input;
using Silk.NET.Maths;
Expand Down Expand Up @@ -255,13 +257,12 @@ private void DrawTextures(IList<Model> models)
ImGui.BeginGroup();
ImGui.Checkbox("Show", ref section.Show);
ImGui.Checkbox("Wireframe", ref section.Wireframe);
ImGui.Checkbox("3", ref section.Wireframe);
ImGui.Checkbox("4", ref section.Wireframe);
ImGui.EndGroup();
ImGui.SameLine();
ImGui.BeginGroup();
if (section.HasDiffuseColor)
{
ImGui.SetNextItemWidth(300);
ImGui.ColorEdit4(section.TexturesLabels[0], ref section.DiffuseColor, ImGuiColorEditFlags.AlphaPreview | ImGuiColorEditFlags.AlphaBar);
}
else
Expand All @@ -274,6 +275,33 @@ private void DrawTextures(IList<Model> models)
ImGui.SameLine();
ImGui.BeginGroup();
ImGui.Image(texture.GetPointer(), new Vector2(88), Vector2.Zero, Vector2.One, Vector4.One, new Vector4(1, 1, 1, .5f));
if (ImGui.IsItemHovered())
{
ImGui.BeginTooltip();
ImGui.Text($"Type: ({texture.Format}) {texture.Type}");
ImGui.Text($"Texture: {texture.Path}.{texture.Name}");
ImGui.Text($"Imported: {texture.ImportedWidth}x{texture.ImportedHeight}");
ImGui.Text($"Mip Used: {texture.Width}x{texture.Height}");
ImGui.Spacing();
ImGui.TextDisabled(texture.Label);
ImGui.EndTooltip();
}

if (ImGui.IsItemClicked())
{
Application.Current.Dispatcher.Invoke(delegate
{
Clipboard.SetText(Utils.FixPath(texture.Path));
texture.Label = "(?) Copied to Clipboard";
});
}

if (i == 3) // emissive, show color
{
ImGui.SameLine();
ImGui.SetNextItemWidth(300);
ImGui.ColorEdit4($"{section.TexturesLabels[i]} Color", ref section.EmissionColor, ImGuiColorEditFlags.NoAlpha);
}
var text = section.TexturesLabels[i];
var width = ImGui.GetCursorPos().X;
ImGui.SetCursorPosX(width + ImGui.CalcTextSize(text).X * 0.5f);
Expand Down Expand Up @@ -381,7 +409,7 @@ private void Theme(ImGuiStylePtr style)
io.ConfigWindowsMoveFromTitleBarOnly = true;
io.ConfigDockingWithShift = true;

// style.WindowPadding = Vector2.Zero;
style.WindowMenuButtonPosition = ImGuiDir.Right;
// style.Colors[(int) ImGuiCol.Text] = new Vector4(0.95f, 0.96f, 0.98f, 1.00f);
// style.Colors[(int) ImGuiCol.TextDisabled] = new Vector4(0.36f, 0.42f, 0.47f, 1.00f);
// style.Colors[(int) ImGuiCol.WindowBg] = new Vector4(0.149f, 0.149f, 0.188f, 0.35f);
Expand Down
43 changes: 36 additions & 7 deletions FModel/Views/Snooper/Texture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Silk.NET.OpenGL;
using System;
using System.Windows;
using CUE4Parse.UE4.Assets.Exports.Texture;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

Expand All @@ -12,18 +13,32 @@ public class Texture : IDisposable
private readonly GL _gl;
private readonly TextureType _type;

public readonly string Type;
public readonly string Name;
public readonly string Path;
public readonly EPixelFormat Format;
public readonly uint ImportedWidth;
public readonly uint ImportedHeight;
public readonly uint Width;
public readonly uint Height;
public string Label;

public Texture(GL gl, TextureType type)
{
_gl = gl;
_handle = _gl.GenTexture();
_type = type;

Label = "(?) Click to Copy";
}

public Texture(GL gl, uint width, uint height) : this(gl, TextureType.MsaaFramebuffer)
{
Width = width;
Height = height;
Bind(TextureUnit.Texture0);

_gl.TexImage2DMultisample(TextureTarget.Texture2DMultisample, Constants.SAMPLES_COUNT, InternalFormat.Rgb, width, height, Silk.NET.OpenGL.Boolean.True);
_gl.TexImage2DMultisample(TextureTarget.Texture2DMultisample, Constants.SAMPLES_COUNT, InternalFormat.Rgb, Width, Height, Silk.NET.OpenGL.Boolean.True);

_gl.TexParameter(TextureTarget.Texture2DMultisample, TextureParameterName.TextureMinFilter, (int) GLEnum.Nearest);
_gl.TexParameter(TextureTarget.Texture2DMultisample, TextureParameterName.TextureMagFilter, (int) GLEnum.Nearest);
Expand All @@ -35,9 +50,11 @@ public Texture(GL gl, uint width, uint height) : this(gl, TextureType.MsaaFrameb

public unsafe Texture(GL gl, int width, int height) : this(gl, TextureType.Framebuffer)
{
Width = (uint) width;
Height = (uint) height;
Bind(TextureUnit.Texture0);

_gl.TexImage2D(TextureTarget.Texture2D, 0, (int) InternalFormat.Rgb, (uint) width, (uint) height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, null);
_gl.TexImage2D(TextureTarget.Texture2D, 0, (int) InternalFormat.Rgb, Width, Height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, null);

_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) GLEnum.Linear);
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) GLEnum.Linear);
Expand All @@ -47,13 +64,21 @@ public unsafe Texture(GL gl, int width, int height) : this(gl, TextureType.Frame
_gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, _handle, 0);
}

public unsafe Texture(GL gl, byte[] data, uint width, uint height) : this(gl, TextureType.Normal)
public unsafe Texture(GL gl, byte[] data, uint width, uint height, UTexture2D texture2D) : this(gl, TextureType.Normal)
{
Type = texture2D.ExportType;
Name = texture2D.Name;
Path = texture2D.Owner?.Name;
Format = texture2D.Format;
ImportedWidth = texture2D.ImportedSize.X;
ImportedHeight = texture2D.ImportedSize.Y;
Width = width;
Height = height;
Bind(TextureUnit.Texture0);

fixed (void* d = &data[0])
{
_gl.TexImage2D(TextureTarget.Texture2D, 0, (int) InternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, d);
_gl.TexImage2D(TextureTarget.Texture2D, 0, (int) InternalFormat.Rgba, Width, Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, d);

_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) GLEnum.LinearMipmapLinear);
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) GLEnum.Linear);
Expand All @@ -72,7 +97,9 @@ public unsafe Texture(GL gl, string[] textures) : this(gl, TextureType.Cubemap)
{
var info = Application.GetResourceStream(new Uri($"/FModel;component/Resources/{textures[t]}.png", UriKind.Relative));
using var img = Image.Load<Rgba32>(info.Stream);
_gl.TexImage2D(TextureTarget.TextureCubeMapPositiveX + t, 0, InternalFormat.Rgba8, (uint) img.Width, (uint) img.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, null);
Width = (uint) img.Width; // we don't care anyway
Height = (uint) img.Height; // we don't care anyway
_gl.TexImage2D(TextureTarget.TextureCubeMapPositiveX + t, 0, InternalFormat.Rgba8, Width, Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, null);

img.ProcessPixelRows(accessor =>
{
Expand All @@ -95,10 +122,12 @@ public unsafe Texture(GL gl, string[] textures) : this(gl, TextureType.Cubemap)

public unsafe Texture(GL gl, uint width, uint height, IntPtr data) : this(gl, TextureType.Normal)
{
Width = width;
Height = height;
Bind(TextureTarget.Texture2D);

_gl.TexStorage2D(GLEnum.Texture2D, 1, SizedInternalFormat.Rgba8, width, height);
_gl.TexSubImage2D(GLEnum.Texture2D, 0, 0, 0, width, height, PixelFormat.Bgra, PixelType.UnsignedByte, (void*) data);
_gl.TexStorage2D(GLEnum.Texture2D, 1, SizedInternalFormat.Rgba8, Width, Height);
_gl.TexSubImage2D(GLEnum.Texture2D, 0, 0, 0, Width, Height, PixelFormat.Bgra, PixelType.UnsignedByte, (void*) data);

_gl.TexParameterI(GLEnum.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
_gl.TexParameterI(GLEnum.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
Expand Down

0 comments on commit 872b01c

Please sign in to comment.