Shader causes glitch when first appearing in SpriteKit camera's viewport

Viewed 113

I have a SpriteKit game in which I'm experimenting with a shader that I found here and here.

The shader, itself, works as expected (with the exception of some undesirable behaviors that are probably irrelevant to this question) -- but when it first enters my camera's viewport, there is a very noticeable glitch (a "jump" or a "skip").

I'm not sure if I should post the code for the shader since it's not mine, so you can find it here.

As for my own code, here's what I've got. This runs when the scene is first presented, but the node doesn't reach the viewport until some time later (after the player scrolls over some distance). It sets the shader on an SKSpriteNode that's given a physicsBody.

let uniforms: [SKUniform] = [
  SKUniform(name: "u_speed", float: 3),
  SKUniform(name: "u_strength", float: 2.5),
  SKUniform(name: "u_frequency", float: 10)
]
let node = SKSpriteNode(texture: someTexture)

node.shader = SKShader(source: "theShaderCodeGoesHere", uniforms: uniforms)

node.physicsBody = SKPhysicsBody(rectangleOf: node.size)
node.physicsBody?.restitution = 0.0
node.physicsBody?.allowsRotation = false
node.physicsBody?.usesPreciseCollisionDetection = false
node.physicsBody?.isDynamic = false
node.physicsBody?.affectedByGravity = false
node.physicsBody?.categoryBitMask = CollisionTypes.something

addChild(node)

I've seen this behavior on both the Simulator and an actual device.

The glitch is accompanied by the following message about an unused variable:

[Metal Compiler Warning] Warning: Compilation succeeded with: program_source:3:19: warning: unused variable 's' constexpr sampler s(coord::normalized

There is no s in the shader code, so I'm not sure what that means.

Question: Can anyone help me understand what could be causing this glitch, and give me some idea how to avoid it?

Thank you!

0 Answers
Related