SpriteKit shader crash iOS 9: SKDefaultShading, gl_FragCoord

狂风中的少年 提交于 2019-12-04 01:43:02

My custom SKShader also stopped working in ios9 on metal devices.

I was able to fix it using v_tex_coord to get the pixel position, and custom uniform to send in the sprite size (There seems to be a bug where u_sprite_size isn't being passed in.)

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

I came here after seeing the message use of undeclared identifier 'gl_FragCoord' but eventually I could solve my problem by replacing

gl_FragCoord.xy / u_sprite_size.xy

with

v_tex_coord

Try to add parameter 'PrefersOpenGL' of type boolean set to 'YES' in info.plist. This will disable Metal

I had this problem too with iOS9. gl_FragCoord stopped working on real devices, but works in the iOS simulator.

Work around.

Create a node with shader

let shader = SKShader(fileNamed: "polka.fsh")
let uniform = SKUniform(name: "u_spriteSize", floatVector2: GLKVector2Make(Float(200), Float(200)))
shader.uniforms.append(uniform)

Update the uniform

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

Shader

vec2 xy = v_tex_coord * u_spriteSize - u_polkaCenter; // new code
//vec2 xy = gl_FragCoord.xy - u_polkaCenter;  // old code that worked before iOS9
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!