着色器程序在 Cocos2d 2.2 Obj.C iOS 12 中不起作用

Shader Program not working in Cocos2d 2.2 Obj.C iOS 12(着色器程序在 Cocos2d 2.2 Obj.C iOS 12 中不起作用)

本文介绍了着色器程序在 Cocos2d 2.2 Obj.C iOS 12 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

  [lightRender beginWithClear:0 g:0 b:0 a:0];

    self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor];

    CC_NODE_DRAW_SETUP();

    CGPoint vertices[3];
    ccColor4F colors[3];

    CGPoint mid = BODY_POSITION(cursor.body);

    float radius = LIGHT_RANGE * lightRadius;             // - (CCRANDOM_0_1() * LIGHT_RANGE * .015);

    float initialAlpha = .7f;

    for (int i = 0; i < LIGHT_PRECISION; i++)
    {
        int nVertices = 0;
        float angle = 2*M_PI/LIGHT_PRECISION * i;
        float nextAngle = 2*M_PI/LIGHT_PRECISION * (i+1);
        int n = i + 1;
        if (n == LIGHT_PRECISION)
            n = 0;

        vertices[nVertices] = ccpAdd(mid, ccp(cosf(angle) * radius * lightLength[i], sinf(angle) * radius * lightLength[i]));
        colors[nVertices++] = (ccColor4F){1, 1, .9, initialAlpha * (1 - lightLength[i])};
        vertices[nVertices] = ccpAdd(mid, ccp(cosf(nextAngle) * radius * lightLength[n], sinf(nextAngle) * radius * lightLength[n]));
        colors[nVertices++] = (ccColor4F){1, 1, .9, initialAlpha * (1 - lightLength[n])};
        vertices[nVertices] = mid;
        colors[nVertices++] = (ccColor4F){1, 1, .8, initialAlpha};

        ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position  | kCCVertexAttribFlag_Color);

        glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
        glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
    }

    [lightRender end];

CC_NODE_DRAW_SETUP 代码:

CC_NODE_DRAW_SETUP Code:

#define CC_NODE_DRAW_SETUP()                                                                    
do {                                                                                            
    ccGLEnable( _glServerState );                                                               
    NSAssert1(_shaderProgram, @"No shader program set for node: %@", self);                     
    [_shaderProgram use];                                                                       
    [_shaderProgram setUniformsForBuiltins];                                    
} while(0)

如何使用 Cocos2d 2.2 Obj.C 项目修复 iOS12 中的着色器代码?如果我在 iOS 7 模拟器中运行,它可以完美运行..相同的代码在 iOS 12 中不起作用

How to fix shader code in iOS12 with Cocos2d 2.2 Obj.C project ? If I run in iOS 7 simulator it works perfect..same code not working in iOS 12

推荐答案

修复 Cocos2d 中的 Shaders 问题.

Fixed Shaders Problem in Cocos2d.

在上面的代码中,主要问题是 CGFloat...将它转换为所有顶点的浮点数.

In Above code, main problem is CGFloat...converted it to float for all vertices.

我还从 Cocos2d-x 中获取了与着色器相关的文件,并将其转换为 Cocos2d 的 obj.C.

Also I took shaders related files from Cocos2d-x and converted it to obj.C for Cocos2d.

现在所有着色器都可以在 Cocos2d v2.2 Obj.C iOS 12、iPhoneX/XR/XSMax 中完美运行..

Now all shaders working Perfect in Cocos2d v2.2 Obj.C iOS 12, iPhoneX/XR/XSMax also..

编码愉快!干杯:)

这篇关于着色器程序在 Cocos2d 2.2 Obj.C iOS 12 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:着色器程序在 Cocos2d 2.2 Obj.C iOS 12 中不起作用

基础教程推荐