SCNText 不显示表情符号 - Swift 中的 ARKit
SCNText Not Displaying Emoji's - ARKit in Swift
我正在制作一个应用程序,将 SCNText
添加到我创建的名为 sceneView
的 ARSCNView
中。文本完美显示如下:(当 SCNText(string: "Test", extrusionDepth: CGFloat(0.01))
时)
屏幕截图 1:
但是当 SCNText(string: "Test", extrusionDepth: CGFloat(0.01))
SCNText
不显示表情符号时:
截图2:
这是完整的代码:
import UIKit
import SceneKit
import ARKit
import Vision
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet weak var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
// Create a new scene
let scene = SCNScene()
// Set the scene to the view
sceneView.scene = scene
// Enable Default Lighting - makes the 3D text a bit poppier.
sceneView.autoenablesDefaultLighting = true
sceneView.showsStatistics = true
// Tap Gesture Recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(gestureRecognize:)))
view.addGestureRecognizer(tapGesture)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Enable plane detection
configuration.planeDetection = .horizontal
// Run the view's session
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
DispatchQueue.main.async {
}
}
@objc func handleTap(gestureRecognize: UITapGestureRecognizer) {
self.create()
}
func create() {
// Get Screen Centre
let screenCentre : CGPoint = CGPoint(x: self.sceneView.bounds.midX, y: self.sceneView.bounds.midY)
let arHitTestResults : [ARHitTestResult] = sceneView.hitTest(screenCentre, types: [.featurePoint])
if let closestResult = arHitTestResults.first {
// Get Coordinates of HitTest
let transform : matrix_float4x4 = closestResult.worldTransform
let worldCoord : SCNVector3 = SCNVector3Make(transform.columns.3.x, transform.columns.3.y, transform.columns.3.z)
// Create 3D Text
let node : SCNNode = createNewParentNode()
sceneView.scene.rootNode.addChildNode(node)
node.position = worldCoord
}
}
func createNewParentNode(_ text : String) -> SCNNode {
let text = SCNText(string: "Test", extrusionDepth: CGFloat(0.01))
var font = UIFont(name: "Futura", size: 0.07)
font = font?.withTraits(traits: .traitBold)
text.font = font
text.alignmentMode = kCAAlignmentCenter
text.firstMaterial?.diffuse.contents = UIColor.orange
text.firstMaterial?.specular.contents = UIColor.white
text.firstMaterial?.isDoubleSided = true
text.chamferRadius = CGFloat(bubbleDepth)
let textNode = SCNNode(geometry: text)
textNode.pivot = SCNMatrix4MakeTranslation( positionx, positiony, positionz) // Don't worry about the positions they work fine
text.scale = SCNVector3Make(0.1, 0.1, 0.1)
let NodeParent = SCNNode()
NodeParent.addChildNode(text)
//Add More SCNNode's to NodeParent
return NodeParent
}
}
extension UIFont {
// Based on:
func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
let descriptor = self.fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
return UIFont(descriptor: descriptor!, size: 0)
}
}
我不想必须将其添加为 SKLabelNode
,因为我想稍后使用 Storyboard 进行集合视图。
跟我用的字体有关系吗?
"SceneKit can create text geometry using any font and style supported
by the Core Text framework, with the exception of bitmap fonts (such
as those that define color emoji characters)".
但是,您仍然可以将表情符号放在 SCNNode(比如框)上,如 material/texture。
编辑:
几个月前,我使用表情符号创建了一个国家国旗 ios 应用程序,这是一个很好的演示 Youtube video - Country Flags iOS App
我正在制作一个应用程序,将 SCNText
添加到我创建的名为 sceneView
的 ARSCNView
中。文本完美显示如下:(当 SCNText(string: "Test", extrusionDepth: CGFloat(0.01))
时)
屏幕截图 1:
但是当 SCNText(string: "Test", extrusionDepth: CGFloat(0.01))
SCNText
不显示表情符号时:
截图2:
import UIKit
import SceneKit
import ARKit
import Vision
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet weak var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
// Create a new scene
let scene = SCNScene()
// Set the scene to the view
sceneView.scene = scene
// Enable Default Lighting - makes the 3D text a bit poppier.
sceneView.autoenablesDefaultLighting = true
sceneView.showsStatistics = true
// Tap Gesture Recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(gestureRecognize:)))
view.addGestureRecognizer(tapGesture)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Enable plane detection
configuration.planeDetection = .horizontal
// Run the view's session
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
DispatchQueue.main.async {
}
}
@objc func handleTap(gestureRecognize: UITapGestureRecognizer) {
self.create()
}
func create() {
// Get Screen Centre
let screenCentre : CGPoint = CGPoint(x: self.sceneView.bounds.midX, y: self.sceneView.bounds.midY)
let arHitTestResults : [ARHitTestResult] = sceneView.hitTest(screenCentre, types: [.featurePoint])
if let closestResult = arHitTestResults.first {
// Get Coordinates of HitTest
let transform : matrix_float4x4 = closestResult.worldTransform
let worldCoord : SCNVector3 = SCNVector3Make(transform.columns.3.x, transform.columns.3.y, transform.columns.3.z)
// Create 3D Text
let node : SCNNode = createNewParentNode()
sceneView.scene.rootNode.addChildNode(node)
node.position = worldCoord
}
}
func createNewParentNode(_ text : String) -> SCNNode {
let text = SCNText(string: "Test", extrusionDepth: CGFloat(0.01))
var font = UIFont(name: "Futura", size: 0.07)
font = font?.withTraits(traits: .traitBold)
text.font = font
text.alignmentMode = kCAAlignmentCenter
text.firstMaterial?.diffuse.contents = UIColor.orange
text.firstMaterial?.specular.contents = UIColor.white
text.firstMaterial?.isDoubleSided = true
text.chamferRadius = CGFloat(bubbleDepth)
let textNode = SCNNode(geometry: text)
textNode.pivot = SCNMatrix4MakeTranslation( positionx, positiony, positionz) // Don't worry about the positions they work fine
text.scale = SCNVector3Make(0.1, 0.1, 0.1)
let NodeParent = SCNNode()
NodeParent.addChildNode(text)
//Add More SCNNode's to NodeParent
return NodeParent
}
}
extension UIFont {
// Based on:
func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
let descriptor = self.fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
return UIFont(descriptor: descriptor!, size: 0)
}
}
我不想必须将其添加为 SKLabelNode
,因为我想稍后使用 Storyboard 进行集合视图。
跟我用的字体有关系吗?
"SceneKit can create text geometry using any font and style supported by the Core Text framework, with the exception of bitmap fonts (such as those that define color emoji characters)".
但是,您仍然可以将表情符号放在 SCNNode(比如框)上,如 material/texture。
编辑: 几个月前,我使用表情符号创建了一个国家国旗 ios 应用程序,这是一个很好的演示 Youtube video - Country Flags iOS App