ARKit学习-5

转载请注明出处
Apple原文地址: https://developer.apple.com/documentation/arkit/arscnview/providing_3d_virtual_content_with_scenekit


Providing 3D Virtual Content with SceneKit 使用 SceneKit 来提供 3D 虚拟元素

  • Use SceneKit to add realistic three-dimensional objects to your AR experience.
    使用 SceneKit 在您的 AR 场景中放置逼真的三维图像。

Overview

  • To place SceneKit content in augmented reality, you'll first need a running AR session (see Building a Basic AR Experience.
  • 要将 SceneKit 元素放到增强现实当中的话,您首先需要运行 AR Session(请参阅构建基本的 AR 场景一节)。
  • Because ARKit automatically matches SceneKit space to the real world, placing a virtual object so that it appears to maintain a real-world position requires that you set the object's SceneKit position appropriately. For example, in a default configuration, the following code places a 10-centimeter cube 20 centimeters in front of the camera's initial position:
  • 由于 ARKit 会自动将 SceneKit 空间与现实世界进行匹配,因此只需要放置一个虚拟对象,然后让其位于一个看起来比较真实的位置,这就需要您适当地设置 SceneKit 对象的位置。例如,在默认配置下,以下代码会将一个 10 厘米的立方体放置在相机初始位置的前 20 厘米处:
let cubeNode = SCNNode(geometry: SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0))
cubeNode.position = SCNVector3(0, 0, -0.2) // SceneKit/AR coordinates are in meters
sceneView.scene.rootNode.addChildNode(cubeNode)
  • The code above places an object directly in the view’s SceneKit scene. The object automatically appears to track a real-world position because ARKit matches SceneKit space to real-world space.
  • 上述代码直接在视图的 SceneKit 场景中放置了一个对象。该对象会自动追踪真实世界的位置,因为 ARKit 将 SceneKit 空间与现实世界空间互相匹配了起来

  • Alternatively, you can use the ARAnchor
    class to track real-world positions, either by creating anchors yourself and adding them to the session or by observing anchors that ARKit automatically creates. For example, when plane detection is enabled, ARKit adds and updates anchors for each detected plane. To add visual content for these anchors, implement ARSCNViewDelegate
    methods such as the following:

  • 此外,您也可以使用 ARAnchor 类来追踪现实世界的位置,可以自行创建锚点并将其添加到 Session 当中,也可以对 ARKit 自动创建的锚点进行观察 (observing)。例如,当水平面检测启用的时候,ARKit 会为每个检测到的水平面添加锚点,并保持更新。要为这些锚点添加可视化内容的话,就需要实现 ARSCNViewDelegate 方法,如下所示:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    // This visualization covers only detected planes.
    guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

    // Create a SceneKit plane to visualize the node using its position and extent.
    let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))
    let planeNode = SCNNode(geometry: plane)
    planeNode.position = SCNVector3Make(planeAnchor.center.x, 0, planeAnchor.center.z)

    // SCNPlanes are vertically oriented in their local coordinate space.
    // Rotate it to match the horizontal orientation of the ARPlaneAnchor.
    planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)

    // ARKit owns the node corresponding to the anchor, so make the plane a child node.
    node.addChildNode(planeNode)
}

Follow Best Practices for Designing 3D Assets / 遵循设计 3D 资源时的最佳实践

  • Use the SceneKit physically based lighting model for materials for a more realistic appearance. (See the SCNMaterial
    class and the Badger: Advanced Rendering in SceneKit sample code project.)
  • 使用 SceneKit 基于物理引擎的照明模型,以便获得更为逼真的外观。(参见SCNMaterial类和 SceneKit 示例代码项目当中的 「Badger: Advanced Rendering」。)

  • Bake ambient occlusion shading so that objects appear properly lit in a wide variety of scene lighting conditions.

  • 打上环境光遮蔽阴影 (Bake ambient occlusion shading),使得物体在各种场景照明条件下能够正常亮起。

  • If you create a virtual object that you intend to place on a real-world flat surface in AR, include a transparent plane with a soft shadow texture below the object in your 3D asset.
  • 如果您打算创建一个虚拟对象,并打算将其放置在 AR 的真实平面 (real-world flat surface) 上,那么请在 3D 素材中的对象下方,添加一个带有柔和阴影纹理的透明平面。

你可能感兴趣的:(ARKit学习-5)