Skip to content

Commit

Permalink
Add support for viewdirection space in hardware shading (#2036)
Browse files Browse the repository at this point in the history
This addresses issue #1656.

Previously, attribute `space` in node `viewdirection_vector3` was unused by GLSL and MSL implementations. This change correctly transforms the view direction from world space to the specified space.
  • Loading branch information
edobrowo authored Oct 3, 2024
1 parent 0653e1d commit 860b8d4
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions source/MaterialXGenShader/Nodes/HwViewDirectionNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,31 @@ ShaderNodeImplPtr HwViewDirectionNode::create()
return std::make_shared<HwViewDirectionNode>();
}

void HwViewDirectionNode::createVariables(const ShaderNode&, GenContext&, Shader& shader) const
void HwViewDirectionNode::createVariables(const ShaderNode& node, GenContext&, Shader& shader) const
{
ShaderStage& vs = shader.getStage(Stage::VERTEX);
ShaderStage& ps = shader.getStage(Stage::PIXEL);

addStageInput(HW::VERTEX_INPUTS, Type::VECTOR3, HW::T_IN_POSITION, vs);
addStageConnector(HW::VERTEX_DATA, Type::VECTOR3, HW::T_POSITION_WORLD, vs, ps);

const ShaderInput* spaceInput = node.getInput(SPACE);
const int space = spaceInput ? spaceInput->getValue()->asA<int>() : OBJECT_SPACE;

addStageUniform(HW::PRIVATE_UNIFORMS, Type::VECTOR3, HW::T_VIEW_POSITION, ps);
addStageConnector(HW::VERTEX_DATA, Type::VECTOR3, HW::T_POSITION_WORLD, vs, ps);
if (space == OBJECT_SPACE || space == MODEL_SPACE)
{
addStageUniform(HW::PRIVATE_UNIFORMS, Type::MATRIX44, HW::T_WORLD_INVERSE_TRANSPOSE_MATRIX, ps);
}
}

void HwViewDirectionNode::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
const HwShaderGenerator& shadergen = static_cast<const HwShaderGenerator&>(context.getShaderGenerator());

const ShaderInput* spaceInput = node.getInput(SPACE);
const int space = spaceInput ? spaceInput->getValue()->asA<int>() : OBJECT_SPACE;

DEFINE_SHADER_STAGE(stage, Stage::VERTEX)
{
VariableBlock& vertexData = stage.getOutputBlock(HW::VERTEX_DATA);
Expand All @@ -44,10 +55,17 @@ void HwViewDirectionNode::emitFunctionCall(const ShaderNode& node, GenContext& c
{
VariableBlock& vertexData = stage.getInputBlock(HW::VERTEX_DATA);
const string prefix = shadergen.getVertexDataPrefix(vertexData);
ShaderPort* position = vertexData[HW::T_POSITION_WORLD];
shadergen.emitLineBegin(stage);
shadergen.emitOutput(node.getOutput(), true, false, context, stage);
shadergen.emitString(" = normalize(" + prefix + position->getVariable() + " - " + HW::T_VIEW_POSITION + ")", stage);
ShaderPort* position = vertexData[HW::T_POSITION_WORLD];
if (space == WORLD_SPACE)
{
shadergen.emitString(" = normalize(" + prefix + position->getVariable() + " - " + HW::T_VIEW_POSITION + ")", stage);
}
else
{
shadergen.emitString(" = normalize((" + HW::T_WORLD_INVERSE_TRANSPOSE_MATRIX + " * vec4(" + prefix + position->getVariable() + " - " + HW::T_VIEW_POSITION + ", 0.0)).xyz)", stage);
}
shadergen.emitLineEnd(stage);
}
}
Expand Down

0 comments on commit 860b8d4

Please sign in to comment.