我在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)
}
我叫这条线
let entity = try! Experience.loadBox(namedFile:sceneName)
无论我想要什么,但我必须使用以下代码:
// 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)
这段代码很愚蠢,我相信有更好的方法。
有什么想法吗?
我认为这是一个“理论”问题,而不是实际问题。首先,我应该说编辑Experience
包含锚点和实体场景的文件不是一个好主意。
在RealityKit和Reality Composer中,如果您在默认场景中创建了单个对象,则存在相当明确的层次结构:
Scene –> AnchorEntity -> ModelEntity
|
Physics
|
Animation
|
Audio
如果在场景中放置了两个3D模型,则它们共享相同的锚点:
Scene –> AnchorEntity – – – -> – – – – – – – – ->
| |
ModelEntity01 ModelEntity02
| |
Physics Physics
| |
Animation Animation
| |
Audio Audio
AnchorEntity
在RealityKit中定义了World Tracking
config的哪些属性正在当前中运行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"))
在本文中,我准备了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)
在Xcode的控制台中,您可以看到ConeAndBox
具有RealityKit中给定名称的场景层次:
And you can see Sphere
scene hierarchy with no names given:
And it's important to note that our combined scene now contains two scenes in an array. Use the following command to print this array:
print(arView.scene.anchors)
It prints:
[ 'mySCENE' : ConeAndBox, '' : Sphere ]
You can reassign a type of tracking via AnchoringComponent
(instead of plane detection you can assign an image detection):
coneAndBoxAnchor.children[0].anchor!.anchoring = AnchoringComponent(.image(group: "AR Resources",
name: "planets"))
For decomposing/reassembling an hierarchical structure of your scene, you need to retrieve all entities and pin them to a single anchor. Take into consideration – tracking one anchor is less intensive task than tracking several ones. And one anchor is much more stable – in terms of the relative positions of scene models – than, for instance, 20 anchors.
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)
现在,您对如何构造场景以及从这些场景检索实体有了更深入的了解。如果需要其他示例,请参见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)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句