mirror of
https://github.com/4sval/FModel.git
synced 2026-06-21 07:20:05 -05:00
ctrl s, don't use this commit for tomorrow
This commit is contained in:
parent
f98c8b34da
commit
09ab40ad37
|
|
@ -1 +1 @@
|
|||
Subproject commit b02fb8c67e3ab7b1c256297a4363dedc6aca951a
|
||||
Subproject commit bcca52dcbbe47b5ba24e34982ca6921c2478d9b1
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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++)
|
||||
{
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -22,6 +23,7 @@ public class Skeleton : IDisposable
|
|||
{
|
||||
BonesIndexByName = new Dictionary<string, int>();
|
||||
BonesTransformByIndex = new Dictionary<int, Transform>();
|
||||
AnimBonesTransformByIndex = new Dictionary<int, Transform>();
|
||||
}
|
||||
|
||||
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<int, Transform>();
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user