Inverted y axis on SKShader using SpriteKit

Hello there 👋

While running the following ViewController.swift on iOS 16, I realized that the y axis of v_tex_coord inside the SKShader has been inverted. The same code display a red bar at the top of the screen for iOS 15.5. This red bar is displayed at the bottom of the screen for iOS 16.

Here is some images to show you the difference:

iOS 15

iOS 16

Can you help me with this please?

Thanks a lot 🙏

import UIKit

import SpriteKit

class ViewController: UIViewController {

    var skView: SKView?
    var scene: SKScene?

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.
        skView = SKView(frame: view.frame)
        scene = SKScene(size: skView?.bounds.size ?? .zero)
        scene?.backgroundColor = UIColor.red
        view.addSubview(skView!)
        skView!.presentScene(scene)
        // Media
        let mediaNode = SKSpriteNode(imageNamed: "archi1")
        mediaNode.size = skView?.bounds.size ?? .zero
        mediaNode.position = skView?.center ?? .zero
        // Effect
        let effectNode = SKEffectNode()
        effectNode.shader = SKShader(source:
        """
            void main() {
                vec2 uv = v_tex_coord;
                vec4 texel = texture2D(u_texture, uv);
                vec4 color = texel;
                vec4 outColor = texel;
                // from top to bottom
                if (uv.y > .8 / 2. + .5) { // bg color
                    color = i_color;
                }
                outColor = mix(texel, color, .6);
                gl_FragColor = outColor;
            }
        """
        , uniforms: [
            SKUniform(name: "i_color", vectorFloat4: vector_float4(1, 0, 0, 0))
        ])
        scene?.addChild(effectNode)
        effectNode.addChild(mediaNode)
    }
}
Answered by Graphics and Games Engineer in 727910022

This is a bug (95579020) and a fix has been identified.

I am experiencing this issue as well. Since fragment shaders are affected but SpriteKit node positioning is not, my guess is the issue is possibly related to the release of Metal 3. I can think of some messy solutions to the issue involving shader uniforms but it would be nice to have a proper solution.

Accepted Answer

This is a bug (95579020) and a fix has been identified.

Bug has been fixed in iOS 16.1 beta 3. 🚀

Inverted y axis on SKShader using SpriteKit
 
 
Q