From d246d6fea83392c089047ad1255bc8c1955cf43d Mon Sep 17 00:00:00 2001 From: codereader Date: Sat, 27 Feb 2021 07:28:34 +0100 Subject: [PATCH] #5532: Remember the expressions used to define the translate/scroll transform, since we need it to display in the material editor --- include/ShaderLayer.h | 3 +++ radiantcore/shaders/Doom3ShaderLayer.cpp | 4 ++-- radiantcore/shaders/Doom3ShaderLayer.h | 15 +++++++++++++-- test/Materials.cpp | 11 +++++++++++ test/resources/tdm/materials/parsertest.mtr | 16 ++++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/include/ShaderLayer.h b/include/ShaderLayer.h index 72e435f062..141ece271f 100644 --- a/include/ShaderLayer.h +++ b/include/ShaderLayer.h @@ -238,6 +238,9 @@ class ShaderLayer */ virtual Vector2 getTranslation() = 0; + // Returns the expression of the given translation component (0 == x, 1 == y) + virtual const shaders::IShaderExpressionPtr& getTranslationExpression(std::size_t index) = 0; + /** * Returns the value of the rotate expression of this stage. */ diff --git a/radiantcore/shaders/Doom3ShaderLayer.cpp b/radiantcore/shaders/Doom3ShaderLayer.cpp index c28a2ec988..5afcc25bb9 100644 --- a/radiantcore/shaders/Doom3ShaderLayer.cpp +++ b/radiantcore/shaders/Doom3ShaderLayer.cpp @@ -109,8 +109,8 @@ Doom3ShaderLayer::Doom3ShaderLayer(ShaderTemplate& material, ShaderLayer::Type t _scale[1] = REG_ONE; // Translation is set to 0,0 by default - _translation[0] = REG_ZERO; - _translation[1] = REG_ZERO; + _translation[0] = _translation[1] = REG_ZERO; + _translationExpression[0] = _translationExpression[1] = NOT_DEFINED; // Rotation is set to 0 by default _rotation = REG_ZERO; diff --git a/radiantcore/shaders/Doom3ShaderLayer.h b/radiantcore/shaders/Doom3ShaderLayer.h index eec04a752c..ae6f4a2677 100644 --- a/radiantcore/shaders/Doom3ShaderLayer.h +++ b/radiantcore/shaders/Doom3ShaderLayer.h @@ -45,6 +45,7 @@ class Doom3ShaderLayer Expressions _expressions; static const IShaderExpressionPtr NULL_EXPRESSION; + static const std::size_t NOT_DEFINED = std::numeric_limits::max(); // The condition register for this stage. Points to a register to be interpreted as bool. std::size_t _condition; @@ -93,6 +94,7 @@ class Doom3ShaderLayer // The register indices of this stage's translate expressions std::size_t _translation[2]; + std::size_t _translationExpression[2]; // The rotation register index std::size_t _rotation; @@ -340,13 +342,22 @@ class Doom3ShaderLayer return Vector2(_registers[_translation[0]], _registers[_translation[1]]); } + const shaders::IShaderExpressionPtr& getTranslationExpression(std::size_t index) + { + assert(index < 2); + auto expressionIndex = _translationExpression[index]; + return expressionIndex != NOT_DEFINED ? _expressions[expressionIndex] : NULL_EXPRESSION; + } + /** * Set the "translate" expressions of this stage, overwriting any previous expressions. */ void setTranslation(const IShaderExpressionPtr& xExpr, const IShaderExpressionPtr& yExpr) { - _expressions.push_back(xExpr); - _expressions.push_back(yExpr); + _translationExpression[0] = _expressions.size(); + _expressions.emplace_back(xExpr); + _translationExpression[1] = _expressions.size(); + _expressions.emplace_back(yExpr); _translation[0] = xExpr->linkToRegister(_registers); _translation[1] = yExpr->linkToRegister(_registers); diff --git a/test/Materials.cpp b/test/Materials.cpp index 618e7b764c..f77a504c1a 100644 --- a/test/Materials.cpp +++ b/test/Materials.cpp @@ -244,4 +244,15 @@ TEST_F(MaterialsTest, MaterialParserDeform) EXPECT_FALSE(material->getDeformExpression(2)); } +TEST_F(MaterialsTest, MaterialParserStageTranslate) +{ + auto material = GlobalMaterialManager().getMaterialForName("textures/parsertest/transform/translation1"); + EXPECT_EQ(material->getAllLayers().front()->getTranslationExpression(0)->getExpressionString(), "3.0"); + EXPECT_EQ(material->getAllLayers().front()->getTranslationExpression(1)->getExpressionString(), "parm3 * 3.0"); + + material = GlobalMaterialManager().getMaterialForName("textures/parsertest/transform/translation2"); + EXPECT_EQ(material->getAllLayers().front()->getTranslationExpression(0)->getExpressionString(), "time"); + EXPECT_EQ(material->getAllLayers().front()->getTranslationExpression(1)->getExpressionString(), "0.5"); +} + } diff --git a/test/resources/tdm/materials/parsertest.mtr b/test/resources/tdm/materials/parsertest.mtr index 28f74d58de..da89f65045 100644 --- a/test/resources/tdm/materials/parsertest.mtr +++ b/test/resources/tdm/materials/parsertest.mtr @@ -125,3 +125,19 @@ textures/parsertest/deform6 { deform particle2 testparticle } + +textures/parsertest/transform/translation1 +{ + { + map _white + translate 3, parm3*3 + } +} + +textures/parsertest/transform/translation2 +{ + { + map _white + scroll time, .5 + } +}