Page 1 of 1

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

Posted: Thu Nov 17, 2022 1:10 pm
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.

Re: Repeat texture in circle mesh (uv calculation)

Posted: Thu Nov 17, 2022 1:42 pm
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 902 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 867 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 866 times

Re: Repeat texture in circle mesh (uv calculation)

Posted: Thu Nov 17, 2022 2:19 pm
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