美文网首页
使用OPenGL ES实现粒子发射器

使用OPenGL ES实现粒子发射器

作者: zhongxiaoyue | 来源:发表于2019-06-23 18:23 被阅读0次

    粒子发射器

    • 粒子发射器实现思路


      粒子发射器

    使用粒子发射器

    使用粒子发射器
    • 示例代码
    1. 设置Context
        self.mContext = [[EAGLContext alloc] initWithAPI:(kEAGLRenderingAPIOpenGLES3)];
        [EAGLContext setCurrentContext:self.mContext];
        GLKView *view = (GLKView *)self.view;
        view.context = self.mContext;
        view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
        view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
        [EAGLContext setCurrentContext:self.mContext];
    
    1. 加载纹理
        NSString *path = [[NSBundle bundleForClass:[self class]]
                          pathForResource:@"ball" ofType:@"png"];
        
        if (path == nil) {
            NSLog(@"ball texture image not found");
            return;
        }
        
        self.ballParticleTexture = [GLKTextureLoader textureWithContentsOfFile:path options:nil error:nil];
        
        self.particleEffect = [[XYPointParticleEffect alloc] init];
        self.particleEffect.texture2d0.name = self.ballParticleTexture.name;
        self.particleEffect.texture2d0.target = self.ballParticleTexture.target;
    
    1. 设置渲染格式
        // 开启深度测试
        glEnable(GL_DEPTH_TEST);
        
        // 开启混合
        glEnable(GL_BLEND);
        
        // 设置混合模式
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    1. 设置添加粒子参数
    void(^block)() = ^{
            
            self.autoSpawnDelta = 0.5f;
            
            //重力
            self.particleEffect.gravity = GLKVector3Make(0.0f, 0.0f, 0.0f);
            
            int n = 100;
            for(int i = 0; i < n; i++)
            {
                //X,Y,Z速度
                float randomXVelocity = -0.5f + 1.0f * (float)random() / (float)RAND_MAX;
                float randomYVelocity = -0.5f + 1.0f * (float)random() / (float)RAND_MAX;
                float randomZVelocity = -0.5f + 1.0f * (float)random() / (float)RAND_MAX;
                
                //创建粒子
                [self.particleEffect
                 addParticleAtPosition:GLKVector3Make(0.0f, 0.0f, 0.0f)
                 velocity:GLKVector3Make(
                                         randomXVelocity,
                                         randomYVelocity,
                                         randomZVelocity)
                 force:GLKVector3Make(0.0f, 0.0f, 0.0f)
                 size:4.0f
                 lifeSpanSeconds:3.2f
                 fadeDurationSeconds:0.5f];
            }
            
        };
    
    1. 更新
    - (void)update {
        
        NSTimeInterval timeElapsed = self.timeSinceFirstResume;
        
        self.particleEffect.elapsedSeconds = timeElapsed;
        
        if (self.autoSpawnDelta < (timeElapsed - self.lastSpawnTime)) {
            
            self.lastSpawnTime = timeElapsed;
            
            //执行block
            block();
        }
    }
    
    1. 绘制
    - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
        glClearColor(0.3, 0.3, 0.3, 1);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        
        
        //准备绘制
        [self.particleEffect prepareToDraw];
        
        //绘制
        [self.particleEffect draw];
    }
    

    Demo

    Demo下载地址

    相关文章

      网友评论

          本文标题:使用OPenGL ES实现粒子发射器

          本文链接:https://www.haomeiwen.com/subject/eqdwqctx.html