Skip to content

Commit d43bd2c

Browse files
committed
refactor(soba): adjust staging with new renderer
1 parent d9bcca6 commit d43bd2c

20 files changed

+494
-539
lines changed

libs/soba/staging/src/lib/accumulative-shadows.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import {
1111
untracked,
1212
viewChild,
1313
} from '@angular/core';
14-
import { NgtGroup, extend, getLocalState, injectBeforeRender, injectStore, omit, pick } from 'angular-three';
14+
import { NgtThreeElements, extend, getInstanceState, injectBeforeRender, injectStore, omit, pick } from 'angular-three';
1515
import { ProgressiveLightMap, SoftShadowMaterial } from 'angular-three-soba/vanilla-exports';
1616
import { mergeInputs } from 'ngxtension/inject-inputs';
17+
import * as THREE from 'three';
1718
import { Group, Mesh, PlaneGeometry } from 'three';
1819

19-
export interface NgtsAccumulativeShadowsOptions extends Partial<NgtGroup> {
20+
export interface NgtsAccumulativeShadowsOptions extends Partial<NgtThreeElements['ngt-group']> {
2021
/** How many frames it can render, more yields cleaner results but takes more time, 40 */
2122
frames: number;
2223
/** If frames === Infinity blend controls the refresh ratio, 100 */
@@ -98,14 +99,11 @@ export class NgtsAccumulativeShadows {
9899
'toneMapped',
99100
]);
100101

101-
lightsRef = viewChild.required<ElementRef<Group>>('lights');
102-
planeRef = viewChild.required<ElementRef<Mesh<PlaneGeometry, InstanceType<typeof SoftShadowMaterial>>>>('plane');
102+
lightsRef = viewChild.required<ElementRef<THREE.Group>>('lights');
103+
planeRef =
104+
viewChild.required<ElementRef<THREE.Mesh<THREE.PlaneGeometry, InstanceType<typeof SoftShadowMaterial>>>>('plane');
103105

104106
private store = injectStore();
105-
private gl = this.store.select('gl');
106-
private camera = this.store.select('camera');
107-
private scene = this.store.select('scene');
108-
private invalidate = this.store.select('invalidate');
109107

110108
private opacity = pick(this.options, 'opacity');
111109
private alphaTest = pick(this.options, 'alphaTest');
@@ -117,22 +115,22 @@ export class NgtsAccumulativeShadows {
117115
if (this.previousPLM) {
118116
this.previousPLM.clear();
119117
}
120-
return (this.previousPLM = new ProgressiveLightMap(this.gl(), this.scene(), this.resolution()));
118+
return (this.previousPLM = new ProgressiveLightMap(this.store.gl(), this.store.scene(), this.resolution()));
121119
});
122120

123-
scale = pick(this.options, 'scale');
124-
toneMapped = pick(this.options, 'toneMapped');
125-
color = pick(this.options, 'color');
126-
colorBlend = pick(this.options, 'colorBlend');
127-
map = computed(() => this.pLM().progressiveLightMap2.texture);
121+
protected scale = pick(this.options, 'scale');
122+
protected toneMapped = pick(this.options, 'toneMapped');
123+
protected color = pick(this.options, 'color');
124+
protected colorBlend = pick(this.options, 'colorBlend');
125+
protected map = computed(() => this.pLM().progressiveLightMap2.texture);
128126

129127
lightsMap = new Map<string, () => void>();
130-
temporal = computed(() => !!this.options().temporal);
131-
frames = computed(() => Math.max(2, this.options().frames));
132-
blend = computed(() =>
128+
private temporal = computed(() => !!this.options().temporal);
129+
private frames = computed(() => Math.max(2, this.options().frames));
130+
private blend = computed(() =>
133131
Math.max(2, this.options().frames === Infinity ? this.options().blend : this.options().frames),
134132
);
135-
count = 0;
133+
private count = 0;
136134

137135
constructor() {
138136
extend({ Group, SoftShadowMaterial, Mesh, PlaneGeometry });
@@ -142,13 +140,13 @@ export class NgtsAccumulativeShadows {
142140
});
143141

144142
effect(() => {
145-
const sceneLS = getLocalState(this.scene());
146-
if (!sceneLS) return;
143+
const sceneInstanceState = getInstanceState(this.store.scene());
144+
if (!sceneInstanceState) return;
147145

148146
// track deps
149147
this.planeRef();
150148
this.options();
151-
sceneLS.objects();
149+
sceneInstanceState.objects();
152150

153151
// Reset internals, buffers, ...
154152
this.reset();
@@ -157,7 +155,12 @@ export class NgtsAccumulativeShadows {
157155
});
158156

159157
injectBeforeRender(() => {
160-
const [frames, temporal, invalidate, limit] = [this.frames(), !!this.temporal(), this.invalidate(), this.limit()];
158+
const [frames, temporal, invalidate, limit] = [
159+
this.frames(),
160+
!!this.temporal(),
161+
this.store.snapshot.invalidate,
162+
this.limit(),
163+
];
161164
if ((temporal || frames === Infinity) && this.count < frames && this.count < limit) {
162165
invalidate();
163166
this.update();
@@ -202,7 +205,7 @@ export class NgtsAccumulativeShadows {
202205
// Update the lightmap and the accumulative lights
203206
for (let i = 0; i < frames; i++) {
204207
this.lightsMap.forEach((lightUpdate) => lightUpdate());
205-
this.pLM().update(this.camera(), this.blend());
208+
this.pLM().update(this.store.camera(), this.blend());
206209
}
207210
// Switch lights off
208211
this.lightsRef().nativeElement.visible = false;

libs/soba/staging/src/lib/backdrop.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import {
77
input,
88
viewChild,
99
} from '@angular/core';
10-
import { checkUpdate, extend, NgtArgs, NgtGroup, omit, pick } from 'angular-three';
10+
import { checkUpdate, extend, NgtArgs, NgtThreeElements, omit, pick } from 'angular-three';
1111
import { mergeInputs } from 'ngxtension/inject-inputs';
12-
import { BufferAttribute, Group, Mesh, PlaneGeometry } from 'three';
12+
import * as THREE from 'three';
13+
import { Group, Mesh, PlaneGeometry } from 'three';
1314

1415
function easeInExpo(x: number) {
1516
return x === 0 ? 0 : Math.pow(2, 10 * x - 10);
1617
}
1718

18-
export interface NgtsBackdropOptions extends Partial<NgtGroup> {
19+
export interface NgtsBackdropOptions extends Partial<NgtThreeElements['ngt-group']> {
1920
floor: number;
2021
segments: number;
2122
receiveShadow?: boolean;
@@ -44,13 +45,13 @@ export class NgtsBackdrop {
4445
protected readonly Math = Math;
4546

4647
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
47-
parameters = omit(this.options, ['floor', 'segments', 'receiveShadow']);
48+
protected parameters = omit(this.options, ['floor', 'segments', 'receiveShadow']);
4849

49-
groupRef = viewChild.required<ElementRef<Group>>('group');
50-
planeRef = viewChild<ElementRef<PlaneGeometry>>('plane');
50+
groupRef = viewChild.required<ElementRef<THREE.Group>>('group');
51+
planeRef = viewChild<ElementRef<THREE.PlaneGeometry>>('plane');
5152

52-
receiveShadow = pick(this.options, 'receiveShadow');
53-
segments = pick(this.options, 'segments');
53+
protected receiveShadow = pick(this.options, 'receiveShadow');
54+
protected segments = pick(this.options, 'segments');
5455
private floor = pick(this.options, 'floor');
5556

5657
constructor() {
@@ -64,7 +65,7 @@ export class NgtsBackdrop {
6465

6566
let i = 0;
6667
const offset = segments / segments / 2;
67-
const position = plane.attributes['position'] as BufferAttribute;
68+
const position = plane.attributes['position'] as THREE.BufferAttribute;
6869
for (let x = 0; x < segments + 1; x++) {
6970
for (let y = 0; y < segments + 1; y++) {
7071
position.setXYZ(

libs/soba/staging/src/lib/bb-anchor.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ import {
88
signal,
99
viewChild,
1010
} from '@angular/core';
11-
import { NgtGroup, NgtVector3, extend, getLocalState, injectBeforeRender, omit, vector3 } from 'angular-three';
12-
import { Box3, Group, Object3D, Vector3 } from 'three';
11+
import {
12+
NgtThreeElements,
13+
NgtVector3,
14+
extend,
15+
getInstanceState,
16+
injectBeforeRender,
17+
omit,
18+
vector3,
19+
} from 'angular-three';
20+
import * as THREE from 'three';
21+
import { Group } from 'three';
1322

14-
const boundingBox = new Box3();
15-
const boundingBoxSize = new Vector3();
23+
const boundingBox = new THREE.Box3();
24+
const boundingBoxSize = new THREE.Vector3();
1625

17-
export interface NgtsBBAnchorOptions extends Partial<NgtGroup> {
26+
export interface NgtsBBAnchorOptions extends Partial<NgtThreeElements['ngt-group']> {
1827
anchor: NgtVector3;
1928
}
2029

@@ -32,9 +41,9 @@ export class NgtsBBAnchor {
3241
options = input.required<NgtsBBAnchorOptions>();
3342
parameters = omit(this.options, ['anchor']);
3443

35-
bbAnchorRef = viewChild.required<ElementRef<Group>>('bbAnchor');
44+
bbAnchorRef = viewChild.required<ElementRef<THREE.Group>>('bbAnchor');
3645

37-
parent = signal<Object3D | null>(null);
46+
private parent = signal<THREE.Object3D | null>(null);
3847
private anchor = vector3(this.options, 'anchor');
3948

4049
constructor() {
@@ -44,11 +53,11 @@ export class NgtsBBAnchor {
4453
// so it becomes a sibling of its initial parent.
4554
// We do that so the children have no impact on a bounding box of a parent.
4655
effect(() => {
47-
const bbAnchorLS = getLocalState(this.bbAnchorRef().nativeElement);
48-
const bbAnchorParent = bbAnchorLS?.parent();
49-
if (bbAnchorParent?.parent) {
50-
this.parent.set(bbAnchorParent);
51-
bbAnchorParent.parent.add(this.bbAnchorRef().nativeElement);
56+
const bbAnchorInstanceState = getInstanceState(this.bbAnchorRef().nativeElement);
57+
const bbAnchorParent = bbAnchorInstanceState?.parent();
58+
if (bbAnchorParent && 'parent' in bbAnchorParent) {
59+
this.parent.set(bbAnchorParent as unknown as THREE.Object3D);
60+
bbAnchorParent['parent'].add(this.bbAnchorRef().nativeElement);
5261
}
5362
});
5463

0 commit comments

Comments
 (0)