SKEffectNode 到 SKTexture? [英] SKEffectNode to an SKTexture?

查看:18
本文介绍了SKEffectNode 到 SKTexture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SKEffectionNodes 有一个 shouldRasterise开关",可以将它们烘焙到位图中,并且在受效果影响的底层节点发生更改之前不会更新它们.

SKEffectionNodes have a shouldRasterise "switch" that bakes them into a bitmap, and doesn't update them until such time as the underlying nodes that are impacted by the effect are changed.

但是我找不到从这个光栅化的图像"创建 SKTexture 的方法.

However I can't find a way to create an SKTexture from this rasterised "image".

是否可以从 SKEffectNode 获取 SKTexture?

Is it possible to get a SKTexture from a SKEffectNode?

推荐答案

我已经想通了,使用工厂解决了我的问题.

I've figured this out, in a way that solves my problems, using a Factory.

阅读BenMobile的耐心和清晰的表述,了解更多关于如何制造工厂的信息:工厂创建和用于制作精灵和形状

Read more on how to make a factory, from BenMobile's patient and clear articulation, here: Factory creation and use for making Sprites and Shapes

模糊 SKTextureSKSpriteNode 会出现空间不足的问题.模糊/发光超出了精灵的边缘.为了解决这个问题,在下面,您将看到我创建了一个成帧器"对象.这只是一个空的 SKSpriteNode,它是要模糊的纹理大小的两倍.要模糊的纹理作为子对象添加到此成帧器"对象.

There's an issue with blurring a SKTexture or SKSpriteNode in that it's going to run out of space. The blur/glow goes beyond the edges of the sprite. To solve this, in the below, you'll see I've created a "framer" object. This is simply an empty SKSpriteNode that's double the size of the texture to be blurred. The texture to be blurred is added as a child, to this "framer" object.

不管这有多骇人听闻,它都能正常工作;)

It works, regardless of how hacky this is ;)

import SpriteKit

class Factory {

    private static let view:SKView = SKView()  // the magic. This is the rendering space

    static func makeShadow(from source: SKTexture, rgb: SKColor, a: CGFloat) -> SKSpriteNode {
        let shadowNode = SKSpriteNode(texture: source)
            shadowNode.colorBlendFactor = 0.5  // near 1 makes following line more effective
            shadowNode.color = SKColor.gray // makes for a darker shadow. White for "glow" shadow
        let textureSize = source.size()
        let doubleTextureSize = CGSize(width: textureSize.width * 2, height: textureSize.height * 2)
        let framer = SKSpriteNode(color: UIColor.clear, size: doubleTextureSize)
            framer.addChild(shadowNode)
        let blurAmount = 10
        let filter = CIFilter(name: "CIGaussianBlur")
            filter?.setValue(blurAmount, forKey: kCIInputRadiusKey)
        let fxNode = SKEffectNode()
            fxNode.filter = filter
            fxNode.blendMode = .alpha
            fxNode.addChild(framer)
            fxNode.shouldRasterize = true
        let tex = view.texture(from: fxNode) // ‘view’ refers to the magic first line
        let shadow = SKSpriteNode(texture: tex)  //WHOOPEE!!! TEXTURE!!!
            shadow.colorBlendFactor = 0.5
            shadow.color = rgb
            shadow.alpha = a
            shadow.zPosition = -1

        return shadow
    }
}

在您可以访问要为其制作阴影或发光纹理的 Sprite 的任何地方:

shadowSprite = Factory.makeShadow(from: button, rgb: myColor, a: 0.33)
shadowSprite.position = CGPoint(x: self.frame.midX, y: self.frame.midY - 5)
addChild(shadowSprite)

-button 是要被赋予阴影的按钮的纹理.a: 是一个 alpha 设置(实际上是透明度级别,0.0 到 1.0,其中 1.0 是完全不透明的)越低,阴影就越亮.

- button is a texture of the button to be given a shadow. a: is an alpha setting (actually transparency level, 0.0 to 1.0, where 1.0 is fully opaque) the lower this is the lighter the shadow will be.

该定位用于将阴影略微放在按钮下方,因此看起来光线从顶部射入,将阴影向下投射到背景上.

The positioning serves to drop the shadow slightly below the button so it looks like light is coming from the top, casting shadows down and onto the background.

这篇关于SKEffectNode 到 SKTexture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆