Skip to content

Commit

Permalink
Fixed Blockbench models!!
Browse files Browse the repository at this point in the history
  • Loading branch information
MrScautHD committed Dec 15, 2024
1 parent 1d7e687 commit b47a69c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Bliss.Test/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected virtual void FixedUpdate() {
this._playingAnim = true;
this._frameCount++;

if (this._frameCount >= this._playerModel.Animations[0].FrameCount) {
if (this._frameCount >= this._playerModel.Animations[2].FrameCount) {
this._frameCount = 0;
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ protected virtual void Draw(GraphicsDevice graphicsDevice, CommandList commandLi

if (this._cam3D.GetFrustum().ContainsBox(this._playerModel.BoundingBox)) {
if (this._playingAnim) {
this._playerModel.UpdateAnimationBones(commandList, this._playerModel.Animations[0], this._frameCount);
this._playerModel.UpdateAnimationBones(commandList, this._playerModel.Animations[2], this._frameCount);
}
this._playerModel.Draw(commandList, this.FullScreenTexture.Framebuffer.OutputDescription, new Transform() { Translation = new Vector3(0, 0.05F, 0)}, Color.Blue);
}
Expand Down
Binary file modified src/Bliss.Test/content/player.glb
Binary file not shown.
24 changes: 19 additions & 5 deletions src/Bliss/CSharp/Geometry/Animations/MeshAmateurBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@ namespace Bliss.CSharp.Geometry.Animations;

public class MeshAmateurBuilder {

/// <summary>
/// The root node of the model. This node typically contains the root transformations for the mesh and references to other child nodes.
/// </summary>
private Node _rootNode;

/// <summary>
/// An array of <see cref="ModelAnimation"/> objects that represent animations applied to the model.
/// </summary>
private ModelAnimation[] _animations;

/// <summary>
/// A dictionary mapping bone IDs to the corresponding <see cref="Bone"/> objects. This allows efficient look-up of bones by their ID or name.
/// </summary>
private Dictionary<uint, Bone> _bonesByName;

/// <summary>
/// An array of transformation matrices for the bones, used for skeletal animation. Each bone has a corresponding transformation matrix.
/// </summary>
private Matrix4x4[] _boneTransformations;

/// <summary>
Expand Down Expand Up @@ -83,8 +97,8 @@ private Dictionary<int, BoneInfo[]> SetupBoneInfos(ModelAnimation animation) {
/// <param name="frame">The current frame index being processed.</param>
/// <param name="parentTransform">The transformation matrix of the parent node.</param>
private void UpdateChannel(Node node, ModelAnimation animation, int frame, AMatrix4x4 parentTransform) {
AMatrix4x4 nodeTransformation = AMatrix4x4.Identity;

AMatrix4x4 nodeTransformation = node.Transform;
if (this.GetChannel(node, animation, out NodeAnimationChannel? channel)) {
AMatrix4x4 scale = this.InterpolateScale(channel!, animation, frame);
AMatrix4x4 rotation = this.InterpolateRotation(channel!, animation, frame);
Expand Down Expand Up @@ -140,7 +154,7 @@ private AMatrix4x4 InterpolateTranslation(NodeAnimationChannel channel, ModelAni

Vector3D start = currentFrame.Value;
Vector3D end = nextFrame.Value;
position = start + (float) delta * (end - start);
position = start + (float) Math.Clamp(delta, 0.0F, 1.0F) * (end - start);
}

return AMatrix4x4.FromTranslation(position);
Expand Down Expand Up @@ -176,7 +190,7 @@ private AMatrix4x4 InterpolateRotation(NodeAnimationChannel channel, ModelAnimat

AQuaternion start = currentFrame.Value;
AQuaternion end = nextFrame.Value;
rotation = AQuaternion.Slerp(start, end, (float) delta);
rotation = AQuaternion.Slerp(start, end, (float) Math.Clamp(delta, 0.0F, 1.0F));
rotation.Normalize();
}

Expand Down Expand Up @@ -214,7 +228,7 @@ private AMatrix4x4 InterpolateScale(NodeAnimationChannel channel, ModelAnimation
Vector3D start = currentFrame.Value;
Vector3D end = nextFrame.Value;

scale = start + (float) delta * (end - start);
scale = start + (float) Math.Clamp(delta, 0.0F, 1.0F) * (end - start);
}

return AMatrix4x4.FromScaling(scale);
Expand Down
31 changes: 30 additions & 1 deletion src/Bliss/CSharp/Geometry/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Numerics;
using System.Text;
using Assimp;
using Assimp.Configs;
using Bliss.CSharp.Effects;
using Bliss.CSharp.Geometry.Animations;
using Bliss.CSharp.Geometry.Conversions;
Expand Down Expand Up @@ -36,7 +37,30 @@ public class Model : Disposable {
/// This includes flipping the winding order, triangulating, pre-transforming vertices,
/// calculating tangent space, and generating smooth normals.
/// </summary>
private const PostProcessSteps DefaultPostProcessSteps = PostProcessSteps.FlipWindingOrder | PostProcessSteps.Triangulate | PostProcessSteps.CalculateTangentSpace | PostProcessSteps.GenerateSmoothNormals;
private const PostProcessSteps DefaultPostProcessSteps = PostProcessSteps.FlipWindingOrder |
PostProcessSteps.Triangulate |
PostProcessSteps.CalculateTangentSpace |
PostProcessSteps.GenerateSmoothNormals |
PostProcessSteps.JoinIdenticalVertices |
PostProcessSteps.FindInvalidData |
PostProcessSteps.ImproveCacheLocality |
PostProcessSteps.FixInFacingNormals |
PostProcessSteps.GenerateUVCoords |
PostProcessSteps.ValidateDataStructure |
PostProcessSteps.FindInstances |
PostProcessSteps.GlobalScale;

/// <summary>
/// A collection of default property configurations applied to the 3D model importer.
/// These configurations include settings to limit vertex bone weights, exclude certain elements,
/// and adjust import behavior for specific file formats like FBX.
/// </summary>
private static readonly List<PropertyConfig> PropertyConfigs = [
new NoSkeletonMeshesConfig(true),
new VertexBoneWeightLimitConfig(4),
new FBXImportCamerasConfig(false),
new FBXStrictModeConfig(false)
];

/// <summary>
/// The default effect applied to models.
Expand Down Expand Up @@ -95,6 +119,11 @@ public Model(GraphicsDevice graphicsDevice, Mesh[] meshes, ModelAnimation[] anim
/// <returns>Returns a new instance of the <see cref="Model"/> class with the loaded meshes.</returns>
public static Model Load(GraphicsDevice graphicsDevice, string path, bool loadMaterial = true, bool flipUv = false) {
using AssimpContext context = new AssimpContext();

foreach (PropertyConfig config in PropertyConfigs) {
context.SetConfig(config);
}

Scene scene = context.ImportFile(path, DefaultPostProcessSteps);

List<Mesh> meshes = new List<Mesh>();
Expand Down

0 comments on commit b47a69c

Please sign in to comment.