Skip to content

Commit f0ee504

Browse files
committed
rename all viewChild to be suffixed with Ref
1 parent c555326 commit f0ee504

32 files changed

+147
-155
lines changed

libs/core/src/lib/utils/parameters.ts

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Signal, computed } from '@angular/core';
2-
import { Vector2, Vector2Tuple, Vector3, Vector3Tuple } from 'three';
3-
import { NgtVector2, NgtVector3 } from '../three-types';
1+
import { computed, Signal, Type } from '@angular/core';
2+
import { Vector2, Vector3, Vector4 } from 'three';
3+
import { NgtVector2, NgtVector3, NgtVector4 } from '../three-types';
44
import { NgtAnyRecord } from '../types';
55

66
type KeysOfType<TObject extends object, TType> = Exclude<
@@ -61,70 +61,58 @@ export function merge<TObject extends object>(
6161
return computed(() => ({ ...toMerge, ...objFn() }));
6262
}
6363

64-
export function vector2<TObject extends object>(
65-
options: Signal<TObject>,
66-
key: KeysOfType<TObject, NgtVector2>,
67-
): Signal<Vector2>;
68-
export function vector2<TObject extends object>(
69-
options: Signal<TObject>,
70-
key: KeysOfType<TObject, NgtVector2>,
71-
keepUndefined: true,
72-
): Signal<Vector2 | undefined>;
73-
export function vector2(options: Signal<NgtAnyRecord>, key: string, keepUndefined = false) {
74-
return computed(
75-
() => {
76-
const value = options()[key];
77-
if (keepUndefined && value == undefined) return undefined;
78-
if (typeof value === 'number') return new Vector2(value, value);
79-
else if (value) return new Vector2(...(value as Vector2Tuple));
80-
else return new Vector2();
81-
},
82-
{ equal: (a, b) => !!a && !!b && a.equals(b) },
83-
);
84-
}
64+
type NgtVectorComputed<
65+
TVector extends Vector2 | Vector3 | Vector4,
66+
TNgtVector = TVector extends Vector2 ? NgtVector2 : TVector extends Vector3 ? NgtVector3 : NgtVector4,
67+
> = {
68+
(input: Signal<TNgtVector>): Signal<TVector>;
69+
(input: Signal<TNgtVector>, keepUndefined: true): Signal<TVector | undefined>;
70+
<TObject extends object>(options: Signal<TObject>, key: KeysOfType<TObject, TNgtVector>): Signal<TVector>;
71+
<TObject extends object>(
72+
options: Signal<TObject>,
73+
key: KeysOfType<TObject, TNgtVector>,
74+
keepUndefined: true,
75+
): Signal<TVector | undefined>;
76+
};
77+
78+
function createVectorComputed<TVector extends Vector2 | Vector3 | Vector4>(vectorCtor: Type<TVector>) {
79+
type TNgtVector = TVector extends Vector2 ? NgtVector2 : TVector extends Vector3 ? NgtVector3 : NgtVector4;
80+
return ((
81+
inputOrOptions: Signal<NgtAnyRecord> | Signal<TNgtVector>,
82+
keyOrKeepUndefined?: string | true,
83+
keepUndefined?: boolean,
84+
) => {
85+
if (typeof keyOrKeepUndefined === 'undefined' || typeof keyOrKeepUndefined === 'boolean') {
86+
keepUndefined = !!keyOrKeepUndefined;
87+
const input = inputOrOptions as Signal<TNgtVector>;
88+
return computed(
89+
() => {
90+
const value = input();
91+
if (keepUndefined && value == undefined) return undefined;
92+
if (typeof value === 'number') return new vectorCtor().setScalar(value);
93+
else if (value) return new vectorCtor(...(value as any));
94+
else return new vectorCtor();
95+
},
96+
{ equal: (a, b) => !!a && !!b && a.equals(b as any) },
97+
);
98+
}
99+
100+
const options = inputOrOptions as Signal<NgtAnyRecord>;
101+
const key = keyOrKeepUndefined as string;
85102

86-
export function vector3(input: Signal<NgtVector3>): Signal<Vector3>;
87-
export function vector3(input: Signal<NgtVector3>, keepUndefined: true): Signal<Vector3>;
88-
export function vector3<TObject extends object>(
89-
options: Signal<TObject>,
90-
key: KeysOfType<TObject, NgtVector3>,
91-
): Signal<Vector3>;
92-
export function vector3<TObject extends object>(
93-
options: Signal<TObject>,
94-
key: KeysOfType<TObject, NgtVector3>,
95-
keepUndefined: true,
96-
): Signal<Vector3 | undefined>;
97-
export function vector3(
98-
inputOrOptions: Signal<NgtAnyRecord> | Signal<NgtVector3>,
99-
keyOrKeepUndefined?: string | true,
100-
keepUndefined?: boolean,
101-
) {
102-
if (typeof keyOrKeepUndefined === 'undefined' || typeof keyOrKeepUndefined === 'boolean') {
103-
keepUndefined = !!keyOrKeepUndefined;
104-
const input = inputOrOptions as Signal<NgtVector3>;
105103
return computed(
106104
() => {
107-
const value = input();
105+
const value = options()[key];
108106
if (keepUndefined && value == undefined) return undefined;
109-
if (typeof value === 'number') return new Vector3(value, value, value);
110-
else if (value) return new Vector3(...(value as Vector3Tuple));
111-
else return new Vector3();
107+
if (typeof value === 'number') return new vectorCtor().setScalar(value);
108+
else if (value) return new vectorCtor(...(value as any));
109+
else return new vectorCtor();
112110
},
113-
{ equal: (a, b) => !!a && !!b && a.equals(b) },
111+
{ equal: (a, b) => !!a && !!b && a.equals(b as any) },
114112
);
115-
}
116-
117-
const options = inputOrOptions as Signal<NgtAnyRecord>;
118-
const key = keyOrKeepUndefined as string;
119-
120-
return computed(
121-
() => {
122-
const value = options()[key];
123-
if (keepUndefined && value == undefined) return undefined;
124-
if (typeof value === 'number') return new Vector3(value, value, value);
125-
else if (value) return new Vector3(...(value as Vector3Tuple));
126-
else return new Vector3();
127-
},
128-
{ equal: (a, b) => !!a && !!b && a.equals(b) },
129-
);
113+
}) as NgtVectorComputed<TVector, TNgtVector>;
130114
}
115+
116+
export const vector2 = createVectorComputed(Vector2);
117+
export const vector3 = createVectorComputed(Vector3);
118+
export const vector4 = createVectorComputed(Vector4);

libs/postprocessing/src/lib/effect-composer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class NgtpEffectComposer {
8686
enableNormalPass = pick(this.options, 'enableNormalPass');
8787
resolutionScale = pick(this.options, 'resolutionScale');
8888

89-
group = viewChild.required<ElementRef<Group>>('group');
89+
groupRef = viewChild.required<ElementRef<Group>>('group');
9090

9191
composerData = computed(() => {
9292
const webGL2Available = isWebGL2Available();
@@ -191,7 +191,7 @@ export class NgtpEffectComposer {
191191
private updatePasses() {
192192
this.autoEffect(() => {
193193
const [group, { composer, normalPass, downSamplingPass }, camera] = [
194-
this.group(),
194+
this.groupRef(),
195195
this.composerData(),
196196
this.camera(),
197197
];

libs/soba/abstractions/src/lib/edges.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class NgtsEdges {
4848
const autoEffect = injectAutoEffect();
4949
afterNextRender(() => {
5050
autoEffect(() => {
51-
const line = this.line().line()?.nativeElement;
51+
const line = this.line().lineRef()?.nativeElement;
5252
if (!line) return;
5353

5454
const lS = getLocalState(line);

libs/soba/abstractions/src/lib/grid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class NgtsGrid {
6363
'side',
6464
]);
6565

66-
mesh = viewChild.required<ElementRef<Mesh>>('mesh');
66+
meshRef = viewChild.required<ElementRef<Mesh>>('mesh');
6767

6868
private plane = new Plane();
6969
private upVector = new Vector3(0, 1, 0);
@@ -104,7 +104,7 @@ export class NgtsGrid {
104104
extend({ Mesh, PlaneGeometry, GridMaterial });
105105

106106
injectBeforeRender(({ camera }) => {
107-
const mesh = this.mesh().nativeElement;
107+
const mesh = this.meshRef().nativeElement;
108108

109109
this.plane.setFromNormalAndCoplanarPoint(this.upVector, this.zeroVector).applyMatrix4(mesh.matrixWorld);
110110

libs/soba/abstractions/src/lib/line.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class NgtsLine {
7373
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
7474
parameters = omit(this.options, ['color', 'vertexColors', 'lineWidth', 'segments', 'linewidth', 'dashed']);
7575

76-
line = viewChild<ElementRef<Line2 | LineSegments2>>('line');
76+
lineRef = viewChild<ElementRef<Line2 | LineSegments2>>('line');
7777

7878
private store = injectStore();
7979
private size = this.store.select('size');

libs/soba/abstractions/src/lib/prism-geometry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class NgtsPrismGeometry {
4444
return new Shape(interpolatedVertices);
4545
});
4646

47-
geometry = viewChild<ElementRef<ExtrudeGeometry>>('geometry');
47+
geometryRef = viewChild<ElementRef<ExtrudeGeometry>>('geometry');
4848

4949
constructor() {
5050
extend({ ExtrudeGeometry });

libs/soba/abstractions/src/lib/text-3d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class NgtsText3D {
6464
'smooth',
6565
]);
6666

67-
mesh = viewChild<ElementRef<Mesh>>('mesh');
67+
meshRef = viewChild<ElementRef<Mesh>>('mesh');
6868

6969
loadedFont = injectFont(this.font);
7070
private smooth = pick(this.options, 'smooth');
@@ -94,7 +94,7 @@ export class NgtsText3D {
9494

9595
afterNextRender(() => {
9696
autoEffect(() => {
97-
const [mesh, smooth, textArgs] = [this.mesh()?.nativeElement, this.smooth(), this.textArgs()];
97+
const [mesh, smooth, textArgs] = [this.meshRef()?.nativeElement, this.smooth(), this.textArgs()];
9898
if (!textArgs || !mesh) return;
9999
if (smooth) {
100100
mesh.geometry = mergeVertices(mesh.geometry, smooth);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ export class NgtsCubeCamera {
117117
camera = this.cubeCamera.camera;
118118
texture = pick(this.cubeCamera.fbo, 'texture');
119119

120-
group = viewChild.required<ElementRef<Group>>('group');
120+
groupRef = viewChild.required<ElementRef<Group>>('group');
121121
cameraContent = contentChild(NgtsCameraContent, { read: TemplateRef });
122122

123123
constructor() {
124124
extend({ Group });
125125

126126
let count = 0;
127127
injectBeforeRender(() => {
128-
const group = this.group().nativeElement;
128+
const group = this.groupRef().nativeElement;
129129
if (!group) return;
130130

131131
const frames = this.options().frames;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import { mergeInputs } from 'ngxtension/inject-inputs';
1919
import { Color, Group, OrthographicCamera, Texture } from 'three';
2020
import { NgtsCameraContent } from './camera-content';
2121

22-
extend({ OrthographicCamera, Group });
23-
2422
export interface NgtsOrthographicCameraOptions extends Partial<NgtOrthographicCamera> {
2523
/** Registers the camera as the system default, fiber will start rendering with it */
2624
makeDefault?: boolean;
@@ -102,6 +100,8 @@ export class NgtsOrthographicCamera {
102100
texture = pick(this.fbo, 'texture');
103101

104102
constructor() {
103+
extend({ OrthographicCamera, Group });
104+
105105
afterNextRender(() => {
106106
this.autoEffect(() => {
107107
if (!this.manual()) {

libs/soba/cameras/src/lib/perspective-camera.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import { mergeInputs } from 'ngxtension/inject-inputs';
1818
import { Color, Group, PerspectiveCamera, Texture } from 'three';
1919
import { NgtsCameraContent } from './camera-content';
2020

21-
extend({ PerspectiveCamera, Group });
22-
2321
export interface NgtsPerspectiveCameraOptions extends Partial<NgtPerspectiveCamera> {
2422
/** Registers the camera as the system default, fiber will start rendering with it */
2523
makeDefault?: boolean;
@@ -79,6 +77,8 @@ export class NgtsPerspectiveCamera {
7977
texture = pick(this.fbo, 'texture');
8078

8179
constructor() {
80+
extend({ PerspectiveCamera, Group });
81+
8282
afterNextRender(() => {
8383
this.autoEffect(() => {
8484
if (!this.manual()) {

0 commit comments

Comments
 (0)