Need help with alpha shader code

Problems building or running the engine, queries about how to use features etc.
Post Reply
scottyp
Gnoblar
Posts: 24
Joined: Sat Jul 05, 2014 3:21 am
x 1

Need help with alpha shader code

Post by scottyp »

Hi all,

I just thought I'd go ahead and post on this forum as I could really get some professional help here. I tried posting this problem to other categories but I couldn't get a response for a good couple of weeks now. Maybe someone is willing to help me out here to straighten and fix this thing out once and for all.

Basically, this is a set of shader files which handles shadows and hopefully including shadows of textures with alpha on them like trees, leaves, decals and others. An internet buddy of mine sent me this code to help out but it is still not working for me.

I'm currently using a mOgre based engine so technically the compositor and shaders are the same as I examined the code.

Any ideas or revisions of this code, or if someone can point me to the right direction where I can get some help regarding this matter will be greatly appreciated.

Attached is the shader code and material (CG/HLSL/PROGRAM/MATERIAL)

Thanks and Good Day. :)
Attachments
Alpha_Shader.zip
(4.33 KiB) Downloaded 21 times
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Need help with alpha shader code

Post by tod »

scottyp wrote:An internet buddy of mine sent me this code to help out but it is still not working for me.
There are many meanings for "not working", if you want help maybe you could explain what exactly is wrong.
scottyp
Gnoblar
Posts: 24
Joined: Sat Jul 05, 2014 3:21 am
x 1

Re: Need help with alpha shader code

Post by scottyp »

Hey tod,

Yes and thanks for the quick reply. :) Apparently, the shadows are still cast without the alpha "cut-out" like when you apply a material to a plane mesh normally. I'm expecting for this type of result at least for leaves or branch textures:

Image

but instead I'm still getting full rectangular shadows on my materials and it's not casting alpha textures properly.

I have attached the last .Material that is being used for reference.

tod wrote:
scottyp wrote:An internet buddy of mine sent me this code to help out but it is still not working for me.
There are many meanings for "not working", if you want help maybe you could explain what exactly is wrong.
Attachments
Leaf.material
(1.48 KiB) Downloaded 22 times
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Need help with alpha shader code

Post by tod »

You seem to have modified the shadow cast pixel shader. The part that removes transparent pixels from the shadow texture is this:

Code: Select all

float4 diffTxt = tex2D(ourDiffTexture, In.texcoords); 
    clip(diffTxt.a - 0.3);
This discards pixels with alpha smaller than 0.3.

What I think is missing is actually associating the diffuse texture with the shadow caster program. In a normal material you have a texture unit to specify the texture, but with shadows I think you must tell Ogre to use that texture when doing shadow casting. I don't know how to do that!

But...
You can have a separate shadow caster material for your leaves, that has a texture unit in it, with the diffuse texture. It will be basically what you have, minus modifications, and with an added texture. Then, in the main material, you can specify the specific leaves_shadow_caster_material to be used by Ogre, with shadow_caster_material.
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Need help with alpha shader code

Post by tod »

Hmm, it seems your material already does this. It specifies different programs for casting/receiving shadows. It should work. You're not doing something stupid like using stencil shadows or something, are you?
scottyp
Gnoblar
Posts: 24
Joined: Sat Jul 05, 2014 3:21 am
x 1

Re: Need help with alpha shader code

Post by scottyp »

Hey tod, Thanks for the help. I really appreciate your time and effort on this problem I'm having. :D

I'm not sure if I'm using stencil shadows, I hope I'm not and how do I check on this?

I don't think I have a separate shader program for it, I assume my friend got that part omitted when he sent me this version without knowing. I was hoping someone could reconstruct or code something similar or better for this alpha shader.

Here's the C# code snippet end part if it is of any help or reference:

Code: Select all

if (pass.ShadowReceiverFragmentProgramName != "")
                {
                    keepTUCount = 1;
                    Pass.LightingEnabled = true;
                    Pass.Ambient = pass.Ambient;
                    Pass.Diffuse = pass.Diffuse;
                    Pass.Specular = pass.Specular;
                    Pass.Shininess = pass.Shininess;
                    Pass.SetRunNTimesPerLight(pass.RunOncePerLight,
                    int origPassTUCount = pass.NumTextureUnitStages;
                    for (int t = 0; t < origPassTUCount; ++t)
                    {
                        int targetIndex = t + 1;
                        TextureUnitState tex = (retPass.NumTextureUnitStages <= targetIndex ?
                                                retPass.CreateTextureUnitState() :
                                                retPass.GetTextureUnitState(targetIndex));
                        pass.GetTextureUnitState(t).CopyTo(tex);
                    }
                    keepTUCount = origPassTUCount + 1;
                   

pass.SetShadowReceiverFragmentProgram(pass.ShadowReceiverFragmentProgramName);


                    // Have to merge the shadow receiver vertex program in 
                    retPass.SetFragmentProgram(pass.ShadowReceiverFragmentProgramName);
                    GpuProgram prg = retPass.FragmentProgram;

                    // Load this program if not done already
                    if (!prg.IsLoaded)
                        prg.Load();
                    // Copy params
                    retPass.FragmentProgramParameters = pass.ShadowReceiverFragmentProgramParameters;
                    // Did we bind a shadow vertex program?
                    if (pass.HasVertexProgram && !retPass.HasVertexProgram)
                    {
                        // We didn't bind a receiver-specific program, so bind the original
                        retPass.SetVertexProgram(pass.VertexProgramName);
                        prg = retPass.VertexProgram;
                        // Load this program if required
                        if (!prg.IsLoaded)
                            prg.Load();
                        // Copy params
                        retPass.VertexProgramParameters = pass.VertexProgramParameters;
                    }
                }//end  if (pass.ShadowReceiverFragmentProgramName != "")
                else
                {                 
                    // Reset any merged fragment programs from last time
                    if (retPass == shadowTextureCustomReceiverPass)
                    {
                        // reset fp?
                        if (retPass.FragmentProgramName != shadowTextureCustomReceiverFragmentProgram)
                        {
                            retPass.SetFragmentProgram(shadowTextureCustomReceiverFragmentProgram);
                            if (retPass.HasFragmentProgram)
                                retPass.FragmentProgramParameters = shadowTextureCustomReceiverFPParams;
                        }
                    }
                    else
                        // Standard shadow receiver pass, reset to no fp
                        retPass.SetFragmentProgram("");
                }
tod wrote:Hmm, it seems your material already does this. It specifies different programs for casting/receiving shadows. It should work. You're not doing something stupid like using stencil shadows or something, are you?
Post Reply