Skip to content

Commit

Permalink
Fix matcap with tcoords (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meakk authored Jul 5, 2023
1 parent 1047726 commit 07d9cc2
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
2 changes: 2 additions & 0 deletions application/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ f3d_test(NAME TestGroupGeometriesColoring DATA mb/recursive ARGS --group-geometr
f3d_test(NAME TestInvalidUpDirection DATA suzanne.ply ARGS -g --up=W REGEXP "W is not a valid up direction" NO_BASELINE)
f3d_test(NAME TestUpDirectionNoSign DATA suzanne.ply ARGS --up=X DEFAULT_LIGHTS)
f3d_test(NAME TestTextureMatCap DATA suzanne.ply ARGS --texture-matcap=${CMAKE_SOURCE_DIR}/testing/data/skin.png DEFAULT_LIGHTS)
f3d_test(NAME TestTextureMatCapWithTCoords DATA WaterBottle.glb ARGS --geometry-only --texture-matcap=${CMAKE_SOURCE_DIR}/testing/data/skin.png DEFAULT_LIGHTS)

if (NOT APPLE)
# This test is broken on apple because of #792
Expand Down Expand Up @@ -237,6 +238,7 @@ if(VTK_VERSION VERSION_GREATER 9.0.1)
f3d_test(NAME TestLineWidth DATA cow.vtk ARGS -e --line-width=5)
f3d_test(NAME TestLineWidthFullScene DATA suzanne.obj ARGS -e --line-width=3 --up=-Y)
f3d_test(NAME TestPointCloudFullScene DATA pointsCloud.gltf ARGS --point-size=20)
f3d_test(NAME TestTextureMatCapWithEdges DATA suzanne.ply ARGS -e --texture-matcap=${CMAKE_SOURCE_DIR}/testing/data/skin.png DEFAULT_LIGHTS)

# Test enabling all animations
f3d_test(NAME TestAnimationAllAnimations DATA InterpolationTest.glb ARGS --animation-index=-1 --animation-time=1)
Expand Down
7 changes: 4 additions & 3 deletions doc/user/OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ Options|Default|Description
\-\-roughness=\<roughness\>|0.3|Set the *roughness coefficient* on the geometry (0.0-1.0). Multiplied with the material texture when present. <br>Requires a default scene.
\-\-metallic=\<metallic\>|0.0|Set the *metallic coefficient* on the geometry (0.0-1.0). Multiplied with the material texture when present. <br>Requires a default scene.
\-\-hdri=\<HDRi file\>||Set the *HDRI* image used to create the environment.<br>The environment act as a light source and is reflected on the material.<br>Valid file format are hdr, exr, png, jpg, pnm, tiff, bmp.
\-\-texture-base-color=\<texture file\>||Set the texture file to control the color of the object. Please note this will be multiplied with the color and opacity options.
\-\-texture-material=\<texture file\>||Set the texture file to control the occlusion, roughness and metallic values of the object. Please note this will be multiplied with the roughness and metallic options, which have impactful default values. To obtain true results, use \-\-roughness=1 \-\-metallic=1.
\-\-texture-emissive=\<texture file\>||Set the texture file to control the emitted light of the object. Please note this will be multiplied with the emissive factor.
\-\-texture-matcap=\<texture file\>||Set the texture file to control the material capture of the object. All other model options for surfaces are ignored if this is set. Must be in linear color space.
\-\-texture-base-color=\<texture file\>||Set the texture file to control the color of the object. Please note this will be multiplied with the color and opacity options. Must be in sRGB color space.
\-\-texture-material=\<texture file\>||Set the texture file to control the occlusion, roughness and metallic values of the object. Please note this will be multiplied with the roughness and metallic options, which have impactful default values. To obtain true results, use \-\-roughness=1 \-\-metallic=1. Must be in linear color space.
\-\-texture-emissive=\<texture file\>||Set the texture file to control the emitted light of the object. Please note this will be multiplied with the emissive factor. Must be in sRGB color space.
\-\-emissive-factor=\<R,G,B\>|1.0, 1.0, 1.0|Set the emissive factor. This value is multiplied with the emissive color when an emissive texture is present.

## Window options
Expand Down
37 changes: 34 additions & 3 deletions library/VTKExtensions/Rendering/vtkF3DPolyDataMapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,20 @@ bool vtkF3DPolyDataMapper::RenderWithMatCap(vtkActor* actor)
void vtkF3DPolyDataMapper::ReplaceShaderColor(
std::map<vtkShader::Type, vtkShader*> shaders, vtkRenderer* ren, vtkActor* actor)
{
if (!this->RenderWithMatCap(actor))
if (this->RenderWithMatCap(actor))
{
auto fragmentShader = shaders[vtkShader::Fragment];
auto FSSource = fragmentShader->GetSource();

std::string customLight = "//VTK::Color::Impl\n"
"vec2 uv = vec2(normalVCVSOutput.xy) * 0.5 + vec2(0.5,0.5);\n"
"vec3 diffuseColor = vec3(0.0);\n"
"vec3 ambientColor = texture(matcap, uv).rgb;\n";

vtkShaderProgram::Substitute(FSSource, "//VTK::Color::Impl", customLight);
fragmentShader->SetSource(FSSource);
}
else
{
this->Superclass::ReplaceShaderColor(shaders, ren, actor);
}
Expand All @@ -214,9 +227,9 @@ void vtkF3DPolyDataMapper::ReplaceShaderLight(
auto fragmentShader = shaders[vtkShader::Fragment];
auto FSSource = fragmentShader->GetSource();

// set final color to gamma-corrected ambient color
std::string customLight = "//VTK::Light::Impl\n"
"vec2 uv = vec2(normalVCVSOutput.xy) * 0.5 + vec2(0.5,0.5);\n"
"gl_FragData[0] = texture(matcap, uv);\n";
"gl_FragData[0] = vec4(pow(ambientColor, vec3(1.0/2.2)), 1.0);\n";

vtkShaderProgram::Substitute(FSSource, "//VTK::Light::Impl", customLight);
fragmentShader->SetSource(FSSource);
Expand All @@ -226,3 +239,21 @@ void vtkF3DPolyDataMapper::ReplaceShaderLight(
this->Superclass::ReplaceShaderLight(shaders, ren, actor);
}
}

//-----------------------------------------------------------------------------
void vtkF3DPolyDataMapper::ReplaceShaderTCoord(
std::map<vtkShader::Type, vtkShader*> shaders, vtkRenderer* ren, vtkActor* actor)
{
if (this->RenderWithMatCap(actor))
{
// disable default behavior of VTK with textures to avoid blending with itself
auto fragmentShader = shaders[vtkShader::Fragment];
std::string FSSource = fragmentShader->GetSource();

vtkShaderProgram::Substitute(FSSource, "//VTK::TCoord::Impl", "");

fragmentShader->SetSource(FSSource);
}

this->Superclass::ReplaceShaderTCoord(shaders, ren, actor);
}
2 changes: 2 additions & 0 deletions library/VTKExtensions/Rendering/vtkF3DPolyDataMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class vtkF3DPolyDataMapper : public vtkOpenGLPolyDataMapper
std::map<vtkShader::Type, vtkShader*> shaders, vtkRenderer* ren, vtkActor* actor) override;
void ReplaceShaderLight(
std::map<vtkShader::Type, vtkShader*> shaders, vtkRenderer* ren, vtkActor* actor) override;
void ReplaceShaderTCoord(
std::map<vtkShader::Type, vtkShader*> shaders, vtkRenderer* ren, vtkActor* actor) override;
///@}

protected:
Expand Down
4 changes: 2 additions & 2 deletions testing/baselines/TestTextureMatCap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions testing/baselines/TestTextureMatCapWithEdges.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions testing/baselines/TestTextureMatCapWithTCoords.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 07d9cc2

Please sign in to comment.