Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions engine/includes/components/armature.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class ENGINE_EXPORT Armature : public NativeBehaviour {
Pose *bindPose() const;
void setBindPose(Pose *pose);

AABBox recalcBounds(const AABBox &aabb) const;

Texture *texture() const;

private:
Expand All @@ -39,7 +37,6 @@ class ENGINE_EXPORT Armature : public NativeBehaviour {

private:
vector<Matrix4> m_invertTransform;
vector<Matrix4> m_transform;
vector<Transform *> m_bones;

Pose *m_bindPose;
Expand Down
10 changes: 10 additions & 0 deletions engine/includes/components/skinnedmeshrender.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ENGINE_EXPORT SkinnedMeshRender : public Renderable {
A_REGISTER(SkinnedMeshRender, Renderable, Components/3D);

A_PROPERTIES(
A_PROPERTY(Vector3, boundsCenter, SkinnedMeshRender::boundsCenter, SkinnedMeshRender::setBoundsCenter),
A_PROPERTY(Vector3, boundsExtent, SkinnedMeshRender::boundsExtent, SkinnedMeshRender::setBoundsExtent),
A_PROPERTYEX(Armature *, armature, SkinnedMeshRender::armature, SkinnedMeshRender::setArmature, "editor=Component"),
A_PROPERTYEX(MeshGroup *, mesh, SkinnedMeshRender::mesh, SkinnedMeshRender::setMesh, "editor=Asset"),
A_PROPERTYEX(Material *, material, SkinnedMeshRender::material, SkinnedMeshRender::setMaterial, "editor=Asset")
Expand All @@ -21,6 +23,12 @@ class ENGINE_EXPORT SkinnedMeshRender : public Renderable {
public:
SkinnedMeshRender();

Vector3 boundsCenter() const;
void setBoundsCenter(Vector3 center);

Vector3 boundsExtent() const;
void setBoundsExtent(Vector3 extent);

Mesh *mesh() const;
void setMesh(Mesh *mesh);

Expand All @@ -43,6 +51,8 @@ class ENGINE_EXPORT SkinnedMeshRender : public Renderable {
void drawGizmosSelected() override;

private:
AABBox m_bounds;

Mesh *m_mesh;

MaterialInstance *m_material;
Expand Down
85 changes: 30 additions & 55 deletions engine/src/components/armature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "components/transform.h"
#include "components/actor.h"

#include "resources/mesh.h"
#include "resources/pose.h"
#include "resources/texture.h"

Expand Down Expand Up @@ -68,15 +69,15 @@ void Armature::update() {

for(uint32_t i = 0; i < m_bones.size(); i++) {
if(i < m_invertTransform.size() && m_bones[i]) {
m_transform[i] = m_bones[i]->worldTransform() * m_invertTransform[i];
}
// Compress data
Matrix4 t = m_transform[i];
t[3] = t[12];
t[7] = t[13];
t[11] = t[14];
Matrix4 t(m_bones[i]->worldTransform() * m_invertTransform[i]);

memcpy(&data[i * M4X3_SIZE], t.mat, M4X3_SIZE);
// Compress data
t[3] = t[12];
t[7] = t[13];
t[11] = t[14];

memcpy(&data[i * M4X3_SIZE], t.mat, M4X3_SIZE);
}
}
m_cache->setDirty();
}
Expand Down Expand Up @@ -110,66 +111,41 @@ Texture *Armature::texture() const {
/*!
\internal
*/
AABBox Armature::recalcBounds(const AABBox &aabb) const {
Vector3 v0, v1;
aabb.box(v0, v1);

Vector3 min( FLT_MAX);
Vector3 max(-FLT_MAX);
for(uint32_t b = 0; b < m_bones.size(); b++) {
Vector3 t0 = m_transform[b] * v0;
Vector3 t1 = m_transform[b] * v1;

min.x = MIN(min.x, t0.x);
min.y = MIN(min.y, t0.y);
min.z = MIN(min.z, t0.z);

min.x = MIN(min.x, t1.x);
min.y = MIN(min.y, t1.y);
min.z = MIN(min.z, t1.z);

max.x = MAX(max.x, t0.x);
max.y = MAX(max.y, t0.y);
max.z = MAX(max.z, t0.z);

max.x = MAX(max.x, t1.x);
max.y = MAX(max.y, t1.y);
max.z = MAX(max.z, t1.z);
}
AABBox result;
result.setBox(min, max);

return result;
}
/*!
\internal
*/
void Armature::loadUserData(const VariantMap &data) {
Component::loadUserData(data);
{
auto it = data.find(POSE);
if(it != data.end()) {
setBindPose(Engine::loadResource<Pose>((*it).second.toString()));
}

auto it = data.find(POSE);
if(it != data.end()) {
setBindPose(Engine::loadResource<Pose>((*it).second.toString()));
}
}
/*!
\internal
*/
VariantMap Armature::saveUserData() const {
VariantMap result = Component::saveUserData();
{
string ref = Engine::reference(bindPose());
if(!ref.empty()) {
result[POSE] = ref;
}

string ref = Engine::reference(bindPose());
if(!ref.empty()) {
result[POSE] = ref;
}

return result;
}

void Armature::drawGizmosSelected() {
for(auto it : m_bones) {
//Handles::drawBone(it->parentTransform(), it);
static Mesh *bone = nullptr;
if(bone == nullptr) {
bone = Engine::loadResource<Mesh>(".embedded/bone.fbx/Bone");
}

if(bone) {
Vector4 color(0.0f, 1.0f, 0.0f, 0.1f);
for(auto it : m_bones) {
Transform *p = it->parentTransform();
Vector3 parent(p->worldPosition());
Gizmos::drawMesh(*bone, color, Matrix4(parent, p->worldQuaternion(), Vector3((it->worldPosition() - parent).length())));
}
}
}

Expand All @@ -180,7 +156,6 @@ void Armature::cleanDirty(Actor *actor) {
uint32_t count = m_bindPose->boneCount();
m_bones.resize(count);
m_invertTransform.resize(count);
m_transform.resize(count);

for(uint32_t c = 0; c < count; c++) {
const Bone *b = m_bindPose->bone(c);
Expand Down
37 changes: 24 additions & 13 deletions engine/src/components/skinnedmeshrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ const char *gMatrices = "skinMatrices";
*/

SkinnedMeshRender::SkinnedMeshRender() :
m_mesh(nullptr),
m_material(nullptr),
m_armature(nullptr) {
m_mesh(nullptr),
m_material(nullptr),
m_armature(nullptr) {

m_bounds.radius = 0.0f;
}
/*!
\internal
Expand All @@ -50,14 +51,21 @@ void SkinnedMeshRender::draw(CommandBuffer &buffer, uint32_t layer) {
\internal
*/
AABBox SkinnedMeshRender::localBound() const {
AABBox result;
if(m_mesh) {
result = m_mesh->bound();
}
if(m_armature) {
result = m_armature->recalcBounds(result);
}
return result;
return m_bounds;
}

Vector3 SkinnedMeshRender::boundsCenter() const {
return m_bounds.center;
}
void SkinnedMeshRender::setBoundsCenter(Vector3 center) {
m_bounds.center = center;
}

Vector3 SkinnedMeshRender::boundsExtent() const {
return m_bounds.extent;
}
void SkinnedMeshRender::setBoundsExtent(Vector3 extent) {
m_bounds.extent = extent;
}
/*!
Returns a Mesh assigned to this component.
Expand All @@ -72,6 +80,10 @@ void SkinnedMeshRender::setMesh(Mesh *mesh) {
m_mesh = mesh;
if(m_mesh) {
setMaterial(m_mesh->material());

if(!m_bounds.isValid()) {
m_bounds = m_mesh->bound();
}
}
}
/*!
Expand Down Expand Up @@ -112,8 +124,7 @@ void SkinnedMeshRender::setArmature(Armature *armature) {
if(m_armature) {
connect(m_armature, _SIGNAL(destroyed()), this, _SLOT(onReferenceDestroyed()));
if(m_material) {
Texture *t = m_armature->texture();
m_material->setTexture(gMatrices, t);
m_material->setTexture(gMatrices, m_armature->texture());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/next/src/math/aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ AABBox::AABBox(const Vector3 &center, const Vector3 &extent) :
Returns true in case of AABBox is valid; otherwise returns false.
*/
bool AABBox::isValid() const {
return (extent.x >= 0.0f) && (extent.y >= 0.0f) && (extent.z >= 0.0f);
return radius > 0.0f;
}
/*!
Grow the AABBox to encapsulate a spehere with \a position and \a radius.
Expand Down
2 changes: 1 addition & 1 deletion worldeditor/bin/editor/meshes/bone.fbx.set
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"md5": "{58357402-cd2a-3d6e-d117-2110431833c4}",
"settings": {
"Compress_Animation": 1,
"Custom_Scale": 1,
"Custom_Scale": 0.1,
"Import_Animation": false,
"Import_Color": true,
"Import_Normals": true,
Expand Down