Blend and shaders

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!
Post Reply
Metalero
Posts: 14
Joined: Tue Sep 07, 2010 4:02 pm

Blend and shaders

Post by Metalero »

I am using pixel shader to apply some effects to a material´s texture.
Now, I also need to make some blending stuff (addition and substractions) wich involves using a material like EMT_ONETEXTURE_BLEND, but if I use that material, I can´t use my shader

The question is:

Is there any way to:

Combine EMT_ONETEXTURE_BLEND with my Pixel shader material?

or

apply blending effects using only pixel shader?

thanks.
porcus
Posts: 149
Joined: Sun May 27, 2007 6:24 pm
Location: Germany

Post by porcus »

You can use something like this in your Pixel Shader:

color=blend*color1+(1.0-blend)*color2

blend must be a value between 0 and 1.
Metalero
Posts: 14
Joined: Tue Sep 07, 2010 4:02 pm

Post by Metalero »

color=blend*color1+(1.0-blend)*color2
But, how do I get Color2 ?

My shader is somethig like :

Code: Select all

"[... extern variables for color effects...]                      \															
sampler2D tex0;	                                                \																																					
		                                                                \
float4 main( float2 texCoord : TEXCOORD0): COLOR0	\							
{										\										
     float4 color = tex2D( tex0, texCoord.xy );	        \								
    [...operations with variables, for color effects...]       \
    return color;								\			
}";
Is any way to get that "color2" wich in my case is the color of wathever is behind the mesh (where is the texture I´m applying this pixel shader)?
porcus
Posts: 149
Joined: Sun May 27, 2007 6:24 pm
Location: Germany

Post by porcus »

I'm not really sure what exactly you are trying to do.
(BTW I also just read in the api doc that EMT_ONETEXTURE_BLEND uses
a different function. The function I posted was just the function I usually
use.)
If you are trying what I guess (fading a texture) I would use (1,1,1,1)
for color2.
Metalero
Posts: 14
Joined: Tue Sep 07, 2010 4:02 pm

Post by Metalero »

No, I´m trying to implement some 2D stuff, using 3D.

I´m using a shader to mix opacity control, color adding, and an effect called tone, wich is similar to color adding, but calculations are different and adds a Grey component to the texture.

Now, I need to set 3 blending options:

None: texture is shown "as is"
Additive : colors of the texture are added to colors of wathever is behind
Substractive: colors of wathever is behind are substracted by colors of the texture

You talk abuou color1 and color2.

In my case, color1 "are" color of every pixel in the texture, and color2 (wich I don´t kwnow how to get) is the color of pixels wish are behind of my texture.
porcus
Posts: 149
Joined: Sun May 27, 2007 6:24 pm
Location: Germany

Post by porcus »

I think you would have to use rendertargets for that. Just render every image individual into the rtt and also use the rtt for a texture layer > 0. In glsl (I don't know if there's an equivalent in hlsl) you can use gl_FragCoord to get the screen coordinates of the current fragment/pixel. You can get the texture coordinates on the rtt by dividing the x and y components by the screen's width-1 and height-1. That means you can get color2 from the rtt at the coordinates (x/(width-1),y/(height-1)).
Post Reply