Skip to main content
GameDev.net gamedev.net
🔒 Locked

HLSL Shader hell: Alpha Blending

Started by reuomi May 13, 2005 at 2:23 PM 18 replies 17k views
Original Post
reuomi
reuomi
I don't get it, i'm very good with C++, but this HLSL is so freaking out there for me, i don't even know where to begin. I want to at least to Alpha bendign for transparency effects, but i cant find a tutorial that i can get my head around. I use Maya 5, and the december edition of directx 9. I need somthign to grab onto, i've read and read and have found very little to help me start. god somone help me
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Jiia
Jiia
edit: I removed the punk that was laughing here

More detail is needed to understand what you're trying to do. You mention HLSL shaders and alpha blending. What type of alpha blending are you wanting? You don't need shaders to impliment alpha blending effects. Your vertices, material colors, lights, and textures can all hold alpha data to make things transparent.

Are you wanting to do an advanced alpha routine using a pixel shader? If not, I doubt you should be looking into shaders to accomplish it. Explain more clearly about what your goal is?

[Edited by - Jiia on May 15, 2005 9:13:32 PM]
Guimo
Guimo
Hi,
I'd suggest you use RenderMonkey or FXComposer in order to test and get the basics of shaders and FX files. Once you understand what happens in the shader, you can add effects in your game but knowing what is really going on.

Luck!
Guimo
reuomi
reuomi
sorry about my vageness, I have just been so fustrated. lets see i'll find an image to show the kidn of effect i want to accomplish

I am wanting to cut away zones of a texture to make things such as leafs and hair like the pitures below.

Leaves:
http://www.3dmodelworks.com/models/thumb/3dplantscom/LeafyGroundcover/leafygc_pict.jpg


Hair:
Polygon- http://www.newtek.com/products/lightwave/tutorials/modeling/hair/images/Image11.gif

Using this texture- http://www.newtek.com/products/lightwave/tutorials/modeling/hair/images/Image13.gif

Gainign this finnal effect:
look at the hair- http://www.newtek.com/products/lightwave/tutorials/modeling/hair/images/Image16.gif
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Nik02
Nik02
You need to set the alpha blending renderstates of the device for the pipeline's alpha values to have any effect. HLSL has nothing to do this, although you can definitely calculate the alpha values in a HLSL shader.
Niko Suni
reuomi
reuomi
I feel really imbarassed but i'm not sure how to do what you said Nik02. i'm a major beginner. The thing is since i use Maya's X plugin it requires me to use the DX shader and attach a .FX file to it to do any effects. Is what you said do-able through a .fx file? any tutorials over this stuff would be helpfull if you can find me some links you would really be doing me a huge favor.
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Nik02
Nik02
Quote:
Original post by reuomi
I feel really imbarassed but i'm not sure how to do what you said Nik02. i'm a major beginner. The thing is since i use Maya's X plugin it requires me to use the DX shader and attach a .FX file to it to do any effects. Is what you said do-able through a .fx file? any tutorials over this stuff would be helpfull if you can find me some links you would really be doing me a huge favor.


Yes, renderstates are generally accessible within .fx files. Set AlphaBlendEnable to true inside either a technique or a pass block. Also, DestBlend and SrcBlend should be set to meaningful values; invsrcalpha and srcalpha respectively enable the standard alpha blending equation where the destination and source colors are linearly interpolated by the alpha value of source pixels.

If you have anything to do with .fx shader programming at all, I recommend downloading the DirectX SDK from MSDN DirectX page. It is the official reference material of DirectX programming, and also contains tutorials regarding usage of .fx framework.
Niko Suni
reuomi
reuomi
I've been usign the directx 9 sdk for a while. Sadly seeing as i have a low end graphics card (NVIDIA GeForce MX 440) a lot of the effects of the tutorials in the sdk are outside my devices ability (according to effectedit)

I don't need much, and i wish i could get my head aroudn this faster it's discuraging, but if anyone could find me, or give me a simple demonstration of a FX file that could produce the results liek i showed above, I would be beyond greatfull.

Thankyou
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Jiia
Jiia
Quote:
Original post by reuomi
I don't need much, and i wish i could get my head aroudn this faster it's discuraging, but if anyone could find me, or give me a simple demonstration of a FX file that could produce the results liek i showed above, I would be beyond greatfull.


I don't understand why you need to do this inside of the fx file? I'm not saying you don't, I just don't understand why.

That said, all you have to do to accomplish this normally is set a few texture stages and render. If your texture contains both color and transparency, I don't think anything needs done at all, other than turning on alpha blending:

Device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE / FALSE ).

So if you load your hair texture in as a complete texture (such as PNG, TGA, or DDS), you just turn that on, set the texture, and render.

Like I said, I don't understand why you're confined to .fx files, so I'm sorry if I'm just wasting your time.
reuomi
reuomi
Sorry, you arn't wateing my time, i'm just in a hole and all. I prefered a FX file becuase with the new directx plug-in for maya it alows me to combien a fx file direftly to a set of polygons through a DX shader and test it before even exporting. but it's becoming more and more obvious the simplier way would to be just doing it outside a fx file in code. well thank you, and i'm sure i annoyed a lot of people with my newbeness, but thats for still sticking with me, i am very greatful.
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
reuomi
reuomi
Thanks everyone after goign over a few sugestions i managed to reach the effect i wanted (i hope) but thanks alot. Just figured i should say so.
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Armadon
Armadon
I would generally setup a nice little scene. Get it working just right using the FFP (Fixed Function Pipeline) and then implement a simple diffuse shader... which would look something like this

float4x4 ModelViewProj;float4 lightPos;struct a2v{ 	float4 position : POSITION;	float4 normal : NORMAL;	float4 tex : TEXCOORD0;};struct v2p{	float4 position : POSITION;	float4 color : COLOR0;	float4 tex : TEXCOORD0;};void main( in a2v IN, out v2p OUT ) {	OUT.position = mul(IN.position, ModelViewProj);	float4 light = lit(dot(normalize((lightPos - IN.position)), IN.normal), 0, 10);	OUT.color = light.yyyz;	OUT.tex = IN.tex;}technique test{	pass p0	{		vertexshader = compile vs_2_0 main();	}}


The input that you need is a simple light that you pass to the shader

//set the light vector to your shader variablepEffect->SetVector("lightPos", &CLightSphere::getInstance()->getPosition());//also pass your modelview projection matrixpEffect->SetMatrix("ModelViewProj", &ModelViewProj);


I hope this helps a bit.
The only way I learnt was to play around with them until I knew what was going on.
Also the SDK docs has some nice explanations on the semantics etc.. and their shader demo's.
reuomi
reuomi
Thanks, I looked over what was said and Niko's idea worked perfectly, thants everyone, the advice worked out awsome
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
reuomi
reuomi
Well, i spoke too soon, seems that even though the effect worked fine in effect editor, it wont render in mesh viewer or in a directx project. I probably made my first mistake by using the "Normal.fx" sample and modifying it to alpha blend. At this point i'm abotu to just go streight to code and do it through there and forget about .fx files. But still I am hesitant, because though it seems liek a pain, if i could do it this way, i could isolate textures and effects to their individual .x files and not have to wory about coding them by hand directly into the application. If you can find where i migth have went wrong (and I'm sure you will) i'd apreiciate it, i've gone over the sdk documentation and i thought i had it, but sadly i didn't.

-thanks.
 texture texture1 < string name = "tiger\\Red_Hair.dds"; >;//texture containing //alpha values// Here is a vector parameter representing the direction// vector from a light source.  The UIDirectional annotation// tells EffectEdit to create a user interface element that can// be used to interactively change the light direction.float3 lightDir<> = {0.577, -0.577, 0.577};string XFile = "tiger\\tiger.x"; // Model to loadtechnique tec0{    pass p0    {        // Set up reasonable material defaults        MaterialAmbient = {0.0, 0.0, 0.0, 0.0};         MaterialDiffuse = {1.0, 1.0, 1.0, 1.0};         MaterialSpecular = {0.0, 0.0, 0.0, 0.0};         MaterialPower = 1000.0f;                // Set up one directional light        LightType[0]      = 3;        LightDiffuse[0]   = {1.0, 1.0, 1.0, 1.0};        LightSpecular[0]  = {10.0, 10.0, 10.0, 1.0};         LightAmbient[0]   = {0.1, 0.1, 0.1, 1.0};        LightDirection[0] = <lightDir>; // Use the vector parameter defined above        LightRange[0]     = 10.0;                // Turn lighting on and use light 0        LightEnable[0] = True;        Lighting = True;        SpecularEnable = True;                // Set up texture stage 0        Texture[0] = <texture1>; // Use the texture parameter defined above        ColorOp[0] = Modulate;        ColorArg1[0] = Texture;        ColorArg2[0] = Diffuse;        AlphaOp[0] = Modulate;        AlphaArg1[0] = Texture;        AlphaArg2[0] = Diffuse;        MinFilter[0] = Linear;        MagFilter[0] = Linear;        MipFilter[0] = Linear;                // Disable texture stage 1        AlphaBlendEnable =true; //where i started the alpha addition        DestBlend = 6;        SrcBlend = 5;        ColorOp[1] = Disable;        AlphaOp[1] = Disable;    } }


[Edited by - reuomi on May 15, 2005 9:59:43 PM]
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Nik02
Nik02
Quote:
Original post by reuomi
Well, i spoke too soon, seems that even though the effect worked fine in effect editor, it wont render in mesh viewer or in a directx project.


Mesh viewer doesn't consider external .fx files. A better program for prototyping .fx files is the Effect Edit.

Your effect file should work - I tested it without modification in Effect Edit, just used my own texture. Make sure that the source texture has an alpha channel with meaningful values (that is, actual transparency and not 0's or 1's all the way). In fact, can you post the texture you're using as .png, if EffectEdit doesn't show the desired results?

Don't give up yet, you're close to the solution [smile]
Niko Suni
reuomi
reuomi
Hello, Photoshop doesn't export Alpha values in PNG format so i converted my dds to bmp. hope it works.

http://i4.photobucket.com/albums/y137/Reuomi/Red_Hair.bmp
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Nik02
Nik02
Quote:
Original post by reuomi
Hello, Photoshop doesn't export Alpha values in PNG format so i converted my dds to bmp. hope it works.

http://i4.photobucket.com/albums/y137/Reuomi/Red_Hair.bmp


BMP doesn't support alpha channels at all. The original DDS is probably a better format then.

By the way, Photoshop does support transparency in PNG files, but not thru separate alpha channels. If you have transparent background (that is, light-gray/white tiles, by default) in your image, the resulting PNG will contain the alpha values defined by the transparency.
Niko Suni
reuomi
reuomi
thanks, i'll give it a shot and post back. but the bmp worked just fine.. became transparent, oh well, i'll test that.
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
reuomi
reuomi
Alright all is good, i'll have the png link below. But before that i feel i should restate my problem.


When writing the effect file above it seemed to work perfectly in effect editor. So i went to maya, took a simple plane and applyed a DX shader and attached the fx file to that shader as instructed in the sdk documentation. I then exported the new mesh to .x format and ran the .x file in MEsh viewer, nothign happend, i switched to view edges and the plane showed up and was pivitable and moveable compleately but the entire object was invisable, even when i loaded the x file into a a working app (checked with other .x file it worked) and it rendered the invisable plane, but i don't understand how it happend. But the effect does work, somthign happend when i exported for maya i think, because the x fiel wouldn't render the texture correctly if at all.


http://i4.photobucket.com/albums/y137/Reuomi/Red_Hair.png
Indy to Pro: Journeyhttp://phiendstudios.blogspot.com
Nik02
Nik02
Quote:
Original post by reuomi
Alright all is good, i'll have the png link below. But before that i feel i should restate my problem.


When writing the effect file above it seemed to work perfectly in effect editor. So i went to maya, took a simple plane and applyed a DX shader and attached the fx file to that shader as instructed in the sdk documentation. I then exported the new mesh to .x format and ran the .x file in MEsh viewer, nothign happend, i switched to view edges and the plane showed up and was pivitable and moveable compleately but the entire object was invisable, even when i loaded the x file into a a working app (checked with other .x file it worked) and it rendered the invisable plane, but i don't understand how it happend. But the effect does work, somthign happend when i exported for maya i think, because the x fiel wouldn't render the texture correctly if at all.


http://i4.photobucket.com/albums/y137/Reuomi/Red_Hair.png


I suspect that the problem you are experiencing is an issue related to Maya effect handling, then. Can't say for sure, because I don't use Maya very often, just at my client's if absolutely needed. The PNG you gave the link to does have a proper transparency channel.

You can control the Effect interfaces in your program independently from the effect instances stored in the mesh - maybe you can use this fact as a workaround (you do lose little convenience this way, though).

If you need help implementing the workaround, just ask!
Niko Suni

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.