当 Scene/Camera 被用户旋转时如何不旋转 SCNSphere 中的换行文本
How to not rotate Wrapped Text in SCNSphere when Scene/Camera is rotated by user
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
}
如何让文本始终在用户视图中,即不让文本随着 camera/scene 旋转而旋转?
最终通过在创建 dotNode
后添加 dotNode.constraints = [SCNBillboardConstraint()]
来完成此操作。
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
}
如何让文本始终在用户视图中,即不让文本随着 camera/scene 旋转而旋转?
最终通过在创建 dotNode
后添加 dotNode.constraints = [SCNBillboardConstraint()]
来完成此操作。