Skip to content

Commit fdb6b03

Browse files
committed
docs: add webgl animation keyframes
1 parent 2e99442 commit fdb6b03

14 files changed

+295
-19
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Component, CUSTOM_ELEMENTS_SCHEMA, inject } from '@angular/core';
2+
import { injectBeforeRender, injectNgtLoader, NgtArgs, NgtCanvas, NgtPush, NgtStore } from 'angular-three';
3+
import { map, tap } from 'rxjs';
4+
import * as THREE from 'three';
5+
import { DRACOLoader, GLTFLoader, RoomEnvironment } from 'three-stdlib';
6+
import Stats from 'three/examples/jsm/libs/stats.module.js';
7+
import { DemoOrbitControls } from '../ui-orbit-controls/orbit-controls.component';
8+
9+
@Component({
10+
standalone: true,
11+
template: `
12+
<ngt-color *args="['#bfe3dd']" attach="background" />
13+
<ngt-value [rawValue]="texture" attach="environment" />
14+
15+
<ngt-primitive *args="[model$ | ngtPush : null]" [position]="[1, 1, 0]" [scale]="0.01" />
16+
17+
<demo-orbit-controls [target]="[0, 0.5, 0]" [enablePan]="false" />
18+
`,
19+
imports: [NgtArgs, NgtPush, DemoOrbitControls],
20+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
21+
})
22+
export class Scene {
23+
private readonly gl = inject(NgtStore).get('gl');
24+
private readonly pmremGenerator = new THREE.PMREMGenerator(this.gl);
25+
26+
readonly texture = this.pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture;
27+
28+
private readonly stats = Stats();
29+
30+
private mixer?: THREE.AnimationMixer;
31+
readonly model$ = injectNgtLoader(
32+
() => GLTFLoader,
33+
'assets/LittlestTokyo.glb',
34+
(loader) => {
35+
const dracoLoader = new DRACOLoader();
36+
dracoLoader.setDecoderPath('assets/draco/');
37+
(loader as GLTFLoader).setDRACOLoader(dracoLoader);
38+
}
39+
).pipe(
40+
tap((model) => {
41+
const scene = model.scene;
42+
this.mixer = new THREE.AnimationMixer(scene);
43+
this.mixer.clipAction(model.animations[0]).play();
44+
}),
45+
map((model) => model.scene)
46+
);
47+
48+
constructor() {
49+
injectBeforeRender(({ delta }) => {
50+
this.mixer?.update(delta);
51+
this.stats.update();
52+
});
53+
this.gl.domElement.parentElement?.appendChild(this.stats.dom);
54+
}
55+
}
56+
57+
@Component({
58+
standalone: true,
59+
template: ` <ngt-canvas [sceneGraph]="SceneGraph" [camera]="{ fov: 40, far: 100, position: [5, 2, 8] }" /> `,
60+
imports: [NgtCanvas],
61+
})
62+
export default class DemoAnimationKeyframes {
63+
readonly SceneGraph = Scene;
64+
}

apps/demo/src/app/app.routes.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,13 @@ export const routes: Routes = [
3030
asset: 'assets/demo/vertex-colors-instances',
3131
},
3232
},
33+
{
34+
path: 'animation-keyframes',
35+
loadComponent: () => import('./animation-keyframes/animation-keyframes.component'),
36+
data: {
37+
description: 'Three.js WebGL animation with keyframes',
38+
link: '/animation-keyframes',
39+
asset: 'assets/demo/animation-keyframes',
40+
},
41+
},
3342
];

apps/demo/src/app/cubes/cubes.component.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, inject, Input, ViewChild } from '@angular/core';
22
import { injectBeforeRender, NgtArgs, NgtCanvas, NgtStore } from 'angular-three';
33
import * as THREE from 'three';
4+
import { DemoOrbitControls } from '../ui-orbit-controls/orbit-controls.component';
45

56
@Component({
67
selector: 'demo-cube',
@@ -53,13 +54,9 @@ export class Cube {
5354
<demo-cube [position]="[1.5, 0, 0]" />
5455
<demo-cube [position]="[-1.5, 0, 0]" />
5556
56-
<ngt-orbit-controls
57-
*args="[camera, glDom]"
58-
[enableDamping]="true"
59-
(beforeRender)="$any($event).object.update()"
60-
/>
57+
<demo-orbit-controls />
6158
`,
62-
imports: [NgtArgs, Cube],
59+
imports: [NgtArgs, Cube, DemoOrbitControls],
6360
schemas: [CUSTOM_ELEMENTS_SCHEMA],
6461
})
6562
export class Scene {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Component, CUSTOM_ELEMENTS_SCHEMA, inject, Input } from '@angular/core';
2+
import { NgtArgs, NgtStore } from 'angular-three';
3+
4+
@Component({
5+
selector: 'demo-orbit-controls',
6+
standalone: true,
7+
template: `
8+
<ngt-orbit-controls
9+
*args="[camera, glDom]"
10+
[enableDamping]="true"
11+
[enablePan]="enablePan"
12+
[autoRotate]="autoRotate"
13+
[target]="target"
14+
(beforeRender)="$any($event).object.update()"
15+
/>
16+
`,
17+
imports: [NgtArgs],
18+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
19+
})
20+
export class DemoOrbitControls {
21+
private readonly store = inject(NgtStore);
22+
readonly camera = this.store.get('camera');
23+
readonly glDom = this.store.get('gl', 'domElement');
24+
25+
@Input() enablePan = true;
26+
@Input() autoRotate = false;
27+
@Input() target = [0, 0, 0];
28+
}

apps/demo/src/app/vertex-colors-instance/vertex-colors-instance.component.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NgtArgs, NgtCanvas, NgtStore } from 'angular-three';
33
// @ts-expect-error no type def for nice-color-palettes
44
import niceColors from 'nice-color-palettes';
55
import * as THREE from 'three';
6+
import { DemoOrbitControls } from '../ui-orbit-controls/orbit-controls.component';
67
const niceColor = niceColors[Math.floor(Math.random() * niceColors.length)];
78

89
@Component({
@@ -53,14 +54,9 @@ export class ColorsInstances {
5354
<ngt-ambient-light />
5455
<ngt-directional-light [intensity]="0.55" [position]="150" />
5556
<demo-colors-instances />
56-
<ngt-orbit-controls
57-
*args="[camera, glDom]"
58-
[enableDamping]="true"
59-
[autoRotate]="true"
60-
(beforeRender)="$any($event).object.update()"
61-
/>
57+
<demo-orbit-controls [autoRotate]="true" />
6258
`,
63-
imports: [NgtArgs, ColorsInstances],
59+
imports: [NgtArgs, ColorsInstances, DemoOrbitControls],
6460
schemas: [CUSTOM_ELEMENTS_SCHEMA],
6561
})
6662
export class Scene {

apps/demo/src/app/view-cube/view-cube.component.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
prepare,
1313
} from 'angular-three';
1414
import * as THREE from 'three';
15+
import { DemoOrbitControls } from '../ui-orbit-controls/orbit-controls.component';
1516

1617
@Component({
1718
selector: 'view-cube',
@@ -80,14 +81,10 @@ export class ViewCube extends NgtRxStore {
8081
<ngt-torus-geometry *args="[1, 0.5, 32, 100]" />
8182
<ngt-mesh-normal-material />
8283
</ngt-mesh>
83-
<ngt-orbit-controls
84-
*args="[camera, glDom]"
85-
[enableDamping]="true"
86-
(beforeRender)="$any($event).object.update()"
87-
/>
84+
<demo-orbit-controls />
8885
<view-cube />
8986
`,
90-
imports: [ViewCube, NgtArgs],
87+
imports: [ViewCube, NgtArgs, DemoOrbitControls],
9188
schemas: [CUSTOM_ELEMENTS_SCHEMA],
9289
})
9390
export class Scene {
3.94 MB
Binary file not shown.
Loading
Binary file not shown.
Binary file not shown.

apps/demo/src/assets/draco/draco_decoder.js

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
190 KB
Binary file not shown.

apps/demo/src/assets/draco/draco_encoder.js

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)