SpriteKit着色器在iOS 9上崩溃:SKDefaultShading、gl_FragCoord。

11

我刚刚安装了iOS 9公测版(版本3),现在使用SpriteKit着色器遇到了很多问题。在iOS 8上,以下代码工作得很好:

_fontShader = [SKShader shaderWithFileNamed:@"TheShader"]; // TODO: iOS9 compatibility issues here
_fontUniform = [SKUniform uniformWithName:@"labelBase" float:0];
[self.fontShader addUniform:self.fontUniform]; // TODO: iOS9 compatibility issues here

_fontEffects = [SKEffectNode node];
self.fontEffects.shader = self.fontShader; // TODO: iOS9 compatibility issues here
self.fontEffects.shouldEnableEffects = YES;
self.fontEffects.shouldCenterFilter = NO;
self.fontEffects.shouldRasterize = YES;
[self addChild:self.fontEffects];

编辑:文件"TheShader.fsh"看起来像这样:

float yPos = gl_FragCoord.y - labelBase; // iOS 9 Compatibility issues here
float gradient = 0.35*(yPos / u_sprite_size.y); // ranges from 0 at base to 0.35 at top

vec4 color = SKDefaultShading(); // the current label color (iOS 9 Compatibility issues here)
color = vec4(gradient + color.r, gradient + color.g, gradient + color.b, color.a);
color.rgb *= color.a; // set background to alpha 0

gl_FragColor = color;

iOS 9系统中,控制台会以以下格式不断输出一堆警告:

2015-07-12 22:43:17.717 ReconInForce[599:110531] Jet: Error Domain=MTLLibraryErrorDomain Code=3 "Compilation failed: 

program_source:8:18: error: use of undeclared identifier 'gl_FragCoord'
    float yPos = gl_FragCoord.y - labelBase[0];
                 ^
program_source:11:18: error: use of undeclared identifier 'SKDefaultShading'
    vec4 color = SKDefaultShading(); // the current label color
             ^
" UserInfo=0x158c084b0 {NSLocalizedDescription=Compilation failed: 

program_source:8:18: error: use of undeclared identifier 'gl_FragCoord'
    float yPos = gl_FragCoord.y - labelBase[0];
             ^
program_source:11:18: error: use of undeclared identifier 'SKDefaultShading'
    vec4 color = SKDefaultShading(); // the current label color
             ^
}

似乎iOS9不支持SKDefaultShading和gl_FragCoord。如何在不使用gl_FragCoord的情况下找到本地像素尺寸?

控制台会一直重复此消息,导致设备变慢。以下是屏幕截图: enter image description here

如果我注释掉上面写着“iOS9兼容性问题”的行,则问题会解决,但我就没有了任何着色器。

还有人遇到这个问题吗?非常感谢帮助。


这很可能是他们那边的一个漏洞。我今天正在更新一些东西,也遇到了这些问题。 - Tony
你解决了吗?我在Xcode 7.0和iOS 9.1公测版1上遇到了同样的问题。 - CloakedEddy
还没有,如果可以的话,请提交一个错误报告。报告越多,修复速度可能就越快! - user2821647
苹果什么时候会关注SpriteKit,我的游戏在iOS9上都崩溃了,我收到了很多差评,这不是我的错。这太糟糕了。 有人已经有雷达号码了吗? - txaidw
SDK文档中没有提到SKDefaultShading被弃用或其他任何信息。我已经提交了一个错误报告,并在文档页面上提供了反馈,请看看他们是否会更新它,https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKShader_Ref/index.html - endavid
显示剩余3条评论
4个回答

6

我看到了消息使用未声明的标识符“gl_FragCoord”,于是来到这里。但最终,我通过将

gl_FragCoord.xy / u_sprite_size.xy

替换为

v_tex_coord

解决了我的问题。


好的,这个对我解决了一个类似的问题。似乎 gl_FragCoord 也有问题。 - Jonny

2
尝试在info.plist中添加类型为布尔值且设置为“是”的参数“PrefersOpenGL”。这将禁用Metal。

2

我的自定义SKShader在使用Metal设备的iOS9上也停止工作了。

我能够通过使用v_tex_coord获取像素位置以及自定义uniform来发送精灵大小来修复它(似乎存在一个问题,即u_sprite_size未被传递)。

[SKUniform uniformWithName:@"u_fixed_size" floatVector2:GLKVector2Make(n.calculateAccumulatedFrame.size.width, n.calculateAccumulatedFrame.size.height)]

请问您能否添加更多信息和代码,以便根据您在接受的答案中提到的内容替换/计算gl_FragCoord。非常感谢! - nzs
这对我来说并没有解决问题。SKDefaultShading() 似乎已经消失了。 - Jonny
顺便说一下,我认为u_sprite_size已经过时了。 - Jonny

1
我也曾在iOS9中遇到这个问题。gl_FragCoord在真实设备上停止工作,但在iOS模拟器上可以工作。
解决方法:
创建一个带有着色器的节点。
let shader = SKShader(fileNamed: "polka.fsh")
let uniform = SKUniform(name: "u_spriteSize", floatVector2: GLKVector2Make(Float(200), Float(200)))
shader.uniforms.append(uniform)

更新制服。
if let uniform = shader.uniformNamed("u_spriteSize") {
    let size = frame.size
    uniform.floatVector2Value = GLKVector2Make(Float(size.width * screenScale), Float(size.height * screenScale))
}

着色器

vec2 xy = v_tex_coord * u_spriteSize - u_polkaCenter; // new code
//vec2 xy = gl_FragCoord.xy - u_polkaCenter;  // old code that worked before iOS9

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