From 2d73375392d7cff7419d65495fbe64fc13511073 Mon Sep 17 00:00:00 2001 From: 4sval Date: Fri, 16 Sep 2022 06:55:44 +0200 Subject: [PATCH] one at a time, take it or leave it. --- FModel/MainWindow.xaml.cs | 2 +- FModel/Resources/default.vert | 7 ++- FModel/Resources/outline.vert | 7 ++- FModel/Views/Snooper/BufferObject.cs | 8 +++ FModel/Views/Snooper/Cube.cs | 74 ++++++++++++++-------------- FModel/Views/Snooper/Model.cs | 51 ++++++++++++++----- FModel/Views/Snooper/Morph.cs | 13 ----- FModel/Views/Snooper/SnimGui.cs | 38 ++++++++------ 8 files changed, 118 insertions(+), 82 deletions(-) diff --git a/FModel/MainWindow.xaml.cs b/FModel/MainWindow.xaml.cs index 85091baf..1e782c99 100644 --- a/FModel/MainWindow.xaml.cs +++ b/FModel/MainWindow.xaml.cs @@ -79,7 +79,7 @@ public partial class MainWindow #if DEBUG await _threadWorkerView.Begin(_ => _applicationView.CUE4Parse.Extract( - "Hk_project/Content/Character/Zurg/Mesh/SKM_Zurg.uasset")); + "/Game/Weapons/FORT_Melee/Pickaxe_Slurp_Monster/Meshes/Slurp_Monster_Axe.uasset")); #endif } diff --git a/FModel/Resources/default.vert b/FModel/Resources/default.vert index b3c922e9..e874738a 100644 --- a/FModel/Resources/default.vert +++ b/FModel/Resources/default.vert @@ -7,9 +7,11 @@ layout (location = 4) in vec4 vColor; layout (location = 5) in ivec4 vBoneIds; layout (location = 6) in vec4 vWeights; layout (location = 7) in mat4 vInstanceMatrix; +layout (location = 11) in vec3 vMorphTarget; uniform mat4 uView; uniform mat4 uProjection; +uniform float uMorphTime; out vec3 fPos; out vec3 fNormal; @@ -18,9 +20,10 @@ out vec4 fColor; void main() { - gl_Position = uProjection * uView * vInstanceMatrix * vec4(vPos, 1.0); + vec3 pos = mix(vPos, vMorphTarget, uMorphTime); + gl_Position = uProjection * uView * vInstanceMatrix * vec4(pos, 1.0); - fPos = vec3(vInstanceMatrix * vec4(vPos, 1.0)); + fPos = vec3(vInstanceMatrix * vec4(pos, 1.0)); fNormal = mat3(transpose(inverse(vInstanceMatrix))) * vNormal; fTexCoords = vTexCoords; fColor = vColor; diff --git a/FModel/Resources/outline.vert b/FModel/Resources/outline.vert index 5d7aeb70..65352125 100644 --- a/FModel/Resources/outline.vert +++ b/FModel/Resources/outline.vert @@ -3,14 +3,17 @@ layout (location = 1) in vec3 vPos; layout (location = 2) in vec3 vNormal; layout (location = 7) in mat4 vInstanceMatrix; +layout (location = 11) in vec3 vMorphTarget; uniform mat4 uView; uniform mat4 uProjection; +uniform float uMorphTime; uniform vec3 viewPos; void main() { - float scaleFactor = distance(vPos, viewPos) * 0.0025; - vec3 scaleVertex = vPos + vNormal * scaleFactor; + vec3 pos = mix(vPos, vMorphTarget, uMorphTime); + float scaleFactor = distance(pos, viewPos) * 0.0025; + vec3 scaleVertex = pos + vNormal * scaleFactor; gl_Position = uProjection * uView * vInstanceMatrix * vec4(scaleVertex, 1.0); } diff --git a/FModel/Views/Snooper/BufferObject.cs b/FModel/Views/Snooper/BufferObject.cs index cea445ee..51847103 100644 --- a/FModel/Views/Snooper/BufferObject.cs +++ b/FModel/Views/Snooper/BufferObject.cs @@ -31,6 +31,14 @@ public class BufferObject : IDisposable where TDataType : unmanaged _gl.BufferSubData(_bufferType, offset * sizeof(TDataType), (nuint) sizeof(TDataType), data); } + public unsafe void Update(Span data) + { + fixed (void* d = data) + { + _gl.BufferSubData(_bufferType, 0, (nuint) (data.Length * sizeof(TDataType)), d); + } + } + public void Bind() { _gl.BindBuffer(_bufferType, _handle); diff --git a/FModel/Views/Snooper/Cube.cs b/FModel/Views/Snooper/Cube.cs index a9820ffc..62975102 100644 --- a/FModel/Views/Snooper/Cube.cs +++ b/FModel/Views/Snooper/Cube.cs @@ -18,48 +18,48 @@ public class Cube : Model 32, 33, 34, 35 }; Vertices = new float[] { - //X Y Z Normals U V - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, - 0.5f, -0.5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0f, 1.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f + -1, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, + -1, 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + -1, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -1, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -1, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, + -1, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; Sections = new Section[1]; diff --git a/FModel/Views/Snooper/Model.cs b/FModel/Views/Snooper/Model.cs index 988a28c5..899eec41 100644 --- a/FModel/Views/Snooper/Model.cs +++ b/FModel/Views/Snooper/Model.cs @@ -18,6 +18,7 @@ public class Model : IDisposable private BufferObject _ebo; private BufferObject _vbo; + private BufferObject _morphVbo; private BufferObject _matrixVbo; private VertexArrayObject _vao; @@ -45,6 +46,8 @@ public class Model : IDisposable public bool IsSavable; public bool DisplayVertexColors; public bool DisplayBones; + public int SelectedMorph; + public float MorphTime; protected Model(UObject owner, string name, string type) { @@ -146,6 +149,13 @@ public class Model : IDisposable _matrixVbo.Unbind(); } + public void UpdateMorph() + { + _morphVbo.Bind(); + _morphVbo.Update(Morphs[SelectedMorph].Vertices); + _morphVbo.Unbind(); + } + public void Setup(GL gl) { _gl = gl; @@ -164,17 +174,28 @@ public class Model : IDisposable _vao.VertexAttributePointer(5, 4, VertexAttribPointerType.Int, _vertexSize, 13); // boneids _vao.VertexAttributePointer(6, 4, VertexAttribPointerType.Float, _vertexSize, 17); // boneweights - TransformsCount = Transforms.Count; - var instanceMatrix = new Matrix4x4[TransformsCount]; - for (var i = 0; i < instanceMatrix.Length; i++) - instanceMatrix[i] = Transforms[i].Matrix; - _matrixVbo = new BufferObject(_gl, instanceMatrix, BufferTargetARB.ArrayBuffer); - _vao.BindInstancing(); - - for (uint morph = 0; morph < Morphs.Length; morph++) - { - Morphs[morph].Setup(gl); + { // instanced models transform + TransformsCount = Transforms.Count; + var instanceMatrix = new Matrix4x4[TransformsCount]; + for (var i = 0; i < instanceMatrix.Length; i++) + instanceMatrix[i] = Transforms[i].Matrix; + _matrixVbo = new BufferObject(_gl, instanceMatrix, BufferTargetARB.ArrayBuffer); + _vao.BindInstancing(); // VertexAttributePointer 7, 8, 9, 10 } + + if (HasMorphTargets) + { + for (uint morph = 0; morph < Morphs.Length; morph++) + { + Morphs[morph].Setup(gl); + if (morph == SelectedMorph) + _morphVbo = new BufferObject(_gl, Morphs[morph].Vertices, BufferTargetARB.ArrayBuffer); + } + _vao.Bind(); + _vao.VertexAttributePointer(11, 3, VertexAttribPointerType.Float, _vertexSize, 1); // morph position + _vao.Unbind(); + } + for (int section = 0; section < Sections.Length; section++) { Sections[section].Setup(_gl); @@ -190,6 +211,7 @@ public class Model : IDisposable } _vao.Bind(); + shader.SetUniform("uMorphTime", MorphTime); shader.SetUniform("display_vertex_colors", DisplayVertexColors); for (int section = 0; section < Sections.Length; section++) { @@ -212,6 +234,7 @@ public class Model : IDisposable _vao.Bind(); shader.Use(); + shader.SetUniform("uMorphTime", MorphTime); for (int section = 0; section < Sections.Length; section++) { if (!Sections[section].Show) continue; @@ -230,9 +253,13 @@ public class Model : IDisposable _vbo.Dispose(); _matrixVbo.Dispose(); _vao.Dispose(); - for (var morph = 0; morph < Morphs.Length; morph++) + if (HasMorphTargets) { - Morphs[morph].Dispose(); + _morphVbo.Dispose(); + for (var morph = 0; morph < Morphs.Length; morph++) + { + Morphs[morph].Dispose(); + } } for (int section = 0; section < Sections.Length; section++) { diff --git a/FModel/Views/Snooper/Morph.cs b/FModel/Views/Snooper/Morph.cs index 885753b6..1830d020 100644 --- a/FModel/Views/Snooper/Morph.cs +++ b/FModel/Views/Snooper/Morph.cs @@ -10,13 +10,9 @@ public class Morph : IDisposable private uint _handle; private GL _gl; - private BufferObject _vbo; - public readonly string Name; public readonly float[] Vertices; - public float Value; - public Morph(float[] vertices, uint vertexSize, UMorphTarget morphTarget) { Name = morphTarget.Name; @@ -49,20 +45,11 @@ public class Morph : IDisposable public void Setup(GL gl) { _gl = gl; - _handle = _gl.CreateProgram(); - - _vbo = new BufferObject(_gl, Vertices, BufferTargetARB.ArrayBuffer); - } - - public void Bind(Shader shader) - { - shader.SetUniform("uMorphTime", Value); } public void Dispose() { - _vbo.Dispose(); _gl.DeleteProgram(_handle); } } diff --git a/FModel/Views/Snooper/SnimGui.cs b/FModel/Views/Snooper/SnimGui.cs index b3ce3d3c..a2f9d22a 100644 --- a/FModel/Views/Snooper/SnimGui.cs +++ b/FModel/Views/Snooper/SnimGui.cs @@ -313,18 +313,26 @@ public class SnimGui : IDisposable ImGui.EndTabItem(); } - ImGui.BeginDisabled(!model.HasMorphTargets); - if (ImGui.BeginTabItem("Shape Keys")) + if (model.HasMorphTargets && ImGui.BeginTabItem("Morph Targets")) { - for (int i = 0; i < model.Morphs.Length; i++) + if (ImGui.BeginListBox("", new Vector2(ImGui.GetContentRegionAvail().X, _propertiesSize.Y / 2))) { - ImGui.PushID(i); - ImGui.DragFloat(model.Morphs[i].Name, ref model.Morphs[i].Value, 0.001f, 0.0f, 1.0f, "%.2f", ImGuiSliderFlags.AlwaysClamp); - ImGui.PopID(); + for (int i = 0; i < model.Morphs.Length; i++) + { + ImGui.PushID(i); + if (ImGui.Selectable(model.Morphs[i].Name, model.SelectedMorph == i)) + { + model.SelectedMorph = i; + model.UpdateMorph(); + } + ImGui.PopID(); + } + ImGui.EndListBox(); + ImGui.Separator(); + ImGui.DragFloat("Value", ref model.MorphTime, 0.001f, 0.0f, 1.0f, "%.2f", ImGuiSliderFlags.AlwaysClamp); } ImGui.EndTabItem(); } - ImGui.EndDisabled(); } ImGui.End(); @@ -349,7 +357,7 @@ public class SnimGui : IDisposable { ImGui.SetNextItemWidth(300); ImGui.ColorEdit4(section.TexturesLabels[0], ref section.DiffuseColor, ImGuiColorEditFlags.AlphaPreview | ImGuiColorEditFlags.AlphaBar); - if (section.Textures[1] is { } normalMap) DrawTexture(normalMap); + if (section.Textures[1] is { } normalMap) DrawTexture(normalMap, "Normal"); } else { @@ -358,7 +366,7 @@ public class SnimGui : IDisposable if (section.Textures[i] is not {} texture) continue; - DrawTexture(texture); + DrawTexture(texture, section.TexturesLabels[i]); if (i == 3) // emissive, show color { @@ -366,11 +374,6 @@ public class SnimGui : IDisposable 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); - ImGui.Text(text); - ImGui.EndGroup(); } } ImGui.EndGroup(); @@ -378,7 +381,7 @@ public class SnimGui : IDisposable ImGui.End(); } - private void DrawTexture(Texture texture) + private void DrawTexture(Texture texture, string label) { ImGui.SameLine(); ImGui.BeginGroup(); @@ -403,6 +406,11 @@ public class SnimGui : IDisposable texture.Label = "(?) Copied to Clipboard"; }); } + + var width = ImGui.GetCursorPos().X; + ImGui.SetCursorPosX(width + ImGui.CalcTextSize(label).X * 0.5f); + ImGui.Text(label); + ImGui.EndGroup(); } private void Draw3DViewport(FramebufferObject framebuffer, Camera camera, IMouse mouse)