Skip to content

Commit a83ba07

Browse files
committed
refactor(soba): adjust performances with new renderer
1 parent 155a68d commit a83ba07

File tree

10 files changed

+101
-110
lines changed

10 files changed

+101
-110
lines changed

libs/core/src/lib/three-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ export type NgtThreeElementsMap = {
124124
* @from node_modules/@types/three/src/core/Object3D.d.ts
125125
*/
126126
'ngt-object3D': Extract<keyof ThreeExports, 'Object3D'>;
127+
/**
128+
* @from node_modules/@types/three/src/objects/LOD.d.ts
129+
*/
130+
'ngt-lOD': Extract<keyof ThreeExports, 'LOD'>;
127131
/**
128132
* @from node_modules/@types/three/src/objects/Mesh.d.ts
129133
*/

libs/soba/performances/src/lib/adaptive-dpr.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,30 @@ export class NgtsAdaptiveDpr {
77

88
constructor() {
99
const store = injectStore();
10-
const gl = store.select('gl');
11-
const active = store.select('internal', 'active');
12-
const current = store.select('performance', 'current');
13-
const initialDpr = store.select('viewport', 'initialDpr');
14-
const setDpr = store.select('setDpr');
1510

1611
effect(() => {
17-
const [_current, pixelated, domElement, _setDpr, _initialDpr] = [
18-
current(),
12+
const [current, pixelated, domElement, setDpr, initialDpr] = [
13+
store.performance.current(),
1914
untracked(this.pixelated),
20-
untracked(gl).domElement,
21-
untracked(setDpr),
22-
untracked(initialDpr),
15+
store.snapshot.gl.domElement,
16+
store.snapshot.setDpr,
17+
store.snapshot.viewport.initialDpr,
2318
];
2419

25-
_setDpr(_current * _initialDpr);
26-
if (pixelated && domElement) domElement.style.imageRendering = _current === 1 ? 'auto' : 'pixelated';
20+
setDpr(current * initialDpr);
21+
if (pixelated && domElement) domElement.style.imageRendering = current === 1 ? 'auto' : 'pixelated';
2722
});
2823

2924
inject(DestroyRef).onDestroy(() => {
30-
const [domElement, _active, _setDpr, _initialDpr, pixelated] = [
31-
untracked(gl).domElement,
32-
untracked(active),
33-
untracked(setDpr),
34-
untracked(initialDpr),
25+
const [domElement, active, setDpr, initialDpr, pixelated] = [
26+
store.snapshot.gl.domElement,
27+
store.snapshot.internal.active,
28+
store.snapshot.setDpr,
29+
store.snapshot.viewport.initialDpr,
3530
untracked(this.pixelated),
3631
];
3732

38-
if (_active) _setDpr(_initialDpr);
33+
if (active) setDpr(initialDpr);
3934
if (pixelated && domElement) domElement.style.imageRendering = 'auto';
4035
});
4136
}

libs/soba/performances/src/lib/adaptive-events.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import { injectStore } from 'angular-three';
55
export class NgtsAdaptiveEvents {
66
constructor() {
77
const store = injectStore();
8-
const current = store.select('performance', 'current');
98
const currentEnabled = store.snapshot.events.enabled;
109

1110
effect((onCleanup) => {
12-
const _current = current();
13-
onCleanup(() => store.snapshot.setEvents({ enabled: _current === 1 }));
11+
const current = store.performance.current();
12+
onCleanup(() => store.snapshot.setEvents({ enabled: current === 1 }));
1413
});
1514

1615
inject(DestroyRef).onDestroy(() => {

libs/soba/performances/src/lib/detailed.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import {
77
input,
88
viewChild,
99
} from '@angular/core';
10-
import { extend, getLocalState, injectBeforeRender, NgtLOD, omit, pick } from 'angular-three';
10+
import { extend, getInstanceState, injectBeforeRender, NgtThreeElements, omit, pick } from 'angular-three';
1111
import { mergeInputs } from 'ngxtension/inject-inputs';
12+
import * as THREE from 'three';
1213
import { LOD } from 'three';
1314

14-
export interface NgtsDetailedOptions extends Partial<NgtLOD> {
15+
export interface NgtsDetailedOptions extends Partial<NgtThreeElements['ngt-lOD']> {
1516
hysteresis: number;
1617
}
1718

@@ -32,19 +33,19 @@ const defaultOptions: NgtsDetailedOptions = {
3233
export class NgtsDetailed {
3334
distances = input.required<number[]>();
3435
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
35-
parameters = omit(this.options, ['hysteresis']);
36+
protected parameters = omit(this.options, ['hysteresis']);
3637

37-
lodRef = viewChild.required<ElementRef<LOD>>('lod');
38+
lodRef = viewChild.required<ElementRef<THREE.LOD>>('lod');
3839
private hysteresis = pick(this.options, 'hysteresis');
3940

4041
constructor() {
4142
extend({ LOD });
4243

4344
effect(() => {
4445
const lod = this.lodRef().nativeElement;
45-
const localState = getLocalState(lod);
46-
if (!localState) return;
47-
const [, distances, hysteresis] = [localState.objects(), this.distances(), this.hysteresis()];
46+
const instanceState = getInstanceState(lod);
47+
if (!instanceState) return;
48+
const [, distances, hysteresis] = [instanceState.objects(), this.distances(), this.hysteresis()];
4849
lod.levels.length = 0;
4950
lod.children.forEach((object, index) => {
5051
lod.levels.push({ object, distance: distances[index], hysteresis });

libs/soba/performances/src/lib/instances/instances.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ import {
99
input,
1010
viewChild,
1111
} from '@angular/core';
12-
import { checkUpdate, extend, injectBeforeRender, NgtInstancedMesh, omit, pick, resolveRef } from 'angular-three';
12+
import { checkUpdate, extend, injectBeforeRender, NgtThreeElements, omit, pick, resolveRef } from 'angular-three';
1313
import { setUpdateRange } from 'angular-three-soba/misc';
1414
import { mergeInputs } from 'ngxtension/inject-inputs';
15-
import { DynamicDrawUsage, InstancedBufferAttribute, InstancedMesh, Matrix4, Quaternion, Vector3 } from 'three';
15+
import * as THREE from 'three';
16+
import { InstancedBufferAttribute, InstancedMesh } from 'three';
1617
import { NgtPositionMesh, PositionMesh } from './position-mesh';
1718

18-
const parentMatrix = new Matrix4();
19-
const instanceMatrix = new Matrix4();
20-
const tempMatrix = new Matrix4();
21-
const translation = new Vector3();
22-
const rotation = new Quaternion();
23-
const scale = new Vector3();
19+
const parentMatrix = new THREE.Matrix4();
20+
const instanceMatrix = new THREE.Matrix4();
21+
const tempMatrix = new THREE.Matrix4();
22+
const translation = new THREE.Vector3();
23+
const rotation = new THREE.Quaternion();
24+
const scale = new THREE.Vector3();
2425

2526
@Component({
2627
selector: 'ngts-instance',
@@ -35,7 +36,7 @@ const scale = new Vector3();
3536
export class NgtsInstance {
3637
options = input({} as Partial<NgtPositionMesh>);
3738

38-
instances = inject(NgtsInstances);
39+
protected instances = inject(NgtsInstances);
3940

4041
positionMeshRef = viewChild.required<ElementRef<PositionMesh>>('positionMesh');
4142

@@ -49,7 +50,7 @@ export class NgtsInstance {
4950
}
5051
}
5152

52-
export interface NgtsInstancesOptions extends Partial<NgtInstancedMesh> {
53+
export interface NgtsInstancesOptions extends Partial<NgtThreeElements['ngt-instanced-mesh']> {
5354
range?: number;
5455
limit: number;
5556
frames: number;
@@ -91,12 +92,12 @@ const defaultOptions: NgtsInstancesOptions = {
9192
changeDetection: ChangeDetectionStrategy.OnPush,
9293
})
9394
export class NgtsInstances {
94-
protected readonly DynamicDrawUsage = DynamicDrawUsage;
95+
protected readonly DynamicDrawUsage = THREE.DynamicDrawUsage;
9596

9697
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
9798
protected parameters = omit(this.options, ['limit', 'frames', 'range']);
9899

99-
instancedMeshRef = viewChild.required<ElementRef<InstancedMesh>>('instancedMesh');
100+
instancedMeshRef = viewChild.required<ElementRef<THREE.InstancedMesh>>('instancedMesh');
100101

101102
private limit = pick(this.options, 'limit');
102103

libs/soba/performances/src/lib/instances/position-mesh.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
import { ElementRef } from '@angular/core';
2-
import { NgtObject3DNode, resolveRef } from 'angular-three';
3-
import {
4-
BufferGeometry,
5-
Color,
6-
Group,
7-
InstancedMesh,
8-
Intersection,
9-
Material,
10-
Matrix4,
11-
Mesh,
12-
MeshBasicMaterial,
13-
Raycaster,
14-
} from 'three';
2+
import { NgtThreeElement, resolveRef } from 'angular-three';
3+
import * as THREE from 'three';
154

16-
export type NgtPositionMesh = NgtObject3DNode<PositionMesh, typeof PositionMesh>;
5+
export type NgtPositionMesh = NgtThreeElement<typeof PositionMesh>;
176

18-
const _instanceLocalMatrix = new Matrix4();
19-
const _instanceWorldMatrix = new Matrix4();
20-
const _instanceIntersects: Intersection[] = [];
21-
const _mesh = new Mesh<BufferGeometry, MeshBasicMaterial>();
7+
const _instanceLocalMatrix = new THREE.Matrix4();
8+
const _instanceWorldMatrix = new THREE.Matrix4();
9+
const _instanceIntersects: THREE.Intersection[] = [];
10+
const _mesh = new THREE.Mesh<THREE.BufferGeometry, THREE.MeshBasicMaterial>();
2211

23-
export class PositionMesh extends Group {
24-
color: Color;
25-
instance: ElementRef<InstancedMesh> | InstancedMesh | null | undefined;
12+
export class PositionMesh extends THREE.Group {
13+
color: THREE.Color;
14+
instance: ElementRef<THREE.InstancedMesh> | THREE.InstancedMesh | null | undefined;
2615

2716
constructor() {
2817
super();
29-
this.color = new Color('white');
18+
this.color = new THREE.Color('white');
3019
this.instance = undefined;
3120
}
3221

@@ -36,7 +25,7 @@ export class PositionMesh extends Group {
3625
}
3726

3827
// And this will allow the virtual instance to receive events
39-
override raycast(raycaster: Raycaster, intersects: Intersection[]) {
28+
override raycast(raycaster: THREE.Raycaster, intersects: THREE.Intersection[]) {
4029
const parent = resolveRef(this.instance);
4130
if (!parent) return;
4231
if (!parent.geometry || !parent.material) return;
@@ -51,7 +40,7 @@ export class PositionMesh extends Group {
5140
// the mesh represents this single instance
5241
_mesh.matrixWorld = _instanceWorldMatrix;
5342
// raycast side according to instance material
54-
if (parent.material instanceof Material) _mesh.material.side = parent.material.side;
43+
if (parent.material instanceof THREE.Material) _mesh.material.side = parent.material.side;
5544
else _mesh.material.side = parent.material[0].side;
5645
_mesh.raycast(raycaster, _instanceIntersects);
5746
// process the result of raycast

libs/soba/performances/src/lib/points/points.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import {
99
input,
1010
viewChild,
1111
} from '@angular/core';
12-
import { checkUpdate, extend, injectBeforeRender, NgtPoints, omit, pick, resolveRef } from 'angular-three';
12+
import { checkUpdate, extend, injectBeforeRender, NgtThreeElements, omit, pick, resolveRef } from 'angular-three';
1313
import { mergeInputs } from 'ngxtension/inject-inputs';
14-
import { BufferAttribute, BufferGeometry, DynamicDrawUsage, Matrix4, Points, Vector3 } from 'three';
14+
import * as THREE from 'three';
15+
import { BufferAttribute, BufferGeometry, Points } from 'three';
1516
import { NgtPositionPoint, PositionPoint } from './position-point';
1617

1718
@Component({
@@ -29,7 +30,7 @@ export class NgtsPoint {
2930

3031
positionPointRef = viewChild.required<ElementRef<PositionPoint>>('positionPoint');
3132

32-
points = inject(NgtsPointsInstances);
33+
protected points = inject(NgtsPointsInstances);
3334

3435
constructor() {
3536
extend({ PositionPoint });
@@ -85,7 +86,7 @@ export class NgtsPointsBuffer {
8586
stride = input<2 | 3>(3);
8687
options = input({} as Partial<NgtPositionPoint>);
8788

88-
pointsRef = viewChild.required<ElementRef<Points>>('points');
89+
pointsRef = viewChild.required<ElementRef<THREE.Points>>('points');
8990

9091
constructor() {
9192
extend({ Points, BufferAttribute, BufferGeometry });
@@ -101,13 +102,13 @@ export class NgtsPointsBuffer {
101102
});
102103
}
103104

104-
protected readonly DynamicDrawUsage = DynamicDrawUsage;
105+
protected readonly DynamicDrawUsage = THREE.DynamicDrawUsage;
105106
}
106107

107-
const parentMatrix = new Matrix4();
108-
const position = new Vector3();
108+
const parentMatrix = new THREE.Matrix4();
109+
const position = new THREE.Vector3();
109110

110-
export interface NgtsPointsInstancesOptions extends Partial<NgtPoints> {
111+
export interface NgtsPointsInstancesOptions extends Partial<NgtThreeElements['ngt-points']> {
111112
range?: number;
112113
limit: number;
113114
}
@@ -157,7 +158,7 @@ export class NgtsPointsInstances {
157158
options = input(defaultInstancesOptions, { transform: mergeInputs(defaultInstancesOptions) });
158159
parameters = omit(this.options, ['limit', 'range']);
159160

160-
pointsRef = viewChild.required<ElementRef<Points>>('points');
161+
pointsRef = viewChild.required<ElementRef<THREE.Points>>('points');
161162

162163
private limit = pick(this.options, 'limit');
163164

@@ -224,5 +225,5 @@ export class NgtsPointsInstances {
224225
};
225226
}
226227

227-
protected readonly DynamicDrawUsage = DynamicDrawUsage;
228+
protected readonly DynamicDrawUsage = THREE.DynamicDrawUsage;
228229
}

libs/soba/performances/src/lib/points/position-point.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { ElementRef } from '@angular/core';
2-
import { NgtObject3DNode, resolveRef } from 'angular-three';
3-
import { Color, Group, Intersection, Matrix4, Points, Ray, Raycaster, Sphere, Vector3 } from 'three';
2+
import { NgtThreeElement, resolveRef } from 'angular-three';
3+
import * as THREE from 'three';
44

5-
export type NgtPositionPoint = NgtObject3DNode<PositionPoint, typeof PositionPoint>;
5+
export type NgtPositionPoint = NgtThreeElement<typeof PositionPoint>;
66

7-
const _inverseMatrix = new Matrix4();
8-
const _ray = new Ray();
9-
const _sphere = new Sphere();
10-
const _position = new Vector3();
7+
const _inverseMatrix = new THREE.Matrix4();
8+
const _ray = new THREE.Ray();
9+
const _sphere = new THREE.Sphere();
10+
const _position = new THREE.Vector3();
1111

12-
export class PositionPoint extends Group {
12+
export class PositionPoint extends THREE.Group {
1313
size: number;
14-
color: Color;
15-
instance: ElementRef<Points> | Points | null | undefined;
14+
color: THREE.Color;
15+
instance: ElementRef<THREE.Points> | THREE.Points | null | undefined;
1616

1717
constructor() {
1818
super();
1919
this.size = 0;
20-
this.color = new Color('white');
20+
this.color = new THREE.Color('white');
2121
this.instance = undefined;
2222
}
2323

@@ -26,7 +26,7 @@ export class PositionPoint extends Group {
2626
return resolveRef(this.instance)?.geometry;
2727
}
2828

29-
override raycast(raycaster: Raycaster, intersects: Intersection[]) {
29+
override raycast(raycaster: THREE.Raycaster, intersects: THREE.Intersection[]) {
3030
const parent = resolveRef(this.instance);
3131
if (!parent || !parent.geometry) return;
3232
const instanceId = parent.userData['instances'].indexOf(this);
@@ -45,13 +45,13 @@ export class PositionPoint extends Group {
4545
const rayPointDistanceSq = _ray.distanceSqToPoint(this.position);
4646

4747
if (rayPointDistanceSq < localThresholdSq) {
48-
const intersectPoint = new Vector3();
48+
const intersectPoint = new THREE.Vector3();
4949
_ray.closestPointToPoint(this.position, intersectPoint);
5050
intersectPoint.applyMatrix4(this.matrixWorld);
5151
const distance = raycaster.ray.origin.distanceTo(intersectPoint);
5252
if (distance < raycaster.near || distance > raycaster.far) return;
5353
intersects.push({
54-
distance: distance,
54+
distance,
5555
distanceToRay: Math.sqrt(rayPointDistanceSq),
5656
point: intersectPoint,
5757
index: instanceId,

libs/soba/performances/src/lib/segments/segment-object.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { NgtNode } from 'angular-three';
2-
import { Color, Vector3 } from 'three';
1+
import { NgtThreeElement } from 'angular-three';
2+
import * as THREE from 'three';
33

4-
export type NgtSegmentObject = NgtNode<SegmentObject, typeof SegmentObject>;
4+
export type NgtSegmentObject = NgtThreeElement<typeof SegmentObject>;
55

66
export class SegmentObject {
7-
color: Color;
8-
start: Vector3;
9-
end: Vector3;
7+
color: THREE.Color;
8+
start: THREE.Vector3;
9+
end: THREE.Vector3;
1010

1111
constructor() {
12-
this.color = new Color('white');
13-
this.start = new Vector3(0, 0, 0);
14-
this.end = new Vector3(0, 0, 0);
12+
this.color = new THREE.Color('white');
13+
this.start = new THREE.Vector3(0, 0, 0);
14+
this.end = new THREE.Vector3(0, 0, 0);
1515
}
1616
}
1717

0 commit comments

Comments
 (0)