SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
MaterialComponent.cpp
Go to the documentation of this file.
1#include <Core/GameObject.hpp>
5
6namespace Sleak {
7
9 GameObject* object, const RefPtr<Material>& material)
10 : Component(object), m_material(material) {}
11
13 Material* material)
14 : Component(object) {
15 if (material)
16 m_material = RefPtr<Material>(material);
17 else
18 m_material = RefPtr<Material>(new Material());
19 }
20
22 Math::Color diffuseColor)
23 : Component(object) {
24 Material* mat = new Material();
25 mat->SetDiffuseColor(diffuseColor);
26 m_material = RefPtr<Material>(mat);
27 }
28
30 GameObject* object, const std::string& diffuseTexture)
31 : Component(object) {
32 Material* mat = new Material();
33 mat->SetDiffuseTexture(diffuseTexture);
34 m_material = RefPtr<Material>(mat);
35 }
36
38 if (m_material) {
39 m_material->Initialize();
40 }
41 bIsInitialized = true;
42 return true;
43 }
44
45 void MaterialComponent::Update(float DeltaTime) {
46 MeshComponent* mesh =
47 owner ? owner->GetComponent<MeshComponent>() : nullptr;
48 if (mesh && mesh->IsInitialized()) return;
49
50 SubmitRenderState();
51 }
52
53 void MaterialComponent::SubmitRenderState() {
54 if (!m_enabled || !m_material)
55 return;
56
58 }
59
60 void MaterialComponent::OnEnable() { m_enabled = true; }
61
62 void MaterialComponent::OnDisable() { m_enabled = false; }
63
65 const RefPtr<Material>& material) {
66 m_material = material;
67 if (bIsInitialized && m_material) {
68 m_material->Initialize();
69 }
70 }
71
73 if (material)
74 m_material = RefPtr<Material>(material);
75 else
76 m_material = RefPtr<Material>(new Material());
77
78 if (bIsInitialized && m_material) {
79 m_material->Initialize();
80 }
81 }
82
84 return m_material;
85 }
86
88 return m_material.IsValid() ? m_material.get() : nullptr;
89 }
90
92 if (m_material)
93 m_material->SetDiffuseColor(color);
94 }
95
97 const std::string& path) {
98 if (m_material)
99 m_material->SetDiffuseTexture(path);
100 }
101
103 const std::string& path) {
104 if (m_material)
105 m_material->SetNormalTexture(path);
106 }
107
108 void MaterialComponent::SetShininess(float shininess) {
109 if (m_material)
110 m_material->SetShininess(shininess);
111 }
112
113 void MaterialComponent::SetMetallic(float metallic) {
114 if (m_material)
115 m_material->SetMetallic(metallic);
116 }
117
118 void MaterialComponent::SetRoughness(float roughness) {
119 if (m_material)
120 m_material->SetRoughness(roughness);
121 }
122}
friend class GameObject
Definition Component.hpp:88
GameObject * owner
Definition Component.hpp:84
bool IsInitialized() const
True once Initialize() has run and succeeded.
Definition Component.hpp:81
Component(GameObject *object)
Definition Component.hpp:61
T * GetComponent()
Finds the first attached component of type T, or nullptr.
void SetShininess(float shininess)
bool Initialize() override
Initializes the owned material's GPU resources.
void SetDiffuseTexture(const std::string &path)
Material * GetMaterialRaw() const
void SetDiffuseColor(Math::Color color)
void SetMetallic(float metallic)
void SetRoughness(float roughness)
void Update(float DeltaTime) override
Submits the material bind unless a sibling mesh does it.
MaterialComponent(GameObject *object, const RefPtr< Material > &material)
const RefPtr< Material > & GetMaterial() const
void SetNormalTexture(const std::string &path)
void SetMaterial(const RefPtr< Material > &material)
Replaces the material, re-initializing it if the component is already initialized.
void SetDiffuseTexture(const RefPtr< Texture > &texture)
Definition Material.cpp:146
void SetDiffuseColor(Math::Color color)
Definition Material.cpp:291
friend class RefPtr
Definition RefPtr.hpp:65
static RenderCommandQueue * GetInstance()
Lazily creates and returns the process-wide singleton instance.
void SubmitBindMaterial(::Sleak::Material *material)
Queues a material bind, switching shader/texture state for subsequent draws.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10