Blending Multiple Texture Splatting Passes

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
DurbeC101
Posts: 1
Joined: Fri Oct 08, 2021 9:14 pm
Location: Philadelphia

Blending Multiple Texture Splatting Passes

Post by DurbeC101 »

Hello all!
I have been using Irrlicht on and off for a few years, but in the last few months I have really been diving into it. I did not have any familiarity with shaders before Irrlicht, so I have been getting familiar with them through the use of two illustrative examples, texture splatting and water.

I have successfully written a texture splatting shader that only uses 3 textures and a splatmap, but I am struggling with the multiple pass one.
I am using the Multitexturingmanager( from freetimcoder) viewtopic.php?f=9&t=38676&p=225314&hili ... re#p225314, and I understand how the shader part works, but I am struggling with the part that blends the passes together.

As you can see in the two images, the stone blends smoothly with the grass, because that is handled in the shader, i.e. they exist on the same splatmap. But, for the pebbles and grass, there is a alpha border in their blending region, which should be handled by the engine, not the shader, since they are part of different shader passes.

Image
Image

The texture still has regions with alpha less than 1.0, whereas I would think it should blend the textures together such that alpha is 1.0 everywhere. This is most obvious to see when my background filters through the terrain (I switch between a skybox and sky dome to see what areas are affected). I am pretty sure the culprit is the blending operations, but I am not very familiar with that, so I simply use the ones from this topic viewtopic.php?f=4&t=51151&p=296627&hili ... ng#p296627, which he says fixed his multi-texturing problems.

Initializing the shader

Code: Select all

const video::E_GPU_SHADING_LANGUAGE shadingLanguage = video::EGSL_DEFAULT;

    if (driverType == video::EDT_OPENGL) {
        psFileName = "../shaders/TexSplatting_ps.glsl";
        vsFileName = "../shaders/TexSplatting_vs.glsl";
        //Create the shader material

        shaderMaterial = driver->getGPUProgrammingServices()->addHighLevelShaderMaterialFromFiles(
            vsFileName, "main", video::EVST_VS_1_1,
            psFileName, "main", video::EPST_PS_1_1,
            this, video::EMT_ONETEXTURE_BLEND, 0, shadingLanguage);
    }
Multiple Draw Passes with new Textures

Code: Select all

for (u32 j = 0; j < array_Nodes[i].array_Passes.size(); j++) {
                array_Nodes[i].Node->setMaterialTexture(0, array_Nodes[i].array_Passes[j]->splat_texture);
                array_Nodes[i].Node->setMaterialTexture(1, array_Nodes[i].array_Passes[j]->red_texture);
                array_Nodes[i].Node->setMaterialTexture(2, array_Nodes[i].array_Passes[j]->green_texture);
                array_Nodes[i].Node->setMaterialTexture(3, array_Nodes[i].array_Passes[j]->blue_texture);
            
                if (array_Nodes[i].Node->getType() == scene::ESNT_TERRAIN) {
                    video::SMaterial material = array_Nodes[i].Node->getMaterial(0);
                    material.MaterialType = (video::E_MATERIAL_TYPE)shaderMaterial;
                    material.BlendOperation = video::EBO_ADD;
                    material.MaterialTypeParam = video::pack_textureBlendFunc(video::EBF_SRC_ALPHA, video::EBF_ONE_MINUS_SRC_ALPHA);

                    driver->setMaterial(material);
                    driver->drawMeshBuffer(((scene::ITerrainSceneNode*)array_Nodes[i].Node)->getRenderBuffer());
                }
Let me know if anything I wrote doesn't make sense. And thanks for the help!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Blending Multiple Texture Splatting Passes

Post by CuteAlien »

I see no obvious error at least, but I'm also not too familiar with that material type. Maybe it helps taking a look at the implementation - which would be COpenGLMaterialRenderer_ONETEXTURE_BLEND in COpenGLMaterialRenderer.h. At least in Irrlicht 1.8 (glsl material renderer passes through the OnSetMaterial function from the base material you set when creating the shader). In Irrlicht trunk it's a bit different as COpenGLSLMaterialRenderer's OnSetMaterial function does handle this itself now (still trying a similar setup than the base material). So results might also a bit depend on the Irrlicht version you are working with (they shouldn't, but there was a heavy rewrite in this part and another heavy fixing rewrite a few years later).

The OnSetMaterial is the stuff that sets up OpenGL, so maybe opengl documentation can help further when you look at that.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply