Skip to content

Commit e5c67ea

Browse files
committed
clean up
1 parent 03e21af commit e5c67ea

File tree

14 files changed

+151
-235
lines changed

14 files changed

+151
-235
lines changed

libs/core/src/lib/store.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import { DOCUMENT } from '@angular/common';
2-
import {
3-
ElementRef,
4-
InjectionToken,
5-
Injector,
6-
Optional,
7-
SkipSelf,
8-
computed,
9-
effect,
10-
runInInjectionContext,
11-
} from '@angular/core';
2+
import { ElementRef, InjectionToken, Injector, Optional, SkipSelf, effect, runInInjectionContext } from '@angular/core';
123
import { Subject, type Observable } from 'rxjs';
134
import * as THREE from 'three';
145
import type { NgtCamera, NgtDomEvent, NgtEventManager, NgtPointerCaptureTarget, NgtThreeEvent } from './events';
@@ -344,19 +335,12 @@ function storeFactory(loop: NgtLoop, document: Document, injector: Injector, par
344335
let oldDpr = state.viewport.dpr;
345336
let oldCamera = state.camera;
346337

347-
const camera = store.select('camera');
348-
const size = store.select('size');
349-
const viewport = store.select('viewport');
350-
351-
const triggers = computed(() => ({
352-
camera: camera(),
353-
size: size(),
354-
viewport: viewport(),
355-
gl: store.get('gl'),
356-
}));
338+
const _camera = store.select('camera');
339+
const _size = store.select('size');
340+
const _viewport = store.select('viewport');
357341

358342
effect(() => {
359-
const { camera, size, viewport, gl } = triggers();
343+
const [camera, size, viewport, gl] = [_camera(), _size(), _viewport(), store.get('gl')];
360344

361345
// Resize camera and renderer on changes to size and pixelratio
362346
if (size !== oldSize || viewport.dpr !== oldDpr) {

libs/soba/abstractions/src/billboard/billboard.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,22 @@ declare global {
3131
schemas: [CUSTOM_ELEMENTS_SCHEMA],
3232
})
3333
export class NgtsBillboard {
34-
private inputs = signalStore<NgtsBillboardState>({
35-
follow: true,
36-
lockX: false,
37-
lockY: false,
38-
lockZ: false,
39-
});
34+
private inputs = signalStore<NgtsBillboardState>({ follow: true, lockX: false, lockY: false, lockZ: false });
4035

4136
@Input() billboardRef = injectNgtRef<Group>();
4237

4338
@Input({ alias: 'follow' }) set _follow(follow: boolean) {
4439
this.inputs.set({ follow });
4540
}
41+
4642
@Input({ alias: 'lockX' }) set _lockX(lockX: boolean) {
4743
this.inputs.set({ lockX });
4844
}
45+
4946
@Input({ alias: 'lockY' }) set _lockY(lockY: boolean) {
5047
this.inputs.set({ lockY });
5148
}
49+
5250
@Input({ alias: 'lockZ' }) set _lockZ(lockZ: boolean) {
5351
this.inputs.set({ lockZ });
5452
}

libs/soba/abstractions/src/text/text.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
EventEmitter,
66
Input,
77
Output,
8-
computed,
98
effect,
109
inject,
1110
} from '@angular/core';
@@ -235,21 +234,19 @@ export class NgtsText {
235234
}
236235

237236
private preloadFont() {
238-
const font = this.inputs.select('font');
239-
const characters = this.inputs.select('characters');
240-
const trigger = computed(() => ({ font: font(), characters: characters() }));
237+
const _font = this.inputs.select('font');
238+
const _characters = this.inputs.select('characters');
241239

242240
effect(() => {
243-
const { font, characters } = trigger();
241+
const [font, characters] = [_font(), _characters()];
244242
const invalidate = this.store.get('invalidate');
245243
preloadFont({ font, characters }, () => invalidate());
246244
});
247245
}
248246

249247
private syncText() {
250248
effect(() => {
251-
this.inputs.state();
252-
const invalidate = this.store.get('invalidate');
249+
const [invalidate] = [this.store.get('invalidate'), this.inputs.state()];
253250
this.troikaText.sync(() => {
254251
invalidate();
255252
if (this.sync.observed) {

libs/soba/cameras/src/camera/camera.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ export abstract class NgtsCamera<TCamera extends NgtCamera> {
4242
@Input() cameraRef = injectNgtRef<TCamera>();
4343

4444
protected store = injectNgtStore();
45-
private cameraResolution = this.inputs.select('resolution');
4645
protected fboRef = injectNgtsFBO(() => ({ width: this.cameraResolution() }));
4746

47+
private cameraResolution = this.inputs.select('resolution');
48+
4849
constructor() {
4950
this.setDefaultCamera();
5051
this.updateProjectionMatrix();

libs/soba/cameras/src/cube-camera/cube-camera.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,53 +57,47 @@ export function injectNgtsCubeCamera(
5757
) {
5858
injector = assertInjectionContext(injectNgtsCubeCamera, injector);
5959
return runInInjectionContext(injector, () => {
60-
const state = computed(() => {
61-
const cameraState = cubeCameraState();
62-
return { ...defaultCubeCameraState, ...cameraState };
63-
});
60+
const state = computed(() => ({ ...defaultCubeCameraState, ...cubeCameraState() }));
61+
const resolution = computed(() => state().resolution);
62+
const near = computed(() => state().near);
63+
const far = computed(() => state().far);
6464

6565
const store = injectNgtStore();
6666

67-
const gl = store.select('gl');
68-
const scene = store.select('scene');
67+
const _gl = store.select('gl');
68+
const _scene = store.select('scene');
6969

70-
const fbo = computed(() => {
71-
const renderTarget = new THREE.WebGLCubeRenderTarget(state().resolution);
70+
const _fbo = computed(() => {
71+
const renderTarget = new THREE.WebGLCubeRenderTarget(resolution());
7272
renderTarget.texture.type = THREE.HalfFloatType;
7373
return renderTarget;
7474
});
7575

7676
effect((onCleanup) => {
77-
const _fbo = fbo();
78-
onCleanup(() => _fbo.dispose());
77+
const fbo = _fbo();
78+
onCleanup(() => fbo.dispose());
7979
});
8080

81-
const camera = computed(() => {
82-
const { near, far } = state();
83-
return new THREE.CubeCamera(near, far, fbo());
84-
});
81+
const _camera = computed(() => new THREE.CubeCamera(near(), far(), _fbo()));
8582

8683
let originalFog: THREE.Scene['fog'];
8784
let originalBackground: THREE.Scene['background'];
8885

8986
const update = computed(() => {
90-
const _scene = scene();
91-
const _gl = gl();
92-
const _camera = camera();
93-
const { envMap, fog } = untracked(state);
87+
const [scene, gl, camera, { envMap, fog }] = [_scene(), _gl(), _camera(), untracked(state)];
9488

9589
return () => {
96-
originalFog = _scene.fog;
97-
originalBackground = _scene.background;
98-
_scene.background = envMap || originalBackground;
99-
_scene.fog = fog || originalFog;
100-
_camera.update(_gl, _scene);
101-
_scene.fog = originalFog;
102-
_scene.background = originalBackground;
90+
originalFog = scene.fog;
91+
originalBackground = scene.background;
92+
scene.background = envMap || originalBackground;
93+
scene.fog = fog || originalFog;
94+
camera.update(gl, scene);
95+
scene.fog = originalFog;
96+
scene.background = originalBackground;
10397
};
10498
});
10599

106-
return { fbo, camera, update };
100+
return { fbo: _fbo, camera: _camera, update };
107101
});
108102
}
109103

@@ -152,10 +146,10 @@ export class NgtsCubeCamera implements OnInit {
152146
@Input() cameraRef = injectNgtRef<Group>();
153147

154148
@ContentChild(NgtsCubeCameraContent, { static: true, read: TemplateRef })
155-
cameraContent!: TemplateRef<{ texture: Signal<THREE.WebGLRenderTarget['texture']> }>;
149+
private cameraContent!: TemplateRef<{ texture: Signal<THREE.WebGLRenderTarget['texture']> }>;
156150

157151
@ViewChild('anchor', { static: true, read: ViewContainerRef })
158-
anchor!: ViewContainerRef;
152+
private anchor!: ViewContainerRef;
159153

160154
/** Resolution of the FBO, 256 */
161155
@Input({ alias: 'resolution' }) set _resolution(resolution: number) {
@@ -182,7 +176,7 @@ export class NgtsCubeCamera implements OnInit {
182176
this.inputs.set({ fog });
183177
}
184178

185-
cubeCamera = injectNgtsCubeCamera(this.inputs.select());
179+
cubeCamera = injectNgtsCubeCamera(this.inputs.state);
186180
private texture = computed(() => this.cubeCamera.fbo().texture);
187181
private contentRef?: EmbeddedViewRef<unknown>;
188182

libs/soba/cameras/src/orthographic-camera/orthographic-camera.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,30 @@ export class NgtsOrthographicCamera extends NgtsCamera<THREE.OrthographicCamera>
5757

5858
@ContentChild(NgtsCameraContent) cameraContent?: NgtsCameraContent;
5959

60-
@Input() set left(left: number) {
60+
@Input({ alias: 'left' }) set _left(left: number) {
6161
this.orthographicInputs.set({ left });
6262
}
6363

64-
@Input() set right(right: number) {
64+
@Input({ alias: 'right' }) set _right(right: number) {
6565
this.orthographicInputs.set({ right });
6666
}
6767

68-
@Input() set top(top: number) {
68+
@Input({ alias: 'top' }) set _top(top: number) {
6969
this.orthographicInputs.set({ top });
7070
}
7171

72-
@Input() set bottom(bottom: number) {
72+
@Input({ alias: 'bottom' }) set _bottom(bottom: number) {
7373
this.orthographicInputs.set({ bottom });
7474
}
7575

76-
private _left = this.orthographicInputs.select('left');
77-
private _right = this.orthographicInputs.select('right');
78-
private _top = this.orthographicInputs.select('top');
79-
private _bottom = this.orthographicInputs.select('bottom');
76+
private left = this.orthographicInputs.select('left');
77+
private right = this.orthographicInputs.select('right');
78+
private top = this.orthographicInputs.select('top');
79+
private bottom = this.orthographicInputs.select('bottom');
8080
private size = this.store.select('size');
8181

82-
cameraLeft = computed(() => this._left() || this.size().width / -2);
83-
cameraRight = computed(() => this._right() || this.size().width / 2);
84-
cameraTop = computed(() => this._top() || this.size().height / 2);
85-
cameraBottom = computed(() => this._bottom() || this.size().height / -2);
82+
cameraLeft = computed(() => this.left() || this.size().width / -2);
83+
cameraRight = computed(() => this.right() || this.size().width / 2);
84+
cameraTop = computed(() => this.top() || this.size().height / 2);
85+
cameraBottom = computed(() => this.bottom() || this.size().height / -2);
8686
}

libs/soba/controls/src/orbit-controls/orbit-controls.ts

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,24 @@ export class NgtsOrbitControls {
7979
@Output() end = new EventEmitter<THREE.Event>();
8080

8181
private store = injectNgtStore();
82+
private invalidate = this.store.select('invalidate');
83+
private performance = this.store.select('performance');
84+
private defaultCamera = this.store.select('camera');
85+
private glDomElement = this.store.select('gl', 'domElement');
86+
87+
private regress = this.inputs.select('regress');
88+
private camera = this.inputs.select('camera');
89+
private domElement = this.inputs.select('domElement');
90+
private keyEvents = this.inputs.select('keyEvents');
91+
private makeDefault = this.inputs.select('makeDefault');
8292

8393
args = computed(() => [this.controlsRef.nativeElement]);
8494
enableDamping = this.inputs.select('enableDamping');
8595

8696
constructor() {
8797
injectBeforeRender(
8898
() => {
89-
const controls = this.controlsRef.untracked;
99+
const controls = this.controlsRef.nativeElement;
90100
if (controls && controls.enabled) {
91101
controls.update();
92102
}
@@ -101,40 +111,28 @@ export class NgtsOrbitControls {
101111
}
102112

103113
private setControls() {
104-
const camera = this.inputs.select('camera');
105-
const defaultCamera = this.store.select('camera');
106-
const trigger = computed(() => ({ camera: camera(), defaultCamera: defaultCamera() }));
107-
108114
effect(() => {
109-
const { camera, defaultCamera } = trigger();
115+
const [camera, defaultCamera, controls] = [
116+
this.camera(),
117+
this.defaultCamera(),
118+
this.controlsRef.nativeElement,
119+
];
110120
const controlsCamera = camera || defaultCamera;
111-
const controls = this.controlsRef.nativeElement;
112121
if (!controls || controls.object !== controlsCamera) {
113122
this.controlsRef.nativeElement = new OrbitControls(controlsCamera as NgtCamera);
114123
}
115124
});
116125
}
117126

118127
private connectElement() {
119-
const glDomElement = this.store.select('gl', 'domElement');
120-
const domElement = this.inputs.select('domElement');
121-
const regress = this.inputs.select('regress');
122-
const invalidate = this.store.select('invalidate');
123-
const keyEvents = this.inputs.select('keyEvents');
124-
125-
const trigger = computed(() => {
126-
const eventsSource = this.store.get('events', 'connected');
127-
return {
128-
keyEvents: keyEvents(),
129-
controls: this.controlsRef.nativeElement,
130-
domElement: domElement() || eventsSource || glDomElement(),
131-
regress: regress(),
132-
invalidate: invalidate(),
133-
};
134-
});
135-
136128
effect((onCleanup) => {
137-
const { domElement, controls, keyEvents } = trigger();
129+
const [keyEvents, domElement, controls] = [
130+
this.keyEvents(),
131+
this.domElement() || this.store.get('events', 'connected') || this.glDomElement(),
132+
this.controlsRef.nativeElement,
133+
this.invalidate(),
134+
this.regress(),
135+
];
138136
if (!controls) return;
139137
if (keyEvents) {
140138
controls.connect(keyEvents === true ? domElement : keyEvents);
@@ -146,11 +144,8 @@ export class NgtsOrbitControls {
146144
}
147145

148146
private makeControlsDefault() {
149-
const makeDefault = this.inputs.select('makeDefault');
150-
const trigger = computed(() => ({ controls: this.controlsRef.nativeElement, makeDefault: makeDefault() }));
151-
152147
effect((onCleanup) => {
153-
const { controls, makeDefault } = trigger();
148+
const [controls, makeDefault] = [this.controlsRef.nativeElement, this.makeDefault()];
154149
if (!controls) return;
155150
if (makeDefault) {
156151
const oldControls = this.store.get('controls');
@@ -161,18 +156,13 @@ export class NgtsOrbitControls {
161156
}
162157

163158
private setEvents() {
164-
const invalidate = this.store.select('invalidate');
165-
const performance = this.store.select('performance');
166-
const regress = this.inputs.select('regress');
167-
168-
const trigger = computed(() => ({
169-
invalidate: invalidate(),
170-
performance: performance(),
171-
regress: regress(),
172-
controls: this.controlsRef.nativeElement,
173-
}));
174159
effect((onCleanup) => {
175-
const { controls, invalidate, performance, regress } = trigger();
160+
const [controls, invalidate, performance, regress] = [
161+
this.controlsRef.nativeElement,
162+
this.invalidate(),
163+
this.performance(),
164+
this.regress(),
165+
];
176166
if (!controls) return;
177167
const changeCallback: (e: THREE.Event) => void = (e) => {
178168
invalidate();

0 commit comments

Comments
 (0)