gpt4 book ai didi

ios - 背景上的 SCNNode 大图像导致崩溃

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:12 27 4
gpt4 key购买 nike

我正在尝试使用 ARKitSceneKit 创建标尺应用程序。我决定根据测量的距离以编程方式创建标尺图像。

这是我用来绘制标尺的扩展:

extension UIImage {

static let dashLineWidth: CGFloat = 2.0
static let dashDistance: CGFloat = 163.0 / 25.4
static let rulerFont: UIFont = .systemFont(ofSize: 15.0, weight: .regular)
static let attributes: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.font: rulerFont,
NSAttributedStringKey.foregroundColor: UIColor.black
]

static func drawRuler(width: CGFloat) -> UIImage? {
let cm = width * 100 // width in centimeters
let size = CGSize(width: dashDistance * cm * 10, height: 50.0)

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
guard let context = UIGraphicsGetCurrentContext() else { return nil }

let background = UIBezierPath(rect: CGRect(origin: .zero, size: size))
context.addPath(background.cgPath)
context.setFillColor(UIColor.white.cgColor)
context.fillPath()

var i: CGFloat = 0.0
var counter: Int = 0
while i < size.width {
let isLongDash = counter % 10 == 0
let isPartDash = counter % 5 == 0
let dashHeight: CGFloat = size.height * (isLongDash ? 0.25 : isPartDash ? 0.15 : 0.07)
UIColor.black.setFill()
UIRectFill(CGRect(x: i - dashLineWidth / 2, y: 0.0, width: dashLineWidth, height: dashHeight))

if isLongDash && counter != 0 {
let value = "\(counter / 10)"
let valueSize: CGSize = value.size(withAttributes: attributes)
value.draw(at: CGPoint(x: i - dashLineWidth / 2 - valueSize.width / 2, y: dashHeight + 5.0), withAttributes: attributes)
}

i += dashDistance
counter += 1
}

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}

func crop(to width: CGFloat, initialWidth: CGFloat) -> UIImage? {
let rect = CGRect(x: 0, y: 0, width: (width / initialWidth) * size.width * scale, height: size.height * scale)
guard let croppedCGImage: CGImage = cgImage?.cropping(to: rect) else { return nil }
return UIImage(cgImage: croppedCGImage)
}
}

所以一开始我只绘制一次 0.5 米的图像以获得更好的性能,然后每次只裁剪需要的部分以显示在 SCNNode 中。

下面是我在 SCNNode 类中尝试的内容:

var ruler: SCNNode = initRuler()
var initialWidth: CGFloat = 0.5
var rulerImage: UIImage? = UIImage.drawRuler(width: initialWidth)

func updateRuler() {
guard let geometry = ruler.geometry as? SCNBox else {
fatalError("Geometry is not SCNBox")
}
let width = geometry.width // in meters
if width > initialWidth - 0.05 {
initialWidth += 0.5
rulerImage = UIImage.drawRuler(width: initialWidth)
}
guard let croppedImage = rulerImage?.crop(to: width, initialWidth: initialWidth) else { return }
let texture = SKTexture(image: croppedImage)
let material = SCNMaterial()
material.diffuse.contents = texture
geometry.materials = [material]
}

SCNNode 的大小变大并且图像也变大时,一切正常。所以大约 1.3 米我撞车了

validateTextureDimensions:759: failed assertion `MTLTextureDescriptor has width (16501) greater than the maximum allowed size of 16384.'

如有任何帮助,我们将不胜感激。我在想是否可以将图像分成几部分然后分配给 Material 。还是有其他方法可以做到这一点?

最佳答案

与其将单个 16501px 宽图像设置为一个 SCNode 代表整个标尺,建立数百个 1cm 的标尺更有意义 段,每个段都有一个纹理,您可以通过编程为带有数字的标尺段绘制纹理。

关于ios - 背景上的 SCNNode 大图像导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50292625/

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