Jump to content

  • Log In with Google      Sign In   
  • Create Account


HLSL Pixel Shader that does palette swap


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 21 November 2012 - 09:40 PM

Hi there,

I have implemented a simple pixel shader which can replace a particular colour in a sprite with another colour.

It looks something like this:


sampler input : register(s0);
float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0 
{  
    float4 colour = tex2D(input, coords); 
    if(colour.r == sourceColours[0].r &&
    colour.g == sourceColours[0].g &&
    colour.b == sourceColours[0].b) return targetColours[0];

    return colour; 
} 

What I would like to do is have the function take in 2 textures, a default table, and a lookup table (both same dimensions).
Grab the current pixel, and find the location XY (coords) of the matching RGB in the default table, and then substitute it with the colour found in the lookup table at XY.

I have figured how to pass the Textures from C# into the function, but I am not sure how to find the coords in the default table by matching the colour.

Could someone kindly assist?

Thanks in advance.

Sponsor:

#2 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 22 November 2012 - 02:03 AM

Hmm, think I got it,

It involves using a for loop to cycle through the a variant of the above code but I am getting this silly error now =(

Shader uses texture addressing operations in a dependency chain that is too complex for the target shader model (ps_2_0) to handle.

#3 Kyall   Members   -  Reputation: 287

Like
0Likes
Like

Posted 22 November 2012 - 02:13 AM

Use a 3D texture instead. That way instead of putting a for loop in your shader ( slow ), you take the color value, and use that value to look up a 3d texture that has the colours you want in it.
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

#4 Hodgman   Moderators   -  Reputation: 24004

Like
1Likes
Like

Posted 22 November 2012 - 02:41 AM

If you're going to do this, it would be a whole lot simpler if the original sprite contained pallete-indices instead of colours.

You could pre-process your sprites once on the CPU to avoid having to execute that ridiculously huge loop in your pixel shader.

Then you'd just have to do "fetch sprite colour (which is an index), fetch colour from pallete at index", instead of "fetch sprite colour, search entire source pallets for colour to determine it's index, fetch colour from new pallete at index".

#5 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 22 November 2012 - 03:13 AM

Hey all, thanks for the replies.

Use a 3D texture instead. That way instead of putting a for loop in your shader ( slow ), you take the color value, and use that value to look up a 3d texture that has the colours you want in it.


Hmm, that would require me to manually insert values into the 3rd dimension, if I have more more than 1 set of colours I want to change depending on the situation, I would need to store more than 1 look up value?


If you're going to do this, it would be a whole lot simpler if the original sprite contained pallete-indices instead of colours.

You could pre-process your sprites once on the CPU to avoid having to execute that ridiculously huge loop in your pixel shader.

Then you'd just have to do "fetch sprite colour (which is an index), fetch colour from pallete at index", instead of "fetch sprite colour, search entire source pallets for colour to determine it's index, fetch colour from new pallete at index".


Similar to what you suggested, I am thinking along the line of using one of the 3 RGB channels as my unique index, and storing my colour table values in an array of 255 elements.

#6 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 22 November 2012 - 11:18 AM

Ok, I have created a 4x1 png and inserted 4 different coloured pixels. I have changed index from 0 to 3 (or any value for that fact) but it always display the colour of the first pixel. If i change index with the var coords in the tex1D method, and move my sprite, it cycles through the colours when I move, but displays the colour of the first pixel when stationary. Do you have any idea why this is happening?

here's the snippet of my function:

    float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0 
    { 
		  
		    float4 colour = tex2D(input, coords); 
	
		    int index = 0; //doesn't matter what value i put, it always return the first pixel of my palette
		    float4 lookUp = tex1D(Palette2Sampler,index);
	 //float4 lookUp = tex1D(Palette2Sampler,coord);
		    // when I run the above and move the sprite, it cycles through the colours, but always
		    // displays the colour of the first pixel when stationary.
	
		    return lookup;
    }

Edited by derrace, 22 November 2012 - 11:18 AM.


#7 phil_t   Crossbones+   -  Reputation: 2480

Like
1Likes
Like

Posted 22 November 2012 - 01:40 PM

Texture coordinates range between (0, 1), not (0, n). Divide your index by n, where n is the number of texels (4 in your case).

Also, if this is DX9, you need to apply the half texel offset so that you're sampling from the middle of the texel (sampling from the edge will be "unstable"). So add (0.5 / 4) to the index.

And make sure you're using point sampling.

#8 Hodgman   Moderators   -  Reputation: 24004

Like
0Likes
Like

Posted 22 November 2012 - 08:56 PM

Also, if this is DX9, you need to apply the half texel offset so that you're sampling from the middle of the texel (sampling from the edge will be "unstable"). So add (0.5 / 4) to the index.

That's not just DX9*, that's how texture coordinates work in all APIs.
centreOfTexel = (texelIndex+0.5)/widthInTexels

*The "DX9 half pixel offset issue" is actually to do with how it addresses screen pixels during rasterization.

#9 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 23 November 2012 - 02:55 AM

Texture coordinates range between (0, 1), not (0, n). Divide your index by n, where n is the number of texels (4 in your case).

Also, if this is DX9, you need to apply the half texel offset so that you're sampling from the middle of the texel (sampling from the edge will be "unstable"). So add (0.5 / 4) to the index.

And make sure you're using point sampling.



Ah, 0 to 1. Got it. In my script, I don't think I need to do any division as it would probably have done it for me. When setting up my colourtable, I would just multiply it by 255 since I am using the bands of the pixel as my index.

Got my algo for pixel shader for palette swap sorta working.. but I am having some issues with it.

http://imgur.com/3pPnO

Bands aren't unique, so I am taking the average of RGB as my index. I don't get a whole number, but I made each lookup area 3x1 (i need to increase it to 3). I am not sure if it's the decimals that is causing the issue. I will need to debug further. Also, I have two default colours with the same value, but expecting different palette swaps.. How can this be achieved?


for now just assume my colour table is 255 x 10. I will hardcode the dividers as well as the y index for now.

[source lang="java"]float4 colour = tex2D(input, coords);float2 index = float2((colour.r + colour.g+colour.b)/3.0 , 4/10.0 + 0.5 / 10.0);float4 lookUp = tex2D(PaletteSampler,index);if(colour.a != 0) { colour.rgb = lookUp.rgb;} return colour;[/source]

sorry, what do you mean by point sampling? Isn't the above point sampling?

Edited by derrace, 23 November 2012 - 03:00 AM.


#10 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 23 November 2012 - 11:15 AM

hmm, so I have added a check and it does look like a lot of pixels aren't being replaced from the lookup table. It almost appears like I got my indices wrong =\

I think I need a better way of indexing. Does anyone have any suggestion?

To recap, currently I am taking the average of the RGB as the neither of the bands are unique =\. My palette (see screenshot attached) has potentially 15 pixels.

http://imgur.com/ujO6E

Replacing my default sprites with a greyscale one is kinda tedious, and I am hoping there's a better way.

Edited by derrace, 23 November 2012 - 11:25 AM.


#11 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 23 November 2012 - 12:17 PM

Alright, I have tried using greyscale as my index.

http://imgur.com/CWZju

Hmm, it looks a bit better after using greyscale, but still far from perfect.. Running out of ideas =\

#12 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 23 November 2012 - 12:33 PM

Argh, turns out when my engine is running at 1x (default resolution) the shader works perfectly, any scale of that (including the default XNA's graphics.ToggleFullScreen();) stuffs it up =\.

I am passing a transformation matrix into my spriteBtach.Begin method to scale my playable window by X amount, as well as using it for my camera + parallax effect.

It appears that the translation or scale operations are messing up my pixel shader?


I need help, badly now =(

Edited by derrace, 23 November 2012 - 12:56 PM.


#13 Kyall   Members   -  Reputation: 287

Like
0Likes
Like

Posted 23 November 2012 - 10:43 PM

You might want to play around with the texture filtering for the sprite batch. Try adding this before the begin call:

this.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;

The default in XNA (according to the interlect) is LinearClamp
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

#14 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 24 November 2012 - 11:23 AM

You might want to play around with the texture filtering for the sprite batch. Try adding this before the begin call:

this.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;

The default in XNA (according to the interlect) is LinearClamp



Hi there, I have tried that, but to no avail.

I have been reading up on this: http://msdn.microsoft.com/en-us/library/ff604998.aspx

But I have no experience with sampler state. Do you know of any tutorials which might help?

I am thinking of using the shader to do my scaling (magnification) just for the sprite. I am really at a lost atm =\

#15 phil_t   Crossbones+   -  Reputation: 2480

Like
1Likes
Like

Posted 24 November 2012 - 02:17 PM

If you're using SpriteBatch, you have to specify the sampler state in the Begin call. Otherwise it will override it with LinearClamp.

So use the overload of SpriteBatch.Begin that takes a SamplerState.

#16 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 25 November 2012 - 01:07 AM

If you're using SpriteBatch, you have to specify the sampler state in the Begin call. Otherwise it will override it with LinearClamp.

So use the overload of SpriteBatch.Begin that takes a SamplerState.


Ah, that did the trick. Thanks a lot!

Are there any sites you could recommend to read up on this? The lack of documentation on MSDN is really slowing my progress and I am just stumbling along with random google searches.... =(

#17 phil_t   Crossbones+   -  Reputation: 2480

Like
0Likes
Like

Posted 25 November 2012 - 01:51 PM

Yeah, the MSDN documentation is pretty awful. The education samples are pretty good though: http://xbox.create.msdn.com/en-US/education/catalog/

I would also recommend Shawn Hargreaves blog, though I suspect your random google searches lead you there pretty frequently anyway :-).

#18 Steve_Segreto   Crossbones+   -  Reputation: 1406

Like
0Likes
Like

Posted 26 November 2012 - 02:21 AM

Replacing my default sprites with a greyscale one is kinda tedious, and I am hoping there's a better way.


I wonder if tinting the player would be good enough instead of a full palette swap ... don't use your existing palette swapping pixel shader, instead treat the image of your character as a greyscale by sampling the original texture and ignoring 2 out of the 3 color channels, then multiply the greyscale by a passed in tintcolor to make the player a different color.

float4 originalColor = tex2d( TexSampler, tex0.xy );
float4 greyColor = float4( originalColor.r, originalColor.r, originalColor.r, originalColor.a );
float4 newColor = greyColor * tintColor;

where you pass tintColor in as a uniform extern.

You might have to experiment with which channel you want to use as the grey scale (r,g or b).

Edited by Steve_Segreto, 26 November 2012 - 02:22 AM.


#19 derrace   Members   -  Reputation: 120

Like
0Likes
Like

Posted 27 November 2012 - 10:09 PM


Replacing my default sprites with a greyscale one is kinda tedious, and I am hoping there's a better way.


I wonder if tinting the player would be good enough instead of a full palette swap ... don't use your existing palette swapping pixel shader, instead treat the image of your character as a greyscale by sampling the original texture and ignoring 2 out of the 3 color channels, then multiply the greyscale by a passed in tintcolor to make the player a different color.

where you pass tintColor in as a uniform extern.

You might have to experiment with which channel you want to use as the grey scale (r,g or b).


Hi Steve, changing my spritesheet to greyscale was a lot easier than I thought, although it's more of a Black and White filter (with same values for RGB).

I had thought of that prior, but I have a problem that with my original palette, I don't have a unique RGB band, so sampling just one of the band is not possible. Selecting the average 2 of the 3 yields duplicate values, and it gets messy if i take the average of all 3. This was why I went with greyscale.

My motivation for getting the palette swap working was because it would be reused in many situations. My palette has 15+ samples, so it would be a nightmare trying to get the right set of colours for try and error with the tinting method. Doing it with a colour table makes it easy to swap the colours, as well as knowing exactly what colours it would be swapping.

#20 derrace   Members   -  Reputation: 120

Like
1Likes
Like

Posted 29 November 2012 - 02:51 AM

Anyway, I got it working. Thanks everyone for their help.

Here's it in action =)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS