Ios 在Cocos2D中切割多边形中的形状孔

Ios 在Cocos2D中切割多边形中的形状孔,ios,opengl-es,cocos2d-iphone,Ios,Opengl Es,Cocos2d Iphone,假设使用CCDrawNode创建的填充多边形: CCDrawNode *polygon = [CCDrawNode node]; CGPoint points[4]; points[0] = ccp(-20.0, -20.0); points[1] = ccp(20.0, -20.0); points[2] = ccp(20.0, 20.0); points[3] = ccp(-20.0, 20.0); [polygon drawPolyWithVerts:points count:4 fi

假设使用CCDrawNode创建的填充多边形:

CCDrawNode *polygon = [CCDrawNode node];

CGPoint points[4];
points[0] = ccp(-20.0, -20.0);
points[1] = ccp(20.0, -20.0);
points[2] = ccp(20.0, 20.0);
points[3] = ccp(-20.0, 20.0);

[polygon drawPolyWithVerts:points count:4 fillColor:[CCColor blackColor] borderWidth:0.0 borderColor:[CCColor clearColor]];
[self addChild:polygon];
我如何在它上切割成型的孔,以便达到类似的效果

孔将以各种配置动态生成,因此不可选择准备一组预制纹理

我尝试使用CCDrawNode并应用混合功能将孔创建为多边形时没有运气:

CCDrawNode *circle = [CCDrawNode node];
[circle drawDot:ccp(0.0, 0.0) radius:10.0 color:[CCColor whiteColor]];
[circle setBlendFunc:(ccBlendFunc){GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA}];
[self addChild:circle];

我相信我尝试了各种混合模式的组合,但没有找到合适的组合

我显然做错了什么,因为这看起来像是使用混合模式可以实现的。或者我必须重写
draw
方法并应用一些OpenGL ES代码吗?我试着玩OpenGL,但结果一事无成。另外,我不确定OpenGL ES(2.0+)与Cocos2D混合的首选方式是什么,因为我知道我应该避免在“即时模式”下使用它

但是如果这不是正确的方法,我想知道你的建议

顺便说一下——CCDrawNode似乎不支持开箱即用的
opacity
属性。有什么方法可以实现它吗?它仅以100%不透明度渲染其内容,而不考虑属性的值

我在iOS 7.1上使用CoCoS2D3.0。

你应该看看这个类。下面是一个例子:

// Square-shaped hole
CCDrawNode *square = [CCDrawNode node];
CGPoint squarePoints[4] = {ccp(-20.0, -20.0), ccp(20.0, -20.0), ccp(20.0, 20.0), ccp(-20.0, 20.0)};
[square drawPolyWithVerts:squarePoints count:4 fillColor:[CCColor blackColor] borderWidth:0.0 borderColor:[CCColor clearColor]];
square.position = ccp(0.0, 0.0);

// Triangle-shaped hole
CCDrawNode *triangle = [CCDrawNode node];
CGPoint trianglePoints[3] = {ccp(-10.0, 25.0), ccp(10.0, 25.0), ccp(0.0, 25.0 + 10.0 * sqrt(3.0))};
[triangle drawPolyWithVerts:trianglePoints count:3 fillColor:[CCColor blackColor] borderWidth:0.0 borderColor:[CCColor clearColor]];
triangle.position = ccp(0.0, 0.0);

// Prepare your stencil
CCNode *stencil = [CCNode node];
[stencil addChild:square];
[stencil addChild:triangle];

// Create a clipping node with the prepared stencil
CCClippingNode *clippingNode = [CCClippingNode clippingNodeWithStencil:stencil];

// Setting the clipping mode to inverted will result in the clipping node drawing its contents everywhere BUT the stencil's non-transparent areas
clippingNode.inverted = YES;

// Create your polygon
CCDrawNode *polygon = [CCDrawNode node];
CGPoint polygonPoints[4] = {ccp(-50.0, -50.0), ccp(40.0, -40.0), ccp(50.0, 50.0), ccp(-40.0, 40.0)};
[polygon drawPolyWithVerts:polygonPoints count:4 fillColor:[CCColor blackColor] borderWidth:0.0 borderColor:[CCColor clearColor]];
polygon.position = ccp(0.0, 0.0);
[clippingNode addChild:polygon];

// Now the clipping node should contain your polygon with shaped-holes in it

如果我没记错的话,使用
CCDrawNode
创建的填充圆在模具中无法正常工作,因此您可能需要创建自己的圆绘制类或使用纹理。

事实上,我需要为绘制圆创建一个附加类。非常感谢你!我应该浏览所有的内置类;int max=8;浮动ang=2*3.1415926/最大值;浮动长度=半径/PTM_比/cos(ang/2);b2Vec2*new1Vecs=(b2Vec2*)calloc(max,sizeof(b2Vec2));对于(int i=0;i
// Square-shaped hole
CCDrawNode *square = [CCDrawNode node];
CGPoint squarePoints[4] = {ccp(-20.0, -20.0), ccp(20.0, -20.0), ccp(20.0, 20.0), ccp(-20.0, 20.0)};
[square drawPolyWithVerts:squarePoints count:4 fillColor:[CCColor blackColor] borderWidth:0.0 borderColor:[CCColor clearColor]];
square.position = ccp(0.0, 0.0);

// Triangle-shaped hole
CCDrawNode *triangle = [CCDrawNode node];
CGPoint trianglePoints[3] = {ccp(-10.0, 25.0), ccp(10.0, 25.0), ccp(0.0, 25.0 + 10.0 * sqrt(3.0))};
[triangle drawPolyWithVerts:trianglePoints count:3 fillColor:[CCColor blackColor] borderWidth:0.0 borderColor:[CCColor clearColor]];
triangle.position = ccp(0.0, 0.0);

// Prepare your stencil
CCNode *stencil = [CCNode node];
[stencil addChild:square];
[stencil addChild:triangle];

// Create a clipping node with the prepared stencil
CCClippingNode *clippingNode = [CCClippingNode clippingNodeWithStencil:stencil];

// Setting the clipping mode to inverted will result in the clipping node drawing its contents everywhere BUT the stencil's non-transparent areas
clippingNode.inverted = YES;

// Create your polygon
CCDrawNode *polygon = [CCDrawNode node];
CGPoint polygonPoints[4] = {ccp(-50.0, -50.0), ccp(40.0, -40.0), ccp(50.0, 50.0), ccp(-40.0, 40.0)};
[polygon drawPolyWithVerts:polygonPoints count:4 fillColor:[CCColor blackColor] borderWidth:0.0 borderColor:[CCColor clearColor]];
polygon.position = ccp(0.0, 0.0);
[clippingNode addChild:polygon];

// Now the clipping node should contain your polygon with shaped-holes in it