Shader returns only white

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
crocs
Prole
Posts: 4
Joined: Mon Oct 03, 2022 2:57 pm

Shader returns only white

Post by crocs »

Hi there! :)
I've seen some other posts that cover a similar topic to this, but none of them explain to me how to correct the issue.

Code: Select all

local pixelShader = love.graphics.newShader[[
    extern vec2 resolution;

    vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ) {
        vec2 largePosition = floor(texture_coords / resolution) * resolution;

        vec4 resultant = Texel(texture, largePosition);

        
        
        return resultant;
    }
]]
The code above is supposed to pixelate the visuals. Resolution is a vector 2 that changes how many pixels it makes.

I see online that you return resultant * colour, but because resultant is always white it will always just return the original colour. Resultant itself is always 1,1,1,1. Can my shader not check the colour on love.draw (using graphics.setcolor)? If not, how can I colour my program so the shader can change it?

Thank you!!
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Shader returns only white

Post by zorg »

Hi and welcome to the forums!

It's late and i might be wrong, but this is how i see it:

The resolution variable storing how many pixels you want to simulate in width and height is not how that works;
if resolution is {1,1}, then you'll get width x height pixels,
if resolution is {2,2}, then you'll get (width/2) x (height/2) pixels,
and so on.

I'm guessing you're passing in such a large pair of numbers to resolution, that you're basically only sampling one pixel, which happens to be fully white.

You could potentially fix this by passing in {actualwidthinpixels/wantedwidthinpixels, actualheightinpixels/wantedheightinpixels} instead.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Shader returns only white

Post by pgimeno »

This will only work correctly if resolution.x is 1/width and resolution.y is 1/height.

Maybe you want the opposite: vec2 largePosition = floor(texture_coords * resolution) / resolution;
crocs
Prole
Posts: 4
Joined: Mon Oct 03, 2022 2:57 pm

Re: Shader returns only white

Post by crocs »

zorg wrote: Mon Oct 03, 2022 8:31 pm Hi and welcome to the forums!

It's late and i might be wrong, but this is how i see it:

The resolution variable storing how many pixels you want to simulate in width and height is not how that works;
if resolution is {1,1}, then you'll get width x height pixels,
if resolution is {2,2}, then you'll get (width/2) x (height/2) pixels,
and so on.

I'm guessing you're passing in such a large pair of numbers to resolution, that you're basically only sampling one pixel, which happens to be fully white.

You could potentially fix this by passing in {actualwidthinpixels/wantedwidthinpixels, actualheightinpixels/wantedheightinpixels} instead.
Hello!
By instead returning the colour of the position (not the colour of the pixel), you can see where each pixel is supposed to go (and it looks pretty cool aswell ).

Code: Select all

local pixelShader = love.graphics.newShader[[
    extern vec2 resolution;

    vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ) {
        vec2 largePosition = floor(screen_coords / resolution) * resolution;

        vec4 resultant = Texel(texture, largePosition);

        
        
        return vec4 (largePosition / love_ScreenSize.xy,1,1   );
    }
]]
The higher the vec2 resolution is, the larger the pixels (not the other way that I thought would be amount of pixels onscreen, thank you!!). Knowing this, I can see exactly what pixel that I should be copying for each square (the upper left corner of each square), but still the Texel() function will always return white. Am I using the wrong function to get the colour of a pixel on screen?
Thank you for the help so far!
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Shader returns only white

Post by Andlac028 »

If you want to make whole game look pixelated, you can just draw everything to smaller canvas than window and then upscale the canvas with nearest filter (with love.graphics.setDefaultFilter or set filter just to canvas) and draw it to full window
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Shader returns only white

Post by ReFreezed »

Note that Texel() expects normalized coordinates which texture_coords is, but screen_coords is in pixels. If resolution is in pixels then both floor(texture_coords/resolution) and floor(screen_coords/resolution) will round to 0 most of the time, so you're sampling the same pixel.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: No registered users and 50 guests