love.graphics.newMesh
Available since LÖVE 0.9.0 |
This function is not supported in earlier versions. |
Creates a new Mesh.
This function can be slow if it is called repeatedly, such as from love.update or love.draw. If you need to use a specific resource often, create it once and store it somewhere it can be reused! |
Contents
Function
Synopsis
mesh = love.graphics.newMesh( vertices, texture, mode )
Arguments
table vertices
- The table filled with vertex information tables for each vertex as follows:
number [1]
- The position of the vertex on the x-axis.
number [2]
- The position of the vertex on the y-axis.
number [3]
- The u texture coordinate. Texture coordinates are normally in the range of [0, 1], but can be greater or less (see WrapMode.)
number [4]
- The v texture coordinate. Texture coordinates are normally in the range of [0, 1], but can be greater or less (see WrapMode.)
number [5] (255)
- The red color component.
number [6] (255)
- The green color component.
number [7] (255)
- The blue color component.
number [8] (255)
- The alpha color component.
Texture texture (nil)
- The Image or Canvas to use when drawing the Mesh. May be nil to use no texture.
MeshDrawMode mode ("fan")
- How the vertices are used when drawing. The default mode "fan" is sufficient for simple convex polygons.
Returns
Mesh mesh
- The new mesh.
Examples
Creates and draws a Mesh identical to a normal drawn image but with different colors at each corner
function love.load()
image = love.graphics.newImage("pig.png")
local vertices = {
{
-- top-left corner (red-tinted)
0, 0, -- position of the vertex
0, 0, -- texture coordinate at the vertex position
255, 0, 0, -- color of the vertex
},
{
-- top-right corner (green-tinted)
image:getWidth(), 0,
1, 0, -- texture coordinates are in the range of [0, 1]
0, 255, 0
},
{
-- bottom-right corner (blue-tinted)
image:getWidth(), image:getHeight(),
1, 1,
0, 0, 255
},
{
-- bottom-left corner (yellow-tinted)
0, image:getHeight(),
0, 1,
255, 255, 0
},
}
-- the Mesh DrawMode "fan" works well for 4-vertex Meshes.
mesh = love.graphics.newMesh(vertices, image, "fan")
end
function love.draw()
love.graphics.draw(mesh, 0, 0)
end
Creates and draws a textured circle with a red tint at the center.
function CreateTexturedCircle(image, segments)
segments = segments or 40
local vertices = {}
-- The first vertex is at the center, and has a red tint. We're centering the circle around the origin (0, 0).
table.insert(vertices, {0, 0, 0.5, 0.5, 255, 0, 0})
-- Create the vertices at the edge of the circle.
for i=0, segments do
local angle = (i / segments) * math.pi * 2
-- Unit-circle.
local x = math.cos(angle)
local y = math.sin(angle)
-- Our position is in the range of [-1, 1] but we want the texture coordinate to be in the range of [0, 1].
local u = (x + 1) * 0.5
local v = (y + 1) * 0.5
-- The per-vertex color defaults to white.
table.insert(vertices, {x, y, u, v})
end
-- The "fan" draw mode is perfect for our circle.
return love.graphics.newMesh(vertices, image, "fan")
end
function love.load()
image = love.graphics.newImage("pig.png")
mesh = CreateTexturedCircle(image)
end
function love.draw()
local radius = 100
local mx, my = love.mouse.getPosition()
-- We created a unit-circle, so we can use the scale parameter for the radius directly.
love.graphics.draw(mesh, mx, my, 0, radius, radius)
end
Creates a circle and draws it more efficiently than love.graphics.circle.
function CreateCircle(segments)
segments = segments or 40
local vertices = {}
-- The first vertex is at the origin (0, 0) and will be the center of the circle.
table.insert(vertices, {0, 0})
-- Create the vertices at the edge of the circle.
for i=0, segments do
local angle = (i / segments) * math.pi * 2
-- Unit-circle.
local x = math.cos(angle)
local y = math.sin(angle)
table.insert(vertices, {x, y})
end
-- The "fan" draw mode is perfect for our circle. We aren't texturing it, so we leave that argument as nil.
return love.graphics.newMesh(vertices, nil, "fan")
end
function love.load()
mesh = CreateCircle()
end
function love.draw()
local radius = 100
local mx, my = love.mouse.getPosition()
-- We created a unit-circle, so we can use the scale parameter for the radius directly.
love.graphics.draw(mesh, mx, my, 0, radius, radius)
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info