简体   繁体   English

SpriteKit中的默认混合如何工作?

[英]How does the default blending in SpriteKit work?

I've started tinkering with SpriteKit for the first time (Xcode 7.1 iOS9), and I'm seeing some unexpected results (both on device and simulator). 我第一次开始修补SpriteKit(Xcode 7.1 iOS9),我看到了一些意想不到的结果(包括设备和模拟器)。

I'd like to be able to set pixels in a sprite to being completely transparent via a shader, but for some reason this is not working as I'd expect. 我希望能够通过着色器将精灵中的像素设置为完全透明,但由于某种原因,这不符合我的预期。

Here's is an example ViewController: 这是ViewController的一个例子:

class ViewController: UIViewController {
    @IBOutlet var sceneView: SKView!
    private var scene = SKScene()

    override func viewDidLoad() {
        super.viewDidLoad()
        sceneView.showsDrawCount = true
        sceneView.showsNodeCount = true
        sceneView.showsFPS = true
        scene.scaleMode = .ResizeFill
        scene.backgroundColor = UIColor.redColor()
        let node = SKSpriteNode()
        node.size = CGSizeMake(64, 64)
        node.position = CGPointMake(64, 64)
        node.shader = SKShader(fileNamed: "Test")
        scene.addChild(node)
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.view.layoutIfNeeded()
        sceneView.presentScene(scene)
    }
}

and here is an example shader: 这是一个示例着色器:

void main()
{
    if (v_tex_coord.x < 0.5) {
        gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0);
    } else {
        gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
    }
}

With the default blend mode of SKBlendModeAlpha, I would expect to see the left half of the sprite as fully transparent (showing the red background), and the right half as green. 使用SKBlendModeAlpha的默认混合模式,我希望看到精灵的左半部分为完全透明(显示红色背景),右半部分为绿色。 But instead, this is what I see: 但相反,这就是我所看到的:

在红色背景的Sp ..

Shouldn't the blending formula for SKBlendModeAlpha be: R = (D * (1 - A)) + (S * A) SKBlendModeAlpha的混合公式不应该是:R =(D *(1 - A))+(S * A)

Which in this case should result in: 在这种情况下应该导致:

    D   A   S   R
R   1   0   0   1
G   0   0   1   0
B   0   0   0   0

    D   A   S   R
R   1   1   0   0
G   0   1   1   1
B   0   1   0   0

(red on the left, green on the right) (左边是红色,右边是绿色)

I've tried manually setting the blending modes on the node, but it makes no difference. 我尝试在节点上手动设置混合模式,但没有区别。

Is this a bug, or am I missing something obvious here? 这是一个错误,还是我错过了一些明显的东西?

Thanks for your help. 谢谢你的帮助。

There was, of course, a simple explanation to my problem. 当然,对我的问题有一个简单的解释。 The color returned in gl_FragColor was expected to have pre-multiplied alpha. gl_FragColor中返回的颜色预计会有倍前的alpha值。 So rather than returning a color value of: 因此,而不是返回颜色值:

gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0);

I should have been returning a color value of: 我应该返回一个颜色值:

gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);

I incorrectly assumed a zero alpha would mean the color component was ignored. 我错误地假设零alpha意味着颜色组件被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM