From 09ab40ad37ef998eba8885eff0094cce21e02775 Mon Sep 17 00:00:00 2001 From: 4sval Date: Mon, 16 Jan 2023 23:16:53 +0100 Subject: [PATCH] ctrl s, don't use this commit for tomorrow --- CUE4Parse | 2 +- FModel/Resources/default.vert | 33 ++++++++--------- FModel/Resources/light.vert | 2 +- FModel/Views/Snooper/Lights/Light.cs | 12 ++++++- .../Snooper/Models/Animations/Animation.cs | 36 +++++++++++-------- .../Snooper/Models/Animations/Skeleton.cs | 11 +++--- FModel/Views/Snooper/Models/Model.cs | 8 ++++- FModel/Views/Snooper/Renderer.cs | 7 +++- FModel/Views/Snooper/Shading/Shader.cs | 2 +- FModel/Views/Snooper/Transform.cs | 6 ++-- 10 files changed, 72 insertions(+), 47 deletions(-) diff --git a/CUE4Parse b/CUE4Parse index b02fb8c6..bcca52dc 160000 --- a/CUE4Parse +++ b/CUE4Parse @@ -1 +1 @@ -Subproject commit b02fb8c67e3ab7b1c256297a4363dedc6aca951a +Subproject commit bcca52dcbbe47b5ba24e34982ca6921c2478d9b1 diff --git a/FModel/Resources/default.vert b/FModel/Resources/default.vert index 6148892c..3a105f9b 100644 --- a/FModel/Resources/default.vert +++ b/FModel/Resources/default.vert @@ -12,13 +12,12 @@ layout (location = 9) in mat4 vInstanceMatrix; layout (location = 13) in vec3 vMorphTargetPos; layout (location = 14) in vec3 vMorphTargetTangent; -//const int MAX_BONES = 140; -//const int MAX_BONE_INFLUENCE = 4; +const int MAX_BONES = 140; uniform mat4 uView; uniform mat4 uProjection; uniform float uMorphTime; -//uniform mat4 uFinalBonesMatrix[MAX_BONES]; +uniform mat4 uFinalBonesMatrix[MAX_BONES]; out vec3 fPos; out vec3 fNormal; @@ -29,24 +28,22 @@ out vec4 fColor; void main() { - vec4 pos = vec4(mix(vPos, vMorphTargetPos, uMorphTime), 1.0); + vec4 bindPos = vec4(mix(vPos, vMorphTargetPos, uMorphTime), 1.0); vec3 tangent = mix(vTangent, vMorphTargetTangent, uMorphTime); -// for(int i = 0 ; i < MAX_BONE_INFLUENCE; i++) -// { -// int boneIndex = int(vBoneIds[i]); -// if(boneIndex == -1) continue; -// if(boneIndex >= MAX_BONES) -// { -// break; -// } -// -// vec4 localPos = uFinalBonesMatrix[boneIndex] * pos; -// pos += localPos * vBoneWeights[i]; -// } - gl_Position = uProjection * uView * vInstanceMatrix * pos; + vec4 finalPos = vec4(0.0); + vec4 weights = normalize(vBoneWeights); + for(int i = 0 ; i < 4; i++) + { + int boneIndex = int(vBoneIds[i]); + if(boneIndex < 0) break; - fPos = vec3(vInstanceMatrix * pos); + finalPos += inverse(uFinalBonesMatrix[boneIndex]) * bindPos * weights[i]; + } + + gl_Position = uProjection * uView * vInstanceMatrix * bindPos; + + fPos = vec3(vInstanceMatrix * bindPos); fNormal = mat3(transpose(inverse(vInstanceMatrix))) * vNormal; fTangent = mat3(transpose(inverse(vInstanceMatrix))) * tangent; fTexCoords = vTexCoords; diff --git a/FModel/Resources/light.vert b/FModel/Resources/light.vert index 06802a1d..0b7778ce 100644 --- a/FModel/Resources/light.vert +++ b/FModel/Resources/light.vert @@ -10,7 +10,7 @@ out vec2 fTexCoords; void main() { - float scale = 0.075; + float scale = 0.0075; mat4 result; result[0] = vec4(scale, 0.0, 0.0, 0.0); result[1] = vec4(0.0, scale, 0.0, 0.0); diff --git a/FModel/Views/Snooper/Lights/Light.cs b/FModel/Views/Snooper/Lights/Light.cs index 49e23b49..b2a40e75 100644 --- a/FModel/Views/Snooper/Lights/Light.cs +++ b/FModel/Views/Snooper/Lights/Light.cs @@ -30,7 +30,7 @@ public abstract class Light : IDisposable }; public readonly FGuid Model; public readonly Texture Icon; - public readonly Transform Transform; + public Transform Transform; public Vector4 Color; public float Intensity; @@ -76,6 +76,13 @@ public abstract class Light : IDisposable _vao.BindInstancing(); // VertexAttributePointer } + public void UpdateMatrices() + { + _matrixVbo.Bind(); + _matrixVbo.Update(0, Transform.Matrix); + _matrixVbo.Unbind(); + } + public void Setup() { _handle = GL.CreateProgram(); @@ -92,6 +99,7 @@ public abstract class Light : IDisposable public void Render(Shader shader) { + GL.Disable(EnableCap.DepthTest); GL.Disable(EnableCap.CullFace); _vao.Bind(); @@ -103,6 +111,7 @@ public abstract class Light : IDisposable GL.DrawArrays(PrimitiveType.Triangles, 0, Indices.Length); GL.Enable(EnableCap.CullFace); + GL.Enable(EnableCap.DepthTest); } public virtual void Render(int i, Shader shader) @@ -114,6 +123,7 @@ public abstract class Light : IDisposable public virtual void ImGuiLight() { + SnimGui.Layout("Position");SnimGui.TooltipCopy(Transform.Matrix.Translation.ToString()); SnimGui.Layout("Color");ImGui.PushID(1); ImGui.ColorEdit4("", ref Color, ImGuiColorEditFlags.NoAlpha); ImGui.PopID();SnimGui.Layout("Intensity");ImGui.PushID(2); diff --git a/FModel/Views/Snooper/Models/Animations/Animation.cs b/FModel/Views/Snooper/Models/Animations/Animation.cs index 51893414..520e8d51 100644 --- a/FModel/Views/Snooper/Models/Animations/Animation.cs +++ b/FModel/Views/Snooper/Models/Animations/Animation.cs @@ -15,21 +15,13 @@ public class Animation : IDisposable public Animation(CAnimSet anim) { - CurrentTime = 0; + CurrentTime = 1; CurrentAnimation = anim; } - public void UpdateAnimation(FMeshBoneInfo[] boneInfos, ref Dictionary bonesTransformByIndex) - { - if (CurrentAnimation != null) - { - // CurrentTime = deltaTime; - CalculateBoneTransform(boneInfos, ref bonesTransformByIndex); - } - } - - public void CalculateBoneTransform(FMeshBoneInfo[] boneInfos, ref Dictionary bonesTransformByIndex) + public Dictionary CalculateBoneTransform(FMeshBoneInfo[] boneInfos, Dictionary bonesTransformByIndex) { + var ret = new Dictionary(); var sequence = CurrentAnimation.Sequences[0]; for (int boneIndex = 0; boneIndex < boneInfos.Length; boneIndex++) { @@ -40,17 +32,31 @@ public class Animation : IDisposable if (CurrentTime < sequence.Tracks[boneIndex].KeyScale.Length) boneScale = sequence.Tracks[boneIndex].KeyScale[CurrentTime]; - if (!bonesTransformByIndex.TryGetValue(boneInfos[boneIndex].ParentIndex, out var parentTransform)) - parentTransform = new Transform { Relation = Matrix4x4.Identity }; + if (!bonesTransformByIndex.TryGetValue(boneIndex, out var originalTransform)) + originalTransform = new Transform { Relation = Matrix4x4.Identity }; - bonesTransformByIndex[boneIndex] = new Transform + if (!ret.TryGetValue(boneInfos[boneIndex].ParentIndex, out var parentTransform)) + parentTransform = new Transform { Relation = Matrix4x4.Identity }; + else boneOrientation.Conjugate(); + + var boneTransform = new Transform { Relation = parentTransform.Matrix, - Rotation = boneOrientation, + Rotation = boneOrientation * originalTransform.Rotation * FQuat.Conjugate(boneOrientation), Position = bonePosition * Constants.SCALE_DOWN_RATIO, Scale = boneScale }; + + // boneTransform.Rotation = originalTransform.Rotation * FQuat.Conjugate(originalTransform.Rotation) * parentTransform.Rotation + boneTransform.Position -= boneTransform.Position - originalTransform.Position; + + // boneTransform.Rotation = originalTransform.Rotation * (boneTransform.Rotation * FQuat.Conjugate(originalTransform.Rotation)); + // boneTransform.Position = originalTransform.Position + (boneTransform.Position - originalTransform.Position); + + ret[boneIndex] = boneTransform; } + + return ret; } public void Dispose() diff --git a/FModel/Views/Snooper/Models/Animations/Skeleton.cs b/FModel/Views/Snooper/Models/Animations/Skeleton.cs index d7ecd0da..70f4f2e9 100644 --- a/FModel/Views/Snooper/Models/Animations/Skeleton.cs +++ b/FModel/Views/Snooper/Models/Animations/Skeleton.cs @@ -14,6 +14,7 @@ public class Skeleton : IDisposable public readonly FReferenceSkeleton ReferenceSkeleton; public readonly Dictionary BonesIndexByName; public Dictionary BonesTransformByIndex; + public Dictionary AnimBonesTransformByIndex; public readonly bool IsLoaded; public Animation Anim; @@ -22,6 +23,7 @@ public class Skeleton : IDisposable { BonesIndexByName = new Dictionary(); BonesTransformByIndex = new Dictionary(); + AnimBonesTransformByIndex = new Dictionary(); } public Skeleton(FPackageIndex package, FReferenceSkeleton referenceSkeleton, Transform transform) : this() @@ -32,7 +34,6 @@ public class Skeleton : IDisposable ReferenceSkeleton = referenceSkeleton ?? UnrealSkeleton.ReferenceSkeleton; BonesIndexByName = ReferenceSkeleton.FinalNameToIndexMap; - BonesTransformByIndex = new Dictionary(); UpdateBoneMatrices(transform.Matrix); } @@ -63,15 +64,15 @@ public class Skeleton : IDisposable parentTransform = new Transform { Relation = matrix }; boneTransform.Relation = parentTransform.Matrix; - BonesTransformByIndex[boneIndex] = boneTransform; + BonesTransformByIndex[boneIndex] = AnimBonesTransformByIndex[boneIndex] = boneTransform; } } public void SetUniform(Shader shader) { - if (!IsLoaded) return; - Anim?.UpdateAnimation(ReferenceSkeleton.FinalRefBoneInfo, ref BonesTransformByIndex); - foreach ((int boneIndex, Transform boneTransform) in BonesTransformByIndex) + if (!IsLoaded || Anim == null) return; + AnimBonesTransformByIndex = Anim.CalculateBoneTransform(ReferenceSkeleton.FinalRefBoneInfo, BonesTransformByIndex); + foreach ((int boneIndex, Transform boneTransform) in AnimBonesTransformByIndex) { shader.SetUniform($"uFinalBonesMatrix[{boneIndex}]", boneTransform.Matrix); } diff --git a/FModel/Views/Snooper/Models/Model.cs b/FModel/Views/Snooper/Models/Model.cs index 70200bf6..95cc18dd 100644 --- a/FModel/Views/Snooper/Models/Model.cs +++ b/FModel/Views/Snooper/Models/Model.cs @@ -230,6 +230,12 @@ public class Model : IDisposable public void UpdateMatrices(Options options) { UpdateMatrices(); + for (int i = 0; i < options.Lights.Count; i++) + { + options.Lights[i].Transform = Skeleton.AnimBonesTransformByIndex[i]; + options.Lights[i].UpdateMatrices(); + } + foreach (var socket in Sockets) { foreach (var attached in socket.AttachedModels) @@ -362,7 +368,7 @@ public class Model : IDisposable shader.SetUniform("uMorphTime", MorphTime); if (!outline) { - // if (HasSkeleton) Skeleton.SetUniform(shader); + if (HasSkeleton) Skeleton.SetUniform(shader); shader.SetUniform("uUvCount", UvCount); shader.SetUniform("uHasVertexColors", HasVertexColors); } diff --git a/FModel/Views/Snooper/Renderer.cs b/FModel/Views/Snooper/Renderer.cs index f1019c59..5c52fa12 100644 --- a/FModel/Views/Snooper/Renderer.cs +++ b/FModel/Views/Snooper/Renderer.cs @@ -131,7 +131,7 @@ public class Renderer : IDisposable } { // light pass - var uNumLights = Math.Min(Options.Lights.Count, 100); + var uNumLights = Math.Min(Options.Lights.Count, 150); _shader.SetUniform("uNumLights", ShowLights ? uNumLights : 0); if (ShowLights) @@ -178,6 +178,11 @@ public class Renderer : IDisposable if (Options.Models.ContainsKey(guid) || !original.TryConvert(out var mesh)) return; Options.Models[guid] = new Model(original, mesh); + foreach (var transform in Options.Models[guid].Skeleton.BonesTransformByIndex.Values) + { + Options.Lights.Add( + new PointLight(guid, Options.Icons["pointlight"], original, original, transform)); + } Options.SelectModel(guid); SetupCamera(Options.Models[guid].Box); } diff --git a/FModel/Views/Snooper/Shading/Shader.cs b/FModel/Views/Snooper/Shading/Shader.cs index 4cbc9b13..5599f260 100644 --- a/FModel/Views/Snooper/Shading/Shader.cs +++ b/FModel/Views/Snooper/Shading/Shader.cs @@ -122,7 +122,7 @@ public class Shader : IDisposable _uniformsLocation.Add(name, location); if (location == -1) { - throw new Exception($"{name} uniform not found on shader."); + // throw new Exception($"{name} uniform not found on shader."); } } return location; diff --git a/FModel/Views/Snooper/Transform.cs b/FModel/Views/Snooper/Transform.cs index ea63f2f2..a36ba7a1 100644 --- a/FModel/Views/Snooper/Transform.cs +++ b/FModel/Views/Snooper/Transform.cs @@ -16,11 +16,11 @@ public class Transform public FQuat Rotation = FQuat.Identity; public FVector Scale = FVector.OneVector; - public Matrix4x4 Matrix => + public Matrix4x4 LocalMatrix => Matrix4x4.CreateScale(Scale.X, Scale.Z, Scale.Y) * Matrix4x4.CreateFromQuaternion(Quaternion.Normalize(new Quaternion(Rotation.X, Rotation.Z, Rotation.Y, -Rotation.W))) * - Matrix4x4.CreateTranslation(Position.X, Position.Z, Position.Y) - * Relation; + Matrix4x4.CreateTranslation(Position.X, Position.Z, Position.Y); + public Matrix4x4 Matrix => LocalMatrix * Relation; public void ImGuiTransform(float speed) {