Cogs.Core
BillboardSystem.cpp
1#include "BillboardSystem.h"
2
3#include "Systems/Core/TransformSystem.h"
4#include "Systems/Core/SpriteRenderSystem.h"
5
6#include "Resources/TextureManager.h"
7#include "Resources/MaterialManager.h"
8
9#include "Context.h"
10
12{
13 auto sprites = context->spriteRenderSystem;
14
15 for (const auto & billboard : pool) {
16 if (!billboard.image) continue;
17 if (billboard.imageSize.x == 0 || billboard.imageSize.y == 0) continue;
18
19 auto spriteRenderer = billboard.getComponent<SpriteRenderComponent>();
20
21 if (!spriteRenderer->isVisible()) continue;
22
23 auto transform = billboard.getComponent<TransformComponent>();
24 const auto & localToWorld = context->transformSystem->getLocalToWorld(transform);
25 const glm::vec3 worldSpacePosition = glm::vec3(localToWorld * glm::vec4(0, 0, 0, 1.0f));
26
27 const glm::vec2 right(billboard.imageSize.x * 0.5f, 0);
28 const glm::vec2 up(0, billboard.imageSize.y * 0.5f);
29
30 glm::vec2 offset(0, 0);
31
32 if (billboard.horizontalAlignment == HorizontalAlignment::Left) offset += right;
33 else if (billboard.horizontalAlignment == HorizontalAlignment::Right) offset -= right;
34
35 if (billboard.verticalAlignment == VerticalAlignment::Bottom) offset += up;
36 else if (billboard.verticalAlignment == VerticalAlignment::Top) offset -= up;
37
38 auto batch = sprites->begin(DefaultSpriteMaterial, "ViewTransformPixelSize", billboard.image, billboard.color, billboard.alwaysOnTop);
39 sprites->addSprite(spriteRenderer, worldSpacePosition, offset, billboard.imageSize, glm::vec4(0, 1, 1, 0));
40
41 if (billboard.flipX || billboard.flipY) {
42 batch->material->setVec2Property(batch->material->material->getVec2Key("imageFlip"), glm::vec2(billboard.flipX ? 1 : 0, billboard.flipY ? 1 : 0));
43 }
44
45 batch->worldPosition = worldSpacePosition;
46 batch->bbox = { worldSpacePosition, worldSpacePosition };
47 batch->flags |= RenderItemFlags::CalculateDepth;
48 batch->blendMode = billboard.blendMode;
49 batch->material->options.blendMode = billboard.blendMode;
50 }
51}
ComponentType * getComponent() const
Definition: Component.h:159
Context * context
Pointer to the Context instance the system lives in.
void update()
Updates the system state to that of the current frame.
ComponentPool< BillboardComponent > pool
Pool of components managed by the system.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Defines a 4x4 transformation matrix for the entity and a global offset for root entities.
@ Bottom
Align the text towards the bottom, making the position minus the font height the baseline of the text...
@ Top
Align the text towards the top, making the position the baseline of the text.