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!
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Shaders - How to setup so that Apha works

Post by hendu »

It seems you didn't do the Z-only writing as described in those pics.

Perhaps the pseudocode helps.

Code: Select all

 
for each object that should be transparent with only one surface
  disable its color writes
  draw it
  enable its color writes
  draw it
done
Any transparent objects should be drawn after anything non-transparent.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

okay but what happens if a non-transparent object partially overlaps a transparent object? Will the transparent object get drawn over the non-transparent, even though the non-transparent is in front of it?
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

Following hybrid´s comment that the artifacts when alpha was used were probably caused by the shader I was using, I stripped all but the pass-through color and alpha from the shader, and it turns out, as the image shows, that its not the shader code.

Image

Here is the shader:

Code: Select all

/*
 
Simple shader with Alpha.
 
keywords: Alpha
date: 20130109
 
*/
 
// GLSL Vertex shader
 
 
float4x4 mWorld;
float4x4 mWorldViewProj: WorldViewProjection;
float4x4 mView : WorldView;
float4x4 mWorldInverseTranspose;
 
float   fAlpha;
float3  LightPosition;  // (0.0, 10.0, 4.0)
float3  lightVec;
 
texture _texture;
 
sampler Texture = 
sampler_state
{
 Texture= <_texture>;
};
 
struct VS_INPUT
{
    float4 position:POSITION;
    float3 normal:NORMAL;
    float2 TexCoord:TEXCOORD0;  
    float3 tangent:TANGENT;
};
 
struct VS_OUTPUT
{
    float4 position:POSITION;
    float2 TexCoord:TEXCOORD0;  
};
 
struct PS_OUTPUT
{
    float4 color:COLOR;
};
 
VS_OUTPUT mainVS(VS_INPUT In)
{
    VS_OUTPUT Out;
 
    Out.position = mul(In.position, mWorldViewProj);
 
    Out.TexCoord = In.TexCoord; 
 
    return Out;
}
 
PS_OUTPUT mainPS(VS_OUTPUT In)
{   
    PS_OUTPUT Out;  
    
    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);
 
    return Out;  
}
 
technique Gooch {
    pass p0 {
        CullMode = None;
        VertexShader = compile vs_2_0 mainVS();
        PixelShader = compile ps_2_0 mainPS();
    }
}
technique GoochforEight {
    pass p0 {
        CullMode = None;    
        PixelShader = compile ps_2_0 mainPS();
        SrcBlend=Srcalpha;
        destblend=invsrcalpha;
    }
}
 
Last edited by robmar on Wed Jan 09, 2013 5:57 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Shaders - How to setup so that Apha works

Post by CuteAlien »

Don't blame Hybrid... if you click on "edit" in your post you will see that your link is still there unchanged. It's simply not in a format recognized by the forum.
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
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

You´re right, sorry hybrid!! :oops:

Anyway, why are these variations in transparency occuring with this shader and alpha, when the built-in transparency works just fine on the donut, with no artifacts?

I´m guess the transparency effect is when the alpha override is used on the stock shader to set a mid level transparency.

It would be useful to modify the irrlicht internal shader so that each material could have an alpha variable...
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Shaders - How to setup so that Apha works

Post by hendu »

robmar wrote:okay but what happens if a non-transparent object partially overlaps a transparent object? Will the transparent object get drawn over the non-transparent, even though the non-transparent is in front of it?
Everything will be in correct order even with overlaps. The Z buffer is taking care of that.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

The problem is the inefficiency doing multiple renders, and my frame rate is already down to 20 when everything is loaded.

If I use EMT_TRANSPARENT_ADD_COLOR and the standard Irrlicht renderer, there are no artifacts, but I guess thats adding colors not using the alpha.

Why should a torus mesh cause these artifacts (not just a torus mesh, but also the coke can mesh I tried)?

And its not my shader, as why should the simplest of shaders have problems with these artifacts, just simply passing the textels through the shader and only changing the alpha value?

This is really what I would like to understand, why is this happening?

Where is this the standard shader for D3D 9? and its OnSetConstants...
I´ve found a number of shaders, not sure which is which yet...
"+mov r0.a, v0.a ; write interpolated vertex alpha value \n"\

I´d like to test patching the engine so that an alpha value can be set in each material which the standard shaders use, to give an transparency control.
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 »

But the artifacts are also not parts of the polygons. They are far too smooth - otherwise you'd have more than some 100k polys for this torus. So I still suspect something in the mesh if it's not the shader.
And who blamed me? If it's still about this empty post with just one broken link: Yes, I edited it as it was empty besides the broken img tag. I tried to replace the colon by the URL escape sequence, but had no luck with that either. But the img was broken before (and you already posted the link as url link already before my edit, so you seem to be aware of the img tag problems...)
mss
Posts: 24
Joined: Mon Oct 29, 2012 6:41 pm

Re: Shaders - How to setup so that Apha works

Post by mss »

What torus mesh are you using? Upload it. I think something is wrong with it.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Shaders - How to setup so that Apha works

Post by hendu »

You cannot get a 2d-cutout-like transparency with a 3d model without something like I described.

Z-only rendering is 2x the speed of normal rendering; even more if you use simpler shaders for that pass.
Why should a torus mesh cause these artifacts (not just a torus mesh, but also the coke can mesh I tried)?
If you enable Z-writing for a transparent part, you will always get artifacts. I get tired of repeating this ;)

The smaller artifacts you get when Z-writing is properly off, are due to in-mesh triangle ordering, and the following:
And its not my shader, as why should the simplest of shaders have problems with these artifacts, just simply passing the textels through the shader and only changing the alpha value?

This is really what I would like to understand, why is this happening?
More than one surface. It's not a flat cardboard made to look like it's 3d, it's a real 3d "glass" object with more surfaces than one.
mss
Posts: 24
Joined: Mon Oct 29, 2012 6:41 pm

Re: Shaders - How to setup so that Apha works

Post by mss »

hendu, those "artifacts" from the picture in the above post is from extra polies inside the torus that shouldn't be there. I'm pretty sure of it. I use the transparent material(s) often enough for glass objects and there have never been any undesired artifacts. I make all my models myself so I know what they're made of and how. When using Irrlicht (or any other 3d engine) you will eventually realize: even if you're just the programmer -- you WILL HAVE TO LEARN 3D MODELLING. There is no way you will be able to properly take advantage of the technology without that basic knowledge.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Shaders - How to setup so that Apha works

Post by hendu »

How nice of you to imply ;) I bet I have more experience than you. (I too can do ad-hom!)

Absolutely I could be wrong. Let's wait for the model to be posted.
mss
Posts: 24
Joined: Mon Oct 29, 2012 6:41 pm

Re: Shaders - How to setup so that Apha works

Post by mss »

hendu wrote:How nice of you to imply ;) I bet I have more experience than you. (I too can do ad-hom!)

Absolutely I could be wrong. Let's wait for the model to be posted.
LOL, you're right. I meant no disrespect in it. For all I know you could be a professional 3d modeler. But I really meant it for the poster more than for you.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Shaders - How to setup so that Apha works

Post by robmar »

Thanks for all the comments, which I just saw. Didn´t see the notify email...

Good point mss, so any recommendations for a user-friendly 3D modelling package for non-modellers with an intuitive interface? (Blender is great but the interface seems so cryptic!)

hendu: "If you enable Z-writing for a transparent part, you will always get artifacts. I get tired of repeating this ;)"

Why will there always be artifacts, why shouldn´t rendering work for any shading method...?

I have uploaded the torus available here if anyone is interested:- http://www.sendspace.com/file/hfqf21
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Shaders - How to setup so that Apha works

Post by hendu »

Because of the inner triangle ordering. Say the back triangle gets drawn before the one in front of it - you get two surfaces. If the front triangle is drawn first, you only get one surface, because you enabled Z-writing.

I hope this clarifies it - you get unpredictable results if you do that.
Post Reply