Gradients (gradient as fill)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Gradients (gradient as fill)

Post by dusoft »

Hi,

I have searched forums extensively as well as Stack Overflow to find out how to approach gradient drawing. I mostly need it for polygons at the moment, but I am sure it would be useful to have it for any shape.

I know there was a discussion regarding this and the best method so far is to use a 1x1px image.

Is there some internal plan to move gradients forward in addition to line and fill modes?

Maybe somebody from the LOVE developers can chip in?

Thanks.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Gradients (gradient as fill)

Post by darkfrei »

dusoft wrote: Tue Mar 12, 2024 5:37 am Hi,

I have searched forums extensively as well as Stack Overflow to find out how to approach gradient drawing. I mostly need it for polygons at the moment, but I am sure it would be useful to have it for any shape.

I know there was a discussion regarding this and the best method so far is to use a 1x1px image.

Is there some internal plan to move gradients forward in addition to line and fill modes?

Maybe somebody from the LOVE developers can chip in?

Thanks.
Mesh! https://love2d.org/wiki/Mesh

How to make lights with shadows with gradient, but without shaders:
viewtopic.php?p=244845#p244845

Code: Select all

function love.load()
	vertices = {
		{100, 50, 0,0, 1,0,0},
		{700, 400, 0,0, 0,1,0},
		{50, 550, 0,0, 0,0,1},
	}
	mesh = love.graphics.newMesh( vertices, mode, usage )
end

function love.draw()
	love.graphics.draw (mesh)
end
Attachments
2024-03-12T07_31_23-Untitled.png
2024-03-12T07_31_23-Untitled.png (204.66 KiB) Viewed 1125 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Gradients (gradient as fill)

Post by dusoft »

Meshes seem to be unnecessary complicated for my use - I am neither using images, nor canvas. I am working with 2D, not 3D.
I am drawing polygons directly and using physics functions. I can simulate gradients via repeated drawing of multiplied polygons, but I will have to benchmark performance here for larger numbers.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Gradients (gradient as fill)

Post by pgimeno »

I make gradients with a 2x1 image and a quad.

Code: Select all

local gradientData = love.image.newImageData(2, 1, 'rgba8',
  '\255\000\000' .. '\255' ..
  '\000\255\000' .. '\255')
local gradient = love.graphics.newImage(gradientData)
gradient:setFilter('linear', 'linear')
local gradientQuad = love.graphics.newQuad(0.5, 0, 1, 1, 2, 1)

function love.draw()
    -- Draw a 160x180 vertical gradient centred at 400,300
    love.graphics.draw(gradient, gradientQuad, 400, 300, math.rad(90), 180, 160, 0.5, 0.5)
end
If you want polygons with a gradient, I'm afraid darkfrei is right, you need meshes and possibly love.math.triangulate() to get a triangle mesh from the polygon. Also you'll need to set the UVs yourself appropriately. Or you can use a stencil to mask the gradient, or a shader.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Gradients (gradient as fill)

Post by darkfrei »

dusoft wrote: Tue Mar 12, 2024 9:39 am Meshes seem to be unnecessary complicated for my use - I am neither using images, nor canvas. I am working with 2D, not 3D.
I am drawing polygons directly and using physics functions. I can simulate gradients via repeated drawing of multiplied polygons, but I will have to benchmark performance here for larger numbers.
Than you can use the meshes with texture?
https://github.com/darkfrei/love2d-lua- ... th-texture
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Gradients (gradient as fill)

Post by dusoft »

Thanks for your suggestions, I will be staying with scaled up overlapping polygons for now.
RNavega
Party member
Posts: 251
Joined: Sun Aug 16, 2020 1:28 pm

Re: Gradients (gradient as fill)

Post by RNavega »

dusoft wrote: Tue Mar 12, 2024 4:33 pm Thanks for your suggestions, I will be staying with scaled up overlapping polygons for now.
Hi. How does that overlap thing work, can you describe it?
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Gradients (gradient as fill)

Post by dusoft »

You can see it on the screenshot (blue ones, yellow is the original polygon that was used for scaling up). I create couple of scaled up polygons of the same shape just drawing over each other (starting from the outside - from large to small). This efficiently creates a gradient, I can control how many polygons (gradient steps) to have.

PS: ignore the gray lines, I just use those for debug purposes.
Attachments
Screenshot_20240315_131237.png
Screenshot_20240315_131237.png (10.21 KiB) Viewed 875 times
Screenshot_20240315_130655.png
Screenshot_20240315_130655.png (10.96 KiB) Viewed 879 times
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Gradients (gradient as fill)

Post by dusoft »

Even better examples with full polygon visible.

Gradient starts around the original polygon (boat shaped), but I could make it start around centroid point, this is just matter of setting the scale and drawing...
Attachments
Screenshot_20240317_061918.png
Screenshot_20240317_061918.png (4.61 KiB) Viewed 824 times
Screenshot_20240317_061850.png
Screenshot_20240317_061850.png (4.35 KiB) Viewed 824 times
Screenshot_20240317_061741.png
Screenshot_20240317_061741.png (4.7 KiB) Viewed 824 times
RNavega
Party member
Posts: 251
Joined: Sun Aug 16, 2020 1:28 pm

Re: Gradients (gradient as fill)

Post by RNavega »

Nice, I see what you're after. Thank you for the images.
Post Reply

Who is online

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