55#include " core/renderer/shaders/register_locations_vertex_shader.h"
66#include " framework/systems/render_system.h"
77#include " resource_loader.h"
8+ #define MAX_NUMBER_OF_CUSTOM_CB 8
89
910void to_json (JSON::json& j, const CustomMaterialData& s)
1011{
@@ -20,6 +21,14 @@ void to_json(JSON::json& j, const CustomMaterialData& s)
2021 {
2122 j[" vertexShaderTextures" ].push_back (texture->getPath ().generic_string ());
2223 }
24+ for (auto & customConstantBuffers : s.customConstantBuffers )
25+ {
26+ j[" customConstantBuffers" ].push_back (customConstantBuffers);
27+ }
28+ for (auto & typeOfCustomConstantBuffers : s.typeOfCustomConstantBuffers )
29+ {
30+ j[" typeOfCustomConstantBuffers" ].push_back (typeOfCustomConstantBuffers);
31+ }
2332}
2433
2534void from_json (const JSON::json& j, CustomMaterialData& s)
@@ -40,6 +49,14 @@ void from_json(const JSON::json& j, CustomMaterialData& s)
4049 s.vertexShaderTextures .push_back (texture);
4150 }
4251 }
52+ for (auto & customConstantBuffers : j.value (" customConstantBuffers" , Vector<float >()))
53+ {
54+ s.customConstantBuffers .push_back (customConstantBuffers);
55+ }
56+ for (auto & typeOfCustomConstantBuffers : j.value (" typeOfCustomConstantBuffers" , Vector<TYPES_OF_BUFFERS>()))
57+ {
58+ s.typeOfCustomConstantBuffers .push_back (typeOfCustomConstantBuffers);
59+ }
4360}
4461
4562void CustomMaterialResourceFile::Load ()
@@ -228,6 +245,9 @@ void CustomMaterialResourceFile::bindVSCB()
228245
229246void CustomMaterialResourceFile::bindPSCB ()
230247{
248+ int size = customConstantBuffers.size () * sizeof (float );
249+ RenderingDevice::GetSingleton ()->editBuffer ((const char *)customConstantBuffers.data (), size, m_PSCB.Get ());
250+ RenderingDevice::GetSingleton ()->setPSCB (CUSTOM_PER_OBJECT_PS_CPP, 1 , m_PSCB.GetAddressOf ());
231251}
232252
233253JSON::json CustomMaterialResourceFile::getJSON () const
@@ -253,7 +273,12 @@ void CustomMaterialResourceFile::reimport()
253273 m_MaterialData = j;
254274 MaterialResourceFile::readJSON (j);
255275
276+ customConstantBuffers = m_MaterialData.customConstantBuffers ;
277+ typeOfCustomConstantBuffers = m_MaterialData.typeOfCustomConstantBuffers ;
278+
256279 recompileShaders ();
280+ float fakeArray[MAX_NUMBER_OF_CUSTOM_CB * 4 ];
281+ m_PSCB = RenderingDevice::GetSingleton ()->createBuffer ((const char *)fakeArray, sizeof (fakeArray), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
257282 m_VSCB = RenderingDevice::GetSingleton ()->createBuffer <PerModelVSCBData>(PerModelVSCBData (), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
258283}
259284
@@ -262,6 +287,73 @@ bool CustomMaterialResourceFile::save()
262287 return saveMaterialData (getJSON ());
263288}
264289
290+ float CustomMaterialResourceFile::getFloat (int index)
291+ {
292+ if (4 * index < customConstantBuffers.size ())
293+ return customConstantBuffers[4 * index];
294+ return 0 .0f ;
295+ }
296+
297+ Vector3 CustomMaterialResourceFile::getFloat3 (int index)
298+ {
299+ Vector3 temp = { 0 .0f , 0 .0f , 0 .0f };
300+ if (4 * index < customConstantBuffers.size ())
301+ {
302+ temp.x = customConstantBuffers[4 * index];
303+ temp.y = customConstantBuffers[4 * index + 1 ];
304+ temp.z = customConstantBuffers[4 * index + 2 ];
305+ }
306+ return temp;
307+ }
308+
309+ Color CustomMaterialResourceFile::getColor (int index)
310+ {
311+ Color temp = { 0 .0f , 0 .0f , 0 .0f , 0 .0f };
312+ if (4 * index < customConstantBuffers.size ())
313+ {
314+ temp.x = customConstantBuffers[4 * index];
315+ temp.y = customConstantBuffers[4 * index + 1 ];
316+ temp.z = customConstantBuffers[4 * index + 2 ];
317+ temp.w = customConstantBuffers[4 * index + 3 ];
318+ }
319+ return temp;
320+ }
321+
322+ bool CustomMaterialResourceFile::setFloat (int index, float value)
323+ {
324+ if (4 * index < customConstantBuffers.size ())
325+ {
326+ customConstantBuffers[4 * index] = value;
327+ return true ;
328+ }
329+ return false ;
330+ }
331+
332+ bool CustomMaterialResourceFile::setFloat3 (int index, Vector3 value)
333+ {
334+ if (4 * index < customConstantBuffers.size ())
335+ {
336+ customConstantBuffers[4 * index] = value.x ;
337+ customConstantBuffers[4 * index + 1 ] = value.y ;
338+ customConstantBuffers[4 * index + 2 ] = value.z ;
339+ return true ;
340+ }
341+ return false ;
342+ }
343+
344+ bool CustomMaterialResourceFile::setColor (int index, Color value)
345+ {
346+ if (4 * index < customConstantBuffers.size ())
347+ {
348+ customConstantBuffers[4 * index] = value.x ;
349+ customConstantBuffers[4 * index + 1 ] = value.y ;
350+ customConstantBuffers[4 * index + 2 ] = value.z ;
351+ customConstantBuffers[4 * index + 3 ] = value.w ;
352+ return true ;
353+ }
354+ return false ;
355+ }
356+
265357void CustomMaterialResourceFile::draw ()
266358{
267359 MaterialResourceFile::draw ();
@@ -442,4 +534,68 @@ void CustomMaterialResourceFile::draw()
442534
443535 ImGui::TreePop ();
444536 }
537+
538+ for (int i = 0 ; i < customConstantBuffers.size (); i += 4 )
539+ {
540+ String customConstantBufferName = " CB Slot " + std::to_string (i / 4 );
541+ switch (typeOfCustomConstantBuffers[i / 4 ])
542+ {
543+ case TYPES_OF_BUFFERS::FLOATCB:
544+ ImGui::DragFloat (customConstantBufferName.c_str (), &customConstantBuffers[i], 0 .01f , 0 .0f , 10 .0f );
545+ break ;
546+ case TYPES_OF_BUFFERS::FLOAT3CB:
547+ ImGui::DragFloat3 (customConstantBufferName.c_str (), &customConstantBuffers[i], 0 .01f , 0 .0f , 10 .0f );
548+ break ;
549+ case TYPES_OF_BUFFERS::COLORCB:
550+ ImGui::ColorPicker4 (customConstantBufferName.c_str (), &customConstantBuffers[i]);
551+ break ;
552+ }
553+ ImGui::Separator ();
554+ }
555+
556+ if (customConstantBuffers.size () < MAX_NUMBER_OF_CUSTOM_CB * sizeof (float ))
557+ {
558+ if (ImGui::Button (ICON_ROOTEX_PLUS " Push float CB" ))
559+ {
560+ float value = 1.0 ;
561+ customConstantBuffers.push_back (value);
562+ customConstantBuffers.push_back (value);
563+ customConstantBuffers.push_back (value);
564+ customConstantBuffers.push_back (value);
565+ typeOfCustomConstantBuffers.push_back (TYPES_OF_BUFFERS::FLOATCB);
566+ }
567+ ImGui::SameLine ();
568+
569+ if (ImGui::Button (ICON_ROOTEX_PLUS " Push float3 CB" ))
570+ {
571+ float value = 1.0 ;
572+ customConstantBuffers.push_back (value);
573+ customConstantBuffers.push_back (value);
574+ customConstantBuffers.push_back (value);
575+ customConstantBuffers.push_back (value);
576+ typeOfCustomConstantBuffers.push_back (TYPES_OF_BUFFERS::FLOAT3CB);
577+ }
578+ ImGui::SameLine ();
579+
580+ if (ImGui::Button (ICON_ROOTEX_PLUS " Push Color CB" ))
581+ {
582+ float value = 1.0 ;
583+ customConstantBuffers.push_back (value);
584+ customConstantBuffers.push_back (value);
585+ customConstantBuffers.push_back (value);
586+ customConstantBuffers.push_back (value);
587+ typeOfCustomConstantBuffers.push_back (TYPES_OF_BUFFERS::COLORCB);
588+ }
589+ ImGui::SameLine ();
590+ }
591+ if (ImGui::Button (ICON_ROOTEX_MINUS " Pop CB" ))
592+ {
593+ customConstantBuffers.pop_back ();
594+ customConstantBuffers.pop_back ();
595+ customConstantBuffers.pop_back ();
596+ customConstantBuffers.pop_back ();
597+ typeOfCustomConstantBuffers.pop_back ();
598+ }
599+ m_MaterialData.customConstantBuffers = customConstantBuffers;
600+ m_MaterialData.typeOfCustomConstantBuffers = typeOfCustomConstantBuffers;
445601}
0 commit comments