Skip to content

Commit 53152a4

Browse files
committed
stats gl
1 parent a1ea9fe commit 53152a4

File tree

6 files changed

+23413
-30978
lines changed

6 files changed

+23413
-30978
lines changed

libs/soba/misc/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './fbo/fbo';
77
export * from './html/html';
88
export * from './sampler/sampler';
99
export * from './shadow/shadow';
10+
export * from './stats-gl/stats-gl';
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { DOCUMENT } from '@angular/common';
2+
import { Directive, Input, computed, effect, inject } from '@angular/core';
3+
import { NgtRef, addAfterEffect, addEffect, injectNgtStore, is, signalStore } from 'angular-three';
4+
import Stats from 'stats-gl';
5+
6+
export type NgtsStatsGLState = {
7+
parent?: NgtRef<HTMLElement>;
8+
containerClass?: string;
9+
config?: ConstructorParameters<typeof Stats>[0];
10+
};
11+
12+
@Directive({ selector: 'ngts-stats-gl', standalone: true })
13+
export class NgtsStatsGL {
14+
private inputs = signalStore<NgtsStatsGLState>();
15+
16+
@Input({ alias: 'parent' }) set _parent(parent: NgtRef<HTMLElement>) {
17+
this.inputs.set({ parent });
18+
}
19+
20+
@Input({ alias: 'containerClass' }) set _containerClass(containerClass: string) {
21+
this.inputs.set({ containerClass });
22+
}
23+
24+
@Input({ alias: 'config' }) set _config(config: NgtsStatsGLState['config']) {
25+
this.inputs.set({ config });
26+
}
27+
28+
private document = inject(DOCUMENT);
29+
private store = injectNgtStore();
30+
private gl = this.store.select('gl');
31+
32+
private config = this.inputs.select('config');
33+
private parent = this.inputs.select('parent');
34+
private containerClass = this.inputs.select('containerClass');
35+
36+
private stats = computed(() => {
37+
const [config, gl] = [this.config() || {}, this.gl()];
38+
const stats = new Stats(config);
39+
stats.init(gl.domElement);
40+
return stats;
41+
});
42+
43+
constructor() {
44+
effect((onCleanup) => {
45+
const [parent, stats, containerClass] = [this.parent(), this.stats(), this.containerClass()];
46+
const node = parent ? (is.ref(parent) ? parent.nativeElement : parent) : this.document.body;
47+
node.appendChild(stats.container);
48+
if (containerClass) stats.container.classList.add(...containerClass.split(' ').filter(Boolean));
49+
50+
const begin = addEffect(() => stats.begin());
51+
const end = addAfterEffect(() => stats.end());
52+
onCleanup(() => {
53+
node.removeChild(stats.container);
54+
begin();
55+
end();
56+
});
57+
});
58+
}
59+
}

libs/soba/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"angular-three": "^2.0.0",
3232
"three": ">=0.148.0",
3333
"stats.js": "^0.17.0",
34+
"stats-gl": "^1.0.0",
3435
"three-mesh-bvh": "^0.5.0 || ^0.6.0",
3536
"three-stdlib": "^2.0.0",
3637
"troika-three-text": "^0.47.0"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { CUSTOM_ELEMENTS_SCHEMA, Component } from '@angular/core';
2+
import { NgtsStatsGL } from 'angular-three-soba/misc';
3+
import { makeDecorators, makeStoryFunction } from '../setup-canvas';
4+
5+
@Component({
6+
standalone: true,
7+
template: `
8+
<ngt-axes-helper />
9+
<ngts-stats-gl />
10+
`,
11+
imports: [NgtsStatsGL],
12+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
13+
})
14+
class DefaultStatsGLStory {}
15+
16+
export default {
17+
title: 'Misc/StatsGL',
18+
decorators: makeDecorators(),
19+
};
20+
21+
export const Default = makeStoryFunction(DefaultStatsGLStory);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"react": "^18.2.0",
104104
"react-dom": "^18.2.0",
105105
"rxjs": "~7.8.1",
106+
"stats-gl": "^1.0.4",
106107
"three-mesh-bvh": "^0.6.3",
107108
"three-stdlib": "^2.24.1",
108109
"troika-three-text": "^0.47.2",

0 commit comments

Comments
 (0)