Spa*_*Dog 1 augmented-reality swift arkit realitykit
我在Experience.rcproject文件中创建了 3 个“场景” ,这是在您使用 xcode 启动新的增强现实项目时创建的。
经常为 3D 工作,我会说这些是场景中的 3 个对象,但在里面Experience.rcproject我添加了 3 个“场景”。每一个内部,都有相同的 3D 模型。第一个附加到水平平面,第二个附加到垂直平面,第三个附加到图像。
我第一次使用 Reality Kit 并在此过程中学习。
我这样做的想法是,当我想要将它附加到水平、垂直或图像时加载正确的对象。
我就是这样完成的。
我修改Experience.swift了 Apple 提供的文件以接受场景名称,如下所示:
public static func loadBox(namedFile:String) throws -> Experience.Box {
guard let realityFileURL = Foundation.Bundle(for: Experience.Box.self).url(forResource: "Experience", withExtension: "reality") else {
throw Experience.LoadRealityFileError.fileNotFound("Experience.reality")
}
let realityFileSceneURL = realityFileURL.appendingPathComponent(namedFile, isDirectory: false)
let anchorEntity = try Experience.Box.loadAnchor(contentsOf: realityFileSceneURL)
return createBox(from: anchorEntity)
}
Run Code Online (Sandbox Code Playgroud)
我叫这条线
let entity = try! Experience.loadBox(namedFile:sceneName)
Run Code Online (Sandbox Code Playgroud)
无论我想要什么,但我必须使用以下代码:
// I have to keep a reference to the entity so I can remove it from its parent and nil
currentEntity?.removeFromParent()
currentEntity = nil
// I have to load the entity again, now with another name
let entity = try! Experience.loadBox(namedFile:sceneName)
// store a reference to it, so I can remove it in the future
currentEntity = entity
// remove the old one from the scene
arView.scene.anchors.removeAll()
// add the new one
arView.scene.anchors.append(entity)
Run Code Online (Sandbox Code Playgroud)
这段代码很愚蠢,我相信有更好的方法。
有什么想法吗?
我认为这是一个“理论”问题而不是实际问题。起初我应该说编辑Experience包含带有锚点和实体的场景的文件不是一个好主意。
在 RealityKit 和 Reality Composer 中,如果您在默认场景中创建单个对象,则有非常明确的层次结构:
Scene –> AnchorEntity -> ModelEntity
|
Physics
|
Animation
|
Audio
Run Code Online (Sandbox Code Playgroud)
如果您在一个场景中放置了两个 3D 模型,它们共享同一个锚点:
Scene –> AnchorEntity – – – -> – – – – – – – – ->
| |
ModelEntity01 ModelEntity02
| |
Physics Physics
| |
Animation Animation
| |
Audio Audio
Run Code Online (Sandbox Code Playgroud)
AnchorEntity在 RealityKit 中定义了World Tracking当前运行的配置属性ARSession:horizontal/vertical平面检测和/或image detection,和/或body detection等。
让我们看看这些参数:
AnchorEntity(.plane(.horizontal, classification: .floor, minimumBounds: [1, 1]))
AnchorEntity(.plane(.vertical, classification: .wall, minimumBounds: [0.5, 0.5]))
AnchorEntity(.image(group: "Group", name: "model"))
Run Code Online (Sandbox Code Playgroud)
在这里您可以阅读有关实体-组件-系统范例的信息。
对于这篇文章,我在 Reality Composer 中准备了两个场景——第一个场景 ( ConeAndBox) 带有水平平面检测,第二个场景 ( Sphere) 带有垂直平面检测。如果您将 RealityKit 中的这些场景组合成一个更大的场景,您将获得两种类型的平面检测——水平和垂直。
在这个场景中,两个圆锥和盒子固定在一个锚上。
在 RealityKit 中,我可以将这些场景组合成一个场景。
// Plane Detection with a Horizontal anchor
let coneAndBoxAnchor = try! Experience.loadConeAndBox()
coneAndBoxAnchor.children[0].anchor?.scale = [7, 7, 7]
coneAndBoxAnchor.goldenCone!.position.y = -0.1 //.children[0].children[0].children[0]
arView.scene.anchors.append(coneAndBoxAnchor)
coneAndBoxAnchor.name = "mySCENE"
coneAndBoxAnchor.children[0].name = "myANCHOR"
coneAndBoxAnchor.children[0].children[0].name = "myENTITIES"
print(coneAndBoxAnchor)
// Plane Detection with a Vertical anchor
let sphereAnchor = try! Experience.loadSphere()
sphereAnchor.steelSphere!.scale = [7, 7, 7]
arView.scene.anchors.append(sphereAnchor)
print(sphereAnchor)
Run Code Online (Sandbox Code Playgroud)
在 Xcode 的控制台中,您可以ConeAndBox使用 RealityKit 中给出的名称查看场景层次结构:
您可以看到Sphere没有给出名称的场景层次结构:
需要注意的是,我们的组合场景现在在一个数组中包含两个场景。使用以下命令打印此数组:
print(arView.scene.anchors)
Run Code Online (Sandbox Code Playgroud)
它打印:
[ 'mySCENE' : ConeAndBox, '' : Sphere ]
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式重新分配跟踪类型AnchoringComponent(您可以分配图像检测而不是平面检测):
coneAndBoxAnchor.children[0].anchor!.anchoring = AnchoringComponent(.image(group: "AR Resources",
name: "planets"))
Run Code Online (Sandbox Code Playgroud)
为了分解/重新组装场景的层次结构,您需要检索所有实体并将它们固定到单个锚点。考虑到 - 跟踪一个锚点比跟踪多个锚点的任务强度要小。就场景模型的相对位置而言,一个锚点比 20 个锚点要稳定得多。
let coneEntity = coneAndBoxAnchor.goldenCone!
coneEntity.position.x = -0.2
let boxEntity = coneAndBoxAnchor.plasticBox!
boxEntity.position.x = 0.01
let sphereEntity = sphereAnchor.steelSphere!
sphereEntity.position.x = 0.2
let anchor = AnchorEntity(.image(group: "AR Resources", name: "planets")
anchor.addChild(coneEntity)
anchor.addChild(boxEntity)
anchor.addChild(sphereEntity)
arView.scene.anchors.append(anchor)
Run Code Online (Sandbox Code Playgroud)
现在您对如何构建场景和从这些场景中检索实体有了更深入的了解。如果您需要其他示例,请查看THIS POST和THIS POST。
显示如何从ExperienceX.rcproject以下位置上传场景的附加代码:
import ARKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
// RC generated "loadGround()" method automatically
let groundArrowAnchor = try! ExperienceX.loadGround()
groundArrowAnchor.arrowFloor!.scale = [2,2,2]
arView.scene.anchors.append(groundArrowAnchor)
print(groundArrowAnchor)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1354 次 |
| 最近记录: |