【问题标题】:Drawing text across sphere in SceneKit在 SceneKit 中跨球体绘制文本
【发布时间】:2017-11-20 08:20:44
【问题描述】:

我刚开始在我的 UIKit 应用程序中使用 SceneKit,目的是显示和操作一些 3D 模型。我需要显示一个球体,上面写着一些简短的文字。我正在像这样渲染球体:

let sphereGeometry = SCNSphere(radius: 1)
let sphereNode = SCNNode(geometry: sphereGeometry)
sphereNode.position = SCNVector3(x: -1, y: 0, z: 8)
sphereGeometry.firstMaterial?.diffuse.contents = UIColor.cyan

self.rootNode.addChildNode(sphereNode)

我尝试使用CATextLayer 来实现我所需要的,但我运气不佳。这样做的正确方法是什么?

【问题讨论】:

  • 你想让文本环绕球体吗?
  • @0x141E 基本上是的,我需要它成为球体表面的一部分

标签: ios text 3d scenekit scnnode


【解决方案1】:

您可以通过创建包含文本的图像将文本环绕在对象的表面,例如,

然后通过

加载图像并将其分配给漫反射的contents属性
    let sphereGeometry = SCNSphere(radius: 1)
    let sphereNode = SCNNode(geometry: sphereGeometry)
    sphereNode.position = SCNVector3(x: 0, y: 0, z: 0)

    if let textImage = UIImage(named:"TextImage") {
        sphereGeometry.firstMaterial?.diffuse.contents = textImage
    }

    scene.rootNode.addChildNode(sphereNode)

或者,您可以通过以下方式以编程方式创建带有文本的图像

func imageWithText(text:String, fontSize:CGFloat = 150, fontColor:UIColor = .black, imageSize:CGSize, backgroundColor:UIColor) -> UIImage? {

    let imageRect = CGRect(origin: CGPoint.zero, size: imageSize)
    UIGraphicsBeginImageContext(imageSize)

    defer {
        UIGraphicsEndImageContext()
    }

    guard let context = UIGraphicsGetCurrentContext() else {
        return nil
    }

    // Fill the background with a color
    context.setFillColor(backgroundColor.cgColor)
    context.fill(imageRect)

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center

    // Define the attributes of the text
    let attributes = [
        NSFontAttributeName: UIFont(name: "TimesNewRomanPS-BoldMT", size:fontSize),
        NSParagraphStyleAttributeName: paragraphStyle,
        NSForegroundColorAttributeName: fontColor
    ]

    // Determine the width/height of the text for the attributes
    let textSize = text.size(attributes: attributes)

    // Draw text in the current context
    text.draw(at: CGPoint(x: imageSize.width/2 - textSize.width/2, y: imageSize.height/2 - textSize.height/2), withAttributes: attributes)

    if let image = UIGraphicsGetImageFromCurrentImageContext() {
        return image
    }
    return nil
}

并将图像应用到球体

    let sphereGeometry = SCNSphere(radius: 1)
    let sphereNode = SCNNode(geometry: sphereGeometry)
    sphereNode.position = SCNVector3(x: 0, y: 0, z: 0)

    if let image = imageWithText(text: "Hello, World!", imageSize: CGSize(width:1024,height:1024), backgroundColor: .cyan) {
        sphereGeometry.firstMaterial?.diffuse.contents = image
    }

    scene.rootNode.addChildNode(sphereNode)

【讨论】:

  • 工作出色!谢谢!
  • 当指定要显示的文本的属性时,有没有办法使它将文本呈现为 HTML?我正在使用自定义 setHTML 方法为 UILabel 做类似的事情,该方法设置 documentType 属性。
  • 我找不到任何可以让你这样做的东西。也许您可以使用NSAttributedString 完成相同或类似的操作。
猜你喜欢
  • 2017-09-18
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多