diff --git a/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.cpp b/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.cpp index 34674aa68cbd2cc53f262ebf7deeb7ac1b1a5dcd..65ce21eb628e90fa2ed74d0d8e0f188a0d141648 100644 --- a/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.cpp +++ b/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.cpp @@ -115,19 +115,21 @@ void CustomAttribToColorPass::execute( bool CustomAttribToColorPass::buildRenderTechnique( const Ra::Engine::Rendering::RenderObject* ro, Ra::Engine::Rendering::RenderTechnique& rt ) const { - if ( m_needConfigRebuild ) - { - m_needConfigRebuild = false; - Ra::Engine::Data::ShaderConfigurationFactory::removeConfiguration( - {"CustomAtt2ClrPass::CustomColorProgram"} ); - } + std::string resourcesRootDir = getResourcesDir(); auto mat = const_cast<Ra::Engine::Rendering::RenderObject*>( ro )->getMaterial(); // Volumes are not allowed here if ( mat->getMaterialAspect() == Ra::Engine::Data::Material::MaterialAspect::MAT_DENSITY ) { return false; } + + if ( m_needConfigRebuild ) + { + m_needConfigRebuild = false; + Ra::Engine::Data::ShaderConfigurationFactory::removeConfiguration( + {"CustomAtt2ClrPass::CustomColorProgram" + mat->getMaterialName()} ); + } if ( auto cfg = Ra::Engine::Data::ShaderConfigurationFactory::getConfiguration( - {"CustomAtt2ClrPass::CustomColorProgram"} ) ) + {"CustomAtt2ClrPass::CustomColorProgram" + mat->getMaterialName()} ) ) { rt.setConfiguration( *cfg, passIndex() ); } else { @@ -191,34 +193,26 @@ bool CustomAttribToColorPass::buildRenderTechnique( "vec4 computeCustomColor(Material mat, vec3 light_dir, vec3 view_dir);\n" "void main()\n" "{\n" - "// All vectors are in world space\n" - "// A material is always evaluated in the fragment local Frame \n" - "// compute matrix from World to local Frame \n" - "vec3 normalWorld = getWorldSpaceNormal();// normalized interpolated normal \n" - "vec3 tangentWorld = getWorldSpaceTangent();// normalized tangent \n" - "vec3 binormalWorld = getWorldSpaceBiTangent();// normalized bitangent \n" - "// discard non opaque fragment \n" - "vec4 bc = getDiffuseColor(material, getPerVertexTexCoord()); \n" - "if (toDiscard(material, bc)) \n" - "discard; \n" - "// Apply normal mapping \n" - "normalWorld = getNormal(material, getPerVertexTexCoord(), \n" - "normalWorld, tangentWorld, binormalWorld);// normalized bump-mapped normal \n" - "binormalWorld = normalize(cross(normalWorld, tangentWorld));// normalized " - "tangent \n" - "tangentWorld = normalize(cross(binormalWorld, normalWorld));// normalized " - "bitangent \n" - "mat3 world2local; \n" - "world2local[0] = vec3(tangentWorld.x, binormalWorld.x, normalWorld.x); \n" - "world2local[1] = vec3(tangentWorld.y, binormalWorld.y, normalWorld.y); \n" - "world2local[2] = vec3(tangentWorld.z, binormalWorld.z, normalWorld.z); \n" - "// transform all vectors in local frame so that N = (0, 0, 1); \n" - "vec3 lightDir = normalize(world2local * in_lightVector);// incident direction \n" - "vec3 viewDir = normalize(world2local * in_viewVector);// outgoing direction\n" + "vec3 normalWorld = getWorldSpaceNormal();\n" + "vec3 tangentWorld = getWorldSpaceTangent();\n" + "vec3 binormalWorld = getWorldSpaceBiTangent();\n" + "vec4 bc = getDiffuseColor(material, getPerVertexTexCoord());\n" + "if (toDiscard(material, bc)) discard;\n" + "normalWorld = getNormal(material, getPerVertexTexCoord(), " + "normalWorld, tangentWorld, binormalWorld);\n" + "binormalWorld = normalize(cross(normalWorld, tangentWorld));\n" + "tangentWorld = normalize(cross(binormalWorld, normalWorld));\n" + "mat3 world2local;\n" + "world2local[0] = vec3(tangentWorld.x, binormalWorld.x, normalWorld.x);\n" + "world2local[1] = vec3(tangentWorld.y, binormalWorld.y, normalWorld.y);\n" + "world2local[2] = vec3(tangentWorld.z, binormalWorld.z, normalWorld.z);\n" + "vec3 lightDir = normalize(world2local * in_lightVector);\n" + "vec3 viewDir = normalize(world2local * in_viewVector);\n" "out_color = computeCustomColor(material, lightDir, viewDir);\n" "}\n"}; - Ra::Engine::Data::ShaderConfiguration theConfig{"CustomAtt2ClrPass::CustomColorProgram"}; + Ra::Engine::Data::ShaderConfiguration theConfig{"CustomAtt2ClrPass::CustomColorProgram" + + mat->getMaterialName()}; theConfig.addShaderSource( Ra::Engine::Data::ShaderType::ShaderType_VERTEX, vertexShaderSource + m_customVertexAttrib ); theConfig.addShaderSource( Ra::Engine::Data::ShaderType::ShaderType_FRAGMENT, diff --git a/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.hpp b/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.hpp index 59b0e2ff6bfe342039feb28169fe3e5bbff241cf..493d7a822b085838d51c31fd8eea7e92e307005f 100644 --- a/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.hpp +++ b/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.hpp @@ -21,7 +21,7 @@ class CustomAttribToColorPass : public RenderPass { public: CustomAttribToColorPass( const std::vector<RenderObjectPtr>* objectsToRender, - const Ra::Core::Utils::Index& idx ); + const Ra::Core::Utils::Index& idx ); ~CustomAttribToColorPass() override; bool buildRenderTechnique( const Ra::Engine::Rendering::RenderObject* ro, @@ -42,7 +42,9 @@ class CustomAttribToColorPass : public RenderPass void setLightManager( const Ra::Engine::Scene::LightManager* lm ); /// Set the custom glsl function for attrib management (vertex) and colorcomputation (fragment) - void setAttribToColorFunc(const std::string &vertex_source, const std::string & fragment_source); + void setAttribToColorFunc( const std::string& vertex_source, + const std::string& fragment_source ); + private: /// The framebuffer used to render this pass std::unique_ptr<globjects::Framebuffer> m_fbo{nullptr}; @@ -54,9 +56,17 @@ class CustomAttribToColorPass : public RenderPass SharedTextures m_outputTexture; /// The custom Attrib Vertex shader function - std::string m_customVertexAttrib{}; + std::string m_customVertexAttrib{"void outputCustomAttribs(){}\n"}; + /// the custom FragmentColor shader function - std::string m_customFragmentColor{}; + /// If not changed, compute the same expression (without ao) than the light stage + std::string m_customFragmentColor{ + "vec4 computeCustomColor(Material mat, vec3 light_dir, vec3 view_dir) {\n" + "vec3 bsdf= evaluateBSDF(material, getPerVertexTexCoord(), light_dir, view_dir);\n" + "vec3 contribution = lightContributionFrom(light, getWorldSpacePosition().xyz);\n" + "return vec4(bsdf * 3.141592 * contribution, 1.0);\n" + "}\n"}; + /// State indicator mutable bool m_needConfigRebuild{true};