ios - SpriteKit - SKLightNode 阴影混合模式

标签 ios iphone swift sprite-kit

我有 3 个 SKLightNode,每个节点都有一种浅色:红色、绿色和蓝色。我想要的效果是由 SKLightNodes 生成的阴影具有混合模式。 Xcode模拟器 Xcode Simulator

我做了一些 photoshop 示例。

这是影子电流行为: This is the shadow current behavior

这是所需的影子行为: This is the shadow behavior desired:

这可以在 SpriteKit 中实现吗?

最佳答案

色彩空间

您好,首先,请注意有不同的色彩空间。 混合 2 种颜色的结果取决于您要使用的颜色空间。

RGB 色彩空间

使用这种空间颜色,您可以表示 2 条光线的交点。

在这种情况下,当混合 2 种颜色时,交叉点更亮。

enter image description here

您可以在 SpriteKit 中使用 blendMode = .add 获得此效果。

这是完整的代码。

import SpriteKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {

        let red = SKShapeNode(circleOfRadius: 100)
        red.fillColor = .red
        red.blendMode = .add
        red.position = CGPoint(x: 0, y: 0)
        addChild(red)

        let green = SKShapeNode(circleOfRadius: 100)
        green.fillColor = .green
        green.blendMode = .add
        green.position = CGPoint(x: 0, y: 100)
        addChild(green)

        let blue = SKShapeNode(circleOfRadius: 100)
        blue.fillColor = .blue
        blue.blendMode = .add
        blue.position = CGPoint(x: 87, y: 50)
        addChild(blue)
    }
}

结果

enter image description here

CMY 色彩空间

此颜色空间代表混合 2 种流体的真实场景。 现在混合颜色会产生一种新的更深的颜色。

enter image description here

您可以在 SpriteKit 中简单地使用 blendMode = .multiply 获得这种效果(并且 IO 建议使用白色背景)。

import SpriteKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {

        self.backgroundColor = .white

        let yellow = SKShapeNode(circleOfRadius: 100)
        yellow.fillColor = .yellow
        yellow.blendMode = .multiply
        yellow.position = CGPoint(x: 0, y: 0)
        addChild(yellow)

        let cyan = SKShapeNode(circleOfRadius: 100)
        cyan.fillColor = .cyan
        cyan.blendMode = .multiply
        cyan.position = CGPoint(x: 0, y: 100)
        addChild(cyan)

        let magenta = SKShapeNode(circleOfRadius: 100)
        magenta.fillColor = .magenta
        magenta.blendMode = .multiply
        magenta.position = CGPoint(x: 87, y: 50)
        addChild(magenta)

    }
}

enter image description here

关于ios - SpriteKit - SKLightNode 阴影混合模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52021698/

相关文章:

ios - 为什么 Cordova 恢复事件不会在 iOS 中使用 sencha 在电源锁定模式/ sleep 模式下触发

ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音

iphone - CGPoint 等价于整数?

ios - swift 在 googlemaps 图层上方添加图层

ios - 如何在UIView顶部设置阴影?

swift - 如何将模块导入到xcode中

ios - 如何以编程方式取消选择 UITextField

ios - 如何向 App Store 提交波兰语应用程序?

ios - 如何手动加载 UITableView

iphone - 导入指令不允许访问该类