Help with this shader, please?

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
User avatar
childonline
Prole
Posts: 15
Joined: Thu Dec 30, 2010 11:06 am

Help with this shader, please?

Post by childonline »

Hello, I tried to modify a shader I found on shadertoy @ https://www.shadertoy.com/view/lsXSWl and make it work on a canvas, but for some reason the canvas on which I apply the shader just renders a black screen.

Any thoughts?

Thanks!

Code: Select all

code = 
[[
extern vec3 iResolution;
extern Image iChannel0;
extern number iGlobalTime;
float rng2(vec2 seed)
{
    return fract(sin(dot(seed * floor(iGlobalTime * 12.), vec2(127.1,311.7))) * 43758.5453123);
}

float rng(float seed)
{
    return rng2(vec2(seed, 1.0));
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = fragCoord.xy / iResolution.xy;
    vec2 blockS = floor(uv * vec2(24., 9.));
    vec2 blockL = floor(uv * vec2(8., 4.));

    float r = rng2(uv);
    vec3 noise = (vec3(r, 1. - r, r / 2. + 0.5) * 1.0 - 2.0) * 0.08;

    float lineNoise = pow(rng2(blockS), 8.0) * pow(rng2(blockL), 3.0) - pow(rng(7.2341), 17.0) * 2.;
    
    vec4 col1 = texture2D(iChannel0, uv);
    vec4 col2 = texture2D(iChannel0, uv + vec2(lineNoise * 0.05 * rng(5.0), 0));
    vec4 col3 = texture2D(iChannel0, uv - vec2(lineNoise * 0.05 * rng(31.0), 0));

    fragColor = vec4(vec3(col1.x, col2.y, col3.z) + noise, 1.0);

}

vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
    vec2 fragCoord = texture_coords * iResolution.xy;
    mainImage( color, fragCoord );
    return color;
}
]]

function love.load()
    --font = love.graphics.newFont("AtariST8x16SystemFont.ttf", 32)
    shader = love.graphics.newShader( code )
    canvas1 = love.graphics.newCanvas( 800, 600 )
    canvas2 = love.graphics.newCanvas( 800, 600 )

    iGlobalTime = 0
end

function love.update( dt )

    -- 1ST CANVAS    
    love.graphics.setCanvas(canvas1)
    --love.graphics.setFont(font)
    for i=0, 64 do
        love.graphics.print('Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!', 0, i*12)
    end
    love.graphics.setCanvas()

    -- 2ND CANVAS
    love.graphics.setCanvas(canvas2)
    love.graphics.setShader(shader)
    iGlobalTime = iGlobalTime + dt
    shader:send("iGlobalTime", iGlobalTime)
    shader:send("iResolution", {800, 600, 1})
    shader:send("iChannel0", canvas1)
    love.graphics.setShader()
    love.graphics.setCanvas()
end

function love.draw( )
  -- love.graphics.draw(canvas1,0,0) -- this shows canvas1 has render data
  love.graphics.draw(canvas2,0,0) -- yet this renders nothing
end
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Help with this shader, please?

Post by arampl »

Seems you forgot draw something to canvas2.
You set shader, send uniforms to it, then leave and draw black screen.
Maybe you need to put "love.graphics.draw(canvas1)" after shader:send("iChannel0", canvas1) or something like this?

btw. You can use love.update event for drawing operations but you shouldn't. It's intended for updating physics, AI and such.
User avatar
childonline
Prole
Posts: 15
Joined: Thu Dec 30, 2010 11:06 am

Re: Help with this shader, please?

Post by childonline »

arampl wrote:Seems you forgot draw something to canvas2.
You set shader, send uniforms to it, then leave and draw black screen.
Maybe you need to put "love.graphics.draw(canvas1)" after shader:send("iChannel0", canvas1) or something like this?
Totally, I guess I had tunnel vision.

Thanks!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests