Plasma Engine  2.0
Loading...
Searching...
No Matches
MeshComponentBase.h
1#pragma once
2
3#include <Core/World/World.h>
4#include <RendererCore/Components/RenderComponent.h>
5#include <RendererCore/Material/MaterialResource.h>
6#include <RendererCore/Meshes/MeshResource.h>
7#include <RendererCore/Pipeline/RenderData.h>
8
9struct plMsgSetColor;
10struct plInstanceData;
11
12class PL_RENDERERCORE_DLL plMeshRenderData : public plRenderData
13{
14 PL_ADD_DYNAMIC_REFLECTION(plMeshRenderData, plRenderData);
15
16public:
17 virtual void FillBatchIdAndSortingKey();
18
20 plMaterialResourceHandle m_hMaterial;
21 plColor m_Color = plColor::White;
22
23 plUInt32 m_uiSubMeshIndex : 30;
24 plUInt32 m_uiFlipWinding : 1;
25 plUInt32 m_uiUniformScale : 1;
26
27 plUInt32 m_uiUniqueID = 0;
28
29protected:
30 PL_FORCE_INLINE void FillBatchIdAndSortingKeyInternal(plUInt32 uiAdditionalBatchData)
31 {
32 m_uiFlipWinding = m_GlobalTransform.HasMirrorScaling() ? 1 : 0;
33 m_uiUniformScale = m_GlobalTransform.ContainsUniformScale() ? 1 : 0;
34
35 const plUInt32 uiMeshIDHash = plHashingUtils::StringHashTo32(m_hMesh.GetResourceIDHash());
36 const plUInt32 uiMaterialIDHash = m_hMaterial.IsValid() ? plHashingUtils::StringHashTo32(m_hMaterial.GetResourceIDHash()) : 0;
37
38 // Generate batch id from mesh, material and part index.
39 plUInt32 data[] = {uiMeshIDHash, uiMaterialIDHash, m_uiSubMeshIndex, m_uiFlipWinding, uiAdditionalBatchData};
40 m_uiBatchId = plHashingUtils::xxHash32(data, sizeof(data));
41
42 // Sort by material and then by mesh
43 m_uiSortingKey = (uiMaterialIDHash << 16) | ((uiMeshIDHash + m_uiSubMeshIndex) & 0xFFFE) | m_uiFlipWinding;
44 }
45};
46
48struct PL_RENDERERCORE_DLL plMsgSetMeshMaterial : public plMessage
49{
50 PL_DECLARE_MESSAGE_TYPE(plMsgSetMeshMaterial, plMessage);
51
52 void SetMaterialFile(const char* szFile);
53 const char* GetMaterialFile() const;
54
57
59 plUInt32 m_uiMaterialSlot = 0xFFFFFFFFu;
60
61 virtual void Serialize(plStreamWriter& inout_stream) const override;
62 virtual void Deserialize(plStreamReader& inout_stream, plUInt8 uiTypeVersion) override;
63};
64
66class PL_RENDERERCORE_DLL plMeshComponentBase : public plRenderComponent
67{
68 PL_DECLARE_ABSTRACT_COMPONENT_TYPE(plMeshComponentBase, plRenderComponent);
69
71 // plComponent
72
73public:
74 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
75 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
76
78 // plRenderComponent
79
80public:
81 virtual plResult GetLocalBounds(plBoundingBoxSphere& ref_bounds, bool& ref_bAlwaysVisible, plMsgUpdateLocalBounds& ref_msg) override;
82
84 // plRenderMeshComponent
85
86public:
89
91 void SetMesh(const plMeshResourceHandle& hMesh);
92 PL_ALWAYS_INLINE const plMeshResourceHandle& GetMesh() const { return m_hMesh; }
93
95 void SetMaterial(plUInt32 uiIndex, const plMaterialResourceHandle& hMaterial);
96 plMaterialResourceHandle GetMaterial(plUInt32 uiIndex) const;
97
98 void SetMeshFile(const char* szFile); // [ property ]
99 const char* GetMeshFile() const; // [ property ]
100
102 void SetColor(const plColor& color); // [ property ]
103 const plColor& GetColor() const; // [ property ]
104
108 void SetSortingDepthOffset(float fOffset); // [ property ]
109 float GetSortingDepthOffset() const; // [ property ]
110
111 void OnMsgSetMeshMaterial(plMsgSetMeshMaterial& ref_msg); // [ msg handler ]
112 void OnMsgSetColor(plMsgSetColor& ref_msg); // [ msg handler ]
113
114protected:
115 virtual plMeshRenderData* CreateRenderData() const;
116
117 plUInt32 Materials_GetCount() const; // [ property ]
118 const char* Materials_GetValue(plUInt32 uiIndex) const; // [ property ]
119 void Materials_SetValue(plUInt32 uiIndex, const char* value); // [ property ]
120 void Materials_Insert(plUInt32 uiIndex, const char* value); // [ property ]
121 void Materials_Remove(plUInt32 uiIndex); // [ property ]
122
123 void OnMsgExtractRenderData(plMsgExtractRenderData& msg) const;
124
125 plMeshResourceHandle m_hMesh;
127 plColor m_Color = plColor::White;
128 float m_fSortingDepthOffset = 0.0f;
129};
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
static const plColor White
#FFFFFF
Definition Color.h:194
Definition DynamicArray.h:81
static plUInt32 xxHash32(const void *pKey, size_t uiSizeInByte, plUInt32 uiSeed=0)
Calculates the 32bit xxHash of the given key.
Definition HashingUtils.cpp:209
static constexpr plUInt32 StringHashTo32(plUInt64 uiHash)
Truncates a 64 bit string hash to 32 bit.
Definition HashingUtils_inl.h:138
Base class for components that render static or animated meshes.
Definition MeshComponentBase.h:67
Definition MeshComponentBase.h:13
Base class for all message types. Each message type has it's own id which is used to dispatch message...
Definition Message.h:22
Base class for objects that should be rendered.
Definition RenderComponent.h:9
Base class for all render data. Render data must contain all information that is needed to render the...
Definition RenderData.h:14
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
PL_ALWAYS_INLINE bool IsValid() const
Returns whether the handle stores a valid pointer to a resource.
Definition ResourceHandle.h:206
PL_ALWAYS_INLINE plUInt64 GetResourceIDHash() const
Returns the Resource ID hash of the exact resource that this handle points to, without acquiring the ...
Definition ResourceHandle.h:216
Reads a world description from a stream. Allows to instantiate that world multiple times in different...
Definition WorldReader.h:47
Stores an entire plWorld in a stream.
Definition WorldWriter.h:13
Definition InstanceDataProvider.h:12
Definition RenderData.h:116
A message to modify the main color of some thing.
Definition SetColorMessage.h:32
This message is used to replace the material on a mesh.
Definition MeshComponentBase.h:49
plMaterialResourceHandle m_hMaterial
The material to be used.
Definition MeshComponentBase.h:56
Definition UpdateLocalBoundsMessage.h:9
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54