代码之家  ›  专栏  ›  技术社区  ›  Kashif Richard Brightwell

当用户旋转场景/相机时,如何在SCNSphere中不旋转包装的文本

  •  0
  • Kashif Richard Brightwell  · 技术社区  · 7 年前
    let dotGeometry = SCNSphere(radius: 0.0005)
    let dotNode = SCNNode(geometry: dotGeometry)
    dotNode.position = dotPosition
    if let image = imageWithText(text: "\(index)", fontSize:20, imageSize: CGSize(width:100,height:100), backgroundColor: .cyan) {
        dotGeometry.firstMaterial?.diffuse.contents = image
    }
    self.rootNode.addChildNode(dotNode)
    

    我正在使用上面的代码创建一个 SCNNode 具有 SCNSphere 在......下面

    除了当用户开始旋转场景时,球体会随着场景旋转,文本会移到球体的后面,而不在用户的视野内,所有这些都可以正常工作。

    func imageWithText(text:String, fontSize:CGFloat, 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 = [
            NSAttributedString.Key.font: UIFont(name: "Arial-BoldMT", size:fontSize),
            NSAttributedString.Key.paragraphStyle: paragraphStyle,
            NSAttributedString.Key.foregroundColor: fontColor
        ]
    
        // Determine the width/height of the text for the attributes
        let textSize = text.size(withAttributes: attributes as [NSAttributedString.Key : Any])
    
    // 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 as [NSAttributedString.Key : Any])
    
        if let image = UIGraphicsGetImageFromCurrentImageContext() {
            return image
        }
        return nil
    }
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Kashif Richard Brightwell    7 年前

    最后通过添加 dotNode.constraints = [SCNBillboardConstraint()] dotNode

    推荐文章