• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Shaders what is wrong with my shader?

S

Shadowblitz16

Guest
can someone tell me what is wrong with my shader and setter script?
it just makes my sprite invisible

Code:
uniform sampler2D palette;
uniform float     paletteIndex;
varying vec2      v_vTexcoord;

void main()
{
    vec4 _color        = texture2D(gm_BaseTexture, v_vTexcoord);
    vec4 _indexedColor = texture2D(palette, vec2(_color.r, paletteIndex));
    gl_FragColor = vec4(_indexedColor.rgb, _color.a); // This way we'll preserve alpha     
}
Code:
/// @description palette_set()
/// @param palette
/// @param index

var _sampler = shader_get_sampler_index(shd_palette, "palette");
var _texture = sprite_get_texture(argument[0], 0);
var _uniform = shader_get_uniform(shd_palette, "paletteIndex");

shader_set(shd_palette)
texture_set_stage(_sampler, _texture);
shader_set_uniform_f(_uniform, argument[1]);
this is my palette
upload_2018-10-6_14-14-14.png
 
Well, i can't see any obvious reason why it should cause your sprite to go invisible.

Is the palette sprite on its own texture page? If not, it could be that you are calculating texture coordinates incorrectly.

Are you remembering to turn the shader off after you have finished drawing with it?
 
Last edited:
S

Shadowblitz16

Guest
ya that seems to be it, however it seems that that white column is messing the colors up.
I'm guessing this has to do with the fact it's indexing the colors instead of using the first row as a lookup table.

EDIT: hmm seems only a palette index of 0 is working
 
Last edited by a moderator:
S

Shadowblitz16

Guest
I'm not I think the shader is doing it automatically
 
your choosing your colors as:

vec4 _indexedColor = texture2D(palette, vec2(_color.r, paletteIndex));

using _colour.r and paletteIndex.

How are you determing what values those two things will have?
 
S

Shadowblitz16

Guest
i'm not sure actually I am just passing the palette sprite and a number i increment from 0 to the palette length
 
ok, well this process will only work if those things have meaningful values.

First, paletteIndex.
Your palette has 16 rows. So paletteIndex (being the y component of your texture coordinates) should have a value n * 1/16, where n has an integer value between 0 and 15, inclusively. We should be sampling from the center of a texel, so we should add 1/32, but as long as texture interpolation is turned off, I don't believe that will be necessary.

Second, _color.r.
Your palette has 4 columns. Which means the x component of your texture coordinates should have a value n * 1/4, where n has an integer value between 0 and 3, inclusive. Again, assuming texture interpolation is turned off, we'll ignore trying to sample from the center of a palette texel. So, I think the easiest thing to do would be to take a _color.r, which will have a value from 0/255 to 255/255, multiply it by 255 to give it a value between 1 and 255, then divide by 4 so that initial values of _color.r of 0, 1, 2, or 3, will result in final values of 0/4, 1/4, 2/4, and 3/4, respectively.

Another reason texture interpolation should be turned off is because your initial _color.r value could be an interpolation between multiple different colors in the event that scaling or positioning of your sprites results in a non one-to-one relation between texels and pixels.

I'm a bit tired, so I hope this makes some kind of sense to you.
 
S

Shadowblitz16

Guest
@flyingsaucerinvasion i think your math is wrong.
(_color.r * 255) = 0-255
0-255 / 4 = 63.75

the r value needs to be 0-1 floored to the nearest color

I have this currently..
Code:
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform sampler2D u_palette;
uniform float     u_offset;
uniform float     u_texWidth;
uniform float     u_texHeight;
void main()
{
    float indexX  = texture2D( gm_BaseTexture, v_vTexcoord ).r * ();
    float indexY  = u_offset / u_texHeight;
    float indexX2 = floor(indexX / (1.0 / u_texWidth )) * (1.0 / u_texWidth );
    float indexY2 = floor(indexY / (1.0 / u_texHeight)) * (1.0 / u_texHeight);
   
    vec4 texel   = texture2D( u_palette, vec2(indexX2, indexY2));
    gl_FragColor = texel * v_vColour;
}
note a remade it to understand it a bit better
 
Top