Shaders - How to setup so that Apha works

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!
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Shaders - How to setup so that Apha works

Post by hybrid »

The torus renders perfectly for me, even in transparent mode. Without shaders, of course, but I doubt that that's the cause. The torus has no texture coords, though, so maybe that's your problem? Not sure if you use them inside the shader.
mss
Posts: 24
Joined: Mon Oct 29, 2012 6:41 pm

Re: Shaders - How to setup so that Apha works

Post by mss »

Image

The torus is okay. I'm going to assume from here on that your shader is not working correctly because it is verified not an Irrlicht problem.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

I posted the shader code earlier in this thread, its just a simple and standard pass-through shader with external alpha input.

if ( fAlpha != 0.0 )
{
float4 col = tex2D(Texture, In.TexCoord);
Out.color = float4(col.xyz, fAlpha);
}
else
Out.color = float4(0.0, 0.0, 0.0, 0.0);

Are there any problems with this? Copied a part above where the texture coords are used...

Also, I left in the old Gooch technique pass definitions, though I don´t think they were used...
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

hendu, I read that irrlicht didn´t do occlusion rendering, I think thats what its called, and just rendered objects in z order one over the other, back-placed objects to front, so that the front objects just overwrite the back ones, but if alpha is used in the shader, then the rendering of front and back surfaces of the same mesh will be additive.

Is that what you mean by "messed up"?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Shaders - How to setup so that Apha works

Post by hybrid »

For proper transparent blending, objects have to be rendered back to front, such that the front objects can blend with the background. For solid objects, the render order does not matter, but you can save some time due to z-fail if you render front to back.
The problem with transparency arises always when objects intersect. If two transparent planes are intersecting, you cannot tell which one to draw first, as either choice would be wrong. At least on the mesh level. Of course you could do this on the poly level, where intersections are less often, but this would kill the frame rate. However, you also get the problem with just one transparent object, if polys or parts of the mesh itself intersect. In all these cases you can get wrong rendering. For most objects, this is not much of a problem, though, as you simply skip backfaces and get only very few intersects or wrong render orders. But it still can happen anywhere in these cases.
Enabling Z-Write makes this problem worse, as this would skip even more faces, and while a wrong order may still result in largely correct blending, a z-buffer discard will always remove the second blend color.
mss
Posts: 24
Joined: Mon Oct 29, 2012 6:41 pm

Re: Shaders - How to setup so that Apha works

Post by mss »

That's not the entire shader code is it?

Have you used the transparency material on the torus without the shader?

Using a texture you can use EMT_TRANSPARENT_ALPHA_CHANNEL as long as the texture itself has alpha'd pixels in it. Without a texture (or with) you would want to use EMT_TRANSPARENT_ADD_COLOR to see a basic transparency effect. EMT_TRANSPARENT_ALPHA_CHANNEL_REF is used for masks like a sniper scope image drawn over a 3d scene to give the perception you're looking through a sniper scope.

I use the transparency material add color currently for things like glass and it works just like it should.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

Not its just a snippet. The complete shader is posted on page 3 here I think.

It uses tex2d so if the torus has bad mapping, maybe that is causing the rings when I use the shader alpha value. Could anyone try to fix the torus and I can try it again?

I still can´t manage to do that with blender, as the interface makes me feel like a blind person in a 747 coclpit!

Thanks for the explanation hybrid, seems that using alpha for a "simple" 3D fade effect is more complex that seems.

Are there any examples of fade effects that solve the surface overlap issue, maybe say in Ogre?
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

Made a new torus in wavefront obj, the texture shows, but has a bad repeat pattern when tiled.

The artifacts still show though.

Any ideas?

Image
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

Same mesh not tiled:-

Image
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Shaders - How to setup so that Apha works

Post by hendu »

Is this with Z writing on or off?
mss
Posts: 24
Joined: Mon Oct 29, 2012 6:41 pm

Re: Shaders - How to setup so that Apha works

Post by mss »

The Torus model is not bad. Disable the shader but keep the built in transparency material and screenshot that.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

With built in transparency, which uses pixel blending not alpha, there are no "artifacts". However that isn´t very useful to know.

Z writing is enabled, as it is for other models that don´t show this patterning.

As I also at times used stereo glasses, I need the Zwrite enabled... I would imagine...
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Shaders - How to setup so that Apha works

Post by hendu »

Then that is causing your artifacts..
mss
Posts: 24
Joined: Mon Oct 29, 2012 6:41 pm

Re: Shaders - How to setup so that Apha works

Post by mss »

robmar wrote: With built in transparency, which uses pixel blending not alpha, there are no "artifacts". However that isn´t very useful to know.
As far as I've learned, all blending techniques are, at the base level, the same. Alpha is simply a component of a 32bit color that can be used by the blending techniques to achieve a custom transparency look.

What you're talking about, I have no idea.

Irrlicht's built-in materials use the fixed-function pipeline to achieve their effects. When you introduce a shader to your program, you're simply adding a custom function into the pipeline. With the way Irrlicht allows you to base a custom shader on a built-in material, I assume your shader would be running after the materials effects have been calculated. I would believe then if you wrote a proper shader that changed the alpha depending on some number you fed in through your program to the shader, probably the objects alpha value, you'd achieve the effect you want and the control of the alpha blending effect you want. But that's just what I've read in the massive shader manuals you can freely find on the internet anywhere and from plucking along in Irrlicht for the past 5 or so years.

From here I would tell you that your result of the test I asked you to perform is simple proof that your shader is jacked up, yo'.

Your next step is this: Write a shader that takes the value of the texture, then outputs that exact same value. No tricks, no bells, no whistles. It should take maybe less than 10 lines to do it. To me, that is a 'pass-through' shader.

Set the shader in Irrlicht to be based off the transparency material you used in the previous test I asked you to perform, and then run it and tell me what you see.

P.S. Irrlicht doesn't, on it's own, handle alpha values. It simply implements them so that you can use them to achieve your own effects if you so choose.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

I did the "next step" shaded already, and posted it here earlier on.

Maybe its just an issue with the torus, as with other meshes its okay.

I would like to find a fix for the surface overlap issue, but disabling z write meses up drawing order.

Oh well, guess it will have to wait til a solution is found.
Post Reply