Difference between revisions of "love.graphics.newMesh"

(Added a couple examples)
m (Creates and draws a textured circle with a red tint at the center.: Fixed the number of loop iterations)
Line 74: Line 74:
 
 
 
-- Create the vertices at the edge of the circle.
 
-- Create the vertices at the edge of the circle.
for theta=0, math.pi*2, (math.pi*2)/segments do
+
for i=0, segments do
 +
local theta = (i / segments) * math.pi * 2
 +
 
 
-- Unit-circle.
 
-- Unit-circle.
 
local x = math.cos(theta)
 
local x = math.cos(theta)

Revision as of 00:02, 16 December 2013

Available since LÖVE 0.9.0
This function is not supported in earlier versions.

Creates a new Mesh.

O.png 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!  



Function

Synopsis

mesh = love.graphics.newMesh( vertices, image, 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.
Image image (nil)
The image to use when drawing the Mesh. May be nil to use no image.
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)
	local 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 theta = (i / segments) * math.pi * 2

		-- Unit-circle.
		local x = math.cos(theta)
		local y = math.sin(theta)
		
		-- 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

See Also


Other Languages