如何实现水波纹?

9

我正在开发一款iPhone游戏。其中需要制作水波纹效果,但我不知道怎样实现。我听说可以用OpenGL来实现,但我对这个概念非常陌生。请问有人能指导我吗?


你最终实现了效果吗? - Thanks
3个回答

14

2

jk:

z=sin(x)+cos(y)

更严重的是,Quartz Composer基本上作为一个效果层为您完成了涟漪效果吗?或者这只是针对iPhone 3.0 SDK宣布的?


那是一个非常昂贵的例程。在游戏中,它绝对会让OpenGL崩溃。我猜他想要一个不错的水效果,这意味着老派的纹理映射技巧。 - pestilence669
抱歉,我知道。我只是开玩笑的。 - dlamblin

0

我找到了水波纹效果的源代码,以下是实现该效果并解决您的问题的代码:

import "HelloWorldLayer.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];
    
    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];
    
    // add layer as a child to scene
    [scene addChild: layer];
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    
    if( (self=[super init])) {

        rippleImage = [ pgeRippleSprite ripplespriteWithFile:@"image_old.png" ];
        [ self addChild:rippleImage ];
        CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello Cocos2D Forum" fontName:@"Marker Felt" fontSize:16];
        label.position = ccp( 80 , 300 );
        [self addChild: label];
        [ [ CCTouchDispatcher sharedDispatcher ] addTargetedDelegate:self priority:0 swallowsTouches:YES ]; 
        
        // schedule update
        [ self schedule:@selector( update: ) ];    
                
    }
    return self;
}

float runtime = 0;

-( BOOL )ccTouchBegan:( UITouch* )touch withEvent:( UIEvent* )event {
    runtime = 0.1f;
    [ self ccTouchMoved:touch withEvent:event ];
    return( YES );
}

-( void )ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint pos;
    
    if ( runtime >= 0.1f ) {
        
        runtime -= 0.1f;
        
        // get touch position and convert to screen coordinates
        pos = [ touch locationInView: [ touch view ] ];
        pos = [ [ CCDirector sharedDirector ] convertToGL:pos ];
    
        // [ rippleImage addRipple:pos type:RIPPLE_TYPE_RUBBER strength:1.0f ];    
        [ rippleImage addRipple:pos type:RIPPLE_TYPE_WATER strength:2.0f ];  
        
        
    }
}

-( void )update:( ccTime )dt {
    
    runtime += dt;
    
    [ rippleImage update:dt ];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)
    
    // don't forget to call "super dealloc"
    [super dealloc];
}
@end

你也可以从Git下载源代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接