From 9d6b655d2f476b44bba977cc52c83be514d0fd30 Mon Sep 17 00:00:00 2001 From: edobrowo Date: Fri, 27 Sep 2024 15:11:31 -0400 Subject: [PATCH] Initial implementation of space-dependent HwViewDirectionNode --- .../Nodes/HwViewDirectionNode.cpp | 57 +++++++++++++++---- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/source/MaterialXGenShader/Nodes/HwViewDirectionNode.cpp b/source/MaterialXGenShader/Nodes/HwViewDirectionNode.cpp index 819ac93029..e97aa2f494 100644 --- a/source/MaterialXGenShader/Nodes/HwViewDirectionNode.cpp +++ b/source/MaterialXGenShader/Nodes/HwViewDirectionNode.cpp @@ -14,29 +14,56 @@ ShaderNodeImplPtr HwViewDirectionNode::create() return std::make_shared(); } -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() : 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(context.getShaderGenerator()); + const ShaderInput* spaceInput = node.getInput(SPACE); + const int space = spaceInput ? spaceInput->getValue()->asA() : 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); + } } } @@ -44,10 +71,18 @@ 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); + 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); } }