Skip to content

Commit

Permalink
Initial implementation of space-dependent HwViewDirectionNode
Browse files Browse the repository at this point in the history
  • Loading branch information
edobrowo committed Sep 27, 2024
1 parent e6b9650 commit 9d6b655
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions source/MaterialXGenShader/Nodes/HwViewDirectionNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,75 @@ 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);
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);
addStageUniform(HW::PRIVATE_UNIFORMS, Type::VECTOR3, HW::T_VIEW_POSITION, ps);

const ShaderInput* spaceInput = node.getInput(SPACE);
const int space = spaceInput ? spaceInput->getValue()->asA<int>() : OBJECT_SPACE;
if (space == WORLD_SPACE)
{
addStageConnector(HW::VERTEX_DATA, Type::VECTOR3, HW::T_POSITION_WORLD, vs, ps);
addStageUniform(HW::PRIVATE_UNIFORMS, Type::VECTOR3, HW::T_VIEW_POSITION, ps);
}
else
{
addStageConnector(HW::VERTEX_DATA, Type::VECTOR3, HW::T_POSITION_OBJECT, vs, ps);
addStageUniform(HW::PRIVATE_UNIFORMS, Type::VECTOR3, HW::T_VIEW_POSITION, ps);
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);
const string prefix = shadergen.getVertexDataPrefix(vertexData);
ShaderPort* position = vertexData[HW::T_POSITION_WORLD];
if (!position->isEmitted())
if (space == WORLD_SPACE)
{
position->setEmitted();
shadergen.emitLine(prefix + position->getVariable() + " = hPositionWorld.xyz", stage);
ShaderPort* position = vertexData[HW::T_POSITION_WORLD];
if (!position->isEmitted())
{
position->setEmitted();
shadergen.emitLine(prefix + position->getVariable() + " = hPositionWorld.xyz", stage);
}
}
else
{
ShaderPort* position = vertexData[HW::T_POSITION_OBJECT];
if (!position->isEmitted())
{
position->setEmitted();
shadergen.emitLine(prefix + position->getVariable() + " = " + HW::T_IN_POSITION, stage);
}
}
}

DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
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);
if (space == WORLD_SPACE)
{
ShaderPort* position = vertexData[HW::T_POSITION_WORLD];
shadergen.emitString(" = normalize(" + prefix + position->getVariable() + " - " + HW::T_VIEW_POSITION + ")", stage);
}
else
{
ShaderPort* position = vertexData[HW::T_POSITION_OBJECT];
shadergen.emitString(" = normalize(" + prefix + position->getVariable() + " - (" + HW::T_WORLD_INVERSE_TRANSPOSE_MATRIX + " * vec4(" + HW::T_VIEW_POSITION + ", 0.0)).xyz)", stage);
}
shadergen.emitLineEnd(stage);
}
}
Expand Down

0 comments on commit 9d6b655

Please sign in to comment.