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

查看:49
本文介绍了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 ?

推荐答案

我已经解决了这个问题,可以通过使用Factory解决我的问题.

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

BenMobile的耐心而清晰的表述中了解更多有关如何建立工厂的信息,请点击此处:="https://stackoverflow.com/questions/40084701/create-use-skview-as-in-a-factory-static-class">工厂创建并用于制作Sprite和Shapes

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

模糊 SKTexture SKSpriteNode 存在一个问题,因为空间将用完.模糊/发光超出了精灵的边缘.为了解决这个问题,在下面,您将看到我已经创建了一个成帧器"对象.这仅仅是一个空的 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天全站免登陆