2012-12-28 60 views
4

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

ClippingNode.h

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

ClippingNode.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。有关我如何完成这样的任务的任何想法?

+0

通过使节点到纹理然后使用的sprite纹理可旋转独立。它是否需要用户与节点交互?如果是这样,你需要在两者之间做一些坐标转换。 – rraallvv

+0

是的,用户正在缩放,平移,在旋转的框架中旋转图片。不知道我应该在哪里创建CCRenderTexture?在ClippingNode ClippingNode的父级? – docchang

回答

0

它只需要CCRenderTexture完成任务的精灵。任何建议将不胜感激。

ClippingNode.h

#import "cocos2d.h" 
@interface ClippingNode : CCNode 
@property (nonatomic, assign) CGSize clippingSize; 
@end 

ClippingNode.m

#import "ClippingNode.h" 
@interface ClippingNode() 
@property (nonatomic, strong) CCRenderTexture * renderTexture; 
@property (nonatomic, strong) CCSprite * clippedSprite; 
@end 


@implementation ClippingNode 
@synthesize renderTexture; 
@synthesize clippedSprite; 
@synthesize clippingSize; 

- (void) setClippingSize:(CGSize)newClippingSize { 

    //assignment 
    clippingSize = newClippingSize; 

    //set contentSize 
    self.contentSize = clippingSize; 

    //configure renderTexture 
    self.renderTexture = [CCRenderTexture renderTextureWithWidth:clippingSize.width height:clippingSize.height]; 
    renderTexture.contentSize = CGSizeMake(clippingSize.width, clippingSize.height); 

    //configure the renderTexture sprite 
    self.clippedSprite = [CCSprite spriteWithTexture:renderTexture.sprite.texture]; 
    clippedSprite.position = self.position; 
    clippedSprite.rotation = rotation_; 
    clippedSprite.scaleY = -1; 
} 

- (void)visit { 
    [renderTexture beginWithClear:0 g:0 b:0 a:1]; 
    [super visit]; 
    [renderTexture end]; 

    [clippedSprite visit]; 
} 

@end 

用法:

CCSprite * spriteImage = ...; 
spriteImage.position = ccp(0,0); 
spriteImage.anchorPoint = ccp(0.5,0.5); 

//configure clipping Node 
self.clippingNode = [ClippingNode node]; 
clippingNode.position = ccp(size.width * 0.5f, size.height * 0.5f); 
clippingNode.anchorPoint = ccp(0.5f, 0.5f); 
clippingNode.rotation = -10.0f; 

//configure clipping region 
[clippingNode setClippingSize:CGSizeMult(spriteImage.boundingBox.size, 1.5f)]; 

//add content to the clipping node 
[clippingNode addChild:spriteImage] 
2

-(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访问方法基本上它创建一个纹理何处渲染的ClippingNode内容

然后翻译的一幕让在剪辑区域的原点是现在在(0,0)

旋转通过rotationAngle

整个场景

启用剪刀

渲染场景

平移,旋转,并呈现包含纹理

enter image description here

+1

我正在使用Cocos2d 2.0。你如何翻译glRotatef和glTranslatef到2.0? – docchang

+0

自从我使用cocos2d已经有一段时间了,还没有用过2.0,我认为它只使用OpenGL ES 2.0,所以它需要在着色器中添加旋转和平移。我正在下载Cocos2d 2.0来测试和更新我的代码 – rraallvv

+0

我正在窃听代码。当使用RenderTexture而不使用'glScissor'时,剪切效果也适用。然而,我的形象和裁剪的位置有点让我感到困惑。我无法得到它我想要的地方。 – docchang