gpt4 book ai didi

swift - 如何将渐变应用于从路径创建的 SKShapeNode

转载 作者:行者123 更新时间:2023-11-30 10:53:41 27 4
gpt4 key购买 nike

我正在尝试将渐变纹理应用于从路径创建的 SKShapeNode,以便线条从纯色变为透明。这适用于 SKSpriteNode,但我无法让它在 SKShapeNode 上工作。有谁知道这个问题的解决方案?像这样的基本东西肯定是可能的吗?

我尝试过各种各样的事情。最明显的一个是我尝试将其应用为描边纹理(请参见下面的代码)。此代码演示了 SpriteNode 上的渐变效果,但相同的纹理在 ShapeNode 上给出不同的结果。

我尝试通过尝试使用着色器来解决这个问题(但我无法解决任何/类似的问题)。我试图用关节将 spriteNode 固定到 shapeNode 上,但我无法让它完美地粘住/固定,因为在我的应用程序中,有问题的线四处移动(作为两个圆之间的切线)并运行进入方向问题。

那么,回到第一个点。我确信这么简单的事情应该是可能的。也许我忽略了一些东西?

以这个简化的代码为例:

// create gradient texture (with help of https://github.com/maximbilan/SKTextureGradient/blob/master/SKTextureGradient.swift)
var testTexture = SKTexture(size: CGSize(width: 200, height: 1), color1: CIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 1.0), color2: CIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.0), direction: GradientDirection.Left)
// create path for shape node
var testPath = CGMutablePath()
testPath.move(to: CGPoint(x: 50, y: 200))
testPath.addLine(to: CGPoint(x: 250, y: 200))
testPath.closeSubpath()
// assign path to shape
var testLine = SKShapeNode()
testLine.path = testPath
testLine.lineWidth = 1
self.addChild(testLine)
// add gradient as strokeTexture
testLine.strokeColor = UIColor.white
testLine.strokeTexture = testTexture
// apply gradient to SKSpriteNode
var testSprite = SKSpriteNode()
testSprite.size = CGSize(width: 200, height: 1)
testSprite.position = CGPoint(x: 50 + (testSprite.frame.width / 2), y: 150)
testSprite.texture = testTexture
self.addChild(testSprite)

输出如下:

如何使第一行(SKShapeNode)看起来像第二行(SKSpriteNode)?

最佳答案

你真的应该感谢我,因为我必须使用 iPhone 而不是模拟器来进行尝试和错误。

testLine.isAntialiased = false

将解决您的问题。

关于swift - 如何将渐变应用于从路径创建的 SKShapeNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189617/

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