Skip to content

Commit

Permalink
Serialize PBS user values in JSON materials
Browse files Browse the repository at this point in the history
  • Loading branch information
darksylinc committed Dec 22, 2021
1 parent f63e0db commit 0adb2b6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions Components/Hlms/Pbs/include/OgreHlmsJsonPbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace Ogre
static void parseScale( const rapidjson::Value &jsonArray, Vector4 &offsetScale );

static inline Vector3 parseVector3Array( const rapidjson::Value &jsonArray );
static inline Vector4 parseVector4Array( const rapidjson::Value &jsonArray );
static inline ColourValue parseColourValueArray(
const rapidjson::Value &jsonArray,
const ColourValue &defaultValue = ColourValue::White );
Expand Down
64 changes: 64 additions & 0 deletions Components/Hlms/Pbs/src/OgreHlmsJsonPbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,20 @@ namespace Ogre
return retVal;
}
//-----------------------------------------------------------------------------------
inline Vector4 HlmsJsonPbs::parseVector4Array( const rapidjson::Value &jsonArray )
{
Vector4 retVal( Vector4::ZERO );

const rapidjson::SizeType arraySize = std::min( 4u, jsonArray.Size() );
for( rapidjson::SizeType i=0; i<arraySize; ++i )
{
if( jsonArray[i].IsNumber() )
retVal[i] = static_cast<float>( jsonArray[i].GetDouble() );
}

return retVal;
}
//-----------------------------------------------------------------------------------
inline ColourValue HlmsJsonPbs::parseColourValueArray( const rapidjson::Value &jsonArray,
const ColourValue &defaultValue )
{
Expand Down Expand Up @@ -536,6 +550,22 @@ namespace Ogre
const rapidjson::Value &subobj = itor->value;
loadTexture( subobj, blocks, PBSM_REFLECTION, pbsDatablock, resourceGroup );
}

itor = json.FindMember( "user_values" );
if( itor != json.MemberEnd() && itor->value.IsObject() )
{
const rapidjson::Value &subobj = itor->value;
for( int i = 0; i < 3; ++i )
{
itor = subobj.FindMember( StringConverter::toString( i ).c_str() );

if( itor != subobj.MemberEnd() && itor->value.IsArray() )
{
pbsDatablock->setUserValue( static_cast<uint8>( i ),
parseVector4Array( itor->value ) );
}
}
}
}
//-----------------------------------------------------------------------------------
void HlmsJsonPbs::toQuotedStr( HlmsPbsDatablock::Workflows value, String &outString )
Expand Down Expand Up @@ -881,6 +911,40 @@ namespace Ogre

if( pbsDatablock->getTexture( PBSM_REFLECTION ) )
saveTexture( "reflection", PBSM_REFLECTION, pbsDatablock, outString );

bool bHasUserValues = false;
for( int i = 0; i < 3 && !bHasUserValues; ++i )
{
const Vector4 userValue = pbsDatablock->getUserValue( static_cast<uint8>( i ) );
if( userValue != Vector4::ZERO )
bHasUserValues = true;
}

if( bHasUserValues )
{
outString += ",\n\t\t\t\"user_values\" :\n\t\t\t{";

bool bFirstElement = true;
for( int i = 0; i < 3; ++i )
{
const Vector4 userValue = pbsDatablock->getUserValue( static_cast<uint8>( i ) );

if( userValue != Vector4::ZERO )
{
char tmpBuffer[64];
LwString keyName( LwString::FromEmptyPointer( tmpBuffer, sizeof( tmpBuffer ) ) );

if( !bFirstElement )
keyName.a( "," );
keyName.a( "\n\t\t\t\t\"", i, "\" : " );
outString += keyName.c_str();
HlmsJson::toStr( userValue, outString );

bFirstElement = false;
}
}
outString += "\n\t\t\t}";
}
}
//-----------------------------------------------------------------------------------
void HlmsJsonPbs::collectSamplerblocks( const HlmsDatablock *datablock,
Expand Down
7 changes: 7 additions & 0 deletions Docs/2.0/JSON/PbsAllSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ including invalid combinations (i.e. having a Metallic texture and a Specular te
{
"texture" : "cubemap.png",
"sampler" : "unique_name"
},

"user_values" :
{
"0" : [0.0, 0.0, 0.0, 0.0],
"1" : [0.0, 0.0, 0.0, 0.0],
"2" : [0.0, 0.0, 0.0, 0.0]
}
}
}
Expand Down

0 comments on commit 0adb2b6

Please sign in to comment.