Convert Shaders to Love2D Shaders

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
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Convert Shaders to Love2D Shaders

Post by darkfrei »

Hi all!

Can you please explain how to convert shaders to the Löve?

For example this one: https://www.shadertoy.com/view/3s3GDn

Code: Select all

void mainImage( out vec4 fragColor, in vec2 fragCoord ){
	vec2 uv = fragCoord/iResolution.xy;
	vec2 pos = 0.5 - uv;
	pos.y /= iResolution.x/iResolution.y;
	float dist = 1.0/length(pos);
	dist *= 0.1;
	dist = pow(dist, 0.8);
	vec3 col = dist * vec3(1.0, 0.5, 0.25);
	col = 1.0 - exp( -col );
	fragColor = vec4(col, 1.0);
}
Now the Love2D, expected: It must be so easy, but not:

Code: Select all

myShader = love.graphics.newShader[[
vec4 effect (vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
{
	vec2 uv = fragCoord/iResolution.xy;
	vec2 pos = 0.5 - uv;
	pos.y /= iResolution.x/iResolution.y;
	float dist = 1.0/length(pos);
	dist *= 0.1;
	dist = pow(dist, 0.8);
	vec3 col = dist * vec3(1.0, 0.5, 0.25);
	col = 1.0 - exp( -col );
	return vec4(col, 1.0);
}
]]
How to rewrite the shader correct?


(or this tutorial, but for Love2d https://inspirnathan.com/posts/65-glow- ... shadertoy/)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Convert Shaders to Love2D Shaders

Post by veethree »

Code: Select all

    shader = love.graphics.newShader[[
        extern vec2 iResolution;
        vec4 effect (vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
        {
            vec2 uv = screen_coords/iResolution.xy;
            vec2 pos = 0.5 - uv;
            pos.y /= iResolution.x/iResolution.y;
            float dist = 1.0/length(pos);
            dist *= 0.1;
            dist = pow(dist, 0.8);
            vec3 col = dist * vec3(1.0, 0.5, 0.25);
            col = 1.0 - exp( -col );
            return vec4(col, 1.0);

        }
        ]]
Here you go. What you were missing is "fragCoord" wasn't defined, It was supposed to be screen_coords. And iResolution, Which should be a vec2 with the resolution of the image wasn't defined. That needs to be passed to the shader via shader:send()
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Convert Shaders to Love2D Shaders

Post by pgimeno »

This thread is about porting a shader too:
viewtopic.php?f=4&t=86345

And this thread is a library for viewing ShaderToy shaders:
viewtopic.php?f=5&t=80885
Post Reply

Who is online

Users browsing this forum: Kakkassery_Joseph and 74 guests