Share a Shader!

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.
girng
Prole
Posts: 40
Joined: Fri Sep 07, 2018 5:42 am

Re: Share a Shader!

Post by girng »

I have a shader that works in Godot, can I still use it in love2d?

It basically just creates an outline around a sprite (of a certain color)
User avatar
wazoowazoo
Prole
Posts: 16
Joined: Sun Jan 22, 2017 2:47 pm

Re: Share a Shader!

Post by wazoowazoo »

I have created a basic water shader, hope you will like it.
water.love
(230.82 KiB) Downloaded 880 times
I am currently working on reflection.
User avatar
wazoowazoo
Prole
Posts: 16
Joined: Sun Jan 22, 2017 2:47 pm

Re: Share a Shader!

Post by wazoowazoo »

I have created another shader, this time a fractal. Left mouse button is to place a fractal, backspace to reset graphics, return (enter) to reset frames, space to pause/unpause and scrolling makes it evolve faster or slower. I hope you will have fun playing around with it ! :)

Fractal.love
(1.25 KiB) Downloaded 603 times

Here's a preview:
Fractal.png
Fractal.png (228.97 KiB) Viewed 40746 times
trabitboy
Prole
Posts: 19
Joined: Sun May 12, 2019 2:09 pm

Re: Share a Shader!

Post by trabitboy »

Hi!

I am currently porting a paint tool to love2d because:
lua is awesome and
love2d is awesome and supports shaders

I am looking for an example of a love 2d shader with 2 textures as input;
scenario:
I want to erase using the gpu,
meaning I want to have painting canvas and an eraser texture as an input, blitting to another canvas as output .

I climbed back this thread quite far but I could find no multiple inputs shader ?

Thanks for any help !
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Share a Shader!

Post by Sasha264 »

trabitboy,
Hi! :awesome:

To pass 2 or more images (canvases, textures) to the shader you can declare they inside shader code using extern directive.
And after that but before using this shader you need to send this texture to a shader using shader:send(...) method https://love2d.org/wiki/Shader:send

Note, that you can ignore "default" texture from the second argument from effect(...) function at all and use only yours named textures, declared using extern Image [name] :cool:

Here is some possible "erasing" shader (but I don't know exact details so it can be adjusted):

Code: Select all

local shader

-- call once per program run
function InitShader()
	local pixelcode = [[
		extern vec2 shift;
		extern Image eraser;

		vec4 effect(vec4 color, Image texture, vec2 tc, vec2 sc)
		{
			vec4 texturePixel = Texel(texture, tc);
			vec4 eraserPixel = Texel(texture, tc + shift);
			texturePixel.a = texturePixel.a * (1.0f - eraserPixel.a);
			return texturePixel;
		}
	]]

	shader = love.graphics.newShader(pixelcode)
end

-- call once for change eraser texture
function SetEraserTexture(eraser)
	shader:send("eraser", eraser)
end

-- call every time you want to move eraser, i.e. every frame
function SetEraserShift(shift)
	shader:send("shift", shift)
end
BTW, I suppose for this question a separate thread will be more appropriate :3
trabitboy
Prole
Posts: 19
Joined: Sun May 12, 2019 2:09 pm

Re: Share a Shader!

Post by trabitboy »

well thanks Sasha264, that is very helpful :awesome:
trabitboy
Prole
Posts: 19
Joined: Sun May 12, 2019 2:09 pm

Re: Share a Shader!

Post by trabitboy »

Here is my example inspired from Sasha264 ,
but offset calculation is not correct in my case .
( getting pretty close )

EDIT: corrected, was not offseting on tex coords but cvs coords

Code: Select all


--this shader substracts alpha of the eraser brush
--offset calculation still incorrect

function love.load()

	offset={}
	offset.x=400.0
	offset.y=400.0

	circle=love.graphics.newImage('circle.png')

	cvs1=love.graphics.newCanvas(640,480)
	love.graphics.setCanvas(cvs1)
	--first canvas is filled with redmod
	love.graphics.clear(1.0,0.0,0.0,1.0)

	--the canvas holding the real image data
	curcvs=cvs1

	
	cvs2=love.graphics.newCanvas(640,480)
	love.graphics.setCanvas(cvs2)
	love.graphics.clear(0.0,0.0,0.0,0.0)

	backcvs=cvs2 -- the back buffer the frag shader will render to

	love.graphics.setCanvas()

	local pixelcode=[[
		//second is the eraser
		extern Image second;
		extern float cvsw;
		extern float brshw;
		extern float cvsh;
		extern float brshh;
		extern float offx;//pixels
		extern float offy;//pixels

		vec4 effect(vec4 color,Image texture,vec2 tc,vec2 sc){
			
			
			float pxw=1.0 / brshw;
			float pxh=1.0/brshh;
			
			
			vec4 texturePixel=Texel(texture,tc);
			
			
			float tcx = tc.x*cvsw/brshw-offx*pxw;
			if (tcx>1.0 || tcx<0.0 ){
				return texturePixel;
			}
			float tcy = tc.y*cvsh/brshh-offy*pxh;
			if (tcy>1.0 || tcy<0.0){
				return texturePixel;
			}
			vec2 rtc=vec2(tcx,tcy);
			
			vec4 secondPixel=Texel(second,rtc);
			
				vec4 ret=texturePixel ;
				ret.a=ret.a-secondPixel.a;
				return ret;
			
		}
	]]
	
	shader=love.graphics.newShader(pixelcode)

	shader:send("second",circle)
	shader:send("cvsw",640.0)
	shader:send("brshw",64.0)
	shader:send("cvsh",480.0)
	shader:send("brshh",64.0)
	shader:send("offx",offset.x)
	shader:send("offy",offset.y)

	--test erase from curcvs to backcvs
	love.graphics.setCanvas(backcvs)
	love.graphics.setShader(shader)
	love.graphics.draw(curcvs)
	love.graphics.setShader()
	love.graphics.setCanvas()

end


function love.draw()

	love.graphics.draw(backcvs)
	love.graphics.print('eraser shader test')


end

Attachments
love2dshadertests.zip
(4.17 KiB) Downloaded 413 times
aoi_saboten
Prole
Posts: 2
Joined: Wed Oct 02, 2019 4:53 pm

Re: Share a Shader!

Post by aoi_saboten »

Hello guys.

Can you look to that CRT-shader used here --> https://github.com/Akylzhan/CRT-shader

Is it possible to make that effect much better? I am worried about color palette since it is very colorful and about display boundaries, can I make it smoother?
User avatar
YounYokel
Prole
Posts: 39
Joined: Thu Oct 03, 2019 5:57 pm

Re: Share a Shader!

Post by YounYokel »

Do anyone have a simple invert colors shader?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 46 guests