Skip to content

Our First 3D Scene

Before diving into the API details of Angular Three, let’s create a simple scene together to get a feel for what it’s like to use Angular Three.

Create the SceneGraph component

This component will be the root of our scene graph.

src/app/scene-graph.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy } from '@angular/core';
2
3
@Component({
4
standalone: true,
5
template: `
6
<!-- we'll fill this in later -->
7
`,
8
schemas: [CUSTOM_ELEMENTS_SCHEMA],
9
changeDetection: ChangeDetectionStrategy.OnPush,
10
})
11
export class SceneGraph {}
  • CUSTOM_ELEMENTS_SCHEMA is required to use Angular Three components.
  • selector is left empty because we’re not rendering SceneGraph as a regular Angular component.

Set up the Canvas

The SceneGraph component will be rendered by the NgtCanvas component.

src/app/app.component.ts
1
import { Component } from '@angular/core';
2
import { NgtCanvas } from 'angular-three';
3
import { SceneGraph } from './scene-graph.component';
4
5
@Component({
6
selector: 'app-root',
7
standalone: true,
8
template: `
9
<ngt-canvas [sceneGraph]="sceneGraph" />
10
`,
11
imports: [NgtCanvas],
12
})
13
export class AppComponent {
14
protected sceneGraph = SceneGraph;
15
}

NgtCanvas uses the sceneGraph input to render the SceneGraph component with the Custom Renderer as well as sets up the following THREE.js building blocks:

  • A WebGLRenderer with anti-aliasing enabled and transparent background.
  • A default PerspectiveCamera with a default position of [x:0, y:0, z:5].
  • A default Scene
  • A render loop that renders the scene on every frame
  • A window:resize event listener that automatically updates the Renderer and Camera when the viewport is resized

Set the dimensions of the canvas

We’ll set up some basic styles in styles.css

src/styles.css
1
html,
2
body {
3
height: 100%;
4
width: 100%;
5
margin: 0;
6
}

Next, let’s set some styles for :host in src/app/app.component.ts

1
import { Component } from '@angular/core';
2
import { NgtCanvas } from 'angular-three';
3
import { SceneGraph } from './scene-graph.component';
4
5
@Component({
6
selector: 'app-root',
7
standalone: true,
8
template: `
9
<ngt-canvas [sceneGraph]="sceneGraph" />
10
`,
11
imports: [NgtCanvas],
12
styles: [`
13
:host {
14
display: block;
15
height: 100dvh;
16
}
17
`],
18
})
19
export class AppComponent {
20
protected sceneGraph = SceneGraph;
21
}

Extending Angular Three Catalogue

As a custom renderer, Angular Three maintains a single catalogue of entities to render. By default, the catalogue is empty. We can extend the catalogue by calling the extend function and pass in a Record of entities. Angular Three then maps the catalogue to Custom Elements tags with the following naming convention:

1
<ngt-{entityName-in-kebab-case} />

For example:

1
import { extend } from 'angular-three';
2
import { Mesh, BoxGeometry } from 'three';
3
4
extend({
5
Mesh, // makes ngt-mesh available
6
BoxGeometry // makes ngt-box-geometry available,
7
MyMesh: Mesh, // makes ngt-my-mesh available
8
});

For the purpose of this guide, we’ll extend THREE.js namespace so we do not have to go back and forth to extend more entities as we go.

src/app/app.component.ts
1
import { NgtCanvas } from 'angular-three';
2
import { NgtCanvas, extend } from 'angular-three';
3
import * as THREE from 'three';
4
5
extend(THREE);
6
7
/* the rest of the code remains the same */

Render a THREE.js Entity

Now that we have extended the THREE.js namespace, we can render a THREE.js entity. Let’s render a cube with a Mesh and BoxGeometry from THREE.js.

src/app/scene-graph.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy } from '@angular/core';
2
3
@Component({
4
standalone: true,
5
template: `
6
<ngt-mesh>
7
<ngt-box-geometry />
8
</ngt-mesh>
9
`,
10
schemas: [CUSTOM_ELEMENTS_SCHEMA],
11
changeDetection: ChangeDetectionStrategy.OnPush,
12
})
13
export class SceneGraph {}

And here’s the result:

Animation

The best way to animate a THREE.js entity is to participate in the animation loop with injectBeforeRender. Let’s animate the cube by rotating it on the X and Y axes.

src/app/scene-graph.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy } from '@angular/core';
2
3
import { injectBeforeRender } from 'angular-three';
4
import { Mesh } from 'three';
5
6
@Component({
7
standalone: true,
8
template: `
9
10
<ngt-mesh #mesh>
11
<ngt-box-geometry />
12
</ngt-mesh>
13
`,
14
schemas: [CUSTOM_ELEMENTS_SCHEMA],
15
changeDetection: ChangeDetectionStrategy.OnPush,
16
})
17
export class SceneGraph {
18
19
meshRef = viewChild.required<ElementRef<Mesh>>('mesh');
20
21
constructor() {
22
23
injectBeforeRender(() => {
24
const mesh = this.meshRef().nativeElement;
25
mesh.rotation.x += 0.01;
26
mesh.rotation.y += 0.01;
27
});
28
}
29
}

Make a Component

Using Angular means we can make components out of our template. Let’s do that for our cube

1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, viewChild } from '@angular/core';
2
import { injectBeforeRender } from 'angular-three';
3
import { Mesh } from 'three';
4
5
@Component({
6
selector: 'app-cube',
7
standalone: true,
8
template: `
9
<ngt-mesh #mesh>
10
<ngt-box-geometry />
11
</ngt-mesh>
12
`,
13
schemas: [CUSTOM_ELEMENTS_SCHEMA]
14
})
15
export class Cube {
16
meshRef = viewChild.required<ElementRef<Mesh>>('mesh');
17
18
constructor() {
19
injectBeforeRender(({ delta }) => {
20
const mesh = this.meshRef().nativeElement;
21
mesh.rotation.x += delta;
22
mesh.rotation.y += delta;
23
});
24
}
25
}

Everything is the same as before, except we now have a Cube component that can have its own state and logic.

We will add 2 states hovered and clicked to the cube component:

  • When the cube is hovered, we’ll change its color
  • When the cube is clicked, we’ll change its scale
src/app/cube.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, viewChild } from '@angular/core';
2
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, viewChild, signal } from '@angular/core';
3
import { injectBeforeRender } from 'angular-three';
4
import { Mesh } from 'three';
5
6
@Component({
7
selector: 'app-cube',
8
standalone: true,
9
template: `
10
<ngt-mesh
11
#mesh
12
[scale]="clicked() ? 1.5 : 1"
13
(pointerover)="hovered.set(true)"
14
(pointerout)="hovered.set(false)"
15
(click)="clicked.set(!clicked())"
16
>
17
<ngt-box-geometry />
18
<ngt-mesh-basic-material [color]="hovered() ? 'darkred' : 'mediumpurple'" />
19
</ngt-mesh>
20
`,
21
schemas: [CUSTOM_ELEMENTS_SCHEMA]
22
})
23
export class Cube {
24
meshRef = viewChild.required<ElementRef<Mesh>>('mesh');
25
26
hovered = signal(false);
27
clicked = signal(false);
28
29
constructor() {
30
injectBeforeRender(() => {
31
const mesh = this.meshRef().nativeElement;
32
mesh.rotation.x += 0.01;
33
mesh.rotation.y += 0.01;
34
});
35
}
36
}

Our cube is now interactive!

Render another Cube

Just like any other Angular component, we can render another cube by adding another <app-cube /> tag to the template. However, we need to render the cube in different positions so we can see them both on the scene.

Let’s do that by adding a position input to the Cube component

src/app/cube.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, viewChild, signal } from '@angular/core';
2
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, viewChild, signal, input } from '@angular/core';
3
4
import { injectBeforeRender } from 'angular-three';
5
import { injectBeforeRender, NgtVector3 } from 'angular-three';
6
import { Mesh } from 'three';
7
8
@Component({
9
selector: 'app-cube',
10
standalone: true,
11
template: `
12
<ngt-mesh
13
#mesh
14
[position]="position()"
15
[scale]="clicked() ? 1.5 : 1"
16
(pointerover)="hovered.set(true)"
17
(pointerout)="hovered.set(false)"
18
(click)="clicked.set(!clicked())"
19
>
20
<ngt-box-geometry />
21
<ngt-mesh-basic-material [color]="hovered() ? 'darkred' : 'mediumpurple'" />
22
</ngt-mesh>
23
`,
24
schemas: [CUSTOM_ELEMENTS_SCHEMA]
25
})
26
export class Cube {
27
position = input<NgtVector3>([0, 0, 0]);
28
29
meshRef = viewChild.required<ElementRef<Mesh>>('mesh');
30
31
hovered = signal(false);
32
clicked = signal(false);
33
34
constructor() {
35
injectBeforeRender(() => {
36
const mesh = this.meshRef().nativeElement;
37
mesh.rotation.x += 0.01;
38
mesh.rotation.y += 0.01;
39
});
40
}
41
}

position input is a NgtVector3 which is an expanded version of THREE.Vector3. It can accept:

  • A THREE.Vector3 instance
  • A tuple of [x, y, z]
  • A scalar value that will be used for all axes
src/app/scene-graph.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy } from '@angular/core';
2
import { Cube } from './cube.component';
3
4
@Component({
5
standalone: true,
6
template: `
7
<app-cube />
8
<app-cube [position]="[1.5, 0, 0]" />
9
<app-cube [position]="[-1.5, 0, 0]" />
10
`,
11
imports: [Cube],
12
schemas: [CUSTOM_ELEMENTS_SCHEMA],
13
changeDetection: ChangeDetectionStrategy.OnPush,
14
})
15
export class SceneGraph {}

and now we have 2 cubes that have their own states, and react to events independently.

Lighting

Let’s add some lights to our scene to make the cubes look more dynamic as they look bland at the moment.

src/app/scene-graph.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy } from '@angular/core';
2
import { Cube } from './cube.component';
3
4
@Component({
5
standalone: true,
6
template: `
7
<ngt-ambient-light [intensity]="0.5" />
8
<ngt-spot-light [position]="10" [intensity]="0.5 * Math.PI" [angle]="0.15" [penumbra]="1" [decay]="0" />
9
<ngt-point-light [position]="-10" [intensity]="0.5 * Math.PI" [decay]="0" />
10
11
<app-cube [position]="[1.5, 0, 0]" />
12
<app-cube [position]="[-1.5, 0, 0]" />
13
`,
14
imports: [Cube],
15
schemas: [CUSTOM_ELEMENTS_SCHEMA],
16
changeDetection: ChangeDetectionStrategy.OnPush,
17
})
18
export class SceneGraph {
19
protected readonly Math = Math;
20
}

Next, we will want to change the Material of the cube to MeshStandardMaterial so that it can be lit by the lights.

src/app/cube.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, viewChild, signal, input } from '@angular/core';
2
import { injectBeforeRender, NgtVector3 } from 'angular-three';
3
import { Mesh } from 'three';
4
5
@Component({
6
selector: 'app-cube',
7
standalone: true,
8
template: `
9
<ngt-mesh
10
#mesh
11
[position]="position()"
12
[scale]="clicked() ? 1.5 : 1"
13
(pointerover)="hovered.set(true)"
14
(pointerout)="hovered.set(false)"
15
(click)="clicked.set(!clicked())"
16
>
17
<ngt-box-geometry />
18
<ngt-mesh-basic-material [color]="hovered() ? 'darkred' : 'mediumpurple'" />
19
<ngt-mesh-standard-material [color]="hovered() ? 'darkred' : 'mediumpurple'" />
20
</ngt-mesh>
21
`,
22
schemas: [CUSTOM_ELEMENTS_SCHEMA],
23
})
24
export class Cube {
25
position = input<NgtVector3>([0, 0, 0]);
26
27
meshRef = viewChild.required<ElementRef<Mesh>>('mesh');
28
29
hovered = signal(false);
30
clicked = signal(false);
31
32
constructor() {
33
injectBeforeRender(() => {
34
const mesh = this.meshRef().nativeElement;
35
mesh.rotation.x += 0.01;
36
mesh.rotation.y += 0.01;
37
});
38
}
39
}

Our cubes look better now, with dimensionality, showing that they are 3D objects.

Bonus: Take control of the camera

Who hasn’t tried to grab the scene and move it around? Let’s take control of the camera and make it move around with OrbitControls.

Terminal window
1
npm install three-stdlib
2
# yarn add three-stdlib
3
# pnpm add three-stdlib

three-stdlib provides a better API to work with THREE.js extra modules like OrbitControls.

src/app/scene-graph.component.ts
1
import { Component, CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy } from '@angular/core';
2
import { injectStore, extend, NgtArgs } from 'angular-three';
3
import { OrbitControls } from 'three-stdlib';
4
import { Cube } from './cube.component';
5
6
extend({ OrbitControls }); // makes ngt-orbit-controls available
7
8
@Component({
9
standalone: true,
10
template: `
11
<ngt-ambient-light [intensity]="0.5" />
12
<ngt-spot-light [position]="10" [intensity]="0.5 * Math.PI" [angle]="0.15" [penumbra]="1" [decay]="0" />
13
<ngt-point-light [position]="-10" [intensity]="0.5 * Math.PI" [decay]="0" />
14
15
<app-cube [position]="[1.5, 0, 0]" />
16
<app-cube [position]="[-1.5, 0, 0]" />
17
18
<ngt-orbit-controls *args="[camera(), glDomElement()]" />
19
<ngt-grid-helper />
20
`,
21
imports: [Cube],
22
imports: [Cube, NgtArgs],
23
schemas: [CUSTOM_ELEMENTS_SCHEMA],
24
changeDetection: ChangeDetectionStrategy.OnPush,
25
})
26
export class SceneGraph {
27
protected readonly Math = Math;
28
29
private store = injectStore();
30
protected camera = this.store.select('camera');
31
protected glDomElement = this.store.select('gl', 'domElement');
32
}

If we were to use OrbitControls in a vanilla THREE.js application, we would need to instantiate it with the camera and WebGLRenderer#domElement.

With Angular Three, we use NgtArgs structural directive to pass Constructor Arguments to the underlying element.

To access the camera and glDomElement, we use injectStore to access the state of the canvas.

And that concludes our guide. We have learned how to create a basic scene, add some lights, and make our cubes interactive. We also learned how to use NgtArgs to pass arguments to the underlying THREE.js elements. Finally, we learned how to use injectStore to access the state of the canvas.

What’s next?

Now that we have a basic understanding of how to create a scene, we can start building more complex scenes.

  • Try different geometries and materials
  • Try different lights
  • Immerse yourself in the world of THREE.js