Skip to content

Commit

Permalink
ctrl s, don't use this commit for tomorrow
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Jan 16, 2023
1 parent f98c8b3 commit 09ab40a
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 48 deletions.
2 changes: 1 addition & 1 deletion CUE4Parse
37 changes: 17 additions & 20 deletions FModel/Resources/default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

fPos = vec3(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;

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;
Expand Down
2 changes: 1 addition & 1 deletion FModel/Resources/light.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion FModel/Views/Snooper/Lights/Light.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +76,13 @@ public void SetupInstances()
_vao.BindInstancing(); // VertexAttributePointer
}

public void UpdateMatrices()
{
_matrixVbo.Bind();
_matrixVbo.Update(0, Transform.Matrix);
_matrixVbo.Unbind();
}

public void Setup()
{
_handle = GL.CreateProgram();
Expand All @@ -92,6 +99,7 @@ public void Setup()

public void Render(Shader shader)
{
GL.Disable(EnableCap.DepthTest);
GL.Disable(EnableCap.CullFace);

_vao.Bind();
Expand All @@ -103,6 +111,7 @@ public void Render(Shader shader)
GL.DrawArrays(PrimitiveType.Triangles, 0, Indices.Length);

GL.Enable(EnableCap.CullFace);
GL.Enable(EnableCap.DepthTest);
}

public virtual void Render(int i, Shader shader)
Expand All @@ -114,6 +123,7 @@ public virtual void Render(int i, Shader shader)

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);
Expand Down
34 changes: 20 additions & 14 deletions FModel/Views/Snooper/Models/Animations/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, Transform> bonesTransformByIndex)
{
if (CurrentAnimation != null)
{
// CurrentTime = deltaTime;
CalculateBoneTransform(boneInfos, ref bonesTransformByIndex);
}
}

public void CalculateBoneTransform(FMeshBoneInfo[] boneInfos, ref Dictionary<int, Transform> bonesTransformByIndex)
public Dictionary<int, Transform> CalculateBoneTransform(FMeshBoneInfo[] boneInfos, Dictionary<int, Transform> bonesTransformByIndex)
{
var ret = new Dictionary<int, Transform>();
var sequence = CurrentAnimation.Sequences[0];
for (int boneIndex = 0; boneIndex < boneInfos.Length; boneIndex++)
{
Expand All @@ -40,17 +32,31 @@ public void CalculateBoneTransform(FMeshBoneInfo[] boneInfos, ref Dictionary<int
if (CurrentTime < sequence.Tracks[boneIndex].KeyScale.Length)
boneScale = sequence.Tracks[boneIndex].KeyScale[CurrentTime];

if (!bonesTransformByIndex.TryGetValue(boneInfos[boneIndex].ParentIndex, out var parentTransform))
if (!bonesTransformByIndex.TryGetValue(boneIndex, out var originalTransform))
originalTransform = new Transform { Relation = Matrix4x4.Identity };

if (!ret.TryGetValue(boneInfos[boneIndex].ParentIndex, out var parentTransform))
parentTransform = new Transform { Relation = Matrix4x4.Identity };
else boneOrientation.Conjugate();

bonesTransformByIndex[boneIndex] = new Transform
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()
Expand Down
11 changes: 6 additions & 5 deletions FModel/Views/Snooper/Models/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Skeleton : IDisposable
public readonly FReferenceSkeleton ReferenceSkeleton;
public readonly Dictionary<string, int> BonesIndexByName;
public Dictionary<int, Transform> BonesTransformByIndex;
public Dictionary<int, Transform> AnimBonesTransformByIndex;
public readonly bool IsLoaded;

public Animation Anim;
Expand All @@ -22,6 +23,7 @@ public Skeleton()
{
BonesIndexByName = new Dictionary<string, int>();
BonesTransformByIndex = new Dictionary<int, Transform>();
AnimBonesTransformByIndex = new Dictionary<int, Transform>();
}

public Skeleton(FPackageIndex package, FReferenceSkeleton referenceSkeleton, Transform transform) : this()
Expand All @@ -32,7 +34,6 @@ public Skeleton(FPackageIndex package, FReferenceSkeleton referenceSkeleton, Tra

ReferenceSkeleton = referenceSkeleton ?? UnrealSkeleton.ReferenceSkeleton;
BonesIndexByName = ReferenceSkeleton.FinalNameToIndexMap;
BonesTransformByIndex = new Dictionary<int, Transform>();
UpdateBoneMatrices(transform.Matrix);
}

Expand Down Expand Up @@ -63,15 +64,15 @@ public void UpdateBoneMatrices(Matrix4x4 matrix)
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);
}
Expand Down
8 changes: 7 additions & 1 deletion FModel/Views/Snooper/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ public void AddInstance(Transform transform)
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)
Expand Down Expand Up @@ -362,7 +368,7 @@ public void Render(Shader shader, bool outline = false)
shader.SetUniform("uMorphTime", MorphTime);
if (!outline)
{
// if (HasSkeleton) Skeleton.SetUniform(shader);
if (HasSkeleton) Skeleton.SetUniform(shader);
shader.SetUniform("uUvCount", UvCount);
shader.SetUniform("uHasVertexColors", HasVertexColors);
}
Expand Down
7 changes: 6 additions & 1 deletion FModel/Views/Snooper/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void Render()
}

{ // 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)
Expand Down Expand Up @@ -178,6 +178,11 @@ private void LoadSkeletalMesh(USkeletalMesh original)
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);
}
Expand Down
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/Shading/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private int GetUniformLocation(string name)
_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;
Expand Down
6 changes: 3 additions & 3 deletions FModel/Views/Snooper/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public static Transform Identity
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)
{
Expand Down

0 comments on commit 09ab40a

Please sign in to comment.