using canvas in texture inspector

This commit is contained in:
4sval 2023-02-11 23:57:42 +01:00
parent 995cd25bc4
commit a77d76ff34
3 changed files with 53 additions and 29 deletions

View File

@ -52,8 +52,6 @@ public class Model : IDisposable
private BufferObject<Matrix4x4> _matrixVbo;
private VertexArrayObject<float, uint> _vao;
protected int VertexSize => _vertexAttributes.Where(x => x.Enabled).Sum(x => x.Size);
protected bool HasVertexColors => _vertexAttributes[(int) EAttribute.Colors].Enabled;
private readonly List<VertexAttribute> _vertexAttributes = new()
{
new VertexAttribute { Size = 1, Enabled = true }, // VertexIndex
@ -66,6 +64,8 @@ public class Model : IDisposable
new VertexAttribute { Size = 4, Enabled = false }, // BoneIds
new VertexAttribute { Size = 4, Enabled = false } // BoneWeights
};
public int VertexSize => _vertexAttributes.Where(x => x.Enabled).Sum(x => x.Size);
public bool HasVertexColors => _vertexAttributes[(int) EAttribute.Colors].Enabled;
private const int _faceSize = 3;
private readonly UObject _export;

View File

@ -336,7 +336,8 @@ public class Material : IDisposable
return ImGui.IsItemHovered() && ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left);
}
public Vector2[] ImGuiTextureInspector(Texture fallback)
private Vector3 _scrolling = new (0.0f, 0.0f, 1.0f);
public void ImGuiTextureInspector(Texture fallback)
{
var texture = GetSelectedTexture() ?? fallback;
if (ImGui.BeginTable("texture_inspector", 2, ImGuiTableFlags.SizingStretchProp))
@ -352,15 +353,54 @@ public class Material : IDisposable
});
}
var largest = ImGui.GetContentRegionAvail();
largest.X -= ImGui.GetScrollX();
largest.Y -= ImGui.GetScrollY();
var io = ImGui.GetIO();
var canvasP0 = ImGui.GetCursorScreenPos();
var canvasSize = ImGui.GetContentRegionAvail();
if (canvasSize.X < 50.0f) canvasSize.X = 50.0f;
if (canvasSize.Y < 50.0f) canvasSize.Y = 50.0f;
var canvasP1 = new Vector2(canvasP0.X + canvasSize.X, canvasP0.Y + canvasSize.Y);
var origin = new Vector2(canvasP0.X + _scrolling.X, canvasP0.Y + _scrolling.Y);
var absoluteMiddle = canvasSize / 2.0f;
var ratio = Math.Min(largest.X / texture.Width, largest.Y / texture.Height);
var size = new Vector2(texture.Width * ratio, texture.Height * ratio);
var pos = ImGui.GetCursorPos();
ImGui.Image(texture.GetPointer(),size, Vector2.Zero, Vector2.One, Vector4.One, new Vector4(1.0f, 1.0f, 1.0f, 0.25f));
return new[] { size, pos };
ImGui.InvisibleButton("canvas", canvasSize, ImGuiButtonFlags.MouseButtonLeft | ImGuiButtonFlags.MouseButtonRight);
if (ImGui.IsItemActive() && ImGui.IsMouseDragging(ImGuiMouseButton.Left))
{
_scrolling.X += io.MouseDelta.X;
_scrolling.Y += io.MouseDelta.Y;
}
else if (ImGui.IsItemHovered() && io.MouseWheel != 0.0f)
{
var zoomFactor = 1.0f + io.MouseWheel * 0.1f;
var mousePosCanvas = io.MousePos - origin;
_scrolling.X -= (mousePosCanvas.X - absoluteMiddle.X) * (zoomFactor - 1);
_scrolling.Y -= (mousePosCanvas.Y - absoluteMiddle.Y) * (zoomFactor - 1);
_scrolling.Z *= zoomFactor;
origin = new Vector2(canvasP0.X + _scrolling.X, canvasP0.Y + _scrolling.Y);
}
var drawList = ImGui.GetWindowDrawList();
drawList.AddRectFilled(canvasP0, canvasP1, 0xFF242424);
drawList.PushClipRect(canvasP0, canvasP1, true);
{
var sensitivity = _scrolling.Z * 25.0f;
for (float x = _scrolling.X % sensitivity; x < canvasSize.X; x += sensitivity)
drawList.AddLine(canvasP0 with { X = canvasP0.X + x }, canvasP1 with { X = canvasP0.X + x }, 0x28C8C8C8);
for (float y = _scrolling.Y % sensitivity; y < canvasSize.Y; y += sensitivity)
drawList.AddLine(canvasP0 with { Y = canvasP0.Y + y }, canvasP1 with { Y = canvasP0.Y + y }, 0x28C8C8C8);
}
drawList.PopClipRect();
drawList.PushClipRect(canvasP0, canvasP1, true);
{
var relativeMiddle = origin + absoluteMiddle;
var ratio = Math.Min(canvasSize.X / texture.Width, canvasSize.Y / texture.Height) * 0.95f * _scrolling.Z;
var size = new Vector2(texture.Width, texture.Height) * ratio / 2f;
drawList.AddImage(texture.GetPointer(), relativeMiddle - size, relativeMiddle + size);
drawList.AddRect(relativeMiddle - size, relativeMiddle + size, 0xFFFFFFFF);
}
drawList.PopClipRect();
}
private Texture GetSelectedTexture()

View File

@ -48,7 +48,6 @@ public class SnimGui
private readonly string _renderer;
private readonly string _version;
private bool _ti_open;
private bool _ti_overlayUv;
private bool _viewportFocus;
private readonly Vector4 _accentColor = new (0.125f, 0.42f, 0.831f, 1.0f);
@ -660,22 +659,7 @@ Snooper aims to give an accurate preview of models, materials, skeletal animatio
s.Renderer.Options.TryGetModel(out var model) &&
s.Renderer.Options.TryGetSection(model, out var section))
{
var vectors = model.Materials[section.MaterialIndex].ImGuiTextureInspector(s.Renderer.Options.Icons["noimage"]);
if (_ti_overlayUv)
{
var size = vectors[0];
var drawList = ImGui.GetWindowDrawList();
drawList.PushClipRect(size, size, true);
ImGui.SetCursorPos(vectors[1]);
ImGui.InvisibleButton("canvas", size, ImGuiButtonFlags.MouseButtonLeft | ImGuiButtonFlags.MouseButtonRight);
drawList.AddLine(new Vector2(0, 0), size, 255, 2f);
drawList.PopClipRect();
}
Popup(() =>
{
if (ImGui.MenuItem("Overlay UVs", null, _ti_overlayUv, false))
_ti_overlayUv = !_ti_overlayUv;
});
model.Materials[section.MaterialIndex].ImGuiTextureInspector(s.Renderer.Options.Icons["noimage"]);
}
ImGui.End(); // if window is collapsed
}
@ -851,7 +835,7 @@ Snooper aims to give an accurate preview of models, materials, skeletal animatio
io.ConfigFlags |= ImGuiConfigFlags.DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
io.ConfigWindowsMoveFromTitleBarOnly = true;
io.ConfigDockingWithShift = true;
// io.ConfigDockingWithShift = true; doesn't work anymore??
style.WindowPadding = new Vector2(4f);
style.FramePadding = new Vector2(3f);