It looks like you're new here. If you want to get involved, click one of these buttons!
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) |
Comments
Its because of is3d flag that is passed to a Mesh constructor first. So the fix is very simple
Likes: MoKaLux
Likes: MoKaLux
Likes: hgy29, MoKaLux, oleg, pie, SinisterSoft
in the past i had a game idea based on shadows/lights and i tried to make such a demo using the clipper polygon library, but it was too slow, and then soon shadow-like game mechanics were used in plenty of games, so my idea became obsolete. i also needed a way to determine if a point is in the shadow or lighted by at least one light, i don't know if that's doable with your approach.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: MoKaLux, pie, talis, SinisterSoft