Page 1 of 1

Help with this shader, please?

Posted: Mon Nov 02, 2015 11:49 pm
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

Re: Help with this shader, please?

Posted: Tue Nov 03, 2015 3:42 am
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.

Re: Help with this shader, please?

Posted: Tue Nov 03, 2015 3:30 pm
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!