将自定义SKShader应用于SKScene

将自定义SKShader应用于SKScene

本文介绍了将自定义SKShader应用于SKScene,以使用Swift对iOS 8 SpriteKit中的整个渲染场景进行像素化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SKScene上创建全屏像素化效果.我了解到,应该有两个选择可以做到这一点:

I'm trying to create a full-screen pixelation effect on SKScene. I've learned that there should be two options to do this:

  • 使用GLES 2.0自定义SKShader.
  • 使用核心图像过滤器.

我尝试添加一个自定义SKShader,该像素应通过像素化来修改整个屏幕.我不确定是否有可能,但是SKScene(这是SKEffectNode的子类)的文档提出了建议:

I've tried to add a custom SKShader that should modify the whole screen by pixelating it. I'm not sure that if it's possible, but documentation from SKScene (which is a subclass of SKEffectNode) suggests it:

可以将SKShader分配给SKScene,如GameScene : SKScene所示:

It's possible to assign a SKShader to the SKScene, as in GameScene : SKScene:

override func didMoveToView(view: SKView) {
    let shader = SKShader(fileNamed: "pixelation.fsh")
    self.shader = shader
    self.shouldEnableEffects = true
}

...但是似乎渲染的缓冲区没有作为u_texture传递给GLES:

... but it seems that the rendered buffer is not passed as the u_texture to the GLES:

void main()
{
    vec2 coord = v_tex_coord;
    coord.x = floor(coord.x * 10.0) / 10.0;
    coord.y = floor(coord.y * 10.0) / 10.0;
    vec4 texture = texture2D(u_texture, coord);
    gl_FragColor = texture;
}

...所以以前的着色器不起作用.

... so the previous shader doesn't work.

如果我将该着色器分配给基于纹理的SKSpriteNode,它将起作用.

If I assign that shader to a texture-based SKSpriteNode, it works.

那么是否可以在渲染完所有节点之后修改整个帧缓冲区(例如对它进行像素化)作为后处理措施?

So is it possible to modify the whole frame buffer (and for example pixelate it) as a post-processing measure after all the nodes have been rendered?

编辑:我找到了一种在OS X中使用Core Image滤镜进行像素化的方法(如何将CIPixellate核心图像过滤器添加到Sprite Kit场景?),但复制该实现不会在iOS上产生任何结果.根据文档 CIPixellate应该为Available in OS X v10.4 and later and in iOS 6.0 and later..

Edit: I found a way to do the pixelation using Core Image filters in OS X (How do you add a CIPixellate Core Image Filter to a Sprite Kit scene?), but copying that implementation doesn't yield any results on iOS. According to the documents CIPixellate should be Available in OS X v10.4 and later and in iOS 6.0 and later..

推荐答案

为了使.shaderSKScene上运行,您需要在场景中将shouldEnableEffects设置为true(对于).

In order to get your .shader running on SKScene, you need to set shouldEnableEffects to true on the scene (same thing goes for SKEffectNode).

从技术上讲,这种方法有效"(应用了着色器),但随后的场景渲染中存在一个错误,该错误会稍加调整大小.

While technically, that "works" (the shader is applied), there's a bug in the rendering of the scene afterwards that gets slightly resized.

因此,到目前为止,使用CoreImage过滤器是最好的方法.

So using CoreImage filters is, so far, the best way to go.

这篇关于将自定义SKShader应用于SKScene,以使用Swift对iOS 8 SpriteKit中的整个渲染场景进行像素化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:47