1#include "BadgeSetSystem.h"
4#include "Systems/Core/CameraSystem.h"
5#include "ViewContext.h"
6#include "Services/DPIService.h"
8#include "Components/Core/MeshComponent.h"
9#include "Components/Core/SceneComponent.h"
10#include "Components/Core/TransformComponent.h"
11#include "Components/Core/InstancedMeshRenderComponent.h"
12#include "Resources/MaterialManager.h"
13#include "Resources/MeshManager.h"
14#include "Resources/VertexFormats.h"
15#include "Systems/Core/TransformSystem.h"
16#include "Resources/TextureManager.h"
18#include "Foundation/Logging/Logger.h"
26Cogs::Core::BadgeSetSystem::~BadgeSetSystem()
32 generateMesh(context);
35void Cogs::Core::BadgeSetSystem::generateMesh(
Context* context)
37 quadMesh = context->meshManager->create();
39 std::vector<glm::vec3> points = {
46 std::vector<glm::vec2> texCoords = {
53 std::vector<uint32_t> indices = { 0,1,2,2,3,0 };
56 quadMesh->setPositions(points);
57 quadMesh->setIndexes(indices);
58 quadMesh->setTexCoords(texCoords);
60 instMaterial = context->materialManager->loadMaterial(
"BadgeMaterial.material");
61 context->materialManager->processLoading();
63 Material* mat = instMaterial.resolve();
64 viewPlaneLocalKey = mat->getFloatKey(
"viewPlaneLocal");
65 viewXLocalKey = mat->getFloatKey(
"viewXLocal");
66 viewYLocalKey = mat->getFloatKey(
"viewYLocal");
67 pointSizeKey = mat->getVec2Key(
"pointSize");
68 anchorPointOffsetKey = mat->getVec2Key(
"anchorPointOffset");
69 nearCutoffKey = mat->getVec2Key(
"nearCutoff");
70 farCutoffKey = mat->getVec2Key(
"farCutoff");
71 nearVisibilityLimitKey = mat->getFloatKey(
"nearVisibilityLimit");
72 farVisibilityLimitKey = mat->getFloatKey(
"farVisibilityLimit");
73 outputAlphaValueKey = mat->getFloatKey(
"outputAlphaValue");
75 iconTextureKey = mat->getTextureKey(
"iconMap");
76 styleTextureKey = mat->getTextureKey(
"styleMap");
77 digitTextureKey = mat->getTextureKey(
"digitMap");
80 picker = std::make_unique<BadgeSetPicker>(
this);
82 LOG_DEBUG(gLogger,
"Registered badge set picker extension");
83 context->
rayPicking->addPickable(picker.get());
86 LOG_ERROR(gLogger,
"Badge set picker extension could not be initialized!");
94 const CameraData& mainCamData = context->cameraSystem->getMainCameraData();
97 if (!badgeSetComp.isActive()) {
102 if (!instancedMeshRenderComponent) {
103 LOG_WARNING_ONCE(gLogger,
"Missing InstancedMeshRendererComponent, skipping Entity update on this system");
109 if (!data.texturesLoaded) {
110 if ((badgeSetComp.styleTexture && badgeSetComp.iconTexture && badgeSetComp.digitTexture)) {
112 if (!badgeSetComp.styleTexture->isActive() ||
113 !badgeSetComp.iconTexture->isActive() ||
114 !badgeSetComp.digitTexture->isActive()) {
115 sceneComponent->
visible =
false;
119 data.texturesLoaded =
true;
120 sceneComponent->
visible =
true;
126 if (!data.initialized) {
129 if (!meshComponent) {
130 LOG_WARNING_ONCE(gLogger,
"Missing MeshComponent, skipping Entity update on this system");
140 LOG_WARNING_ONCE(gLogger,
"icon texture provided is not a valid texture handle");
144 LOG_WARNING_ONCE(gLogger,
"digit texture provided is not a valid texture handle");
148 if (badgeSetComp.positions.size() != badgeSetComp.icons.size() || badgeSetComp.positions.size() != badgeSetComp.styles.size())
150 LOG_WARNING_ONCE(gLogger,
"Positions, icons and styles must contain the same number of elements");
154 std::vector<glm::vec4> interleavedData;
155 interleavedData.resize(badgeSetComp.positions.size());
156 for (
size_t i = 0; i < badgeSetComp.positions.size(); i++) {
157 interleavedData[i] = glm::vec4(badgeSetComp.numbers[i], badgeSetComp.styles[i], 0, badgeSetComp.icons[i]);
160 instancedMeshRenderComponent->
instanceMesh = context->meshManager->create();
161 instancedMeshRenderComponent->
instanceMesh->setInstancePositions(badgeSetComp.positions);
163 instancedMeshRenderComponent->
instanceMesh->setInstanceColors(interleavedData);
166 instancedMeshRenderComponent->
instanceCount =
static_cast<uint32_t
>(-1);
169 data.materialInstance = instancedMeshRenderComponent->
material ? instancedMeshRenderComponent->
material : context->materialInstanceManager->createMaterialInstance(instMaterial);
170 data.materialInstance->setOption(
"CullMode",
"None");
171 data.materialInstance->setVariant(
"AlphaTest",
"None");
173 instancedMeshRenderComponent->
material = data.materialInstance;
175 data.initialized =
true;
185 glm::mat4 viewFromLocal = mainCamData.viewMatrix * context->transformSystem->getLocalToWorld(trComp);
186 data.viewPlaneLocal = glm::vec4(0.f, 0.f, -1.f, 0.f) * viewFromLocal;
187 data.viewXLocal = glm::vec3(1.f, 0.f, 0.f) * glm::mat3(viewFromLocal);
188 data.viewYLocal = glm::vec3(0.f, 1.f, 0.f) * glm::mat3(viewFromLocal);
191 data.materialInstance->setVec4Property(viewPlaneLocalKey, data.viewPlaneLocal);
193 data.materialInstance->setVec4Property(viewXLocalKey, glm::vec4(data.viewXLocal, 1.f));
194 data.materialInstance->setVec4Property(viewYLocalKey, glm::vec4(data.viewYLocal, 1.f));
196 data.materialInstance->setFloatProperty(nearVisibilityLimitKey, badgeSetComp.nearVisibilityLimit);
197 data.materialInstance->setFloatProperty(farVisibilityLimitKey, badgeSetComp.farVisibilityLimit);
198 data.materialInstance->setFloatProperty(outputAlphaValueKey, badgeSetComp.outputAlphaValue);
201 data.materialInstance->setFloatProperty(nearCutoffKey, badgeSetComp.nearScalingCutoff);
202 data.materialInstance->setFloatProperty(farCutoffKey, badgeSetComp.farScalingCutoff);
204 data.materialInstance->setVec2Property(pointSizeKey, glm::vec2(badgeSetComp.badgeSize));
206 data.materialInstance->setVec2Property(anchorPointOffsetKey, glm::vec2(-badgeSetComp.anchorPoint.x, -badgeSetComp.anchorPoint.y));
208 data.materialInstance->setVariant(
"Textured",
true);
211 data.materialInstance->setTextureProperty(iconTextureKey, badgeSetComp.iconTexture);
214 data.materialInstance->setTextureProperty(styleTextureKey, badgeSetComp.styleTexture);
217 data.materialInstance->setTextureProperty(digitTextureKey, badgeSetComp.digitTexture);
220 if (badgeSetComp.positions.size() != badgeSetComp.icons.size() || badgeSetComp.positions.size() != badgeSetComp.styles.size())
222 LOG_WARNING_ONCE(gLogger,
"Positions, icons and styles must contain the same number of elements");
227 instancedMeshRenderComponent->
instanceMesh = context->meshManager->create();
230 instancedMeshRenderComponent->
instanceMesh->setInstancePositions(badgeSetComp.positions);
232 std::vector<glm::vec4> interleavedData;
233 interleavedData.resize(badgeSetComp.positions.size());
234 for (
size_t i = 0; i < badgeSetComp.positions.size(); i++) {
235 interleavedData[i] = glm::vec4(badgeSetComp.numbers[i], badgeSetComp.styles[i], 0, badgeSetComp.icons[i]);
238 instancedMeshRenderComponent->
instanceMesh->setInstanceColors(interleavedData);
void setChanged()
Sets the component to the ComponentFlags::Changed state with carry.
ComponentType * getComponent() const
virtual void initialize(Context *context) override
Initialize the system.
virtual void cleanup(Context *context) override
Provided for custom cleanup logic in derived systems.
virtual void initialize(Context *context)
Initialize the system.
void update()
Updates the system state to that of the current frame.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
std::unique_ptr< class RayPicking > rayPicking
RayPicking service instance.
Renders a mesh using instancing.
uint32_t startIndex
Start vertex index to render from.
uint32_t instanceCount
Instance count. uint32_t(-1) to draw all remaining instances.
MaterialInstanceHandle material
Material instance used to render the mesh.
uint32_t startInstance
Start instance.
MeshHandle instanceMesh
Mesh containing instancing data.
Contains a handle to a Mesh resource to use when rendering using the MeshRenderComponent.
MeshHandle meshHandle
Handle to a Mesh resource to use when rendering.
constexpr void unsetRenderFlag(RenderFlags flag)
Unset the given flag.
constexpr void setRenderFlag(RenderFlags flag)
Sets the given flag.
Contains information on how the entity behaves in the scene.
bool visible
If the entity this component is a member of should be visible.
Log implementation class.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
@ ParentPickable
Parent pickable, used by the SceneSystem to toggle child entity pickable on/off.
@ SelfVisibility
Self visibility, used by the SceneSystem to toggle entity visibility on/off.
@ SelfPickable
Self pickable, used by the SceneSystem to toggle entity pickable on/off.
Contains geometry calculations and generation.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
@ TriangleList
List of triangles.
Component for showing lots of badges.
std::vector< glm::vec3 > positions
Badge positions.
std::vector< int > icons
Icon (index) to be rendered on top of style.
std::vector< int > styles
Style (index) used for background behind icon.
Contains data describing a Camera instance and its derived data structured such as matrix data and vi...
void clear()
Clear all data from the Mesh, returning it to its initial non-indexed state with no streams and no su...
@ Clamp
Texture coordinates are clamped to the [0, 1] range.