What's the difference between Entity and ModelEntity in RealityKit?

1.8k Views Asked by At

I am new to SwiftUI, ARKit And RealityKit so sorry for my very basic question I have been told to use these methods to load models

Entity.load(named: "abc.usdz") 

and

ModelEntity.load(named: "abc.usdz")

so what's the difference between a ModelEntity and Entity ?

1

There are 1 best solutions below

0
On

First of all, ModelEntity is a child class of main parent nodes' class called Entity.

According to documentation:

An entity represents ‘something’ in a scene. Offsprings like PerspectiveCamera, SpotLight or ModelEntity inherit from Entity class.

class Entity : HasHierarchy, HasSynchronization, HasTransform

ModelEntity (like any other entities) is more specific. It represents a model which is rendered and optionally simulated. It inherits from Entity class and conforms to HasModel and HasPhysics protocols.

class ModelEntity : Entity, HasModel, HasPhysics


Often two Entity classes are interchangeable (when setting components). For example you can use Entity instead of SpotLight class.

Entity().components[SpotLightComponent] = SpotLightComponent(color: .red, 
                                                         intensity: 2000, 
                                               innerAngleInDegrees: 45, 
                                               outerAngleInDegrees: 90, 
                                                 attenuationRadius: 5.0)

vs

SpotLight().components[SpotLightComponent] = SpotLightComponent(color: .red, 
                                                            intensity: 2000, 
                                                  innerAngleInDegrees: 45, 
                                                  outerAngleInDegrees: 90, 
                                                    attenuationRadius: 5.0)

However, the following example can be given as an exception. You need to exactly use Entity class when type casting.

arView.installGestures([.all], for: entity as! Entity & HasCollision)