如何解决RealityKit中的Entity和ModelEntity有什么区别?
我是SwiftUI,ARKit和RealityKit的新手,非常抱歉我的基本问题 我被告知要使用这些方法加载模型
Entity.load(named: "abc.usdz")
和
ModelEntity.load(named: "abc.usdz")
那么ModelEntity和Entity有什么区别?
解决方法
首先,ModelEntity
是主要父节点类的子类,称为Entity
。
根据文档:
实体表示场景中的“某物”。诸如PerspectiveCamera,SpotLight或ModelEntity之类的后代都从Entity类继承。
class Entity : HasHierarchy,HasSynchronization,HasTransform
ModelEntity(像其他实体一样)更为具体。它表示一个模型,该模型被渲染并可以选择模拟。它继承自Entity类,并符合HasModel和HasPhysics协议。
class ModelEntity : Entity,HasModel,HasPhysics
通常两个实体类是可互换的(在设置组件时)。例如,您可以使用Entity
而不是SpotLight
类。
Entity().components[SpotLightComponent] = SpotLightComponent(color: .red,intensity: 2000,innerAngleInDegrees: 45,outerAngleInDegrees: 90,attenuationRadius: 5.0)
vs
SpotLight().components[SpotLightComponent] = SpotLightComponent(color: .red,attenuationRadius: 5.0)
但是,可以给出以下示例作为例外。向下广播时,您需要完全使用Entity
类。
arView.installGestures([.all],for: entity as! Entity & HasCollision)