Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
MeshComponent.cpp
Go to the documentation of this file.
1#include "scene/components/MeshComponent.h"
2#include "render/Material.h"
3#include "render/Mesh.h"
4#include "render/RenderQueue.h"
5#include "scene/GameObject.h"
6#include "Engine.h"
7
8namespace eng
9{
10 MeshComponent::MeshComponent(const std::shared_ptr<Material>& material, const std::shared_ptr<Mesh>& mesh)
11 : m_material(material), m_mesh(mesh)
12 {
13 }
14
15 void MeshComponent::LoadProperties(const nlohmann::json& json)
16 {
17 if (json.contains("material"))
18 {
19 auto& matObj = json["material"];
20 const std::string path = matObj.value("path", "");
21 auto mat = Material::Load(path);
22 if (mat && matObj.contains("params"))
23 {
24 auto& paramsObj = matObj["params"];
25
26 // Floats
27 if (paramsObj.contains("float"))
28 {
29 for (auto& p : paramsObj["float"])
30 {
31 std::string name = p.value("name", "");
32 float value = p.value("value", 0.0f);
33 mat->SetParam(name, value);
34 }
35 }
36
37 // Float2
38 if (paramsObj.contains("float2"))
39 {
40 for (auto& p : paramsObj["float2"])
41 {
42 std::string name = p.value("name", "");
43 float v0 = p.value("value0", 0.0f);
44 float v1 = p.value("value1", 0.0f);
45 mat->SetParam(name, v0, v1);
46 }
47 }
48
49 // Float3
50 if (paramsObj.contains("float3"))
51 {
52 for (auto& p : paramsObj["float3"])
53 {
54 std::string name = p.value("name", "");
55 float v0 = p.value("value0", 0.0f);
56 float v1 = p.value("value1", 0.0f);
57 float v2 = p.value("value2", 0.0f);
58 mat->SetParam(name, glm::vec3(v0, v1, v2));
59 }
60 }
61
62 // Textures
63 if (paramsObj.contains("textures"))
64 {
65 for (auto& p : paramsObj["textures"])
66 {
67 std::string name = p.value("name", "");
68 std::string texPath = p.value("path", "");
69 auto texture = Texture::Load(texPath);
70
71 mat->SetParam(name, texture);
72 }
73 }
74 }
75 SetMaterial(mat);
76 }
77
78 if (json.contains("mesh"))
79 {
80 auto& meshObj = json["mesh"];
81 const std::string type = meshObj.value("type", "box");
82 if (type == "box")
83 {
84 glm::vec3 extents;
85 extents.x = meshObj.value("x", 1.0f);
86 extents.y = meshObj.value("y", 1.0f);
87 extents.z = meshObj.value("z", 1.0f);
88 auto mesh = Mesh::CreateBox(extents);
90 }
91 else if (type == "sphere")
92 {
93 float r = meshObj.value("r", 1.0f);
94 auto mesh = Mesh::CreateSphere(r, 16, 16);
96 }
97 }
98 }
99
100 void MeshComponent::Update(float deltaTime)
101 {
102 if (!m_material || !m_mesh)
103 {
104 return;
105 }
106
107 RenderCommand command;
108 command.material = m_material.get();
109 command.mesh = m_mesh.get();
111
112 auto& renderQueue = Engine::GetInstance().GetRenderQueue();
113 renderQueue.Submit(command);
114 }
115
116 void MeshComponent::SetMaterial(const std::shared_ptr<Material>& material)
117 {
118 m_material = material;
119 }
120
121 void MeshComponent::SetMesh(const std::shared_ptr<Mesh>& mesh)
122 {
123 m_mesh = mesh;
124 }
125}
std::shared_ptr< Material > material
std::shared_ptr< Mesh > mesh
GameObject * GetOwner()
Definition Component.cpp:15
static Engine & GetInstance()
Definition Engine.cpp:50
RenderQueue & GetRenderQueue()
Definition Engine.cpp:200
glm::mat4 GetWorldTransform() const
static std::shared_ptr< Material > Load(const std::string &path)
Definition Material.cpp:70
void Update(float deltaTime) override
MeshComponent()=default
void SetMesh(const std::shared_ptr< Mesh > &mesh)
void LoadProperties(const nlohmann::json &json) override
void SetMaterial(const std::shared_ptr< Material > &material)
static std::shared_ptr< Mesh > CreateBox(const glm::vec3 &extents=glm::vec3(1.0f))
Definition Mesh.cpp:86
static std::shared_ptr< Mesh > CreateSphere(float radius, int sectors, int stacks)
Definition Mesh.cpp:187
void Submit(const RenderCommand &command)
static std::shared_ptr< Texture > Load(const std::string &path)
Definition Texture.cpp:53
Material * material
Definition RenderQueue.h:18
glm::mat4 modelMatrix
Definition RenderQueue.h:19