转载请注明出处
Appple原文地址: https://developer.apple.com/documentation/arkit/arskview/providing_2d_virtual_content_with_spritekit
Providing 2D Virtual Content with SpriteKit(使用 SpriteKit 来提供 2D 虚拟元素)
Use SpriteKit to place two-dimensional images in 3D space in your AR experience.
使用 SpriteKit 在您的 3D AR 场景中放置二维图像。
Overview
- To place SpriteKit content in augmented reality, you'll first need a running AR session (see Building a Basic AR Experience).
要将 SpriteKit 元素放进增强现实当中,您首先需要运行 AR Session(请参阅构建基本的 AR 场景一节)。
SpriteKit is inherently for 2D visual content, but augmented reality involves real-world 3D spaces. Use the ARSKView
class to create AR experiences by providing 2D sprites (SKNode
objects) that correspond to real-world 3D positions (ARAnchor
objects). When the user moves the device, the view automatically rotates and scales the SpriteKit nodes corresponding to anchors so that they appear to track the real world seen by the camera.SpriteKit 只包含有 2D 元素,但是增强现实则涉及到现实世界的 3D 空间。因此,使用ARSKView类来创建 AR 场景,这样就可以在真实世界对应的 3D 位置 ( ARAnchor对象) 中放置 2D 精灵元素 ( SKNode) 对象)。当用户移动设备的时候,视图会自动旋转并缩放与锚点相对应的 SpriteKit 结点,看起来这些元素能够追踪相机所看到的真实世界。
For example, you can place 2D images that appear to float in 3D space:
举个例子,您可以将 2D 图像以漂浮的方式,放置在 3D 空间当中:
// Create a transform with a translation of 0.2 meters in front of the camera.
var translation = matrix_identity_float4x4
translation.columns.3.z = -0.2
let transform = simd_mul(view.session.currentFrame.camera.transform, translation)
// Add a new anchor to the session.
let anchor = ARAnchor(transform: transform)
view.session.add(anchor: anchor)
// (Elsewhere...) Provide a label node to represent the anchor.
func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
return SKLabelNode(text: "👾")
}
- The view(_:nodeFor:)
method above returns an SKLabelNode
object, which displays a text label. Like most SpriteKit nodes, this class creates a 2D visual representation, so the ARSKView
class presents the node in a billboard style: The sprite scales and rotates (around its z-axis) so that it appears to follow the 3D position of its anchor, but always faces toward the camera. - 上面的这个 view(_:nodeFor:)方法将会返回一个 SKLabelNode对象,用以展示文本标签。与大多数 SpriteKit 结点一样,这个类将会创建一个 2D 的可视化表示,因此ARSKView类将会以广告牌的样式来展示这个结点:精灵可以通过(围绕 z 轴)缩放以及旋转,让其看起来能够跟随锚点的 3D 位置,但是却始终面向相机。