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.
User avatar
Ratchet
Citizen
Posts: 67
Joined: Mon Apr 08, 2013 10:32 am

Threshold Shader

Post by Ratchet »

I need a simple Threshold Shader and found this one:

https://www.shadertoy.com/view/4ssGR8

Would someone so kind and make it run in LOVE 0.10.2 please? I never understand this shader language stuff. Many thanks :)
macOS 10.14 Mojave | LÖVE 11.2
ghurk
Prole
Posts: 15
Joined: Tue Apr 25, 2017 11:05 pm

Re: Share a Shader!

Post by ghurk »

quick remake for love2d v.0.10.2:

Code: Select all

shaderCustom = love.graphics.newShader [[

extern float soft = 0.3;
extern float threshold = 0.3;

vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
{
	float f = soft/2.0;
	float a = threshold - f;
	float b = threshold + f;

	vec4 tx = Texel( texture, texture_coords );
	float l = (tx.x + tx.y + tx.z) / 3.0;
	vec3 col = vec3( smoothstep(a, b, l) );
	
	return vec4( col, 1 )*color;
}
]]
in draw event:

Code: Select all

threshold = 0.3
soft = 0.3

function love.draw( dt )

	--both preset at 0.3 in shader, but values can be changed using these 2 lines below. extreme value changes will result in ugly results.
	shaderCustom.send( shaderCustom, "soft", soft )
	shaderCustom.send( shaderCustom, "threshold", threshold )

	love.graphics.setShader( shaderCustom )
		love.graphics.draw( mm, 50, 50, 0, 2.5, 2.5 )
	love.graphics.setShader()
end
draws white color on black background. threshold controls size of the white parts, soft is blur amount.

and here is version for white on transparent background:

Code: Select all

--white on transparent bkg
shaderCustom = love.graphics.newShader [[

extern float soft = 0.3;
extern float threshold = 0.3;

vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
{
	float f = soft/2.0;
	float a = threshold - f;
	float b = threshold + f;

	vec4 tx = Texel( texture, texture_coords );
	float l = (tx.x + tx.y + tx.z) / 3.0;
	
	return vec4( smoothstep(a, b, l) )*color;
}
]]
in both cases, if you get problems with whole drawable being white (partially transparent), set "soft" to 0.0.
occurs with extreme changes to threshold.

forgot the *color. white on transparent background can be recolored using love.graphics.setColor(255,255,255,255) so dont request any specific color variants please :p
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: Share a Shader!

Post by DarkShroom »

EDIT2: furthermore i am even unsure about this whole shader as it seems to even work if you remove some of the setShader things... alas, shaders forever in mystery, it's like few people seem to understand them!

EDIT: this was for the smoke shader from the link at the bottom of the guide http://blogs.love2d.org/content/beginners-guide-shaders i dunno how this forum is organised! but the shader did not work with the latest love version (like seemingly 99% of examples i download)

Hi

I had to refactor this to work on the latest version:

Code: Select all

local Shader;
local firstCanvas;
local secondCanvas;
local currentCanvas
local otherCanvas
local lastPoint = {x=0,y=0}

function love.load()
	-- love._openConsole()
	
	Shader = love.graphics.newShader[[

vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
		{

		number pW = 1/love_ScreenSize.x;//pixel width 
		number pH = 1/love_ScreenSize.y;//pixel height

		vec4 pixel = Texel(texture, texture_coords );//This is the current pixel 

		vec2 coords = vec2(texture_coords.x-pW,texture_coords.y);
		vec4 Lpixel = Texel(texture, coords );//Pixel on the left

		coords = vec2(texture_coords.x+pW,texture_coords.y);
		vec4 Rpixel = Texel(texture, coords );//Pixel on the right

		coords = vec2(texture_coords.x,texture_coords.y-pH);
		vec4 Upixel = Texel(texture, coords );//Pixel on the up

		coords = vec2(texture_coords.x,texture_coords.y+pH);
		vec4 Dpixel = Texel(texture, coords );//Pixel on the down

		pixel.a += 10 * 0.0166667 * (Lpixel.a + Rpixel.a + Dpixel.a * 3 + Upixel.a - 6 * pixel.a);

		pixel.rgb = vec3(1.0,1.0,1.0);


		return pixel;

		}
		]]

	firstCanvas = love.graphics.newCanvas()
	secondCanvas = love.graphics.newCanvas()

	currentCanvas = firstCanvas
	otherCanvas = secondCanvas

	lastPoint.x = love.mouse.getX()
	lastPoint.y = love.mouse.getY()
end

local fps = 0;
local mouseParticles = {};


function love.draw()
	love.graphics.setColor(0,0,0,255)
	love.graphics.rectangle("fill",0,0,100,50)
	love.graphics.setColor(255,255,255,255)
	love.graphics.print("FPS: " .. fps,10,10)

	if(#mouseParticles > 0)then 
		love.graphics.setCanvas(firstCanvas)
		love.graphics.setColor(255,255,255,255)
		for i=1,#mouseParticles do 
			local p = mouseParticles[i];
			love.graphics.circle("fill",p.x,p.y,10);
		end
		love.graphics.setCanvas()
		mouseParticles = {}
		love.graphics.setColor(255,255,255,255)
	end


	love.graphics.setCanvas(otherCanvas)
	love.graphics.setShader(Shader);
	love.graphics.draw(currentCanvas)
	love.graphics.setShader();
	love.graphics.setCanvas()

	
	-- currentCanvas:clear()
	refactor_canvas = love.graphics.getCanvas()
	love.graphics.setCanvas(currentCanvas)
	love.graphics.clear()
	love.graphics.setCanvas(refactor_canvas)



	love.graphics.setCanvas(currentCanvas)
	love.graphics.setShader(Shader);
	love.graphics.draw(otherCanvas)
	love.graphics.setShader();
	love.graphics.setCanvas()

	love.graphics.draw(currentCanvas)

	-- otherCanvas:clear() --HAD TO REFACTOR THIS, dunno if it's convoluted
	refactor_canvas = love.graphics.getCanvas()
	love.graphics.setCanvas(otherCanvas)
	love.graphics.clear()
	love.graphics.setCanvas(refactor_canvas)



	
end

local c2 = 0;
local sum = 0;

local lastModified = love.filesystem.getLastModified("main.lua")


function love.update(dt)
	------------To make the code update in real time
	if(love.filesystem.getLastModified("main.lua") ~= lastModified)then 
		local testFunc = function()
			love.filesystem.load('main.lua')
		end
		local test,e = pcall(testFunc)
		if(test)then 
		 	love.filesystem.load('main.lua')()
		 	love.run()
		else 
			print(e)
		end
		lastModified = love.filesystem.getLastModified("main.lua")
	end

	-----------Get average FPS
	c2 = c2 + 1;
	sum = sum + dt;
	if(sum > 1)then 
		sum = sum / c2;
		fps = math.floor(1/sum);
		c2 = 0;
		sum = 0;
	end

	-----Add smoke
	if(love.mouse.isDown(1))then 
		local x,y = love.mouse.getPosition()
		local p = {};
		p.x = x; p.y = y;
		mouseParticles[#mouseParticles+1] = p;

		local dx = p.x - lastPoint.x;
		local dy = p.y - lastPoint.y;
		local dist = math.sqrt(dx * dx + dy * dy);

		---if there is a gap, fill it
		if(dist > 5)then 
			local angle = math.atan2(dy,dx);
			local cosine = math.cos(angle);
			local sine = math.sin(angle)
			for i=1,dist,1 do 
				local p2 = {};
				p2.x = lastPoint.x + i * cosine;
				p2.y = lastPoint.y + i * sine;
				mouseParticles[#mouseParticles+1] = p2;
			end
		end

		lastPoint.x = x; lastPoint.y = y;
	else
		--if mouse is up
		local x,y = love.mouse.getPosition()
		lastPoint.x = x; lastPoint.y = y;
	end
end



The main weird bit was i had to replace:

currentCanvas:clear() and otherCanvas:clear()

with a thing that went

Code: Select all

	refactor_canvas = love.graphics.getCanvas()
	love.graphics.setCanvas(currentCanvas)
	love.graphics.clear()
	love.graphics.setCanvas(refactor_canvas)

So it's like you used to be able to clear any canvas, now just the current one... anyway it works :)

this shader retains information from the previous screens, which i need (no-one can seem to answer my support thread)
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: Share a Shader!

Post by alberto_lara »

I modified it to work full screen and you're right; it's generally too blurry and dark. It can be sharpened up but at the cost of losing the shadow mask effect, which is mainly what I'm after in a CRT shader.
How do I remove the split? I'm still seeing 3 bars :(
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Share a Shader!

Post by Ref »

The mysteries of shaders. Don't have a clue as to how this shader works!
Attachments
tri.love
(1.42 KiB) Downloaded 709 times
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Share a Shader!

Post by Ref »

Doesn't seem to be too much interest in this topic.
I'm still amazed at what people can do with them.

(ESC to exit)
Attachments
head.love
Just a demo!
(4.12 KiB) Downloaded 636 times
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Share a Shader!

Post by zorg »

Ref wrote: Fri Mar 23, 2018 2:08 am The mysteries of shaders. Don't have a clue as to how this shader works!
Got this error for that; running 11.1:

Code: Select all

Error: Cannot link shader program object:
Fragment info
-------------
0(52) : warning C7533: global variable gl_FragColor is deprecated after version 120
0(44) : error C5121: multiple bindings to output semantic "COL0"

stack traceback:
        [string "boot.lua"]:637: in function <[string "boot.lua"]:633>
        [C]: in function 'newShader'
        tri.lua:60: in main chunk
        [C]: in function 'require'
        main.lua:2: in main chunk
        [C]: in function 'require'
        [string "boot.lua"]:475: in function <[string "boot.lua"]:311>
        [C]: in function 'xpcall'
        [string "boot.lua"]:645: in function <[string "boot.lua"]:639>
        [C]: in function 'xpcall'
The Head one is awesome though, although it runs with 15 fps here :P

Edit: Thanks for the new version!
Last edited by zorg on Tue Sep 04, 2018 12:05 am, edited 1 time in total.
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
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Share a Shader!

Post by Ref »

Ops! Old version now updated to 11.1
Best
Attachments
newTri.love
Corrected shader
(1.41 KiB) Downloaded 621 times
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Share a Shader!

Post by Nixola »

Should Tri be this?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Share a Shader!

Post by zorg »

Nixola wrote: Wed Sep 05, 2018 9:09 am Should Tri be this?
Nope, more like this:
like_this.png
like_this.png (112.29 KiB) Viewed 42702 times
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.
Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests