[SOLVED] Repeat texture in circle mesh (uv calculation)

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
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

[SOLVED] Repeat texture in circle mesh (uv calculation)

Post by Bigfoot71 »

Hello lovers !

I need help on how to repeat a texture in a circle mesh.
I already have a function that fills a circular mesh with a texture, here it is:

Code: Select all

local createCricleMesh = function(image, r)
    local cos, sin, pi = math.cos, math.sin, math.pi

    local d = 2*r
    local segments = math.round(pi/math.acos(1-.33/r))
    local vertices = {{0, 0, 0.5, 0.5}}

    for i=0, segments do

        local angle = (i / segments) * pi * 2

        local x = cos(angle) * r
        local y = sin(angle) * r

        local u = 0.5 + x / d
        local v = 0.5 + y / d

        vertices[#vertices+1] = {x, y, u, v}

    end

    local mesh = love.graphics.newMesh(vertices, "fan")
    mesh:setTexture(image)

    return mesh
end
It gives me something like this:
Image

But I would rather have something like this:
Image

How can I do ?

I am attaching a demo with all the resources you need if you want to try it.
Attachments
mesh-test.love
(71.35 KiB) Downloaded 36 times
Last edited by Bigfoot71 on Thu Nov 17, 2022 2:20 pm, edited 1 time in total.
My avatar code for the curious :D V1, V2, V3.
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Repeat texture in circle mesh (uv calculation)

Post by darkfrei »

Useful links:
https://love2d.org/wiki/Texture:setWrap
https://love2d.org/wiki/WrapMode

Code: Select all

local createCricleMap = function(image, r)
	local cos, sin, pi = math.cos, math.sin, math.pi
	local d = 2*r
	local segments = math.round(pi/math.acos(1-.33/r))
	local vertices = {{0, 0, 0.5, 0.5}}
	image:setWrap( "repeat" )
	for i=0, segments do
		local angle = (i / segments) * pi * 2
		local x = cos(angle) * r
		local y = sin(angle) * r
		local u = (0.5 + 4*x / d)
		local v = (0.5 + 4*y / d)
		vertices[#vertices+1] = {x, y, u, v}
	end
	local mesh = love.graphics.newMesh(vertices, "fan")
	mesh:setTexture(image)
	return mesh
end
2022-11-17T14_46_22-Untitled.png
2022-11-17T14_46_22-Untitled.png (26.15 KiB) Viewed 679 times
But what is Cricle Map?


Update:

Code: Select all

local createCricleMap = function(image, r)
	local cos, sin, pi = math.cos, math.sin, math.pi
	local d = 2*r
	local segments = math.round(pi/math.acos(1-.33/r))
	local vertices = {{0, 0, 0.5, 0.5, 1,1,1,1}}
	image:setWrap( "repeat" )
	for i=0, segments do
		local angle = (i / segments) * pi * 2
		local x = cos(angle) * r
		local y = sin(angle) * r
		local u = (0.5 + 4*x / d)
		local v = (0.5 + 4*y / d)
		vertices[#vertices+1] = {x, y, u, v, 1,1,1,0}
	end
	local mesh = love.graphics.newMesh(vertices, "fan")
	mesh:setTexture(image)
	return mesh
end
2022-11-17T17_08_06-Untitled.png
2022-11-17T17_08_06-Untitled.png (111.47 KiB) Viewed 644 times
Update2:

Code: Select all

local createCricleMap = function(image, r)
	local cos, sin, pi = math.cos, math.sin, math.pi
	local d = 2*r
	local segments = math.round(pi/math.acos(1-.33/r))
	local vertices = {{0, 0, 0.5, 0.5, 1,1,1,1}}
	image:setWrap( "repeat" )
	for i=0, segments do
		local angle = (i / segments) * pi * 2
		local x = cos(angle) * r
		local y = sin(angle) * r
		local u = (0.5 + 4*x / d)
		local v = (0.5 + 4*y / d)
		vertices[#vertices+1] = {x, y, u, v, 0,0,0,0}
	end
	local mesh = love.graphics.newMesh(vertices, "fan")
	mesh:setTexture(image)
	return mesh
end
2022-11-17T17_09_17-Untitled.png
2022-11-17T17_09_17-Untitled.png (95.97 KiB) Viewed 643 times
Last edited by darkfrei on Thu Nov 17, 2022 4:09 pm, edited 2 times in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Repeat texture in circle mesh (uv calculation)

Post by Bigfoot71 »

Wow! I tried with setWrap but I couldn't and I understand why now, thank you !
darkfrei wrote: Thu Nov 17, 2022 1:42 pm But what is Cricle Map?
And yes it's a small error on my part, I have a version in a "cheat sheet" folder called "createCircleMesh" and another that I put in my game that I renamed "createCircleMap" and I copied the one from the game in the demo by mistake.

Edit: ouch, I hadn't even noticed it said "Cricle" all this time thanks to auto-completion, shame on me :3
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 55 guests