Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Problem with mesh — Gideros Forum

Problem with mesh

rrraptorrrraptor Member
edited April 2020 in General questions
I have a simple class that allows me to create a circle mesh. BUT, I have a very strange bug, that I cant fix, because I have no idea why it happens. The problem is that when I try to apply color to a mesh by passing a value to an object constructor, whole mesh just breaks, but with default color it works perfectly.
Gif:

Code:
local cos,sin=math.cos,math.sin
 
-- r - raidus
-- color - color of all vertices
-- alpha - alpha of all vertices
-- tex - texture to use 
-- scaleTexture - flag to scale texture if texture size is bigger (or smaller) than circle radius
local function precalculateCircle(r, color, alpha, tex, scaleTexture)
	color = color or 0xffffff
	alpha = alpha or 1
 
	--print(">",("0x%06x"):format(color),alpha)
 
	local _vertices = {}
	local _indices = {}
	local _colors = {}
	local _texture = {}
 
	-- texture scale factor
	local tsW = 1
	local tsH = 1
 
	-- calculate it if needed
	if tex and scaleTexture then 
		tsW = tex:getWidth() / (r*2)
		tsH = tex:getHeight() / (r*2)
	end
 
	-- MAX_MESH_POINTS is a macros. Its maximum amount of vertices.
	local step = 360 / MAX_MESH_POINTS
	-- vertex count
	local vCount = 1
	-- total amomunt of points count
	local nCount = 0
	-- indices count
	local iCount = 0
	for i = step, 360, step do 
		-- deeg to rad
		local a = ^<i
		-- calculate point position
		local cosa = cos(a)
		local sina = sin(a)
		local x = cosa * r
		local y = sina * r
 
		nCount += 1
 
		-- fill arrays with data (:
		_vertices[vCount + 0] = x
		_vertices[vCount + 1] = y
		_colors[vCount + 0] = color
		_colors[vCount + 1] = alpha
 
		_texture[vCount + 0] = (x * tsW) + (r * tsW)
		_texture[vCount + 1] = (y * tsH) + (r * tsH)	
 
		if nCount <= 3 then 
			_indices[nCount] = nCount
			iCount += 1
		else
			_indices[iCount + 1] = 1
			_indices[iCount + 2] = nCount-1
			_indices[iCount + 3] = nCount
			iCount += 3
		end
		vCount += 2
	end
 
	return _vertices,_indices,_colors,_texture
end
 
local Cache = {}
MeshBuilder = Core.class(Mesh)
 
function MeshBuilder:init(color, alpha)
	self._color = color or 0xffffff
	self._alpha = alpha or 1
 
	self._textures = {}
	self._textureAutoScale = true
end
--
function MeshBuilder:setTexture(texture, slot)
	slot = slot or 0
	Mesh.setTexture(self, texture, slot)
	self._textures[slot] = texture
end
--
function MeshBuilder:setTextureAutoScale(flag)
	self._textureAutoScale = flag
end
--
function MeshBuilder:clearCache()
	Cache = nil
	collectgarbage()
	Cache = {}
end
--
function MeshBuilder:createCircle(r, forceUpdate, textureSlot)
	textureSlot = textureSlot or 0
	-- take data from chache to not generate same circle over and over again
	local data = Cache[r]
	-- if there is no cached circle of that radius, generate data for it
	if not data or forceUpdate then 
		-- generate vertices positions, colors for each of them, texture coords and mesh indices
		local v,i,c,t = precalculateCircle(r, self._color, self._alpha, self._textures[textureSlot], self._textureAutoScale)
		-- add to chache
		Cache[r] = {v=v,i=i,c=c,t=t}
		-- set mesh data
		self:setVertexArray(v)
		self:setColorArray(c)
		self:setIndexArray(i)
		self:setTextureCoordinateArray(t)
	-- if there is a cached data, use it
	else
		self:setVertexArray(data.v)
		self:setIndexArray(data.i)
		self:setColorArray(data.c)
		self:setTextureCoordinateArray(data.t)
	end
end
-----------------------------------
------------ Test code ------------
-----------------------------------
application:setBackgroundColor(0x323232)
 
local tex = Texture.new("t.png", true)
 
local test = MeshBuilder.new(0xff00000)
--test:setTexture(tex)
test:createCircle(64)
stage:addChild(test)
 
function mouseMove(e)
	test:setPosition(e.x,e.y)
end
 
stage:addEventListener(Event.MOUSE_MOVE, mouseMove)
bug.gif 319.1K

Comments

Sign In or Register to comment.