Entity Component System in aframe
Join 6000+ Students
Upgrade your programming skills to include Virtual Reality. A premium step-by-step training course to get you building real world Virtual Reality apps and website components. You’ll have access to 40+ WebVR & Unity lessons along with their source code.
Start Learning Now
Aframe, the VR development library from Mozilla uses Entity Component System pattern. In this post, we’re going to look more into what entity component system in aframe is and how it affects the development experience.
Aframe
We’ve talked previously about how to get started in aframe. Aframe makes it easy to develop in VR. It is a wrapper over threejs making easy for even newcomers to get started in VR. It doesn’t stop you from developing more sophisticated components later on with threejs either. That’s what makes it great.
Entity component system
Entity component system has three parts.
- Entity
- Component
- System
We’re going to see each of this in detail.
Entity
Entity is a general purpose object that does and renders nothing. It just acts as a wrapper.
1 2 3 |
<a-entity> |
In its basic form, it does nothing. We can attach components to it to make it more useful. For example, we can give it a shape by connecting a geometry component.
1 2 3 |
<a-entity geometry="primitive: box"></a-entity> |
We can access entity by using DOM APIs.
1 2 3 |
<a-entity id="vr"></a-entity> |
1 2 3 |
var element = document.querySelector('#vr'); |
Once we retrieve an entity via DOM API, we can access its properties. Let’s look at the available properties of the entity.
Components
It gives access to all the components of the entity.
isPlaying
Checks whether the entity is active and playing.
object3DMap
It gives access to THREE.Object3D of the entity. For example, for an entity with geometry and light components attached, object3DMap can look like this.
1 2 3 4 5 6 |
{ light: <THREE.Light Object>, mesh: <THREE.Mesh Object> } |
sceneEl
It is the reference to the scene element of the entity. a-scene
is used to represent the scene.
Component
The Component is the reusable module that can be used with entities to give it properties such as appearance and behavior. Components are built using threejs and JavaScript code. They make it easy to create modules that can be called from HTML. Component names represent HTML attributes, and the value of attributes represent the data.
Let’s look at a simple component in aframe, the position component.
1 2 3 4 5 6 7 8 9 10 11 |
AFRAME.registerComponent('position', { schema: { type: 'vec3' }, update: function () { var object3D = this.el.object3D; var data = this.data; object3D.position.set(data.x, data.y, data.z); } }); |
You’d use the position component as follows.
1 2 3 |
<a-entity position="2 3 4"></a-entity> |
Position component is an example for single property component.
Single property component
Single property component can represent data by a single value.
Multi property component
Multi-property component can represent data by several properties and values. When written in HTML, they resemble inline CSS properties.
1 2 3 4 |
<!-- light is the name of the component --> <a-entity light="type: point; color: green"></a-entity> |
Creating aframe components
You might be wondering how to create aframe components. AFRAME.registerComponent can be used to register new components.
Registering a component requires a component name. Then we define the schema and the lifecycle methods of the components. To see how to register a component, let’s look at the position component that we checked earlier.
1 2 3 4 5 6 7 8 9 10 |
AFRAME.registerComponent('position', { schema: { type: 'vec3' }, update: function () { var object3D = this.el.object3D; var data = this.data; object3D.position.set(data.x, data.y, data.z); } }); |
In the position component, the schema is vec3 which gives the position component a {x, y, z} object.
Components also has properties and methods.
You can also specify dependencies for your components. For example, if your component requires another component for it to work properly then you can specify it as a dependency. It ensures that the dependency components will be initialized before your the current component.
Multiple instance components
Usually, the components can only have one instance in a scene. For example, an entity can only have one geometry component. But some components can have multiple instances. For example, sound component.
1 2 3 |
a-entity sound="src: url(sound1.mp3)" sound__1="src: url(sound2.mp3)"></a-entity> |
The component definition of a multiple instancing component requires multiple: true
in the component definition.
1 2 3 4 5 6 |
AFRAME.registerComponent('multi-instance', { multiple: true, // ... }); |
System
The System provides a global scope, services, and management for the components.
A system can be accessed by the scene element and can access the components interface.
Registering and using a system
Registering a system is similar to registering a component. It uses registerComponent
instead of registerSystem
.
1 2 3 4 5 |
AFRAME.registerSystem('name-of-component', { // ... }); |
If the name of the system matches the name of the component, the component can access the system within the component as this.system
.
Why use a system to manage components
- Separation of logic and data: System helps you to separate logic and data if needed. With this pattern, components only worry about managing data through the lifecycle methods, while the system will handle the logic.
- Gather all components: One common pattern to manage multiple components with a system is by registering the component with a system. The system can then have references to all its components.
Using Entity Component System in aframe
This article was a basic introduction to Entity Component System (ECS) pattern used by aframe. Understanding this pattern will help you to build VR components and working VR apps with aframe.
If you have any questions, suggestions on how to improve the content, don’t forget to leave a comment below.