Skip to content

Commit

Permalink
#5532: Change lightFalloffImage handling, since only the last occurre…
Browse files Browse the repository at this point in the history
…nce of lightFalloffImage and lightFalloffCubeMap will actually make it into the effective map, if both are declared.
  • Loading branch information
codereader committed Mar 21, 2021
1 parent 3bc65a4 commit fdd8a4c
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 243 deletions.
13 changes: 9 additions & 4 deletions include/ishaders.h
Expand Up @@ -341,14 +341,19 @@ class Material
// will immediately mark this Material as modified.
virtual IEditableShaderLayer::Ptr getEditableLayer(std::size_t index) = 0;

/// Return the 2D light falloff texture, if this is a light shader
/// Return the light falloff texture, if this is a light shader
virtual TexturePtr lightFalloffImage() = 0;

// Return the expression of the light falloff texture for use with this shader.
// Return the expression of the light falloff map for use with this shader.
virtual shaders::IMapExpression::Ptr getLightFalloffExpression() = 0;

// Return the expression of the light falloff cubemap for use with this shader.
virtual shaders::IMapExpression::Ptr getLightFalloffCubeMapExpression() = 0;
// Return the type of the light fall off image
// (can be MapType::Map (lightFalloffImage or MapType::CameraCubeMap for lightFalloffCubeMap)
virtual IShaderLayer::MapType getLightFalloffCubeMapType() = 0;

// Set the type of the light fall off image
// (can be MapType::Map (lightFalloffImage or MapType::CameraCubeMap for lightFalloffCubeMap)
virtual void setLightFalloffCubeMapType(IShaderLayer::MapType type) = 0;

// greebo: Returns the description as defined in the material
virtual std::string getDescription() const = 0;
Expand Down
381 changes: 196 additions & 185 deletions install/ui/materialeditor.fbp

Large diffs are not rendered by default.

51 changes: 30 additions & 21 deletions install/ui/materialeditor.xrc
Expand Up @@ -668,28 +668,37 @@
</object>
</object>
<object class="sizeritem">
<option>0</option>
<flag>wxALIGN_CENTER_VERTICAL|wxEXPAND</flag>
<border>0</border>
<object class="wxTextCtrl" name="MaterialLightFalloffMap">
<value></value>
</object>
</object>
<object class="sizeritem">
<option>0</option>
<flag>wxALIGN_CENTER_VERTICAL|wxALL</flag>
<border>0</border>
<object class="wxStaticText" name="m_staticText421">
<label>lightFalloffCubeMap</label>
<wrap>-1</wrap>
</object>
</object>
<object class="sizeritem">
<option>0</option>
<flag>wxALIGN_CENTER_VERTICAL|wxALL|wxEXPAND</flag>
<option>1</option>
<flag>wxEXPAND</flag>
<border>0</border>
<object class="wxTextCtrl" name="MaterialLightFalloffCubeMap">
<value></value>
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<option>1</option>
<flag>wxALIGN_CENTER_VERTICAL</flag>
<border>0</border>
<object class="wxTextCtrl" name="MaterialLightFalloffMap">
<value></value>
</object>
</object>
<object class="sizeritem">
<option>0</option>
<flag>wxALIGN_CENTER_VERTICAL|wxLEFT</flag>
<border>6</border>
<object class="wxStaticText" name="m_staticText53">
<label>Type:</label>
<wrap>-1</wrap>
</object>
</object>
<object class="sizeritem">
<option>0</option>
<flag>wxALIGN_CENTER_VERTICAL|wxLEFT</flag>
<border>6</border>
<object class="wxChoice" name="MaterialLightFalloffCubeMapType">
<selection>0</selection>
<content />
</object>
</object>
</object>
</object>
<object class="sizeritem">
Expand Down
16 changes: 14 additions & 2 deletions radiant/ui/materials/MaterialEditor.cpp
Expand Up @@ -234,10 +234,22 @@ void MaterialEditor::setupMaterialProperties()
getControl<wxSpinCtrl>("MaterialSpectrumValue")->Enable(ev.IsChecked());
});

// For light fall off images, only cameracubemap and map are allowed
auto lightFallOffCubeMapType = getControl<wxChoice>("MaterialLightFalloffCubeMapType");
lightFallOffCubeMapType->AppendString(shaders::getStringForMapType(IShaderLayer::MapType::Map));
lightFallOffCubeMapType->AppendString(shaders::getStringForMapType(IShaderLayer::MapType::CameraCubeMap));

lightFallOffCubeMapType->Bind(wxEVT_CHOICE, [this, lightFallOffCubeMapType] (wxCommandEvent& ev)
{
if (_materialUpdateInProgress || !_material) return;
_material->setLightFalloffCubeMapType(shaders::getMapTypeForString(lightFallOffCubeMapType->GetStringSelection().ToStdString()));
});

_materialBindings.emplace(std::make_shared<SpinCtrlMaterialBinding<wxSpinCtrl>>(getControl<wxSpinCtrl>("MaterialSpectrumValue"),
[](const MaterialPtr& material) { return material->getSpectrum(); },
[this](const MaterialPtr& material, const int& value)
{
if (_materialUpdateInProgress || !_material) return;
material->setSpectrum(value);
},
[this]() { onMaterialChanged(); }));
Expand Down Expand Up @@ -1090,8 +1102,8 @@ void MaterialEditor::updateMaterialPropertiesFromMaterial()
auto lightFalloffMap = _material->getLightFalloffExpression();
getControl<wxTextCtrl>("MaterialLightFalloffMap")->SetValue(lightFalloffMap ? lightFalloffMap->getExpressionString() : "");

auto lightFalloffCubeMap = _material->getLightFalloffCubeMapExpression();
getControl<wxTextCtrl>("MaterialLightFalloffCubeMap")->SetValue(lightFalloffCubeMap ? lightFalloffCubeMap->getExpressionString() : "");
auto lightFalloffCubeMapType = _material->getLightFalloffCubeMapType();
getControl<wxChoice>("MaterialLightFalloffCubeMapType")->SetStringSelection(shaders::getStringForMapType(lightFalloffCubeMapType));

// Spectrum
bool hasSpectrum = (_material->getParseFlags() & Material::PF_HasSpectrum) != 0 || _material->getSpectrum() != 0;
Expand Down
10 changes: 8 additions & 2 deletions radiantcore/shaders/CShader.cpp
Expand Up @@ -81,9 +81,15 @@ IMapExpression::Ptr CShader::getLightFalloffExpression()
return _template->getLightFalloff();
}

IMapExpression::Ptr CShader::getLightFalloffCubeMapExpression()
IShaderLayer::MapType CShader::getLightFalloffCubeMapType()
{
return _template->getLightFalloffCubeMap();
return _template->getLightFalloffCubeMapType();
}

void CShader::setLightFalloffCubeMapType(IShaderLayer::MapType type)
{
ensureTemplateCopy();
_template->setLightFalloffCubeMapType(type);
}

/*
Expand Down
3 changes: 2 additions & 1 deletion radiantcore/shaders/CShader.h
Expand Up @@ -102,7 +102,8 @@ class CShader final :
IEditableShaderLayer::Ptr getEditableLayer(std::size_t index) override;

IMapExpression::Ptr getLightFalloffExpression() override;
IMapExpression::Ptr getLightFalloffCubeMapExpression() override;
IShaderLayer::MapType getLightFalloffCubeMapType() override;
void setLightFalloffCubeMapType(IShaderLayer::MapType type) override;
std::string getRenderBumpArguments() override;
std::string getRenderBumpFlatArguments() override;

Expand Down
6 changes: 4 additions & 2 deletions radiantcore/shaders/ShaderTemplate.cpp
Expand Up @@ -26,7 +26,7 @@ ShaderTemplate::ShaderTemplate(const ShaderTemplate& other) :
_name(other._name),
_currentLayer(new Doom3ShaderLayer(*this)),
_lightFalloff(other._lightFalloff),
_lightFalloffCubeMap(other._lightFalloffCubeMap),
_lightFalloffCubeMapType(other._lightFalloffCubeMapType),
fogLight(other.fogLight),
ambientLight(other.ambientLight),
blendLight(other.blendLight),
Expand Down Expand Up @@ -473,11 +473,13 @@ bool ShaderTemplate::parseLightKeywords(parser::DefTokeniser& tokeniser, const s
}
else if (!fogLight && token == "lightfalloffimage")
{
_lightFalloffCubeMapType = IShaderLayer::MapType::Map;
_lightFalloff = MapExpression::createForToken(tokeniser);
}
else if (token == "lightfalloffcubemap")
{
_lightFalloffCubeMap = MapExpression::createForToken(tokeniser);
_lightFalloffCubeMapType = IShaderLayer::MapType::CameraCubeMap;
_lightFalloff = MapExpression::createForToken(tokeniser);
}
else if (token == "spectrum")
{
Expand Down
59 changes: 33 additions & 26 deletions radiantcore/shaders/ShaderTemplate.h
Expand Up @@ -41,7 +41,7 @@ class ShaderTemplate final

// Map expressions
MapExpressionPtr _lightFalloff;
MapExpressionPtr _lightFalloffCubeMap;
IShaderLayer::MapType _lightFalloffCubeMapType;

/* Light type booleans */
bool fogLight;
Expand Down Expand Up @@ -107,26 +107,27 @@ class ShaderTemplate final
* \brief
* Construct a ShaderTemplate.
*/
ShaderTemplate(const std::string& name, const std::string& blockContents)
: _name(name),
_currentLayer(new Doom3ShaderLayer(*this)),
fogLight(false),
ambientLight(false),
blendLight(false),
_cubicLight(false),
_materialFlags(0),
_cullType(Material::CULL_BACK),
_clampType(CLAMP_REPEAT),
_surfaceFlags(0),
_surfaceType(Material::SURFTYPE_DEFAULT),
_deformType(Material::DEFORM_NONE),
_spectrum(0),
_sortReq(SORT_UNDEFINED), // will be set to default values after the shader has been parsed
_polygonOffset(0.0f),
_coverage(Material::MC_UNDETERMINED),
_blockContents(blockContents),
_parsed(false),
_parseFlags(0)
ShaderTemplate(const std::string& name, const std::string& blockContents) :
_name(name),
_currentLayer(new Doom3ShaderLayer(*this)),
_lightFalloffCubeMapType(IShaderLayer::MapType::Map),
fogLight(false),
ambientLight(false),
blendLight(false),
_cubicLight(false),
_materialFlags(0),
_cullType(Material::CULL_BACK),
_clampType(CLAMP_REPEAT),
_surfaceFlags(0),
_surfaceType(Material::SURFTYPE_DEFAULT),
_deformType(Material::DEFORM_NONE),
_spectrum(0),
_sortReq(SORT_UNDEFINED), // will be set to default values after the shader has been parsed
_polygonOffset(0.0f),
_coverage(Material::MC_UNDETERMINED),
_blockContents(blockContents),
_parsed(false),
_parseFlags(0)
{
_decalInfo.stayMilliSeconds = 0;
_decalInfo.fadeMilliSeconds = 0;
Expand Down Expand Up @@ -330,11 +331,17 @@ class ShaderTemplate final
return _lightFalloff;
}

const MapExpressionPtr& getLightFalloffCubeMap()
{
if (!_parsed) parseDefinition();
return _lightFalloffCubeMap;
}
IShaderLayer::MapType getLightFalloffCubeMapType()
{
if (!_parsed) parseDefinition();
return _lightFalloffCubeMapType;
}

void setLightFalloffCubeMapType(IShaderLayer::MapType type)
{
if (!_parsed) parseDefinition();
_lightFalloffCubeMapType = type;
}

std::size_t addLayer(IShaderLayer::Type type);
void removeLayer(std::size_t index);
Expand Down
19 changes: 19 additions & 0 deletions test/Materials.cpp
Expand Up @@ -787,4 +787,23 @@ TEST_F(MaterialsTest, MaterialParserRgbaExpressions)
EXPECT_EQ(diffuse->getColourExpression(IShaderLayer::COMP_ALPHA)->getExpressionString(), "time * 7.0");
}

TEST_F(MaterialsTest, MaterialParserLightfallOff)
{
auto material = GlobalMaterialManager().getMaterial("textures/parsertest/lights/lightfalloff1");

EXPECT_EQ(material->getLightFalloffCubeMapType(), IShaderLayer::MapType::Map);
EXPECT_EQ(material->getLightFalloffExpression()->getExpressionString(), "makeIntensity(lights/squarelight1a.tga)");

material = GlobalMaterialManager().getMaterial("textures/parsertest/lights/lightfalloff2");

EXPECT_EQ(material->getLightFalloffCubeMapType(), IShaderLayer::MapType::CameraCubeMap);
EXPECT_EQ(material->getLightFalloffExpression()->getExpressionString(), "lights/squarelight1a");

material = GlobalMaterialManager().getMaterial("textures/parsertest/lights/lightfalloff3");

// Second lightFallOff declaration overrides the first one in the material
EXPECT_EQ(material->getLightFalloffCubeMapType(), IShaderLayer::MapType::CameraCubeMap);
EXPECT_EQ(material->getLightFalloffExpression()->getExpressionString(), "lights/squarelight1a");
}

}
38 changes: 38 additions & 0 deletions test/resources/tdm/materials/parsertest.mtr
Expand Up @@ -450,3 +450,41 @@ textures/parsertest/colourexpr10
alpha time*7
}
}

textures/parsertest/lights/lightfalloff1
{
lightFalloffImage makeintensity( lights/squarelight1a.tga )
spectrum 2
{
forceHighQuality
map lights/fullambient.tga
colored
zeroClamp
}
}

textures/parsertest/lights/lightfalloff2
{
lightFalloffCubeMap lights/squarelight1a
spectrum 2
{
forceHighQuality
map lights/fullambient.tga
colored
zeroClamp
}
}


textures/parsertest/lights/lightfalloff3
{
lightFalloffImage makeintensity( lights/squarelight1a.tga )
lightFalloffCubeMap lights/squarelight1a
spectrum 2
{
forceHighQuality
map lights/fullambient.tga
colored
zeroClamp
}
}

0 comments on commit fdd8a4c

Please sign in to comment.