HLSL make terrain transparent

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Donner
Posts: 87
Joined: Fri May 18, 2007 11:27 am

HLSL make terrain transparent

Post by Donner »

Hey,

I guess I'm just missing something very basic, but I just can't get a HLSL shader to make a terrain transparent.

What I want is to be able to "hide" a terrain partially by using a pixel shader and making it partially transparent. But it seems like the alpha channel is being ignored or something...

Now i broke down the problem, so the shader should only set the color of the terrain to RED (which it does), and the alpha channel to 0 (invisible) which it doesn't. It just shows a solid red terrain, although I used EMT_TRANSPARENT_ADD_COLOR as base material.

This is my HLSL shader code:

Code: Select all

float4x4 matViewProjection : ViewProjection;
 
struct VS_INPUT 
{ 
   float4 Position : POSITION0; 
   float2 alphamap : TEXCOORD0; 
   float2 tex : TEXCOORD1;
   float4 vertexcolor : COLOR0;
}; 
 
struct VS_OUTPUT 
{ 
   float4 Position : POSITION0; 
   float2 alphamap : TEXCOORD0; 
   float2 tex : TEXCOORD1;
   float4 vertexcolor : COLOR0;
};
 
struct PS_OUTPUT 
{ 
   float4 col : COLOR0; 
}; 
 
VS_OUTPUT vs_main( VS_INPUT Input ) 
{
 
   VS_OUTPUT Output;
   Output.Position = mul( Input.Position, matViewProjection );
   Output.alphamap = Input.alphamap;
   Output.tex = Input.tex;
   Output.vertexcolor = Input.vertexcolor;
 
   return( Output ); 
} 
 
PS_OUTPUT ps_main(in VS_OUTPUT input) 
{ 
   PS_OUTPUT output = (PS_OUTPUT)0;
 
   output.col.r = 255;
   output.col.g = 0;
   output.col.b = 0;
   output.col.a = 0;// seems to be ignored
 
   return output; 
}

And this is the C++-Irrlicht-Code:

Code: Select all

irr::video::SMaterial TerrainMaterial;
 
class CAlphaShaderCallBack : public irr::video::IShaderConstantSetCallBack
{
    void OnSetConstants(irr::video::IMaterialRendererServices* services, irr::s32 userData)
    {
        irr::video::IVideoDriver* tmpVideoDriver = services->getVideoDriver();
        irr::core::matrix4 worldViewProj;
        worldViewProj = tmpVideoDriver->getTransform(irr::video::ETS_PROJECTION);
        worldViewProj *= tmpVideoDriver->getTransform(irr::video::ETS_VIEW);
        worldViewProj *= tmpVideoDriver->getTransform(irr::video::ETS_WORLD);
        services->setVertexShaderConstant("matViewProjection", worldViewProj.pointer(), 16);
    }
};
 
 
int main()
{
    irr::SIrrlichtCreationParameters params;
    params.DriverType= irr::video::EDT_DIRECT3D9;
    params.WindowSize = core::dimension2d<u32>(800, 600);
    IrrlichtDevice* device = createDeviceEx(params);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
 
 
    // add camera
    scene::ICameraSceneNode* camera =
        smgr->addCameraSceneNodeFPS(0,100.0f,1.2f);
    camera->setPosition(irr::core::vector3df(0,10,0));
 
    // add terrain scene node
    scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode("terrain-heightmap.bmp");
 
    irr::io::path shaderFileName ="shader.hlsl";
 
    CAlphaShaderCallBack* tmpShaderCallback = new CAlphaShaderCallBack();
    irr::video::IGPUProgrammingServices* tmpGpu = driver->getGPUProgrammingServices();
 
    TerrainMaterial.MaterialType = (irr::video::E_MATERIAL_TYPE)(tmpGpu->addHighLevelShaderMaterialFromFiles(
                shaderFileName, "vs_main", irr::video::EVST_VS_2_0,
                shaderFileName, "ps_main", irr::video::EPST_PS_2_0,
                tmpShaderCallback, irr::video::EMT_TRANSPARENT_ADD_COLOR));
 
 
 
    tmpShaderCallback->drop();
    terrain->getMaterial(0) = TerrainMaterial;
 
    while(device->run())
        if (device->isWindowActive())
        {
            driver->beginScene(true, true, video::SColor(255, 0, 0, 100));
 
            smgr->drawAll();
            driver->endScene();
        }
 
    device->drop();
 
    return 0;
}
Last edited by Donner on Tue Oct 02, 2012 4:45 pm, edited 1 time in total.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: HLSL make terrain transparent

Post by hybrid »

You need to use EMT_TRANSPARENT_ALPHA for alpha channel transparency.
Donner
Posts: 87
Joined: Fri May 18, 2007 11:27 am

Re: HLSL make terrain transparent

Post by Donner »

Hey, thanks for the reply!

I forgot to mention, that I tried EMT_TRANSPARENT_ALPHA_CHANNEL as well, but it didn't work either...
I tried almost all the material types that seemed to make sense to me...
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: HLSL make terrain transparent

Post by mongoose7 »

I think you need blending. I haven't followed through on it so I don't know what is required, but you may need to set a material parameter and also a blend function. I think there is a XXX_ONE_ XXX texture mode.
Donner
Posts: 87
Joined: Fri May 18, 2007 11:27 am

Re: HLSL make terrain transparent

Post by Donner »

@mongoose7: Thank you for your effort!
Although I have to admit, I don't really understand what I have to do... do you mean to set the EMT_ONETEXTURE_BLEND as material type? Just tried it, it didn't work either...

Hmm, I'm wondering if it can really be that difficult to just get something transparent using a shader? Still got the feeling that I'm doing wrong something very essential...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: HLSL make terrain transparent

Post by hybrid »

Well, example 10 does almost the same that you want to achieve, and it works there with the proper base material choice only. Maybe look at that example.
Post Reply