Fading out effect with TRANPARENT_ALPHA_CHANNEL

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
Tihtinen
Posts: 76
Joined: Fri Jan 08, 2010 3:12 pm
Location: Finland

Fading out effect with TRANPARENT_ALPHA_CHANNEL

Post by Tihtinen »

Hello, I want to smoothly fade out a scenenode. Normally, I would go about this by manipulating the vertex alpha channel and having a material type of TRANSPARENT_VERTEX_ALPHA. But now I need to have transparency based on texture alpha channel and I can't go about modifying that on the fly.

To further specify the problem, I intend to fade out a billboard node with alpha channeled texture.

How would you go about this problem?
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

You could do it very simply with a shader, or creating your own material type in the irrlicht source.
nathanf534
Posts: 199
Joined: Tue Dec 09, 2008 2:55 am

Post by nathanf534 »

sorry If I am kinda stealing this thread... but

You could do it very simply with a shader
Could you tell us how you would do that?

Ive started learning glsl a little bit ago, and have no idea how you would give the shader information on how transparent the material should be at a certain time.
while(signatureEmpty){cout<<wittyComment();}
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

This is for Cg, so the syntax will be different, but it would be something like this:

Code: Select all

void fadingPixel(float2 uv : TEXCOORD0,uniform sampler2D texture,uniform float alpha,out float4 color : COLOR)
{
 color = tex2D(texture,uv)*alpha
}
And then alpha would be set in the shader callback.

I belive I remember seeing a material type that did image alpha and also considered vertex alpha, check in the code snippets forum.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

You may find this post useful: http://irrlicht.sourceforge.net/phpBB2/ ... 684#188684

If that doesn't work, and you're using D3D and not scared of modifying Irrlicht code, then you can try this material renderer:

Code: Select all

//! TRANSPARENT_DIFFUSE_ALPHA material renderer
class CD3D9MaterialRenderer_TRANSPARENT_DIFFUSE_ALPHA : public CD3D9MaterialRenderer
{
public:

	CD3D9MaterialRenderer_TRANSPARENT_DIFFUSE_ALPHA(IDirect3DDevice9* p, video::IVideoDriver* d)
		: CD3D9MaterialRenderer(p, d) {}

	virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
		bool resetAllRenderstates, IMaterialRendererServices* services)
	{
		services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);

		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
		{
			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);

			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);

			pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
			pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
			pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);

			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
			pID3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
			pID3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
		}

		pID3DDevice->SetRenderState(D3DRS_TEXTUREFACTOR, material.DiffuseColor.color);
	}

	virtual void OnUnsetMaterial()
	{
		pID3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
	}

	//! Returns if the material is transparent. The scene managment needs to know this
	//! for being able to sort the materials by opaque and transparent.
	virtual bool isTransparent() const
	{
		return true;
	}
};
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
nathanf534
Posts: 199
Joined: Tue Dec 09, 2008 2:55 am

Post by nathanf534 »

Best solution (imo) is to add an "alpha" value to scene nodes that is used in the fixed function pipeline
That is from the link you posted.

This is what I want to know how to do in opengl.

What DtD showed seems like it can only be used as a global alpha value. What if I want a different alpha value for each scene node? How would I pass that kind of information. I can't find out how to do this anywhere.
while(signatureEmpty){cout<<wittyComment();}
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

No, my method is per node. Just use a different alpha value in your callback. Remember, I'm using IrrCg, it is a bit different with GLSL and the built-in shader support in Irrlicht. IrrCg does work with OpenGL, however.

EG: This might be your callback.

Code: Select all

 void AlphaShaderCallback::OnSetConstants(IrrCg::ICgServices *gpu, CGprogram vert, CGprogram pix, const irr::video::SMaterial &mat)
 {
  gpu->EnableTexture(cgGetNamedParameter(pix,"texture"),mat.TextureLayer[0].Texture);
  gpu->setParameterf(cgGetNamedParameter(pix,"alpha"),mat.MaterialTypeParam);
 }
Then, with the material, set the "MaterialTypeParam" to your alpha value (As 0.f to 1.f, not 0 to 255.)
Last edited by DtD on Thu Jul 22, 2010 9:36 pm, edited 1 time in total.
Frank Dodd
Posts: 208
Joined: Sun Apr 02, 2006 9:20 pm

Post by Frank Dodd »

My IrrlichtWrapper contains a LOD node that changes the meshes applied to child nodes at different distances but it also has a fading effect once the node gets over a specified distance. it requires lighting to be turned on. It has some references to custom material effects I have coded into my version of the IrrlichtWrapper too (all the source for that is available as well).
Post Reply