Skip to content

Commit

Permalink
#6052: Create infrastructure to set the decal info structure through …
Browse files Browse the repository at this point in the history
…the Material interface.
  • Loading branch information
codereader committed Aug 5, 2022
1 parent fbf84b7 commit a92ea77
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
12 changes: 12 additions & 0 deletions include/ishaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ class Material
int fadeMilliSeconds;
Vector4 startColour;
Vector4 endColour;

DecalInfo() :
stayMilliSeconds(0),
fadeMilliSeconds(0),
startColour(0,0,0,0),
endColour(0,0,0,0)
{}
};

enum Coverage
Expand Down Expand Up @@ -289,6 +296,11 @@ class Material
/// Retrieves the decal info structure of this material.
virtual DecalInfo getDecalInfo() const = 0;

// Sets the decal info structure on this material.
// If the structure is not empty, it will enable the ParseFlag PF_HasDecalInfo,
// an empty/defaulted decalInfo structure will clear the flag
virtual void setDecalInfo(const DecalInfo& info) = 0;

/// Returns the coverage type of this material, also needed by the map compiler.
virtual Coverage getCoverage() const = 0;

Expand Down
6 changes: 6 additions & 0 deletions radiantcore/shaders/CShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ Material::DecalInfo CShader::getDecalInfo() const
return _template->getDecalInfo();
}

void CShader::setDecalInfo(const DecalInfo& info)
{
ensureTemplateCopy();
return _template->setDecalInfo(info);
}

Material::Coverage CShader::getCoverage() const
{
return _template->getCoverage();
Expand Down
1 change: 1 addition & 0 deletions radiantcore/shaders/CShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class CShader final :
int getSpectrum() const override;
void setSpectrum(int spectrum) override;
DecalInfo getDecalInfo() const override;
void setDecalInfo(const DecalInfo& info) override;
Coverage getCoverage() const override;
std::string getDescription() const override;
void setDescription(const std::string& description) override;
Expand Down
8 changes: 8 additions & 0 deletions radiantcore/shaders/ShaderTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ class ShaderTemplate final :
return _decalInfo;
}

void setDecalInfo(const Material::DecalInfo& info)
{
ensureParsed();
_decalInfo = info;

onTemplateChanged();
}

Material::Coverage getCoverage()
{
ensureParsed();
Expand Down
71 changes: 70 additions & 1 deletion test/MaterialExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ TEST_F(MaterialExportTest, Deform)
expectDefinitionContains(material, "deform particle2 testparticle");
}

TEST_F(MaterialExportTest, DecalInfo)
TEST_F(MaterialExportTest, DecalInfoPreservation)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/decalinfo");
expectDefinitionContains(material, "decalinfo");
Expand All @@ -287,6 +287,75 @@ TEST_F(MaterialExportTest, DecalInfo)
expectDefinitionContains(material, "decalinfo 14.3 1.5 ( 0.9 0.8 0.7 0.6 ) ( 0.5 0.5 0.4 0.3 )");
}

TEST_F(MaterialExportTest, DecalInfoManipulation)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/empty");
expectDefinitionDoesNotContain(material, "decalinfo");

EXPECT_FALSE(material->getParseFlags() & Material::PF_HasDecalInfo) << "Empty material shouldn't the decal info flag set";

// Setting a default decalInfo structure shouldn't do anything
Material::DecalInfo info;
material->setDecalInfo(info);

EXPECT_FALSE(material->getParseFlags() & Material::PF_HasDecalInfo) << "Default decalInfo shouldn't cause the flag to be set";

info.stayMilliSeconds = 10;
material->setDecalInfo(info);

EXPECT_TRUE(material->getParseFlags() & Material::PF_HasDecalInfo) << "Flag should be set now";
EXPECT_EQ(material->getDecalInfo().stayMilliSeconds, info.stayMilliSeconds);
EXPECT_EQ(material->getDecalInfo().fadeMilliSeconds, info.fadeMilliSeconds);
EXPECT_EQ(material->getDecalInfo().startColour, info.startColour);
EXPECT_EQ(material->getDecalInfo().endColour, info.endColour);

// Definition time unit is seconds
expectDefinitionContains(material, "decalinfo 0.01 0 ( 0 0 0 0 ) ( 0 0 0 0 )");

info.stayMilliSeconds = 40000;
info.fadeMilliSeconds = 10000;
info.startColour = Vector4(0.5, 0.6, 0.8, 1.0);
info.endColour = Vector4(0.1, 0.2, 0.3, 0.0);
material->setDecalInfo(info);

EXPECT_TRUE(material->getParseFlags() & Material::PF_HasDecalInfo) << "Flag should be set now";
EXPECT_EQ(material->getDecalInfo().stayMilliSeconds, info.stayMilliSeconds);
EXPECT_EQ(material->getDecalInfo().fadeMilliSeconds, info.fadeMilliSeconds);
EXPECT_EQ(material->getDecalInfo().startColour, info.startColour);
EXPECT_EQ(material->getDecalInfo().endColour, info.endColour);

expectDefinitionContains(material, "decalinfo 40 10 ( 0.5, 0.6, 0.8, 1 ) ( 0.1, 0.2, 0.3, 0 )");

// Clear the structure again
Material::DecalInfo emptyInfo;
material->setDecalInfo(emptyInfo);

EXPECT_FALSE(material->getParseFlags() & Material::PF_HasDecalInfo) << "Flag should be cleared again";

expectDefinitionDoesNotContain(material, "decalinfo");
expectDefinitionDoesNotContain(material, "decalInfo");
}

// Check that DECAL_MACRO is used where applicable
TEST_F(MaterialExportTest, DecalMacroUsage)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/empty");
EXPECT_EQ(string::trim_copy(material->getDefinition()), "");

#if 0
DECAL_MACRO
decalInfo 10 30 ( 1 1 1 10 ) ( -5 -5 -5 0 ) // fade rgb down fast (clamps to 0), but leave alpha clamped at 1 for the first nine seconds
{
blend gl_zero, gl_one_minus_src_alpha
map makealpha(textures/darkmod/decals/cracks/cannonball_hole)
clamp // we don't want it to tile if the projection extends past the bounds
vertexColor
}

expectDefinitionDoesNotContain(material, "DECAL_MACRO");
#endif
}

TEST_F(MaterialExportTest, RenderBump)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/renderBump1");
Expand Down

0 comments on commit a92ea77

Please sign in to comment.