Skip to content

Commit 1307f68

Browse files
committed
Added changeRootTransform to loadGLTF
1 parent 321251d commit 1307f68

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

engine/core/subsystem/MeshSystem.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ void MeshSystem::createTorus(MeshComponent& mesh, float radius, float ringRadius
16721672
mesh.needReload = true;
16731673
}
16741674

1675-
bool MeshSystem::loadGLTF(Entity entity, const std::string filename, bool asyncLoad, bool skipEntities){
1675+
bool MeshSystem::loadGLTF(Entity entity, const std::string filename, bool asyncLoad, bool skipEntities, bool changeRootTransform){
16761676
MeshComponent& mesh = scene->getComponent<MeshComponent>(entity);
16771677
ModelComponent& model = scene->getComponent<ModelComponent>(entity);
16781678
Transform& transform = scene->getComponent<Transform>(entity);
@@ -1764,13 +1764,27 @@ bool MeshSystem::loadGLTF(Entity entity, const std::string filename, bool asyncL
17641764
}
17651765
}
17661766

1767-
// Only apply the GLTF root transform when the entity still has the default transform.
17681767
Matrix4 matrix = getGLTFMeshGlobalMatrix(meshNode, model, nodesParent);
1769-
bool hasDefaultTransform = (transform.position == Vector3::ZERO)
1770-
&& (transform.rotation == Quaternion::IDENTITY)
1771-
&& (transform.scale == Vector3::UNIT_SCALE);
1772-
if (hasDefaultTransform) {
1773-
matrix.decompose(transform.position, transform.scale, transform.rotation);
1768+
1769+
if (changeRootTransform) {
1770+
bool hasDefaultPosition = (transform.position == Vector3::ZERO);
1771+
bool hasDefaultRotation = (transform.rotation == Quaternion::IDENTITY);
1772+
bool hasDefaultScale = (transform.scale == Vector3::UNIT_SCALE);
1773+
1774+
Vector3 newPosition;
1775+
Vector3 newScale;
1776+
Quaternion newRotation;
1777+
matrix.decompose(newPosition, newScale, newRotation);
1778+
1779+
if (hasDefaultPosition) {
1780+
transform.position = newPosition;
1781+
}
1782+
if (hasDefaultRotation) {
1783+
transform.rotation = newRotation;
1784+
}
1785+
if (hasDefaultScale) {
1786+
transform.scale = newScale;
1787+
}
17741788
transform.needUpdate = true;
17751789
}
17761790

@@ -2786,7 +2800,7 @@ bool MeshSystem::createOrUpdateModel(Entity entity, ModelComponent& model, MeshC
27862800
if (ext == "obj"){
27872801
ret = loadOBJ(entity, model.filename, false);
27882802
}else{
2789-
ret = loadGLTF(entity, model.filename, false, skipEntities);
2803+
ret = loadGLTF(entity, model.filename, false, skipEntities, false);
27902804
}
27912805

27922806
if (ret){

engine/core/subsystem/MeshSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace Supernova{
6666
void createCylinder(MeshComponent& mesh, float baseRadius=1, float topRadius=1, float height=2, unsigned int slices=36, unsigned int stacks=18);
6767
void createCapsule(MeshComponent& mesh, float baseRadius=1, float topRadius=1, float height=2, unsigned int slices=36, unsigned int stacks=18);
6868
void createTorus(MeshComponent& mesh, float radius=1, float ringRadius=0.5, unsigned int sides=36, unsigned int rings=16);
69-
bool loadGLTF(Entity entity, const std::string filename, bool asyncLoad=false, bool skipEntities=false);
69+
bool loadGLTF(Entity entity, const std::string filename, bool asyncLoad=false, bool skipEntities=false, bool changeRootTransform=true);
7070
bool loadOBJ(Entity entity, const std::string filename, bool asyncLoad=false);
7171

7272
void createInstancedMesh(Entity entity);

0 commit comments

Comments
 (0)