分配给场景的着色器无法正常工作

时间:2023-01-23 07:29:55

I've been trying to create a custom shader in my iOS application to add post-processing effect. So, I assign my shader to the scene and see how it is affected in a strange way. It looks like my scene has displacement... Could you be so kind to point where I am wrong. I believe there is a simple explanation I didn't get :) Thank you in advance.

我一直在尝试在我的iOS应用程序中创建自定义着色器以添加后处理效果。因此,我将着色器指定给场景,并以奇怪的方式查看它是如何受到影响的。看起来我的场景有位移......你能不能指出我错了。我相信有一个简单的解释我没有得到:)谢谢你提前。

My test code:

我的测试代码:

var shader = SKShader(fileNamed: "Effect.fsh")

self.shouldEnableEffects = true
self.shader = shader

var node = SKSpriteNode(color: UIColor.blueColor(), size: size)
node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))

self.addChild(node)

My simple shader which does nothing:

我的简单着色器什么都不做:

void main()
{
    gl_FragColor = texture2D(u_texture, v_tex_coord);
}

1 个解决方案

#1


I had this problem too when trying to create a blur effect using shaders and an SKEffectNode (SKScene is actually a subclass of SKEffectNode so I believe we have the same problem) I'm pretty sure the offset is a bug with shaders and SKEffectNode.

当我尝试使用着色器和SKEffectNode创建模糊效果时也遇到了这个问题(SKScene实际上是SKEffectNode的子类,所以我相信我们有同样的问题)我很确定偏移是着色器和SKEffectNode的错误。

My advice would be to see if there's another way to can achieve the effect you're going for? Perhaps you can apply use the shader property on SKSpriteNode? That would only apply the shader to that specific node though.

我的建议是看看是否有另一种方法可以达到你想要的效果?也许您可以在SKSpriteNode上应用shader属性?这只会将着色器应用于该特定节点。

In my situation I was able to adjust the xScale and yScale of the SKEffectNode which made things a marginally better, however it's so much hassle trying to get it in the correct place I'd see if there's another solution before resorting to that.

在我的情况下,我能够调整SKEffectNode的xScale和yScale,这使得事情稍微好一点,但是在尝试将它放到正确的位置时我会看到是否有另一个解决方案,然后才采取这种方法。

Good luck!

#1


I had this problem too when trying to create a blur effect using shaders and an SKEffectNode (SKScene is actually a subclass of SKEffectNode so I believe we have the same problem) I'm pretty sure the offset is a bug with shaders and SKEffectNode.

当我尝试使用着色器和SKEffectNode创建模糊效果时也遇到了这个问题(SKScene实际上是SKEffectNode的子类,所以我相信我们有同样的问题)我很确定偏移是着色器和SKEffectNode的错误。

My advice would be to see if there's another way to can achieve the effect you're going for? Perhaps you can apply use the shader property on SKSpriteNode? That would only apply the shader to that specific node though.

我的建议是看看是否有另一种方法可以达到你想要的效果?也许您可以在SKSpriteNode上应用shader属性?这只会将着色器应用于该特定节点。

In my situation I was able to adjust the xScale and yScale of the SKEffectNode which made things a marginally better, however it's so much hassle trying to get it in the correct place I'd see if there's another solution before resorting to that.

在我的情况下,我能够调整SKEffectNode的xScale和yScale,这使得事情稍微好一点,但是在尝试将它放到正确的位置时我会看到是否有另一个解决方案,然后才采取这种方法。

Good luck!