Best way to change clothing on a billboard sprite?

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
Markuss
Posts: 20
Joined: Tue May 02, 2023 4:48 pm

Best way to change clothing on a billboard sprite?

Post by Markuss »

Ive got an iBillboardSceneNode that is my player avatar in a 3d world. My sprite always faces the camera like the sprites in the original doom. I have layers of armor and weapons that ld like to overlay as they are equipped and unequipped. Is the best way to do this via having other billboards in front of my sprite? That way if I want to put on armor I just change the texture on the billboard that covers my avatar's billboard?
Maybe there is another better method that allows me to blend textures together and then put the mixed texture on my sprite billboard?

Thanks!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Best way to change clothing on a billboard sprite?

Post by CuteAlien »

Best solution for this is most likely to work with a shader material so you can code the combination of several textures yourself. For this specific case it's probably not yet that complicated to write the shader. If you use OpenGL or D3D9 you can start by copying the shaders from the Irrlicht shader example (for Burnings Software renderer maybe as well, somehow it seems to support some GLSL by now). Then figure out how to send more textures (if you tell me which driver you want to support I can probably tell you how to do that, as usual there's more than one option of doing it). Then code the combination of textures, like check if alpha color in texture2 is > 0 then use texture2 otherwise texture1. Pixel shader code is run for every pixel so no loops or any complicated stuff needed. Take a look at media/opengl.frag to see how trivial pixel shaders can be (the slightly more complex vertex shader can just be copied for your case). sampler2D is the thing that reads your texture, you will need more of those then. Up to 4 textures are easy with Irrlicht (with Irrlicht svn trunk up to 8 textures). After that Irrlicht is sadly getting somewhat complicated.

Trying to solve this with fixed function pipeline (without shader code) and single billboards is a bit tricky. I don't think any of the default materials supports this specific case already. It might be possible somehow with blend functions and drawing sprites which never overlap (so the replaced parts are black on the base sprite), but it restricts yourself a lot and not even sure if it would work.

Solving it with several nodes drawn over each other could be done. But if speed matters that will get slow (after a few thousand nodes). Also you will have to work around the SceneManager for this: The target would be to draw the nodes at the exact same position but your own custom order. Then draw the later nodes (those with the textures on top) by ignoring the zbuffer (SMaterial.ZBuffer set to irr::video::ECFN_DISABLED) so it overwrites earlier pixels. Sadly controlling the order in which your nodes are rendered isn't possible with the SceneManager and BillBoards. Billboards always register automatically as solid or transparent which are both sorted every frame in drawAll. So for this to work you have to work around the SceneManager:

One way to do that is using custom scene nodes. Meaning you copy the billboard code and then start modifying it. For example in the render function you could call the "driver->setMaterial(Buffer->Material); driver->drawMeshBuffer(Buffer);" several times but switching out the first texture before changing the material and disabling zbuffer from the second texture on.

Another way is not using the SceneManager at all, but calling render() for your BillboardSceneNodes directly yourself.Then you have full control over the order (but you lose some features like automatic clipping nodes). Again first billboard would be rendered as usual and following ones (with textures overwriting) have zbuffer disabled.

But really - shaders will power you up way more! Can be a bit more work to get started (but you can always ask here). After that they are totally worth it as you have so much more control over what is going on. For example later you might want to color sprites or add effects and so on. All that is super hard/nearly impossible without shader code and can be easy with pixel shaders.
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
Markuss
Posts: 20
Joined: Tue May 02, 2023 4:48 pm

Re: Best way to change clothing on a billboard sprite?

Post by Markuss »

Thanks! I give the shader method a try!
Post Reply