[SOLVED] Draw texture mesh in polygon with n points

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
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

[SOLVED] Draw texture mesh in polygon with n points

Post by AdrianN »

Hi everyone, I was recently able to finish creating a destructible environment, using box2d and polygon library. I'm working to optimizate it.

I'm using chain shape.

https://github.com/AlexarJING/polygon

Image

Now, I need to draw with textures. I'm using mesh to draw on the polygon. The problem is, the polygons are almost random.
My texture is a quad of a spritesheet.
I don't understand about u,v texture coordinate and how to calculate dynamically.
I create the polygon in tiled without any specific order. Example polygon_1 have 7 points, polygon_2 10 points.


source code

Code: Select all

--arrecife.lua
local Class = require "libs.hump.class"
local molde_objetos = require "entidades.animacion.objetos.molde_objetos"
local polybool = require "libs.polygon.polybool"
local modelo_destruccion_otros  = require "entidades.logica.modelos.modelo_destruccion_otros"

local arrecife = Class{
  __includes = {modelo_destruccion_otros} 
}

function arrecife:init(polygon,entidades)
  self.tipo_indice=6
  
  self.entidades=entidades
  
  self.poligono ={}
  local vertices = {}
  
  if type(polygon[1]) == "table" then
    for _,data in ipairs(polygon) do
      --polygon
      table.insert(self.poligono,data.x)
      table.insert(self.poligono,data.y)
    end
  else
    self.poligono = polygon
  end
  
  --mesh
  for i=1,#self.poligono,2 do
    table.insert(vertices,{self.poligono[i],self.poligono[i+1],0,1,255, 255, 255})
  end
  
	self.entidades:add_obj("destruible",self)
  
  self.collider=py.newBody(self.entidades.world,0,0,"kinematic")
  self.shape=py.newChainShape( true,self.poligono  )
	self.fixture=py.newFixture(self.collider,self.shape)
  
  self.fixture:setUserData( {data="destruible",obj=self, pos=10} )
  
  self.mesh= lg.newMesh(vertices, "fan")
  
  
  self.img=img.objetos["image"]
  self.quad=img.objetos[6]
  
  self.mesh:setTexture(self.img,self.quad)
  
  
  modelo_destruccion_otros.init(self,"destruible")
  
  self.otro_poligono=nil
end

function arrecife:update(dt)
  if self.otro_poligono then
    self:nuevo_poligono(self.otro_poligono)
  end
end

function arrecife:draw()
  lg.draw(self.mesh, self.ox,self.oy)
end

function arrecife:nuevo_poligono(poligono_enemigo)
  
  local nuevo_poligono = polybool(self.poligono, poligono_enemigo, "not")
  
  if #nuevo_poligono<4 then
    for i=1, #nuevo_poligono ,1 do
        arrecife(nuevo_poligono[i],self.entidades)
      
    end
  end
    
  self:remove() 

end

function arrecife:poligono_recorte(x,y)
  local dis=2.5
  self.otro_poligono =  {-5*dis+x,-8.66*dis+y,
  5*dis+x,-8.66*dis+y,
  10*dis+x,0*dis+y,
  5*dis+x,8.66*dis+y,
  -5*dis+x,8.66*dis+y,
  -10*dis+x,0*dis+y}
end

return arrecife
Last edited by AdrianN on Mon Jul 01, 2019 5:55 pm, edited 3 times in total.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Draw texture mesh in polygon with n points

Post by pgimeno »

The UVs are texture coordinates in the range 0..1, where 0 is the left (for u) or bottom (for v) side, and 1 is the right or top side. In wrap mode, values below 0 or above 1 cause copies of the texture to be drawn.

What you need to do first is decide how the polygon fits in the texture. I guess it's larger than the texture, because you want the texture to repeat, therefore you will need U and V coordinates above 1. Each vertex needs to be assigned a coordinate in the texture. So you need a coordinate transform from vertex coordinates to texture coordinates, and assign each vertex a UV.

So for example, if you want your texture to appear as 5x5 units of world coordinates, you take the vertex coordinate (in world coordinates) and divide it by 5 (that's the transform in this case), and you have the UV.

Problem is, I don't think that will work with quads, because quads themselves are portions of textures, not textures, therefore they can't be wrapped with the same method. You may need to use a full texture for that.
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: Draw texture mesh in polygon with n points

Post by AdrianN »

pgimeno wrote: Sun Jun 30, 2019 11:25 am The UVs are texture coordinates in the range 0..1, where 0 is the left (for u) or bottom (for v) side, and 1 is the right or top side. In wrap mode, values below 0 or above 1 cause copies of the texture to be drawn.

What you need to do first is decide how the polygon fits in the texture. I guess it's larger than the texture, because you want the texture to repeat, therefore you will need U and V coordinates above 1. Each vertex needs to be assigned a coordinate in the texture. So you need a coordinate transform from vertex coordinates to texture coordinates, and assign each vertex a UV.

So for example, if you want your texture to appear as 5x5 units of world coordinates, you take the vertex coordinate (in world coordinates) and divide it by 5 (that's the transform in this case), and you have the UV.

Problem is, I don't think that will work with quads, because quads themselves are portions of textures, not textures, therefore they can't be wrapped with the same method. You may need to use a full texture for that.
Yes, I splited in a image my texture.
I've already been able to replicate the logic in a polygon with 5 vertices.

https://stackoverflow.com/questions/316 ... e-result-i
I found a code searching the web, but I couldn't understand it, so far.
I still need a way to auto generate, but it's already a start.

Thanks pgimeno .
Attachments
texture.love
(13.05 KiB) Downloaded 134 times
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] Draw texture mesh in polygon with n points

Post by pgimeno »

That's the part about deciding how you want to apply the texture to the mesh. That's what I meant when I said that "What you need to do first is decide how the polygon fits in the texture".

I was under the impression that you were after something like the attached. Would you please clarify how you want the texture applied?
Attachments
mesh-texture.love
(3.92 KiB) Downloaded 186 times
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: [SOLVED] Draw texture mesh in polygon with n points

Post by AdrianN »

pgimeno wrote: Mon Jul 01, 2019 10:25 am That's the part about deciding how you want to apply the texture to the mesh. That's what I meant when I said that "What you need to do first is decide how the polygon fits in the texture".

I was under the impression that you were after something like the attached. Would you please clarify how you want the texture applied?
Yes, something similar I was looking to fill my polygon. I just was didn't understand how mesh working.
I was looking for a way to repeat the texture several times inside the polygon, and let it adapt to the clipping.

I just quick tested using the "fan" mode in my game and the texture initially adapts, but after some clippings the texture overflows.
Image

Edit: I just adapted your code to my polygon and it looks like it was what I needed. Function works fine.Thanks

Image
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] Draw texture mesh in polygon with n points

Post by pgimeno »

AdrianN wrote: Mon Jul 01, 2019 5:24 pm I just quick tested using the "fan" mode in my game and the texture initially adapts, but after some clippings the texture overflows.
I think you're confusing the texture with the triangles that make the mesh. A mesh is a set of triangles, exactly like the ones that are drawn in 3D engines. What you see is some of the triangles overflowing, with a texture applied.

The fan option in mesh works by adding a triangle from one of the vertices to each pair of the other vertices. I believe that's the technique that love.graphics.polygon("fill", ...) uses. It works well with convex polygons, like this:

Image

(you can see why it's called "fan" [abanico, in Spanish]). But when you're using non-convex polygons, it fails because it can create a triangle that overlaps one of the areas. I can't find any good image that shows the problem and I'm lazy to make one, sorry.

That's where love.math.triangulate comes to the rescue. It can subdivide any polygon into triangles even when they are concave, as long as it does not self-intersect (yours does not, so you're fine). These triangles don't necessarily follow any of the patterns offered by any of the automatic mesh triangulation options (fan or strip), so they need to be drawn one by one (triangle list).

AdrianN wrote: Mon Jul 01, 2019 5:24 pm Edit: I just adapted your code to my polygon and it looks like it was what I needed. Function works fine.Thanks
Woot!
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: [SOLVED] Draw texture mesh in polygon with n points

Post by AdrianN »

Sure, I'm a newbie using mesh, but I'm getting what you mean and theory, hehe.
I didn't know about the triangles and that application in polygon. Annotated.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 207 guests