Page 1 of 1

Non-Image objects for GLSL shaders

Posted: Sun May 06, 2018 4:57 am
by QwertyDragon83
I just started messing with GLSL shaders tonight, and I can see how this is a very powerful tool. It took me a while, but I figured most of it out, I think..

However, I have a bit of an issue. I can't seem to figure out how to apply shaders to non-image objects, such as circles and rectangles. If I want to make a blurry rectangle, I shouldn't have to make a rectangle image and draw it to the screen using a blurring shader, I should just be able to use a shader on the rectangle drawing function. Am I wrong?

I noticed that the input for the effect() function is an Image. Is there a way to input a rectangle as an image? Here's my code:

Code: Select all

function love.load()
	shader2 = love.graphics.newShader[[
		vec4 effect(vec4 color, Image texture, vec2 tc, vec2 screen_coords){
			vec4 pix = Texel(texture, tc);//This is the current pixel color
			pix.a = tc.x+tc.y;
			return pix;
		}
	]]
	test = love.graphics.newImage("test.png");
end

function love.draw()
	love.graphics.setShader(shader2);
	love.graphics.setColor(0, 1, 0, 1);
	love.graphics.draw(test, 20, 20);
	love.graphics.rectangle("line", 300, 300, 100, 30);
	love.graphics.setShader();
end
The image is drawn fine and shaded correctly, but the rectangle does not appear at all. Keep in mind I am using love2d v11.1, so rgba values are 0 to 1. I looked for a question like this and couldn't find anything.. Hopefully this hasn't been asked before.

Re: Non-Image objects for GLSL shaders

Posted: Sun May 06, 2018 8:30 am
by zorg
Hi and welcome to the forums!

The basic shapes löve allows you to draw are untextured meshes. No texture, no pixel manipulation; you can still mess with the vertices though in a vertex shader.

Re: Non-Image objects for GLSL shaders

Posted: Sun May 06, 2018 2:25 pm
by pgimeno
Apart from what zorg said, unfortunately you don't have enough information in the shader to know the coordinates of the figure drawn, or the ST of the quad (only the UV), but you can compensate for that by using shader:send. You can then use the screen coordinates for reference.

Re: Non-Image objects for GLSL shaders

Posted: Sun May 06, 2018 2:40 pm
by Ref
Glad you've got it figured out.
I'm still struggling and hope you post what you come up with.
Best!
Edit: Second shader (primitives) a little more relevant.