feat: add rotation gizmo

This commit is contained in:
Nick Fisher
2024-12-12 16:28:55 +08:00
parent 0ad73d06e0
commit def85614d8
17 changed files with 4056 additions and 7790 deletions

View File

@@ -30,12 +30,12 @@ extern "C"
return reinterpret_cast<TMaterialProvider*>(provider);
}
EMSCRIPTEN_KEEPALIVE TGizmo *SceneManager_createGizmo(TSceneManager *tSceneManager, TView *tView, TScene *tScene)
EMSCRIPTEN_KEEPALIVE TGizmo *SceneManager_createGizmo(TSceneManager *tSceneManager, TView *tView, TScene *tScene, TGizmoType tGizmoType)
{
auto sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *scene = reinterpret_cast<Scene *>(tScene);
auto *view = reinterpret_cast<View *>(tView);
auto gizmo = sceneManager->createGizmo(view, scene);
auto gizmo = sceneManager->createGizmo(view, scene, static_cast<SceneManager::GizmoType>(tGizmoType));
return reinterpret_cast<TGizmo *>(gizmo);
}

View File

@@ -81,12 +81,11 @@ extern "C"
const auto child = Entity::import(childId);
const auto parent = Entity::import(parentId);
const auto &childInstance = tm->getInstance(child);
if (!childInstance.isValid())
{
Log("Child instance is not valid");
TRACE("Can't set transform parent, child instance is not valid");
return;
}

View File

@@ -540,12 +540,13 @@ extern "C"
TSceneManager *tSceneManager,
TView *tView,
TScene *tScene,
TGizmoType tGizmoType,
void (*onComplete)(TGizmo*)
) {
std::packaged_task<void()> lambda(
[=]() mutable
{
auto *gizmo = SceneManager_createGizmo(tSceneManager, tView, tScene);
auto *gizmo = SceneManager_createGizmo(tSceneManager, tView, tScene, tGizmoType);
onComplete(gizmo);
});
auto fut = _rl->add_task(lambda);

View File

@@ -24,7 +24,7 @@ namespace thermion
Engine *engine,
View *view,
Scene *scene,
Material *material) : _source(sceneAsset),
Material *material) noexcept : _source(sceneAsset),
_engine(engine),
_view(view),
_scene(scene),
@@ -41,6 +41,27 @@ namespace thermion
createAxisInstance(Axis::Z);
}
// move constructor so we don't re-create all entities/materials when moving
Gizmo::Gizmo(Gizmo &&other) noexcept : _source(other._source),
_engine(other._engine),
_view(other._view),
_scene(other._scene),
_material(other._material),
_parent(other._parent),
_scale(other._scale),
_entities(std::move(other._entities)),
_materialInstances(std::move(other._materialInstances)),
_hitTest(std::move(other._hitTest)),
_axes(std::move(other._axes))
{
other._source = nullptr;
other._engine = nullptr;
other._view = nullptr;
other._scene = nullptr;
other._material = nullptr;
other._parent = {};
}
void Gizmo::createAxisInstance(Gizmo::Axis axis)
{
auto &rm = _engine->getRenderableManager();
@@ -52,7 +73,7 @@ namespace thermion
TRACE("Created Gizmo axis glTF instance with head entity %d", instance->getEntity());
materialInstance->setParameter("baseColorFactor", inactiveColors[axis]);
materialInstance->setParameter("scale", 4.0f);
materialInstance->setParameter("scale", _scale);
auto hitTestEntity = instance->findEntityByName("HitTest");
TRACE("Created hit test entity %d for axis %d", hitTestEntity, axis);
@@ -73,8 +94,8 @@ namespace thermion
{
auto *hitTestMaterialInstance = _material->createInstance();
_materialInstances.push_back(hitTestMaterialInstance);
hitTestMaterialInstance->setParameter("baseColorFactor", math::float4{1.0f, 0.0f, 1.0f, 0.5f});
hitTestMaterialInstance->setParameter("scale", 4.0f);
hitTestMaterialInstance->setParameter("baseColorFactor", math::float4{0.0f, 0.0f, 0.0f, 0.0f});
hitTestMaterialInstance->setParameter("scale", _scale);
rm.setMaterialInstanceAt(renderableInstance, 0, hitTestMaterialInstance);
}
}
@@ -86,7 +107,7 @@ namespace thermion
auto &tm = _engine->getTransformManager();
auto transformInstance = tm.getInstance(instance->getEntity());
tm.setTransform(transformInstance, transform);
// parent this entity's transform to the Gizmo _parent entity
tm.setParent(transformInstance, tm.getInstance(_parent));

View File

@@ -42,7 +42,8 @@
#include "scene/GeometrySceneAssetBuilder.hpp"
#include "UnprojectTexture.hpp"
#include "resources/gizmo_glb.h"
#include "resources/translation_gizmo_glb.h"
#include "resources/rotation_gizmo_glb.h"
extern "C"
{
@@ -135,7 +136,7 @@ namespace thermion
_engine->destroy(_unlitFixedSizeMaterial);
_engine->destroy(_gizmoMaterial);
_cameras.clear();
_grid = nullptr;
_gltfResourceLoader->asyncCancelLoad();
@@ -152,37 +153,62 @@ namespace thermion
AssetLoader::destroy(&_assetLoader);
}
SceneAsset *SceneManager::createGrid() {
if(!_grid) {
SceneAsset *SceneManager::createGrid()
{
if (!_grid)
{
_grid = std::make_unique<GridOverlay>(*_engine);
}
return _grid.get();
}
bool SceneManager::isGridEntity(utils::Entity entity) {
if(!_grid) {
bool SceneManager::isGridEntity(utils::Entity entity)
{
if (!_grid)
{
return false;
}
if(entity == _grid->getEntity()) {
if (entity == _grid->getEntity())
{
return true;
}
for(int i =0; i < _grid->getChildEntityCount(); i++) {
if(entity == _grid->getChildEntities()[i]) {
for (int i = 0; i < _grid->getChildEntityCount(); i++)
{
if (entity == _grid->getChildEntities()[i])
{
return true;
}
}
return false;
}
Gizmo *SceneManager::createGizmo(View *view, Scene *scene)
Gizmo *SceneManager::createGizmo(View *view, Scene *scene, GizmoType type)
{
if(!_gizmoGlb) {
_gizmoGlb = loadGlbFromBuffer(GIZMO_GLB_GIZMO_DATA, GIZMO_GLB_GIZMO_SIZE, 100, true, 4, 0, false, false);
TRACE("Creating gizmo type %d", type);
Gizmo *raw;
switch (type)
{
case GizmoType::TRANSLATION:
if (!_translationGizmoGlb)
{
TRACE("Translation gizmo source not found, loading");
_translationGizmoGlb = loadGlbFromBuffer(TRANSLATION_GIZMO_GLB_TRANSLATION_GIZMO_DATA, TRANSLATION_GIZMO_GLB_TRANSLATION_GIZMO_SIZE, 100, true, 4, 0, false, false);
}
raw = new Gizmo(_translationGizmoGlb, _engine, view, scene, _unlitFixedSizeMaterial);
TRACE("Built translation gizmo");
break;
case GizmoType::ROTATION:
if (!_rotationGizmoGlb)
{
TRACE("Rotation gizmo source not found, loading");
_rotationGizmoGlb = loadGlbFromBuffer(ROTATION_GIZMO_GLB_ROTATION_GIZMO_DATA, ROTATION_GIZMO_GLB_ROTATION_GIZMO_SIZE, 100, true, 4, 0, false, false);
}
raw = new Gizmo(_rotationGizmoGlb, _engine, view, scene, _unlitFixedSizeMaterial);
TRACE("Built rotation gizmo");
break;
}
auto gizmo = std::make_unique<Gizmo>(_gizmoGlb, _engine, view, scene, _unlitFixedSizeMaterial);
auto *raw = gizmo.get();
_sceneAssets.push_back(std::move(gizmo));
_sceneAssets.push_back(std::unique_ptr<Gizmo>(raw));
return raw;
}
@@ -286,8 +312,7 @@ namespace thermion
asset,
_assetLoader,
_engine,
_ncm
);
_ncm);
auto filamentInstance = asset->getInstance();
size_t entityCount = filamentInstance->getEntityCount();
@@ -375,16 +400,16 @@ namespace thermion
asset,
_assetLoader,
_engine,
_ncm
);
_ncm);
auto sceneAssetInstance = sceneAsset->createInstance();
if(addToScene) {
if (addToScene)
{
sceneAssetInstance->addAllEntities(_scene);
}
sceneAssetInstance->setPriority(_engine->getRenderableManager(), priority);
sceneAssetInstance->setLayer(_engine->getRenderableManager(), layer);
auto *raw = sceneAsset.get();
_sceneAssets.push_back(std::move(sceneAsset));