gpt4 book ai didi

ios - Swift SpriteKit 反向掩码不适用于设备,但适用于模拟器

转载 作者:可可西里 更新时间:2023-11-01 01:10:21 24 4
gpt4 key购买 nike

完成此操作的代码非常简单:

var cropNode = SKCropNode()
var shape = SKShapeNode(rectOf: CGSize(width:100,height:100))
shape.fillColor = SKColor.orange

var shape2 = SKShapeNode(rectOf: CGSize(width:25,height:25))
shape2.fillColor = SKColor.red
shape2.blendMode = .subtract
shape.addChild(shape2)

cropNode.addChild(shape)
cropNode.position = CGPoint(x:150,y:170)
cropNode.maskNode=shape
container.addChild(cropNode)

相同的代码,相同的 iOS,不同的结果 = no bueno

image

最佳答案

这是一个使用着色器为您生成掩码节点的方法:

func generateMaskNode(from mask:SKNode) -> SKNode
{
var returningNode : SKNode!
autoreleasepool
{
let view = SKView()
//First let's flatten the node
let texture = view.texture(from: mask)
let node = SKSpriteNode(texture:texture)
//Next apply the shader to the flattened node to allow for color swapping
node.shader = SKShader(fileNamed: "shader.fsh")
let texture2 = view.texture(from: node)
returningNode = SKSpriteNode(texture:texture2)

}
return returningNode
}

它需要你创建一个名为shader.fsh的文件,里面的代码如下所示:

void main() {

// Find the pixel at the coordinate of the actual texture
vec4 val = texture2D(u_texture, v_tex_coord);

// If the color value of that pixel is 0,0,0
if (val.r == 0.0 && val.g == 0.0 && val.b == 0.0) {
// Turn the pixel off
gl_FragColor = vec4(0.0,0.0,0.0,0.0);
}
else {
// Otherwise, keep the original color
gl_FragColor = val;
}
}

要使用它,它要求您使用黑色像素而不是 alpha 作为确定裁剪内容的方法,因此您的代码现在应该如下所示:

var cropNode = SKCropNode()
var shape = SKShapeNode(rectOf: CGSize(width:100,height:100))
shape.fillColor = SKColor.orange

var shape2 = SKShapeNode(rectOf: CGSize(width:25,height:25))
shape2.fillColor = SKColor.orange
shape2.blendMode = .subtract
shape.addChild(shape2)

let mask = generateMaskNode(from:shape)

cropNode.addChild(shape)
cropNode.position = CGPoint(x:150,y:170)
cropNode.maskNode=mask
container.addChild(cropNode)

subtract 在模拟器而不是设备上工作的原因是因为模拟器减去了 alpha channel ,而设备却没有。该设备实际上行为正确,因为 alpha 不应该被减去,它应该被忽略。

请注意,您不必选择黑色作为裁剪颜色,您可以更改着色器以允许您选择任何颜色,只需更改行:

if (val.r == 0.0 && val.g == 0.0 && val.b == 0.0) 

到你想要的颜色。 (就像你的情况一样。你可以说 r = 0 g = 1 b = 0 只在绿色上裁剪)

Result of above code on a device

编辑:我想指出减法混合不是必需的,这也可行:

var cropNode = SKCropNode()
var shape = SKShapeNode(rectOf: CGSize(width:100,height:100))
shape.fillColor = SKColor.orange

var shape2 = SKShapeNode(rectOf: CGSize(width:25,height:25))
shape2.fillColor = SKColor.black
shape2.blendMode = .replace
shape.addChild(shape2)

let mask = generateMaskNode(from:shape)

cropNode.addChild(shape)
cropNode.position = CGPoint(x:150,y:170)
cropNode.maskNode=mask
container.addChild(cropNode)

现在我无法测试,这就引出了一个问题,我的功能是否还需要。理论上下面的代码应该可以工作,因为它用上面的代码替换了底层像素,所以理论上 alpha 应该转移过来。如果有人可以对此进行测试,请告诉我它是否有效。

var cropNode = SKCropNode()
var shape = SKShapeNode(rectOf: CGSize(width:100,height:100))
shape.fillColor = SKColor.orange

var shape2 = SKShapeNode(rectOf: CGSize(width:25,height:25))
shape2.fillColor = SKColor(red:0,green:0,blue:0,alpha:0)
shape2.blendMode = .replace
shape.addChild(shape2)

cropNode.addChild(shape)
cropNode.position = CGPoint(x:150,y:170)
cropNode.maskNode= shape.copy() as! SKNode
container.addChild(cropNode)

replace 只替换颜色不替换alpha

关于ios - Swift SpriteKit 反向掩码不适用于设备,但适用于模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47759584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com