只要表情符号是 2D 元素,最好使用 SpriteKit 框架来上传它们,而不是 SceneKit。但是,当然,您也可以选择 SceneKit。因此,您可以通过两种方式在 ARKit 中使用表情符号:
使用 SpriteKit。在这种情况下,您在 ARSKView 中生成的所有 2D 精灵始终面向相机。因此,如果相机围绕真实场景的某个特定点移动,所有精灵都会围绕它们面向相机的轴心点旋转。
使用 SceneKit。在 ARSCNView 中,您可以将所有精灵用作 3D 几何体的纹理。此纹理可以用于平面、立方体、球体或任何自定义模型,这取决于您。例如,要制作一个平面(上面有表情符号纹理)面对相机,请使用SCNBillboardConstraint 约束。
您在ViewController 中的代码可能如下所示:
// Element's index coming from `collectionView`
var i: Int = 0
func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
let emojiArray = ["?","?","?","?","?"]
let emojiNode = SKLabelNode(text: emojiArray[i])
emojiNode.horizontalAlignmentMode = .center
emojiNode.verticalAlignmentMode = .center
return emojiNode
}
...在Scene.swift 文件中:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let sceneView = self.view as? ARSKView else { return }
if let currentFrame = sceneView.session.currentFrame {
var translation = matrix_identity_float4x4
translation.columns.3.z = -0.75 // 75 cm from camera
let transform = simd_mul(currentFrame.camera.transform, translation)
let anchor = ARAnchor(transform: transform)
sceneView.session.add(anchor: anchor)
}
}
或者,如果您使用命中测试,您的代码可能如下所示:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let sceneView = self.view as? ARSKView else { return }
if let touchLocation = touches.first?.location(in: sceneView) {
if let hit = sceneView.hitTest(touchLocation, types: .featurePoint).first {
sceneView.session.add(anchor: ARAnchor(transform: hit.worldTransform))
}
}
}
如果您想创建一个包含可供选择的表情符号的UICollectionView 叠加层,请阅读the following post。
如果您想创建一个 SKView 叠加层,其中包含可供选择的表情符号,请阅读 the following post。