微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带旋转的 SKShapeNode fillTexture

如何解决带旋转的 SKShapeNode fillTexture

我无法弄清楚旋转该节点时 fillTextureSKShapeNode 属性是如何工作的。在这个例子中,我围绕中心创建了一个由 4 个旋转叶片组成的风扇。然而,当叶片旋转时,所应用的纹理的强度会发生变化。为什么?我希望渐变纹理应用程序随着旋转保持不变。

func buildBackground() {
        
        let bg = SKShapeNode.init(rect: UIScreen.main.bounds)
        bg.fillColor = .orange
        
        self.addChild(bg) // self == SKScene

        let size = UIScreen.main.bounds.size
        let fan = SKNode.init()
        let H = size.height
        let W = size.width/2
        let blades = 4

        let triangle = CGMutablePath.init()
        triangle.move(to: .zero)
        triangle.addLine(to: CGPoint(-W,H))
        triangle.addLine(to: CGPoint(+W,H))
        triangle.addLine(to: .zero)
        triangle.closeSubpath()
        
        let da = 360.0/CGFloat(blades)
        var r : CGFloat = 0
        
        let texture = SKTexture.init(size: size,color0: .white,color1: .clear,radius0: 0.25,radius1: 1)

        for i in 0..<blades {
            let blade = SKShapeNode.init(path: triangle)
            fan.addChild(blade)
            blade.fillTexture = texture
            blade.linewidth = 0
            blade.fillColor = .black
            blade.zRotation = r.degreesToradians
            r += da
        }

        bg.addChild(fan)
        
        fan.position = bg.frame.center
        
        fan.run(.repeatForever(.rotate(byAngle: -0.1,duration: 0.2)))
        
        fan.alpha = 0.5
    }
public extension SKTexture {

    convenience init(size       : CGSize,color0     : SKColor,color1     : SKColor,radius0    : CGFloat,// 0
                     radius1    : CGFloat) // 1
    {
        let coreImageContext    = CIContext(options: nil)
        let gradientFilter      = CIFilter(name: "CIRadialGradient")!
        let inputCenter         = CIVector.init(x: size.width/2,y: size.height/2)
        let inpuTradius0        = radius0 * size.diagonal/2
        let inpuTradius1        = radius1 * size.diagonal/2
        
        gradientFilter.setDefaults()
        gradientFilter.setValue(inputCenter,forKey: "inputCenter")
        gradientFilter.setValue(inpuTradius0,forKey: "inpuTradius0")
        gradientFilter.setValue(inpuTradius1,forKey: "inpuTradius1")
        gradientFilter.setValue(color0.asCIColor,forKey: "inputColor0")
        gradientFilter.setValue(color1.asCIColor,forKey: "inputColor1")
        
        let coreImage = coreImageContext.createCGImage(gradientFilter.outputimage!,from: .init(size)) //CGRect(x: 0,y: 0,width: size.width,height: size.height))
        
        self.init(cgImage: coreImage!)
    }

}

enter image description here

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。