Fixed material property setting at runtime #214
|
@ -17,6 +17,7 @@ of DigiPen Institute of Technology is prohibited.
|
||||||
// Project Includes
|
// Project Includes
|
||||||
#include "Resource/SHHandle.h"
|
#include "Resource/SHHandle.h"
|
||||||
#include "SHCommonTypes.h"
|
#include "SHCommonTypes.h"
|
||||||
|
#include "SH_API.h"
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
|
@ -35,7 +36,7 @@ namespace SHADE
|
||||||
Describes a Pipeline along with it's associated properties for this instance.
|
Describes a Pipeline along with it's associated properties for this instance.
|
||||||
*/
|
*/
|
||||||
/***********************************************************************************/
|
/***********************************************************************************/
|
||||||
class SHMaterial
|
class SH_API SHMaterial
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -43,9 +43,9 @@ namespace SHADE
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------*/
|
||||||
struct OverrideData
|
struct OverrideData
|
||||||
{
|
{
|
||||||
size_t Index;
|
uint32_t Index;
|
||||||
size_t DataSize;
|
uint32_t DataSize;
|
||||||
size_t StoredDataOffset;
|
uint32_t StoredDataOffset;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -11,6 +11,7 @@ of DigiPen Institute of Technology is prohibited.
|
||||||
*//*************************************************************************************/
|
*//*************************************************************************************/
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "SHMaterialInstance.h"
|
#include "SHMaterialInstance.h"
|
||||||
|
#include "SHMaterial.h"
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
|
@ -34,9 +35,20 @@ namespace SHADE
|
||||||
dataStore.reset(new char[dataStoreSize]);
|
dataStore.reset(new char[dataStoreSize]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if this was stored before
|
||||||
|
const uint32_t VAR_IDX = SHADER_INFO->GetVariableIndex(key);
|
||||||
|
auto existingOverride = std::find_if(overrideData.begin(), overrideData.end(), [&](const OverrideData& od)
|
||||||
|
{
|
||||||
|
return od.Index == VAR_IDX;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Otherwise, create it
|
||||||
|
if (existingOverride == overrideData.end())
|
||||||
|
{
|
||||||
OverrideData od;
|
OverrideData od;
|
||||||
od.Index = SHADER_INFO->GetVariableIndex(key);
|
od.Index = VAR_IDX;
|
||||||
od.DataSize = sizeof(T);
|
od.DataSize = sizeof(T);
|
||||||
|
|
||||||
if (overrideData.empty())
|
if (overrideData.empty())
|
||||||
{
|
{
|
||||||
od.StoredDataOffset = 0;
|
od.StoredDataOffset = 0;
|
||||||
|
@ -47,12 +59,14 @@ namespace SHADE
|
||||||
od.StoredDataOffset = lastInsertedData.StoredDataOffset + lastInsertedData.DataSize;
|
od.StoredDataOffset = lastInsertedData.StoredDataOffset + lastInsertedData.DataSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get offset and modify the memory directly
|
|
||||||
T* dataPtr = reinterpret_cast<T*>(dataStore.get() + od.StoredDataOffset);
|
|
||||||
*dataPtr = value;
|
|
||||||
|
|
||||||
// Save the override data information
|
// Save the override data information
|
||||||
overrideData.emplace_back(std::move(od));
|
overrideData.emplace_back(std::move(od));
|
||||||
|
existingOverride = overrideData.end() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get offset and modify the memory directly
|
||||||
|
T* dataPtr = reinterpret_cast<T*>(dataStore.get() + existingOverride->StoredDataOffset);
|
||||||
|
*dataPtr = value;
|
||||||
|
|
||||||
// Flag
|
// Flag
|
||||||
dataWasChanged = true;
|
dataWasChanged = true;
|
||||||
|
@ -73,8 +87,19 @@ namespace SHADE
|
||||||
{
|
{
|
||||||
return PROP_IDX == data.Index;
|
return PROP_IDX == data.Index;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// No overrides, we get from the base material instead
|
||||||
if (prop == overrideData.end())
|
if (prop == overrideData.end())
|
||||||
|
{
|
||||||
|
if (baseMaterial)
|
||||||
|
{
|
||||||
|
return baseMaterial->GetProperty<T>(key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
throw std::invalid_argument("Attempted to get an property that was not set previously!");
|
throw std::invalid_argument("Attempted to get an property that was not set previously!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get offset and return the memory directly
|
// Get offset and return the memory directly
|
||||||
T* dataPtr = reinterpret_cast<T*>(dataStore.get() + prop->StoredDataOffset);
|
T* dataPtr = reinterpret_cast<T*>(dataStore.get() + prop->StoredDataOffset);
|
||||||
|
|
|
@ -91,6 +91,7 @@ namespace SHADE
|
||||||
{
|
{
|
||||||
SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
|
SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
|
||||||
material = gfxSystem->AddMaterialInstanceCopy(sharedMaterial);
|
material = gfxSystem->AddMaterialInstanceCopy(sharedMaterial);
|
||||||
|
matChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return material;
|
return material;
|
||||||
|
|
Loading…
Reference in New Issue