Skip to content

Commit

Permalink
[ERA-1] started ragdoll skinning
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuradov committed Aug 27, 2024
1 parent 334c9fb commit 32a69de
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 105 deletions.
47 changes: 21 additions & 26 deletions modules/core/src/animation/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "rendering/debug_visualization.h"

#include "px/features/px_ragdoll.h"

#include <algorithm>

namespace era_engine::animation
Expand Down Expand Up @@ -700,14 +702,14 @@ namespace era_engine::animation
}
}

void animation_component::update(const ref<multi_mesh>& mesh, eallocator& arena, float dt, trs* transform)
void animation_component::update(const ref<multi_mesh>& mesh, eallocator& arena, float dt, trs* transform, physics::px_ragdoll_component* ragdoll)
{
const dx_mesh& dxMesh = mesh->mesh;
animation_skeleton& skeleton = mesh->skeleton;

currentGlobalTransforms = 0;

if (animation && animation->valid())
if ((!ragdoll || !ragdoll->simulated) && animation && animation->valid())
{
auto [vb, skinningMatrices] = skinObject(dxMesh.vertexBuffer, dxMesh.vertexBuffer.positions->elementCount, (uint32)skeleton.joints.size());

Expand All @@ -733,6 +735,22 @@ namespace era_engine::animation
if (animation->finished)
controller->stateMachine.update();
}
else if (ragdoll && ragdoll->ragdoll && ragdoll->simulated)
{
auto [vb, skinningMatrices] = skinObject(dxMesh.vertexBuffer, dxMesh.vertexBuffer.positions->elementCount, (uint32)skeleton.joints.size());

prevFrameVertexBuffer = currentVertexBuffer;
currentVertexBuffer = vb;

static std::vector<trs> transforms = ragdoll->apply();

for (int i = 0; i < skeleton.joints.size(); ++i)
{
skinningMatrices[i] = trsToMat4(transforms[i]) * skeleton.joints[i].invBindTransform;
}

currentGlobalTransforms = &transforms[0];
}
else
{
currentVertexBuffer = dxMesh.vertexBuffer;
Expand Down Expand Up @@ -830,7 +848,7 @@ namespace era_engine::animation
*vertices++ = { vrt1, limbTypeColors[parentJoint.limbType] };
*vertices++ = { vrt2, limbTypeColors[parentJoint.limbType] };
#else
* vertices++ = { skeleton.joints[joint.parentID].bindTransform.col3.xyz, limbTypeColors[parentJoint.limbType] };
*vertices++ = { skeleton.joints[joint.parentID].bindTransform.col3.xyz, limbTypeColors[parentJoint.limbType] };
*vertices++ = { joint.bindTransform.col3.xyz, limbTypeColors[parentJoint.limbType] };
#endif
}
Expand All @@ -847,27 +865,6 @@ namespace era_engine::animation
renderDebug<debug_unlit_line_pipeline::position_color>(trsToMat4(transform), vb, ib, vec4(1.f, 1.f, 1.f, 1.f), renderPass, true);

#else
#if 0
auto [vb, vertexPtr] = dxContext.createDynamicVertexBuffer(sizeof(position_color), limb_type_count * 2);
auto [ib, indexPtr] = dxContext.createDynamicIndexBuffer(sizeof(uint16), limb_type_count * 2);
position_color* vertices = (position_color*)vertexPtr;
indexed_line16* lines = (indexed_line16*)indexPtr;
for (uint32 i = 0; i < limb_type_count; ++i)
{
const auto& limb = skeleton.limbs[i];
*vertices++ = { limb.mean - limb.principalAxis / transform.scale * 0.2f, limbTypeColors[i] };
*vertices++ = { limb.mean + limb.principalAxis / transform.scale * 0.2f, limbTypeColors[i] };
*lines++ = { (uint16)(2 * i), (uint16)(2 * i + 1) };
}
renderDebug<debug_unlit_line_pipeline::position_color>(trsToMat4(transform), vb, ib, vec4(1.f, 1.f, 1.f, 1.f), renderPass, true);
#else
for (uint32 i = 0; i < limb_type_count; ++i)
{
const auto& limb = skeleton.limbs[i];
Expand All @@ -885,8 +882,6 @@ namespace era_engine::animation
renderWireCapsule(a, b, limb.dimensions.radius * transform.scale.x, vec4(limbTypeColors[i], 1.f), renderPass, true);
}
}
#endif
#endif
}

Expand Down
7 changes: 6 additions & 1 deletion modules/core/src/animation/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ namespace era_engine
{
struct multi_mesh;
struct ldr_render_pass;

namespace physics
{
struct px_ragdoll_component;
}
}

namespace era_engine::animation
Expand Down Expand Up @@ -249,7 +254,7 @@ namespace era_engine::animation
struct animation_component
{
void initialize(std::vector<animation_clip>& clips, size_t startIndex = 0);
void update(const ref<multi_mesh>& mesh, eallocator& arena, float dt, trs* transform = 0);
void update(const ref<multi_mesh>& mesh, eallocator& arena, float dt, trs* transform = 0, physics::px_ragdoll_component* ragdoll = nullptr);
void drawCurrentSkeleton(const ref<multi_mesh>& mesh, const trs& transform, ldr_render_pass* renderPass) const;

ref<animation_instance> animation = nullptr;
Expand Down
12 changes: 10 additions & 2 deletions modules/core/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ namespace era_engine

if (auto mesh = loadAnimatedMeshFromFileAsync(getAssetPath("/resources/assets/resident-evil-tyrant/source/UmodelExport.fbx")))
{
auto& en = scene.createEntity("Tiran")
auto& en = scene.createEntity("Ragdoll")
.addComponent<transform_component>(vec3(0.0f), quat::identity, vec3(0.1f))
.addComponent<animation::animation_component>()
.addComponent<dynamic_transform_component>()
Expand Down Expand Up @@ -807,14 +807,22 @@ namespace era_engine

//if (renderer->mode != renderer_mode_pathtraced)
{
for (auto [entityHandle, anim, mesh, transform] : scene.group(component_group<animation::animation_component, mesh_component, transform_component>).each())
for (auto [entityHandle, anim, mesh, transform] : scene.group(component_group<animation::animation_component, mesh_component, transform_component>, component_group<physics::px_ragdoll_component>).each())
{
anim.update(mesh.mesh, stackArena, dt, &transform);

if (anim.drawSceleton)
anim.drawCurrentSkeleton(mesh.mesh, transform, &ldrRenderPass);
}

for (auto [entityHandle, anim, mesh, transform, ragdoll] : scene.group(component_group<animation::animation_component, mesh_component, transform_component, physics::px_ragdoll_component>).each())
{
anim.update(mesh.mesh, stackArena, dt, &transform, &ragdoll);

if (anim.drawSceleton)
anim.drawCurrentSkeleton(mesh.mesh, transform, &ldrRenderPass);
}

scene_lighting lighting;
lighting.spotLightBuffer = spotLightBuffer[dxContext.bufferedFrameID];
lighting.pointLightBuffer = pointLightBuffer[dxContext.bufferedFrameID];
Expand Down
Loading

0 comments on commit 32a69de

Please sign in to comment.