简体   繁体   中英

iOS 9 Sprite Kit Custom Shader seems to be ignored

I must be missing something simple here... I hope.

I'm having trouble making a custom shader work with Xcode 7 and iOS 9 SDK. Starting with the sprite kit template I changed the GameScene to look like this:

class GameScene: SKScene {

    var backgroundShader = SKShader(fileNamed: "BackgroundShader")

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder) is not used in this app")
    }

    override init(size: CGSize) {
        super.init(size: size)

        anchorPoint = CGPoint(x: 0.5, y: 0.5)

        let background = SKSpriteNode(color: SKColor.brownColor(), size: size)
        background.shader = backgroundShader
        addChild(background)

    }
}

My "BackgroundShader" looks like this:

void main()
{
    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0)
}

So, I expect that my background should be green but instead it is brown. I've also tried using a texture for the SKSpriteNode but that doesn't help.

I'm nearly positive this worked in iOS 8 but I'm also not finding other people complaining that custom shaders aren't working so I'm a bit lost.

Another note is that there's nothing printing to the console about the shader. For example if I purposefully create an error in my fragment shader I'd expect there to be log messages in the console but there aren't. It's almost as if the custom shader is just being ignored. I have, of course, verified that the shader object exists when setting the value; so it is reading the shader file but never seems to compile it.

I figured out my problem... To get OpenGL related debug information you must run on an actual device. In the simulator I got no information at all. However, as soon as I ran on a device it started spewing tons of data that helped track down the problem.

So what's wrong with the above code? I forgot a ';' in the shader. I'm blaming swift for this one ;)

By the way, when the shader is correct it does also work in the simulator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM