Model3D和RealityView在visionOS中有什么区别?

huangapple go评论84阅读模式
英文:

What is the difference between Model3D and RealityView in visionOS?

问题

我认为RealityView应该能够做更多花哨的东西,但我仍然想弄清楚它们之间的确切区别。目前,它们只是太相似了,例如它们都需要一个实体名称来初始化。我想知道何时使用Model3D就足够了,以及何时必须使用RealityView?

英文:

I think RealityView should be able to do more fancy stuffs, but I still want to figure out the exact differences between them. For now, they are just too similar to each other, e.g. they both need an Entity name to get initialized. I want to know when a Model3D is just enough, and when we must use a RealityView?

答案1

得分: 5

以下是要翻译的内容:

RealityView

苹果工程师的标准做法是为视图提供多个初始化方法。visionOS的RealityView也不例外。让我们看一下init(make:update:attachments:)初始化方法,其中包含三个闭包,您可以在其中:

  • 创建、设置和配置初始3D场景的内容(make闭包)
  • 根据视图状态的变化更新场景内容(可选的update闭包)
  • 实现用于在3D场景中使用的附件视图的ViewBuilder(attachments闭包)

还有一个带有占位符init(make:update:placeholder:)初始化方法。

使用RealityView可以异步加载(这是可选操作)并在RealityKit应用程序中显示丰富的3D内容。RealityView将一个符合RealityViewContentProtocol的结构传递给makeupdate闭包,您可以使用它来向场景添加和移除RealityKit的实体。实体必须是3D基元、.usdz模型或来自Reality Composer Pro的场景。

RealityView { content, attachments in
    // 代码
} update: { content, attachments in
    // 代码           
} attachments: {
    // 代码         
}

就像在ARSCNView中一样(这是AR和VR的SceneKit的共生体),您不必使用锚点,但使用锚点可以让您实现强大的AR场景,比如图像跟踪平面跟踪手部跟踪等。

让我们看看代码中的RealityView是什么样子,控制台中会打印什么基本对象:

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {
        RealityView { content in
            if let model = try? await Entity.load(named: "car") {
                model.scale /= 10
                
                let anchor = AnchorEntity()
                anchor.addChild(model)
                content.add(anchor)
                
                print(content)
            }
        }
    }
}

换句话说,RealityView更像是visionOS SwiftUI应用程序的RealityKit视图,您可以在其中访问场景的组装点。不要忘记锚点是RealityView层次结构的重要组成部分,在Model3D中不是这种情况。

Model3D和RealityView在visionOS中有什么区别?


Model3D

SwiftUI的Model3D视图比RealityView更简单,用于异步加载和显示来自指定URL(.usdz.reality模型)或来自应用程序捆绑包的模型。在模型加载完成之前,SwiftUI会显示一个占位符。生成的模型包含在ResolvedModel3D视图中。在使用Model3D视图时,您无法像在RealityView中那样分解3D场景。

Model3D(url: url) { phase in
    if let model = phase.model {
        // 代码
    } else if phase.error != nil {
        // 错误描述    
    } else {
        // 占位符
    }
}

就像RealityView一样,Model3D视图有几个初始化方法。

让我们看看代码中的init(named:bundle:content:placeholder:)初始化方法是什么样子,控制台中会打印什么基本对象:

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {
        Model3D(named: "car") { model in
            model.resizable()

            let _ = print(model)
        } placeholder: {
            ProgressView()
        }
    }
}

换句话说,Model3D是SwiftUI的3D视图,包含RealityKit的场景。

Model3D和RealityView在visionOS中有什么区别?

英文:

RealityView

Standard practice for Apple engineers is to give views several initializers. visionOS RealityView isn't an exception here. Let's take a look at RealityView's init(make:update:attachments:) initializer with three closures, where you can:

  • create, set up and configure the initial 3D scene's content (make closure)
  • update scene's content in response to changes in view's state (optional update closure)
  • implement ViewBuilder of attachment views to use in 3D scene (attachments closure)

There's also init(make:update:placeholder:) initializer with a placeholder.

Use RealityView to asynchronously load (it's an optional action) and display a rich 3D content in RealityKit app. RealityView passes a struct conforming to RealityViewContentProtocol to the make and update closures, which you can use to add and remove RealityKit's entities to a scene. Entities must be 3D primitives, .usdz models, or scenes from Reality Composer Pro.

RealityView { content, attachments in
    // code
} update: { content, attachments in
    // code           
} attachments: {
    // code         
}

Just like in ARSCNView (it's a SceneKit's symbiosis of AR and VR), you are not obliged to use an anchor, however, using anchors gives you the opportunity to implement robust AR scenarios – such as image tracking, plane tracking, hand tracking, etc.

Let's see how RealityView looks in code and what base object will be printed in console:

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {
        RealityView { content in
            if let model = try? await Entity.load(named: "car") {
                model.scale /= 10
                
                let anchor = AnchorEntity()
                anchor.addChild(model)
                content.add(anchor)
                
                print(content)
            }
        }
    }
}

In other words, RealityView is rather a RealityKit's view for visionOS SwiftUI apps where you get an access to a scene's assembly point. And don't forget that anchors are an important part of the RealityView hierarchical structure, which isn't the case in Model3D.

Model3D和RealityView在visionOS中有什么区别?



Model3D

SwiftUI's Model3D view is simpler than RealityView, and is used to asynchronously load and display a model from the specified URL (.usdz or .reality models) or from app's bundle. Until the model loads, SwiftUI displays a placeholder. The resulted model is contained in ResolvedModel3D view. Working with Model3D view you are unable to decompose a 3D scene like in RealityView.

Model3D(url: url) { phase in
    if let model = phase.model {
        // code
    } else if phase.error != nil {
        // error description    
    } else {
        // placeholder
    }
}

Just like RealityView, Model3D view has several initializers.

Let's see how init(named:bundle:content:placeholder:) initializer looks in code and what base object will be printed in console:

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {
        Model3D(named: "car") { model in
            model.resizable()

            let _ = print(model)
        } placeholder: {
            ProgressView()
        }
    }
}

In other words, Model3D is the SwiftUI's 3D view holding RealityKit's scene.

Model3D和RealityView在visionOS中有什么区别?

答案2

得分: 0

这里还值得注意的是,不管在 SwiftUI 的哪个位置,所有的 RealityView 和 Model3D 实例都共享一个唯一的层次结构。

通过任何实体的 .parent 属性,可以向上遍历到一个名为 Window Context Entity 的共同根,还有一些新的树形实体类型,比如 CALayerEntity

英文:

It is also interesting to note that all instances of RealityView and Model3D share a unique hierarchy, regardless of where they are located in SwiftUI.

Using the .parent property of any entity, one can climb to a common root named Window Context Entity and there are also a number of intriguing new entity types in the tree like CALayerEntity

huangapple
  • 本文由 发表于 2023年7月23日 16:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定