Skip to content

Commit 3f178f0

Browse files
Chau TranChau Tran
Chau Tran
authored and
Chau Tran
committed
fix(soba): adjust environment to use ngtsEnvironmentInput as a provider
1 parent f775b53 commit 3f178f0

File tree

5 files changed

+82
-104
lines changed

5 files changed

+82
-104
lines changed

libs/soba/staging/src/environment/environment-cube.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1-
import { Directive, computed, effect, inject } from '@angular/core';
2-
import { NgtStore } from 'angular-three';
1+
import { Directive, Input, computed, effect, inject } from '@angular/core';
2+
import { NgtStore, requestAnimationInInjectionContext } from 'angular-three';
33
import { NgtsEnvironmentInput } from './environment-input';
44
import { injectNgtsEnvironment, setEnvProps } from './utils';
55

66
@Directive({
77
selector: 'ngts-environment-cube',
88
standalone: true,
99
})
10-
export class NgtsEnvironmentCube extends NgtsEnvironmentInput {
10+
export class NgtsEnvironmentCube {
11+
protected readonly environmentInput = inject(NgtsEnvironmentInput);
1112
readonly #store = inject(NgtStore);
12-
readonly textureRef = injectNgtsEnvironment(this.environmentParams);
13+
readonly textureRef = injectNgtsEnvironment(this.environmentInput.environmentParams);
14+
15+
@Input() set background(background: boolean) {
16+
this.environmentInput.set({ background });
17+
}
1318

1419
constructor() {
15-
super();
16-
this.set({ background: false });
17-
this.#setEnvProps();
20+
this.environmentInput.patch({ background: false });
21+
requestAnimationInInjectionContext(() => {
22+
this.#setEnvProps();
23+
});
1824
}
1925

2026
#setEnvProps() {
2127
const scene = this.#store.select('scene');
2228
const trigger = computed(() => ({
2329
defaultScene: scene(),
24-
scene: this.environmentScene(),
25-
background: this.environmentBackground(),
26-
blur: this.environmentBlur(),
30+
scene: this.environmentInput.environmentScene(),
31+
background: this.environmentInput.environmentBackground(),
32+
blur: this.environmentInput.environmentBlur(),
2733
texture: this.textureRef.nativeElement,
2834
}));
2935

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, computed, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
1+
import { Component, computed, CUSTOM_ELEMENTS_SCHEMA, inject } from '@angular/core';
22
import { extend, NgtArgs } from 'angular-three';
33
import { GroundProjectedEnv } from 'three-stdlib';
44
import { NgtsEnvironmentInput } from './environment-input';
@@ -11,27 +11,23 @@ extend({ GroundProjectedEnv });
1111
selector: 'ngts-environment-ground',
1212
standalone: true,
1313
template: `
14-
<ngts-environment-map
15-
[background]="environmentBackground()"
16-
[blur]="environmentBlur()"
17-
[scene]="environmentScene()"
18-
[map]="texture()"
19-
/>
14+
<ngts-environment-map [map]="texture()" [background]="environmentInput.environmentBackground()" />
2015
<ngt-ground-projected-env *args="groundArgs()" [scale]="scale()" [height]="height()" [radius]="radius()" />
2116
`,
2217
imports: [NgtsEnvironmentMap, NgtArgs],
2318
schemas: [CUSTOM_ELEMENTS_SCHEMA],
2419
})
25-
export class NgtsEnvironmentGround extends NgtsEnvironmentInput {
26-
readonly #defaultTexture = injectNgtsEnvironment(this.environmentParams);
20+
export class NgtsEnvironmentGround {
21+
protected readonly environmentInput = inject(NgtsEnvironmentInput);
22+
readonly #defaultTexture = injectNgtsEnvironment(this.environmentInput.environmentParams);
2723

2824
readonly texture = computed(() => {
2925
const defaultTexture = this.#defaultTexture.nativeElement;
30-
return this.environmentMap() || defaultTexture;
26+
return this.environmentInput.environmentMap() || defaultTexture;
3127
});
3228

3329
readonly groundArgs = computed(() => (this.texture() ? [this.texture()] : []));
34-
readonly height = computed(() => (this.environmentGround() as any)?.height);
35-
readonly radius = computed(() => (this.environmentGround() as any)?.radius);
36-
readonly scale = computed(() => (this.environmentGround() as any)?.scale ?? 1000);
30+
readonly height = computed(() => (this.environmentInput.environmentGround() as any)?.height);
31+
readonly radius = computed(() => (this.environmentInput.environmentGround() as any)?.radius);
32+
readonly scale = computed(() => (this.environmentInput.environmentGround() as any)?.scale ?? 1000);
3733
}

libs/soba/staging/src/environment/environment-map.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
import { Directive, computed, effect, inject } from '@angular/core';
2-
import { NgtStore } from 'angular-three';
1+
import { Directive, Input, computed, effect, inject } from '@angular/core';
2+
import { NgtStore, requestAnimationInInjectionContext } from 'angular-three';
33
import { NgtsEnvironmentInput } from './environment-input';
44
import { setEnvProps } from './utils';
55

66
@Directive({
77
selector: 'ngts-environment-map',
88
standalone: true,
99
})
10-
export class NgtsEnvironmentMap extends NgtsEnvironmentInput {
10+
export class NgtsEnvironmentMap {
11+
protected readonly environmentInput = inject(NgtsEnvironmentInput);
1112
readonly #store = inject(NgtStore);
1213

14+
@Input() set map(map: THREE.Texture) {
15+
this.environmentInput.set({ map });
16+
}
17+
18+
@Input() set background(background: boolean) {
19+
this.environmentInput.set({ background });
20+
}
21+
1322
constructor() {
14-
super();
15-
this.set({ background: false });
16-
this.#setEnvProps();
23+
this.environmentInput.patch({ background: false });
24+
requestAnimationInInjectionContext(() => {
25+
this.#setEnvProps();
26+
});
1727
}
1828

1929
#setEnvProps() {
2030
const scene = this.#store.select('scene');
2131
const trigger = computed(() => ({
2232
defaultScene: scene(),
23-
scene: this.environmentScene(),
24-
background: this.environmentBackground(),
25-
blur: this.environmentBlur(),
26-
texture: this.environmentMap(),
33+
scene: this.environmentInput.environmentScene(),
34+
background: this.environmentInput.environmentBackground(),
35+
blur: this.environmentInput.environmentBlur(),
36+
texture: this.environmentInput.environmentMap(),
2737
}));
2838

2939
effect((onCleanup) => {

libs/soba/staging/src/environment/environment-portal.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
NgtRenderState,
1111
NgtStore,
1212
prepare,
13+
requestAnimationInInjectionContext,
1314
} from 'angular-three';
1415
import * as THREE from 'three';
1516
import { CubeCamera } from 'three';
@@ -28,45 +29,53 @@ extend({ CubeCamera });
2829
<ng-template ngtPortalContent>
2930
<ng-content />
3031
<ngt-cube-camera *args="cameraArgs()" [ref]="cubeCameraRef" />
31-
<ng-container *ngIf="environmentFiles() || environmentPreset(); else environmentMap">
32-
<ngts-environment-cube
33-
[background]="true"
34-
[files]="environmentFiles()"
35-
[preset]="environmentPreset()"
36-
[path]="environmentPath()"
37-
[extensions]="environmentExtensions()"
38-
/>
32+
<ng-container
33+
*ngIf="
34+
environmentInput.environmentFiles() || environmentInput.environmentPreset();
35+
else environmentMap
36+
"
37+
>
38+
<ngts-environment-cube [background]="true" />
3939
</ng-container>
4040
<ng-template #environmentMap>
41-
<ngts-environment-map
42-
[background]="true"
43-
[map]="environmentMap()"
44-
[extensions]="environmentExtension()"
45-
/>
41+
<ngts-environment-map [background]="true" [map]="environmentInput.environmentMap()" />
4642
</ng-template>
4743
</ng-template>
4844
</ngt-portal>
4945
`,
5046
imports: [NgtPortal, NgtPortalContent, NgtsEnvironmentMap, NgtsEnvironmentCube, NgIf, NgtArgs],
5147
schemas: [CUSTOM_ELEMENTS_SCHEMA],
5248
})
53-
export class NgtsEnvironmentPortal extends NgtsEnvironmentInput {
49+
export class NgtsEnvironmentPortal {
50+
protected readonly environmentInput = inject(NgtsEnvironmentInput);
5451
readonly #store = inject(NgtStore);
5552

5653
readonly virtualSceneRef = injectNgtRef<THREE.Scene>(prepare(new THREE.Scene()));
5754
readonly cubeCameraRef = injectNgtRef<THREE.CubeCamera>();
5855

5956
readonly #fbo = computed(() => {
60-
const fbo = new THREE.WebGLCubeRenderTarget(this.environmentResolution());
57+
const fbo = new THREE.WebGLCubeRenderTarget(this.environmentInput.environmentResolution());
6158
fbo.texture.type = THREE.HalfFloatType;
6259
return fbo;
6360
});
64-
readonly cameraArgs = computed(() => [this.environmentNear(), this.environmentFar(), this.#fbo()]);
61+
readonly cameraArgs = computed(() => [
62+
this.environmentInput.environmentNear(),
63+
this.environmentInput.environmentFar(),
64+
this.#fbo(),
65+
]);
6566

6667
constructor() {
67-
super();
68-
this.set({ near: 1, far: 1000, resolution: 256, frames: 1, background: false, preset: undefined });
69-
this.#setEnvProps();
68+
this.environmentInput.patch({
69+
near: 1,
70+
far: 1000,
71+
resolution: 256,
72+
frames: 1,
73+
background: false,
74+
preset: undefined,
75+
});
76+
requestAnimationInInjectionContext(() => {
77+
this.#setEnvProps();
78+
});
7079
injectBeforeRender(this.#onBeforeRender.bind(this, 1));
7180
}
7281

@@ -78,10 +87,10 @@ export class NgtsEnvironmentPortal extends NgtsEnvironmentInput {
7887
gl: gl(),
7988
defaultScene: scene(),
8089
fbo: this.#fbo(),
81-
scene: this.environmentScene(),
82-
background: this.environmentBackground(),
83-
frames: this.environmentFrames(),
84-
blur: this.environmentBlur(),
90+
scene: this.environmentInput.environmentScene(),
91+
background: this.environmentInput.environmentBackground(),
92+
frames: this.environmentInput.environmentFrames(),
93+
blur: this.environmentInput.environmentBlur(),
8594
virtualScene: this.virtualSceneRef.nativeElement,
8695
cubeCamera: this.cubeCameraRef.nativeElement,
8796
}));
@@ -95,7 +104,7 @@ export class NgtsEnvironmentPortal extends NgtsEnvironmentInput {
95104
}
96105

97106
#onBeforeRender(count: number, { gl }: NgtRenderState) {
98-
const { frames } = this.get();
107+
const { frames } = this.environmentInput.get();
99108
if (frames === Infinity || count < frames) {
100109
const camera = this.cubeCameraRef.nativeElement;
101110
const virtualScene = this.virtualSceneRef.nativeElement;

libs/soba/staging/src/environment/environment.ts

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,63 +13,19 @@ export class NgtsEnvironmentContent {}
1313
selector: 'ngts-environment',
1414
standalone: true,
1515
template: `
16-
<ngts-environment-ground
17-
*ngIf="environmentGround(); else noGround"
18-
[ground]="environmentGround()"
19-
[map]="environmentMap()"
20-
[scene]="environmentScene()"
21-
[blur]="environmentBlur()"
22-
[background]="environmentBackground()"
23-
[preset]="environmentPreset()"
24-
[frames]="environmentFrames()"
25-
[far]="environmentFar()"
26-
[near]="environmentNear()"
27-
[resolution]="environmentResolution()"
28-
[files]="environmentFiles()"
29-
[path]="environmentPath()"
30-
[extensions]="environmentExtensions()"
31-
/>
16+
<ngts-environment-ground *ngIf="environmentGround(); else noGround" />
3217
<ng-template #noGround>
3318
<ngts-environment-map
3419
*ngIf="environmentMap(); else noMap"
3520
[map]="environmentMap()"
36-
[scene]="environmentScene()"
37-
[blur]="environmentBlur()"
3821
[background]="environmentBackground()"
3922
/>
4023
<ng-template #noMap>
41-
<ngts-environment-portal
42-
*ngIf="content; else noPortal"
43-
[frames]="environmentFrames()"
44-
[far]="environmentFar()"
45-
[near]="environmentNear()"
46-
[resolution]="environmentResolution()"
47-
[map]="environmentMap()"
48-
[background]="environmentBackground()"
49-
[blur]="environmentBlur()"
50-
[scene]="environmentScene()"
51-
[files]="environmentFiles()"
52-
[path]="environmentPath()"
53-
[preset]="environmentPreset()"
54-
[extensions]="environmentExtensions()"
55-
>
24+
<ngts-environment-portal *ngIf="content; else noPortal">
5625
<ng-container *ngTemplateOutlet="content" />
5726
</ngts-environment-portal>
5827
<ng-template #noPortal>
59-
<ngts-environment-cube
60-
[frames]="environmentFrames()"
61-
[far]="environmentFar()"
62-
[near]="environmentNear()"
63-
[resolution]="environmentResolution()"
64-
[map]="environmentMap()"
65-
[background]="environmentBackground()"
66-
[blur]="environmentBlur()"
67-
[scene]="environmentScene()"
68-
[files]="environmentFiles()"
69-
[path]="environmentPath()"
70-
[preset]="environmentPreset()"
71-
[extensions]="environmentExtensions()"
72-
/>
28+
<ngts-environment-cube [background]="environmentBackground()" />
7329
</ng-template>
7430
</ng-template>
7531
</ng-template>
@@ -82,6 +38,7 @@ export class NgtsEnvironmentContent {}
8238
NgIf,
8339
NgTemplateOutlet,
8440
],
41+
providers: [{ provide: NgtsEnvironmentInput, useExisting: NgtsEnvironment }],
8542
schemas: [CUSTOM_ELEMENTS_SCHEMA],
8643
})
8744
export class NgtsEnvironment extends NgtsEnvironmentInput {

0 commit comments

Comments
 (0)