forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPickingRenderer.cpp
More file actions
120 lines (108 loc) · 2.45 KB
/
Copy pathPickingRenderer.cpp
File metadata and controls
120 lines (108 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "PCH.h"
#include "Component/PickingRenderer.h"
#include "Game/GameObject.h"
#include "Game/World.h"
#include "Render/Mesh.h"
namespace sh::game
{
SH_GAME_API PickingRenderer::PickingRenderer(GameObject& owner) :
MeshRenderer(owner)
{
id = PickingIdManager::AssignId(this);
//SH_INFO_FORMAT("ID: {}", id);
this->mat = world.materials.GetResource("PickingMaterial");
renderer = owner.GetComponent<MeshRenderer>();
if (core::IsValid(renderer))
{
auto mesh = renderer->GetMesh();
if (core::IsValid(mesh))
this->mesh = mesh;
}
}
SH_GAME_API PickingRenderer::~PickingRenderer()
{
PickingIdManager::Erase(id);
}
SH_GAME_API void PickingRenderer::Awake()
{
}
SH_GAME_API void PickingRenderer::BeginUpdate()
{
if (!core::IsValid(camera))
return;
if (core::IsValid(renderer))
{
if (this->mesh != renderer->GetMesh())
SetMesh(renderer->GetMesh());
}
if (!camera->pickingCallback.Empty())
{
for (auto& [cam, drawable] : drawables)
{
if (!cam->active)
continue;
float r = ((id & 0x000000FF) >> 0) / 255.0f;
float g = ((id & 0x0000FF00) >> 8) / 255.0f;
float b = ((id & 0x00FF0000) >> 16) / 255.0f;
float a = 1.f;
struct alignas(16) Uniform
{
glm::vec4 color;
} uniform{ {r, g, b, a} };
drawable->SetUniformData(1, &uniform, render::IDrawable::Stage::Fragment);
}
Super::Update();
}
}
SH_GAME_API void PickingRenderer::Update()
{
}
SH_GAME_API void PickingRenderer::SetCamera(PickingCamera& camera)
{
this->camera = &camera;
CleanDrawables();
CreateDrawable(this->camera);
}
SH_GAME_API void PickingRenderer::OnPropertyChanged(const core::reflection::Property& prop)
{
if (prop.GetName() == "mesh")
{
CleanDrawables();
CreateDrawable(this->camera);
}
if (prop.GetName() == "camera")
{
SetCamera(*camera);
}
}
SH_GAME_API auto PickingIdManager::AssignId(PickingRenderer* renderer) -> uint32_t
{
if (emptyId.empty())
{
ids.insert({ nextId, renderer });
return nextId++;
}
else
{
uint32_t id = emptyId.front();
emptyId.pop();
ids.insert({ id, renderer });
return id;
}
}
SH_GAME_API auto PickingIdManager::Get(uint32_t id) -> PickingRenderer*
{
auto it = ids.find(id);
if (it == ids.end())
return nullptr;
return it->second;
}
SH_GAME_API void PickingIdManager::Erase(uint32_t id)
{
auto it = ids.find(id);
if (it == ids.end())
return;
ids.erase(it);
emptyId.push(id);
}
}//namespace