22#include "aeongames/AeonEngine.hpp"
23#include "aeongames/Mesh.hpp"
24#include "aeongames/Pipeline.hpp"
25#include "aeongames/Skeleton.hpp"
26#include "aeongames/Animation.hpp"
28#include "aeongames/Buffer.hpp"
29#include "aeongames/Renderer.hpp"
30#include "aeongames/Node.hpp"
31#include "aeongames/Model.hpp"
32#include "ModelComponent.h"
36 static const StringId ModelStringId{
"Model Component"};
43 ModelComponent::~ModelComponent() =
default;
50 static constexpr std::array<const StringId, 3> ModelComponentPropertyIds
61 return ModelComponentPropertyIds.size();
66 return ModelComponentPropertyIds.data();
73 case ModelComponentPropertyIds[0]:
75 case ModelComponentPropertyIds[1]:
77 case ModelComponentPropertyIds[2]:
87 case ModelComponentPropertyIds[0]:
88 if ( std::holds_alternative<std::string> ( aProperty ) )
90 SetModel ( {
"Model"_crc32, std::get<std::string> ( aProperty ) } );
92 else if ( std::holds_alternative<std::filesystem::path> ( aProperty ) )
94 SetModel ( {
"Model"_crc32, std::get<std::filesystem::path> ( aProperty ).string() } );
96 else if ( std::holds_alternative<uint32_t> ( aProperty ) )
98 SetModel ( {
"Model"_crc32, std::get<uint32_t> ( aProperty ) } );
101 case ModelComponentPropertyIds[1]:
102 if ( std::holds_alternative<size_t> ( aProperty ) )
107 case ModelComponentPropertyIds[2]:
108 if ( std::holds_alternative<double> ( aProperty ) )
129 mActiveAnimation = aActiveAnimation;
130 mCurrentSample = mStartingFrame;
135 return mActiveAnimation;
140 mStartingFrame = mCurrentSample = aAnimationDelta;
145 return mStartingFrame;
150 if (
auto model = mModel.Cast<
Model>() )
153 for (
auto& i : model->GetAssemblies() )
155 if (
Mesh * mesh = std::get<0> ( i ).Cast<Mesh>() )
157 aabb += mesh->GetAABB();
161 const Skeleton* skeleton{ model->GetSkeleton() };
164 float* skeleton_buffer =
reinterpret_cast<float*
> ( mSkeleton.data() );
165 assert ( ( skeleton->
GetJoints().size() * sizeof (
float ) * 16 ) < mSkeleton.size() );
166 const std::vector<ResourceId>& animations = model->GetAnimations();
167 if ( animations.size() && animations.size() > mActiveAnimation )
170 auto animation = animations[mActiveAnimation].Cast<
Animation>();
171 mCurrentSample = animation->
AddTimeToSample ( mCurrentSample, aDelta );
172 for (
size_t i = 0; i < skeleton->
GetJoints().size(); ++i )
174 Matrix4x4 matrix{ animation->GetTransform ( i, mCurrentSample ) *
175 skeleton->
GetJoints() [i].GetInvertedTransform() };
176 memcpy ( skeleton_buffer + ( i * 16 ), matrix.
GetMatrix4x4(), sizeof (
float ) * 16 );
183 for (
size_t i = 0; i < skeleton->
GetJoints().size(); ++i )
185 memcpy ( skeleton_buffer + ( i * 16 ), identity.
GetMatrix4x4(), sizeof (
float ) * 16 );
194 if (
auto model = mModel.Cast<
Model>() )
198 const Skeleton* skeleton{model->GetSkeleton() };
201 size_t used_bones_size = skeleton->
GetJoints().size() *
sizeof ( float ) * 16;
202 assert ( used_bones_size <= mSkeleton.size() );
204 skeleton_accessor.
WriteMemory ( 0, used_bones_size, mSkeleton.data() );
205 skeleton_accessor_ptr = &skeleton_accessor;
207 for (
auto& i : model->GetAssemblies() )
212 *std::get<0> ( i ).Cast<
Mesh>(),
213 *std::get<1> ( i ).Cast<
Pipeline>(),
215 skeleton_accessor_ptr );
Header for 4x4 matrix class.
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
Axis Aligned Bounding Box class.
Stores skeletal animation data including keyframes and bone transforms.
DLL double AddTimeToSample(double aSample, double aTime) const
Advance a sample position by a time delta.
Provides access to a region within a memory pool buffer.
DLL void WriteMemory(size_t aOffset, size_t aSize, const void *aData=nullptr) const
Write data into the buffer region.
Represents a surface material with uniform properties and texture samplers.
4 by 4 matrix in colum mayor order.
DLL const float *const GetMatrix4x4() const
Get a pointer to the internal matrix data.
Represents a polygon mesh with vertex attributes and index data.
void Update(Node &aNode, double aDelta) final
Update the component state.
void SetModel(const ResourceId &aModel)
Sets the model resource.
size_t GetPropertyCount() const final
Get the number of properties exposed by this component.
void ProcessMessage(Node &aNode, uint32_t aMessageType, const void *aMessageData) final
Process an incoming message.
void SetProperty(uint32_t, const Property &aProperty) final
Set the value aProperty for the property identified by aId.
void SetActiveAnimation(size_t aActiveAnimation) noexcept
Sets the active animation index.
ModelComponent()
Default constructor.
void Render(const Node &aNode, Renderer &aRenderer, void *aWindowId) final
Render the component.
const ResourceId & GetModel() const noexcept
Returns the current model resource identifier.
void SetStartingFrame(double aAnimationDelta) noexcept
Sets the starting frame for the active animation.
Property GetProperty(const StringId &aId) const final
Get the value of a property.
static const StringId & GetClassId()
Returns the class identifier for the ModelComponent.
const double & GetStartingFrame() const noexcept
Returns the starting frame for the active animation.
const StringId * GetPropertyInfoArray() const final
Get the array of property identifiers.
const size_t & GetActiveAnimation() const noexcept
Returns the active animation index.
const StringId & GetId() const final
Get the unique identifier for this component type.
Represents a 3D model composed of assemblies (pipeline, material, mesh), a skeleton,...
Scene graph node representing an entity in the game world.
DLL void SetAABB(const AABB &aAABB)
Set the axis-aligned bounding box.
DLL const Transform & GetGlobalTransform() const
Get the global (world-space) transform.
Rendering pipeline resource.
Abstract base class for rendering backends.
virtual void Render(void *aWindowId, const Matrix4x4 &aModelMatrix, const Mesh &aMesh, const Pipeline &aPipeline, const Material *aMaterial=nullptr, const BufferAccessor *aSkeleton=nullptr, Topology aTopology=Topology::TRIANGLE_LIST, uint32_t aVertexStart=0, uint32_t aVertexCount=0xffffffff, uint32_t aInstanceCount=1, uint32_t aFirstInstance=0) const =0
Issues a draw call for a mesh with the given pipeline and optional material.
virtual BufferAccessor AllocateSingleFrameUniformMemory(void *aWindowId, size_t aSize)=0
Allocates uniform buffer memory that is valid for a single frame.
Identifies a resource by its type and path CRC32 hashes.
void Store() const
Construct and store the resource in the cache if not already present.
std::string GetPathString() const
Get the original resource path string from the hash.
Skeletal animation resource containing a hierarchy of joints.
DLL const std::vector< Joint > & GetJoints() const
Get the joint list.
CRC-based compile-time string identifier.
<- This is here just for the literals
std::variant< int, long, long long, unsigned, unsigned long, unsigned long long, float, double, std::string, std::filesystem::path > Property
A variant type that can hold any commonly used property value.