5 Replies - 874 Views - Last Post: 28 April 2011 - 09:15 PM

#1 angrydwarfz  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 60
  • Joined: 07-December 08

Effect file trouble

Posted 22 April 2011 - 05:28 AM

Hello, DreamInCode!

I was messing around with some effect files, trying to make an effect that overlays every pixel of a sprite with a certain color, while maintaining its transparency. I wrote this HLSL file, but no matter what color I set the flashColor parameter to (using the effect.Parameters['flashColor'].SetValue(new Vector3(1,0,0)) method), the color the sprite gets drawn with is still white!

sampler TextureSampler : register(s0);

Float intensity;
float3 flashColor;

float4 main(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
    float4 tex = tex2D(TextureSampler, texCoord);

	float4 newCol = (flashColor.r, flashColor.g, flashColor.b, color.a * tex.a);

	float4 final = lerp(tex.rgba, newCol, intensity);

	return final;
}


technique ToFlashColor
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 main();
    }
}



Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Effect file trouble

#2 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Effect file trouble

Posted 26 April 2011 - 08:36 PM

Here's a quick modification of your code that works for me:

main(float 4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
    float4 tex = tex2D(TextureSampler, texCoord);
    float4 newCol = float4(flashColor.r, flashColor.g, flashColor.b, tex.a);

    return lerp(tex, newCol, intensity);
}



All I really changed was this line:
float4 newCol = float4(flashColor.r, flashColor.g, flashColor.b, tex.a);

Was This Post Helpful? 2
  • +
  • -

#3 angrydwarfz  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 60
  • Joined: 07-December 08

Re: Effect file trouble

Posted 27 April 2011 - 10:26 AM

Thank you very much, it works perfectly now. I had forgotten the actual method name for the float4 constructor. And I was also multiplying the pixel's alpha with the blend color's alpha (which didn't make sense since (I think) you couldn't really see the effect).

What I'm wondering about is why I didn't get an error message for this line:
float4 newCol = (flashColor.r, flashColor.g, flashColor.b, color.a * tex.a);


Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Effect file trouble

Posted 27 April 2011 - 12:14 PM

I'm not sure why.

You could also do this (which is what I thought you were trying to do at first :P ):
float4 newCol = { flashColor.r, flashColor.g, flashColor.b, tex.a };

Was This Post Helpful? 0
  • +
  • -

#5 angrydwarfz  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 60
  • Joined: 07-December 08

Re: Effect file trouble

Posted 28 April 2011 - 08:21 AM

After reading through my code a few times I found out what I initially intended to do, haha. The thing is, the intensity parameter was there only to regulate the overall intensity of the effect. The spritebatch that drew all the flashing sprites drew lots of sprites which were in different moments of the flashing animations. That's why I wanted to multiply the intensity of the effect with the alpha of the blend color. After fixing the call of the spritebatch draw (I was blending with a Color(r,g,b,a) instead of Color(r,g,B)*a) and modifying the shader code a little, it all works awesomely now. :D

This is how the color creation line finally ended up looking like :D :
float4 newCol = float4(flashColor.r, flashColor.g, flashColor.b, color.a * tex.a * intensity);


Heh, I'm learning new stuff every day, which is awesome!
Also, here's the effect in action! (Ignore the unfinished enemy sprite)
Attached Image
Was This Post Helpful? 0
  • +
  • -

#6 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Effect file trouble

Posted 28 April 2011 - 09:15 PM

Nice work, I'm glad you got it working the way you wanted. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1