gpt4 book ai didi

cocos2d-iphone - Cocos2d 中的旋转剪切节点

转载 作者:行者123 更新时间:2023-12-01 04:00:03 25 4
gpt4 key购买 nike

我在这里使用裁剪节点:http://www.learn-cocos2d.com/2011/01/cocos2d-gem-clippingnode

剪辑节点.h

#import "cocos2d.h"
@interface ClippingNode : CCNode {
CGRect clippingRegionInNodeCoordinates;
CGRect clippingRegion;
}
@property (nonatomic) CGRect clippingRegion;
@end

剪裁节点.m
#import "ClippingNode.h"

@interface ClippingNode (PrivateMethods)
-(void) deviceOrientationChanged:(NSNotification*)notification;
@end

@implementation ClippingNode
-(CGRect) clippingRegion {
return clippingRegionInNodeCoordinates;
}

-(void) setClippingRegion:(CGRect)region {

// keep the original region coordinates in case the user wants them back unchanged
clippingRegionInNodeCoordinates = region;
self.position = clippingRegionInNodeCoordinates.origin;
self.contentSize = clippingRegionInNodeCoordinates.size;

// convert to retina coordinates if needed
region = CC_RECT_POINTS_TO_PIXELS(region);

// respect scaling
clippingRegion = CGRectMake(region.origin.x * scaleX_, region.origin.y * scaleY_,
region.size.width * scaleX_, region.size.height * scaleY_);
}

-(void) setScale:(float)newScale {
[super setScale:newScale];
// re-adjust the clipping region according to the current scale factor
[self setClippingRegion:clippingRegionInNodeCoordinates];
}

-(void) deviceOrientationChanged:(NSNotification*)notification {
// re-adjust the clipping region according to the current orientation
[self setClippingRegion:clippingRegionInNodeCoordinates];
}

-(void) visit {

glEnable(GL_SCISSOR_TEST);
CGPoint worldPosition = [self convertToWorldSpace:CGPointZero];
const CGFloat s = [[CCDirector sharedDirector] contentScaleFactor];
glScissor((clippingRegion.origin.x) + (worldPosition.x*s), (clippingRegion.origin.y) + (worldPosition.y*s),
(clippingRegion.size.width), (clippingRegion.size.height));

[super visit];

glDisable(GL_SCISSOR_TEST);

}
@end

但是,我需要剪辑一个旋转的 CCNode。关于我如何完成这样的任务的任何想法?

最佳答案

用这个替换类 ClippingNode 中的访问方法

-(void) visit
{

float rotationAngle = 15;

glPushMatrix();

CCRenderTexture* renderTexture = [[CCRenderTexture renderTextureWithWidth:512 height:512] retain];

glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, clippingRegion.size.width, clippingRegion.size.height);

[renderTexture begin];

glPushMatrix();
glRotatef(rotationAngle, 0, 0, 1);
glTranslatef(-clippingRegion.origin.x, -clippingRegion.origin.y, 0);

[super visit];

glPopMatrix();

[renderTexture end];

glDisable(GL_SCISSOR_TEST);

renderTexture.sprite.position = CGPointMake(clippingRegion.origin.x , clippingRegion.origin.y);
renderTexture.sprite.anchorPoint = CGPointMake(0, 1);
renderTexture.sprite.rotation = rotationAngle;
[renderTexture.sprite visit];

[renderTexture release];

glPopMatrix();
}

基本上它会创建一个纹理来渲染 ClippingNode 内容

然后平移场景,使剪切区域的原点现在位于 (0,0)

通过rotationAngle旋转整个场景

启用剪刀

渲染场景

平移、旋转和渲染包含纹理的 Sprite

enter image description here

关于cocos2d-iphone - Cocos2d 中的旋转剪切节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14073785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com