如何将自己的Reality Composer场景加载到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)
  }

我叫这条线

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)

这段代码很愚蠢,我相信有更好的方法。

有什么想法吗?

安迪·费多罗夫(Andy Fedoroff)

RealityKit / Reality Composer中的层次结构

我认为这是一个“理论”问题,而不是实际问题。首先,我应该说编辑Experience包含锚点和实体场景的文件不是一个好主意。

在RealityKit和Reality Composer中,如果您在默认场景中创建了单个对象,则存在相当明确的层次结构:

Scene –> AnchorEntity -> ModelEntity 
                              |
                           Physics
                              |
                          Animation
                              |
                            Audio
                          

如果在场景中放置了两个3D模型,则它们共享相同的锚点:

Scene –> AnchorEntity – – – -> – – – – – – – – ->
                             |                  |
                       ModelEntity01      ModelEntity02
                             |                  |
                          Physics            Physics
                             |                  |
                         Animation          Animation
                             |                  |
                           Audio              Audio

AnchorEntity在RealityKit中定义了World Trackingconfig的哪些属性正在当前中运行ARSessionhorizontal/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的两个场景

在本文中,我准备了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"))


Retrieving entities and connecting them to new AnchorEntity

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)


Useful links

现在,您对如何构造场景以及从这些场景检索实体有了更深入的了解。如果需要其他示例,请参见THIS POSTTHIS 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

RealityKit –从同一Reality Composer项目中加载另一个场景

来自分类Dev

如何将自己添加到sudoers列表中?

来自分类Dev

如何将自己的mfile包含到matlab库中?

来自分类Dev

如何将自己的图像插入到 SQLite 表中?

来自分类Dev

如何将自己的软件添加到Buildroot Linux软件包中?

来自分类Dev

如何将自己的.class添加到已编译的.jar文件中?

来自分类Dev

如何将自己的画布设置到PdfDocument.Page中

来自分类Dev

Fedora:如何将自己重新添加到sudoers文件中?

来自分类Dev

我如何将自己附加到在无法访问的终端中运行的进程上?

来自分类Dev

如何将自己添加到错误报告中?

来自分类Dev

如何将自定义UIView从Xib文件加载到UIViewController中的UIView中?

来自分类Dev

如何将自己的php包GitHub项目与composer集成在一起使用并同时进行开发

来自分类Dev

如何将 Scene Builder 中创建的场景加载到 JavaFX 应用程序主类中?

来自分类Dev

您如何将自定义模块加载到Lua中?

来自分类Dev

如何将自定义的DaoAuthenticationProvider加载到Spring Context中?

来自分类Dev

如何将自定义DLL加载到PowerShell中

来自分类Dev

如何将自定义user.config xml加载到字典中

来自分类Dev

如何让我的数据库值(使用MySQL)加载到场景中并且可以看到?

来自分类Dev

如何将自己的.py文件作为模块导入到Sublime中的另一个.py文件

来自分类Dev

您如何将自己的 Oauth2 Passport 策略添加到 FeathersJS 应用程序中?

来自分类Dev

不小心将主用户从 sudo 组中删除时如何将自己添加到 sudoers

来自分类Dev

GitHub:如何将自己拥有的存储库转移到自己拥有的组织中,并保存在我的私人帐户中?

来自分类Dev

如何将每个图像文件加载到自己的画布元素中

来自分类Dev

在Reality Composer生成的场景中以编程方式设置纹理

来自分类Dev

如何将自己的Tripadvisor(或其他自定义图标)添加到Divi Wordpress主题的社交媒体图标中?

来自分类Dev

当我在Debian机器上没有root用户访问权限时,如何将自己添加到sudoers中?

来自分类Dev

如何将自定义表情符号加载到键盘?

来自分类Dev

如何将自己的类型输入表格

来自分类常见问题

如何将自己的项目从自己的项目导入Playground

Related 相关文章

  1. 1

    RealityKit –从同一Reality Composer项目中加载另一个场景

  2. 2

    如何将自己添加到sudoers列表中?

  3. 3

    如何将自己的mfile包含到matlab库中?

  4. 4

    如何将自己的图像插入到 SQLite 表中?

  5. 5

    如何将自己的软件添加到Buildroot Linux软件包中?

  6. 6

    如何将自己的.class添加到已编译的.jar文件中?

  7. 7

    如何将自己的画布设置到PdfDocument.Page中

  8. 8

    Fedora:如何将自己重新添加到sudoers文件中?

  9. 9

    我如何将自己附加到在无法访问的终端中运行的进程上?

  10. 10

    如何将自己添加到错误报告中?

  11. 11

    如何将自定义UIView从Xib文件加载到UIViewController中的UIView中?

  12. 12

    如何将自己的php包GitHub项目与composer集成在一起使用并同时进行开发

  13. 13

    如何将 Scene Builder 中创建的场景加载到 JavaFX 应用程序主类中?

  14. 14

    您如何将自定义模块加载到Lua中?

  15. 15

    如何将自定义的DaoAuthenticationProvider加载到Spring Context中?

  16. 16

    如何将自定义DLL加载到PowerShell中

  17. 17

    如何将自定义user.config xml加载到字典中

  18. 18

    如何让我的数据库值(使用MySQL)加载到场景中并且可以看到?

  19. 19

    如何将自己的.py文件作为模块导入到Sublime中的另一个.py文件

  20. 20

    您如何将自己的 Oauth2 Passport 策略添加到 FeathersJS 应用程序中?

  21. 21

    不小心将主用户从 sudo 组中删除时如何将自己添加到 sudoers

  22. 22

    GitHub:如何将自己拥有的存储库转移到自己拥有的组织中,并保存在我的私人帐户中?

  23. 23

    如何将每个图像文件加载到自己的画布元素中

  24. 24

    在Reality Composer生成的场景中以编程方式设置纹理

  25. 25

    如何将自己的Tripadvisor(或其他自定义图标)添加到Divi Wordpress主题的社交媒体图标中?

  26. 26

    当我在Debian机器上没有root用户访问权限时,如何将自己添加到sudoers中?

  27. 27

    如何将自定义表情符号加载到键盘?

  28. 28

    如何将自己的类型输入表格

  29. 29

    如何将自己的项目从自己的项目导入Playground

热门标签

归档