18Mesh::Mesh(
const ResourceDescriptor<Mesh>& descriptor )
21 setBounds(_boundingBox);
24void Mesh::addSubMesh(
const Handle<SubMesh> subMesh,
const U32 index)
26 _subMeshList.emplace_back(MeshData{ subMesh, index });
29void Mesh::setNodeData(
const MeshNodeData& nodeStructure)
31 _nodeStructure = nodeStructure;
34void Mesh::setMaterialTpl(
const Handle<Material> material)
36 Object3D::setMaterialTpl(material);
38 for (
const Mesh::MeshData& subMesh : _subMeshList)
40 Get(subMesh._mesh)->setMaterialTpl(material);
44void Mesh::setAnimationCount(
const size_t animationCount )
46 _animationCount = animationCount;
47 if ( _animationCount == 0 && _animator !=
nullptr)
51 else if ( _animationCount > 0u && _animator ==
nullptr)
53 _animator = std::make_unique<SceneAnimator>();
57SceneAnimator* Mesh::getAnimator() const noexcept
59 return _animator.get();
62SceneGraphNode* Mesh::addSubMeshNode(SceneGraphNode* parentNode,
const U32 meshIndex)
64 constexpr U32 normalMask =
to_base(ComponentType::NAVIGATION) |
65 to_base(ComponentType::TRANSFORM) |
66 to_base(ComponentType::BOUNDS) |
67 to_base(ComponentType::RENDERING) |
68 to_base(ComponentType::RIGID_BODY) |
69 to_base(ComponentType::NAVIGATION);
71 constexpr U32 skinnedMask =
to_base(ComponentType::ANIMATION) |
72 to_base(ComponentType::INVERSE_KINEMATICS) |
73 to_base(ComponentType::RAGDOLL);
77 const MeshData& meshData = _subMeshList[meshIndex];
79 SubMesh* subMesh =
Get( meshData._mesh );
81 SceneGraphNodeDescriptor subMeshDescriptor;
82 subMeshDescriptor._usageContext = parentNode->usageContext();
83 subMeshDescriptor._instanceCount = parentNode->instanceCount();
84 subMeshDescriptor._nodeHandle =
FromHandle(meshData._mesh);
85 subMeshDescriptor._componentMask = normalMask;
87 if ( subMesh->boneCount() > 0u )
89 subMeshDescriptor._componentMask |= skinnedMask;
92 Util::StringFormat( subMeshDescriptor._name,
"{}_{}", parentNode->name().c_str(), meshIndex );
94 SceneGraphNode* sgn = parentNode->addChildNode(subMeshDescriptor);
95 sgn->get<TransformComponent>()->setPosition(subMesh->getWorldOffset());
100void Mesh::processNode(SceneGraphNode* parentNode,
const MeshNodeData& node)
102 for (
const U32 idx : node._meshIndices)
104 addSubMeshNode(parentNode, idx);
107 if (!node._children.empty())
109 ResourceDescriptor<TransformNode> nodeDescriptor{ node._name +
"_transform" };
111 SceneGraphNodeDescriptor tempNodeDescriptor;
112 tempNodeDescriptor._usageContext = parentNode->usageContext();
113 tempNodeDescriptor._instanceCount = parentNode->instanceCount();
114 tempNodeDescriptor._name = node._name.c_str();
116 tempNodeDescriptor._componentMask =
to_base(ComponentType::TRANSFORM) |
117 to_base(ComponentType::NETWORKING);
118 for (
const MeshNodeData& it : node._children)
120 SceneGraphNode* targetSGN = parentNode;
121 if (!it._transform.isIdentity())
123 targetSGN = parentNode->addChildNode(tempNodeDescriptor);
124 targetSGN->get<TransformComponent>()->setTransforms(it._transform);
126 processNode(targetSGN, it);
132void Mesh::postLoad(SceneGraphNode* sgn)
134 sgn->get<TransformComponent>()->setTransforms(_nodeStructure._transform);
135 processNode(sgn, _nodeStructure);
138 PlatformContext& pContext = sgn->context();
140 Attorney::SceneAnimatorMeshImporter::buildBuffers(*_animator, pContext.gfx() );
142 registerEditorComponent( pContext );
145 EditorComponentField playAnimationsField = {};
146 playAnimationsField._name =
"Toogle Animation Playback";
147 playAnimationsField._data = &_playAnimationsOverride;
148 playAnimationsField._type = EditorComponentFieldType::SWITCH_TYPE;
149 playAnimationsField._basicType = PushConstantType::BOOL;
150 playAnimationsField._readOnly =
false;
151 _editorComponent->registerField(
MOV( playAnimationsField ) );
154 Object3D::postLoad(sgn);
159 return Object3D::postLoad();
162bool Mesh::load( PlatformContext& context )
164 if ( MeshImporter::loadMesh( context,
this ) )
166 return Object3D::load(context);
174 for ( Mesh::MeshData& subMesh : _subMeshList )
178 _subMeshList.clear();
179 return Object3D::unload();
183 dataOut << _transform;
184 dataOut << _meshIndices;
185 dataOut <<
to_U32(_children.size());
188 child.serialize(dataOut);
195 dataIn >> _transform;
196 dataIn >> _meshIndices;
198 dataIn >> childCount;
199 _children.
resize(childCount);
202 child.deserialize(dataIn);
void resize(size_t newsize)
Resizes the underlying storage to 'newsize' bytes and resets the read and write head positions.
Handle console commands that start with a forward slash.
constexpr SceneNodeType GetSceneNodeType()
constexpr U32 to_U32(const T value)
FORCE_INLINE void DestroyResource(Handle< T > &handle, const bool immediate=false)
FORCE_INLINE Handle< T > CreateResource(const ResourceDescriptor< T > &descriptor, bool &wasInCache, std::atomic_uint &taskCounter)
SceneNodeHandle FromHandle(const Handle< T > handle)
FORCE_INLINE T * Get(const Handle< T > handle)
constexpr auto to_base(const Type value) -> Type