Skip to content

Commit

Permalink
#5532: Remember the expressions used to define the translate/scroll t…
Browse files Browse the repository at this point in the history
…ransform, since we need it to display in the material editor
  • Loading branch information
codereader committed Feb 27, 2021
1 parent 666817f commit d246d6f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/ShaderLayer.h
Expand Up @@ -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.
*/
Expand Down
4 changes: 2 additions & 2 deletions radiantcore/shaders/Doom3ShaderLayer.cpp
Expand Up @@ -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;
Expand Down
15 changes: 13 additions & 2 deletions radiantcore/shaders/Doom3ShaderLayer.h
Expand Up @@ -45,6 +45,7 @@ class Doom3ShaderLayer
Expressions _expressions;

static const IShaderExpressionPtr NULL_EXPRESSION;
static const std::size_t NOT_DEFINED = std::numeric_limits<std::size_t>::max();

// The condition register for this stage. Points to a register to be interpreted as bool.
std::size_t _condition;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions test/Materials.cpp
Expand Up @@ -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");
}

}
16 changes: 16 additions & 0 deletions test/resources/tdm/materials/parsertest.mtr
Expand Up @@ -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
}
}

0 comments on commit d246d6f

Please sign in to comment.