Skip to content

Commit b41f1d6

Browse files
committed
refactor(soba): update gizmos with new renderer
1 parent b62b21d commit b41f1d6

File tree

9 files changed

+331
-322
lines changed

9 files changed

+331
-322
lines changed

libs/soba/gizmos/src/lib/gizmo-helper/gizmo-helper.ts

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,47 @@ import {
88
Directive,
99
effect,
1010
ElementRef,
11+
Injector,
1112
input,
1213
output,
13-
Signal,
1414
TemplateRef,
1515
viewChild,
1616
} from '@angular/core';
17-
import { extend, hasListener, injectBeforeRender, injectStore, NgtPortal, NgtPortalContent, pick } from 'angular-three';
17+
import {
18+
extend,
19+
hasListener,
20+
injectBeforeRender,
21+
injectStore,
22+
NgtPortal,
23+
NgtPortalAutoRender,
24+
NgtPortalContent,
25+
pick,
26+
} from 'angular-three';
1827
import { NgtsOrthographicCamera } from 'angular-three-soba/cameras';
1928
import CameraControls from 'camera-controls';
2029
import { mergeInputs } from 'ngxtension/inject-inputs';
21-
import { Group, Matrix4, Object3D, Quaternion, Scene, Vector3 } from 'three';
30+
import * as THREE from 'three';
31+
import { Group } from 'three';
2232
import { OrbitControls } from 'three-stdlib';
2333

2434
const turnRate = 2 * Math.PI; // turn rate in angles per second
25-
const dummy = new Object3D();
26-
const matrix = new Matrix4();
27-
const [q1, q2] = [new Quaternion(), new Quaternion()];
28-
const target = new Vector3();
29-
const targetPosition = new Vector3();
35+
const dummy = new THREE.Object3D();
36+
const matrix = new THREE.Matrix4();
37+
const [q1, q2] = [new THREE.Quaternion(), new THREE.Quaternion()];
38+
const target = new THREE.Vector3();
39+
const targetPosition = new THREE.Vector3();
3040

3141
@Directive({ selector: 'ng-template[gizmoHelperContent]' })
3242
export class NgtsGizmoHelperContent {
33-
static ngTemplateContextGuard(_: NgtsGizmoHelperContent, ctx: unknown): ctx is { container: Object3D } {
43+
static ngTemplateContextGuard(
44+
_: NgtsGizmoHelperContent,
45+
ctx: unknown,
46+
): ctx is { container: THREE.Object3D; injector: Injector } {
3447
return true;
3548
}
3649
}
3750

38-
type ControlsProto = { update(delta?: number): void; target: Vector3 };
51+
type ControlsProto = { update(delta?: number): void; target: THREE.Vector3 };
3952

4053
export interface NgtsGizmoHelperOptions {
4154
alignment:
@@ -66,16 +79,15 @@ const defaultOptions: NgtsGizmoHelperOptions = {
6679
6780
<ngt-portal
6881
[container]="scene"
69-
[autoRender]="true"
70-
[autoRenderPriority]="_renderPriority"
82+
[autoRender]="_renderPriority"
7183
[state]="{ events: { priority: _renderPriority + 1 } }"
7284
>
73-
<ng-template portalContent let-injector="injector" let-container="container">
85+
<ng-template portalContent let-injector="injector">
7486
<ngts-orthographic-camera [options]="{ makeDefault: true, position: [0, 0, 200] }" />
7587
<ngt-group #gizmo [position]="[x(), y(), 0]">
7688
<ng-container
7789
[ngTemplateOutlet]="content()"
78-
[ngTemplateOutletContext]="{ container, injector }"
90+
[ngTemplateOutletContext]="{ container: scene, injector }"
7991
[ngTemplateOutletInjector]="injector"
8092
/>
8193
</ngt-group>
@@ -84,7 +96,7 @@ const defaultOptions: NgtsGizmoHelperOptions = {
8496
`,
8597
schemas: [CUSTOM_ELEMENTS_SCHEMA],
8698
changeDetection: ChangeDetectionStrategy.OnPush,
87-
imports: [NgtPortal, NgtPortalContent, NgtsOrthographicCamera, NgTemplateOutlet],
99+
imports: [NgtPortal, NgtPortalAutoRender, NgtPortalContent, NgtsOrthographicCamera, NgTemplateOutlet],
88100
})
89101
export class NgtsGizmoHelper {
90102
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
@@ -94,41 +106,38 @@ export class NgtsGizmoHelper {
94106
protected margin = pick(this.options, 'margin');
95107
protected alignment = pick(this.options, 'alignment');
96108

97-
protected scene = new Scene();
109+
protected scene = new THREE.Scene();
98110

99111
protected content = contentChild.required(NgtsGizmoHelperContent, { read: TemplateRef });
100112

101-
private gizmoRef = viewChild<ElementRef<Group>>('gizmo');
113+
private gizmoRef = viewChild<ElementRef<THREE.Group>>('gizmo');
102114
private virtualCameraRef = viewChild(NgtsOrthographicCamera);
103115

104116
private store = injectStore();
105-
private size = this.store.select('size');
106-
private mainCamera = this.store.select('camera');
107-
private defaultControls = this.store.select('controls') as unknown as Signal<ControlsProto>;
108-
private invalidate = this.store.select('invalidate');
109117

110118
protected x = computed(() => {
111119
const alignment = this.alignment();
112120
if (alignment.endsWith('-center')) return 0;
113121

114-
const [{ width }, [marginX]] = [this.size(), this.margin()];
122+
const [width, [marginX]] = [this.store.size.width(), this.margin()];
115123
return alignment.endsWith('-left') ? -width / 2 + marginX : width / 2 - marginX;
116124
});
117125
protected y = computed(() => {
118126
const alignment = this.alignment();
119127
if (alignment.startsWith('center-')) return 0;
120128

121-
const [{ height }, [marginY]] = [this.size(), this.margin()];
129+
const [height, [marginY]] = [this.store.size.height(), this.margin()];
122130
return alignment.startsWith('top-') ? height / 2 - marginY : -height / 2 + marginY;
123131
});
124132

125133
private animating = false;
126134
private radius = 0;
127-
private focusPoint = new Vector3(0, 0, 0);
128-
private defaultUp = new Vector3(0, 0, 0);
135+
private focusPoint = new THREE.Vector3(0, 0, 0);
136+
private defaultUp = new THREE.Vector3(0, 0, 0);
129137

130138
constructor() {
131139
extend({ Group });
140+
132141
effect(() => {
133142
this.updateDefaultUpEffect();
134143
});
@@ -141,7 +150,11 @@ export class NgtsGizmoHelper {
141150

142151
if (!virtualCamera || !gizmo) return;
143152

144-
const [defaultControls, mainCamera, invalidate] = [this.defaultControls(), this.mainCamera(), this.invalidate()];
153+
const [defaultControls, mainCamera, invalidate] = [
154+
this.store.snapshot.controls as unknown as ControlsProto,
155+
this.store.snapshot.camera,
156+
this.store.snapshot.invalidate,
157+
];
145158

146159
// Animate step
147160
if (this.animating) {
@@ -179,8 +192,12 @@ export class NgtsGizmoHelper {
179192
});
180193
}
181194

182-
tweenCamera(direction: Vector3) {
183-
const [defaultControls, invalidate, mainCamera] = [this.defaultControls(), this.invalidate(), this.mainCamera()];
195+
tweenCamera(direction: THREE.Vector3) {
196+
const [defaultControls, invalidate, mainCamera] = [
197+
this.store.snapshot.controls as unknown as ControlsProto,
198+
this.store.snapshot.invalidate,
199+
this.store.snapshot.camera,
200+
];
184201

185202
this.animating = true;
186203
if (defaultControls) {
@@ -202,7 +219,7 @@ export class NgtsGizmoHelper {
202219
}
203220

204221
private updateDefaultUpEffect() {
205-
const mainCamera = this.mainCamera();
222+
const mainCamera = this.store.camera();
206223
this.defaultUp.copy(mainCamera.up);
207224
}
208225

libs/soba/gizmos/src/lib/gizmo-helper/gizmo-viewcube.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
pick,
2121
} from 'angular-three';
2222
import { mergeInputs } from 'ngxtension/inject-inputs';
23-
import { BoxGeometry, CanvasTexture, Group, Mesh, MeshBasicMaterial, Vector3 } from 'three';
23+
import * as THREE from 'three';
24+
import { BoxGeometry, Group, Mesh, MeshBasicMaterial } from 'three';
2425
import { NgtsGizmoHelper } from './gizmo-helper';
2526

2627
type XYZ = [number, number, number];
@@ -55,7 +56,7 @@ const defaultFaceMaterialOptions: NgtsViewcubeCommonOptions = {
5556
[attach]="['material', index()]"
5657
[map]="texture()"
5758
[color]="materialColor()"
58-
[transparent]="true"
59+
transparent
5960
[opacity]="opacity()"
6061
/>
6162
`,
@@ -76,7 +77,6 @@ export class FaceMaterial {
7677

7778
private document = inject(DOCUMENT);
7879
private store = injectStore();
79-
private gl = this.store.select('gl');
8080

8181
protected opacity = pick(this.options, 'opacity');
8282
protected materialColor = computed(() => (this.hover() ? this.hoverColor() : 'white'));
@@ -88,7 +88,7 @@ export class FaceMaterial {
8888
this.color(),
8989
this.textColor(),
9090
this.strokeColor(),
91-
this.gl(),
91+
this.store.gl(),
9292
];
9393

9494
const canvas = this.document.createElement('canvas');
@@ -105,7 +105,7 @@ export class FaceMaterial {
105105
context.fillStyle = textColor;
106106
context.fillText(faces[index].toUpperCase(), 64, 76);
107107

108-
const texture = new CanvasTexture(canvas);
108+
const texture = new THREE.CanvasTexture(canvas);
109109
texture.anisotropy = gl.capabilities.getMaxAnisotropy() || 1;
110110

111111
return texture;
@@ -179,7 +179,7 @@ export class FaceCube {
179179
})
180180
export class EdgeCube {
181181
dimensions = input.required<XYZ>();
182-
position = input.required<Vector3>();
182+
position = input.required<THREE.Vector3>();
183183
hoverColor = input(colors.hover, {
184184
transform: (value: string | undefined) => {
185185
if (value === undefined) return colors.hover;
@@ -242,7 +242,7 @@ export class NgtsGizmoViewcube {
242242

243243
protected hoverColor = pick(this.options, 'hoverColor');
244244

245-
protected corners: Vector3[] = [
245+
protected corners: THREE.Vector3[] = [
246246
[1, 1, 1],
247247
[1, 1, -1],
248248
[1, -1, 1],
@@ -253,7 +253,7 @@ export class NgtsGizmoViewcube {
253253
[-1, -1, -1],
254254
].map(this.makePositionVector);
255255
protected cornerDimensions: XYZ = [0.25, 0.25, 0.25];
256-
protected edges: Vector3[] = [
256+
protected edges: THREE.Vector3[] = [
257257
[1, 1, 0],
258258
[1, 0, 1],
259259
[1, 0, -1],
@@ -281,6 +281,6 @@ export class NgtsGizmoViewcube {
281281
}
282282

283283
private makePositionVector(xyz: number[]) {
284-
return new Vector3(...xyz).multiplyScalar(0.38);
284+
return new THREE.Vector3(...xyz).multiplyScalar(0.38);
285285
}
286286
}

libs/soba/gizmos/src/lib/gizmo-helper/gizmo-viewport.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,17 @@ import {
1515
hasListener,
1616
injectStore,
1717
NgtArgs,
18+
NgtEuler,
1819
NgtEventHandlers,
19-
NgtGroup,
20+
NgtThreeElements,
2021
NgtThreeEvent,
2122
NgtVector3,
2223
omit,
2324
pick,
2425
} from 'angular-three';
2526
import { mergeInputs } from 'ngxtension/inject-inputs';
26-
import {
27-
BoxGeometry,
28-
CanvasTexture,
29-
ColorRepresentation,
30-
Group,
31-
Mesh,
32-
MeshBasicMaterial,
33-
Sprite,
34-
SpriteMaterial,
35-
} from 'three';
27+
import * as THREE from 'three';
28+
import { BoxGeometry, Group, Mesh, MeshBasicMaterial, Sprite, SpriteMaterial } from 'three';
3629
import { NgtsGizmoHelper } from './gizmo-helper';
3730

3831
@Component({
@@ -56,8 +49,8 @@ export class Axis {
5649
return value;
5750
},
5851
});
59-
color = input<ColorRepresentation>();
60-
rotation = input<NgtVector3>([0, 0, 0]);
52+
color = input<THREE.ColorRepresentation>();
53+
rotation = input<NgtEuler>([0, 0, 0]);
6154

6255
constructor() {
6356
extend({ Group, Mesh, BoxGeometry, MeshBasicMaterial });
@@ -94,15 +87,14 @@ export class AxisHead {
9487
private document = inject(DOCUMENT);
9588
private gizmoHelper = inject(NgtsGizmoHelper);
9689
private store = injectStore();
97-
private gl = this.store.select('gl');
9890

9991
protected texture = computed(() => {
10092
const [arcStyle, label, labelColor, font, gl] = [
10193
this.arcStyle(),
10294
this.label(),
10395
this.labelColor(),
10496
this.font(),
105-
this.gl(),
97+
this.store.gl(),
10698
];
10799

108100
const canvas = this.document.createElement('canvas');
@@ -123,7 +115,7 @@ export class AxisHead {
123115
context.fillText(label, 32, 41);
124116
}
125117

126-
const texture = new CanvasTexture(canvas);
118+
const texture = new THREE.CanvasTexture(canvas);
127119
texture.anisotropy = gl.capabilities.getMaxAnisotropy() || 1;
128120
return texture;
129121
});
@@ -160,7 +152,7 @@ export class AxisHead {
160152
}
161153
}
162154

163-
export interface NgtsGizmoViewportOptions extends Partial<NgtGroup> {
155+
export interface NgtsGizmoViewportOptions extends Partial<NgtThreeElements['ngt-group']> {
164156
axisColors: [string, string, string];
165157
axisScale?: [number, number, number];
166158
labels: [string, string, string];
@@ -265,7 +257,7 @@ export class NgtsGizmoViewport {
265257
protected readonly Math = Math;
266258

267259
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
268-
parameters = omit(this.options, [
260+
protected parameters = omit(this.options, [
269261
'axisColors',
270262
'axisScale',
271263
'labels',

0 commit comments

Comments
 (0)