From 731c4981c9039f353b8955fb9c8cb7533b5b0c05 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Sat, 24 Aug 2024 15:58:49 +0800 Subject: [PATCH] use inactive/active color gizmo --- materials/gizmo.mat | 4 +- thermion_dart/native/include/Gizmo.hpp | 31 +++++++++++++--- thermion_dart/native/src/Gizmo.cpp | 51 +++++++++++++++++++++----- 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/materials/gizmo.mat b/materials/gizmo.mat index 7ee85818..5b7bc75b 100644 --- a/materials/gizmo.mat +++ b/materials/gizmo.mat @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1ce59256956f5dbb76f6ac3c51e4811f71483427db689afae5f0fd982a53b90f -size 647 +oid sha256:3158461d081f058dcb9582ce19cc2daedc73abbe758ba5094c94df89028d8c4d +size 981 diff --git a/thermion_dart/native/include/Gizmo.hpp b/thermion_dart/native/include/Gizmo.hpp index b138f3ec..4ca793e9 100644 --- a/thermion_dart/native/include/Gizmo.hpp +++ b/thermion_dart/native/include/Gizmo.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -20,15 +21,19 @@ #include "Aabb2.h" +namespace thermion_filament { + using namespace filament; using namespace utils; - class Gizmo { + + enum Axis { X, Y, Z}; + public: - Gizmo(Engine* const engine); - void updateTransform(); - void destroy(Engine* const engine); + Gizmo(Engine& engine); + void updateTransform(Camera& camera, const Viewport& vp); + void destroy(); Entity x() { return _entities[0]; }; @@ -41,8 +46,24 @@ class Gizmo { Entity center() { return _entities[3]; }; + + void highlight(Entity entity); + void unhighlight(); private: + Engine &_engine; utils::Entity _entities[4]; Material* _material; MaterialInstance* _materialInstances[4]; -}; \ No newline at end of file + math::float3 inactiveColors[3] { + math::float3 { 0.75f, 0.0f, 0.0f }, + math::float3 { 0.0f, 0.75f, 0.0f }, + math::float3 { 0.0f, 0.0f, 0.75f }, + }; + math::float3 activeColors[3] { + math::float3 { 1.0f, 0.0f, 0.0f }, + math::float3 { 0.0f, 1.0f, 0.0f }, + math::float3 { 0.0f, 0.0f, 1.0f }, + }; +}; + +} \ No newline at end of file diff --git a/thermion_dart/native/src/Gizmo.cpp b/thermion_dart/native/src/Gizmo.cpp index 0bdc12f6..39bd255e 100644 --- a/thermion_dart/native/src/Gizmo.cpp +++ b/thermion_dart/native/src/Gizmo.cpp @@ -68,7 +68,8 @@ Gizmo::Gizmo(Engine &engine) : _engine(engine) { delete[] static_cast(buffer); })); RenderableManager::Builder(1) - .boundingBox({{}, {centerCubeSize, centerCubeSize, centerCubeSize}}) + .boundingBox({{-centerCubeSize, -centerCubeSize, -centerCubeSize}, + {centerCubeSize, centerCubeSize, centerCubeSize}}) .material(0, _materialInstances[3]) .layerMask(0xFF, 2) .priority(0) @@ -138,28 +139,27 @@ Gizmo::Gizmo(Engine &engine) : _engine(engine) _entities[i] = entityManager.create(); _materialInstances[i] = _material->createInstance(); - math::float3 color; + auto baseColor = inactiveColors[i]; + math::mat4f transform; switch (i) { - case 0: // X-axis (Red) - color = {1.0f, 0.0f, 0.0f}; + case Axis::X: transform = math::mat4f::rotation(math::F_PI_2, math::float3{0, 1, 0}); break; - case 1: // Y-axis (Green) - color = {0.0f, 1.0f, 0.0f}; + case 1: transform = math::mat4f::rotation(-math::F_PI_2, math::float3{1, 0, 0}); break; - case 2: // Z-axis (Blue) - color = {0.0f, 0.0f, 1.0f}; + case 2: break; } - _materialInstances[i]->setParameter("color", color); + _materialInstances[i]->setParameter("color", baseColor); RenderableManager::Builder(1) - .boundingBox({{0, 0, (lineLength + arrowLength) / 2}, {arrowWidth / 2, arrowWidth / 2, (lineLength + arrowLength) / 2}}) + .boundingBox({{-arrowWidth, -arrowWidth, 0}, + {arrowWidth, arrowWidth, lineLength + arrowLength}}) .material(0, _materialInstances[i]) .geometry(0, RenderableManager::PrimitiveType::TRIANGLES, vb, ib, 0, 54) .priority(0) @@ -178,6 +178,37 @@ Gizmo::Gizmo(Engine &engine) : _engine(engine) } } +void Gizmo::highlight(Entity entity) { + auto &rm = _engine.getRenderableManager(); + auto renderableInstance = rm.getInstance(entity); + auto materialInstance = rm.getMaterialInstanceAt(renderableInstance, 0); + + math::float3 baseColor; + if(entity == x()) { + baseColor = activeColors[Axis::X]; + } else if(entity == y()) { + baseColor = activeColors[Axis::Y]; + } else if(entity == z()) { + baseColor = activeColors[Axis::Z]; + } else { + baseColor = math::float3 { 1.0f, 1.0f, 1.0f }; + } + + materialInstance->setParameter("color", baseColor); +} + +void Gizmo::unhighlight() { + auto &rm = _engine.getRenderableManager(); + + for(int i = 0; i < 3; i++) { + auto renderableInstance = rm.getInstance(_entities[i]); + auto materialInstance = rm.getMaterialInstanceAt(renderableInstance, 0); + + math::float3 baseColor = inactiveColors[i]; + materialInstance->setParameter("color", baseColor); + } +} + void Gizmo::destroy() { auto& rm = _engine.getRenderableManager();