Skip to content

Commit df00fb7

Browse files
committed
refactor(soba): adjust misc with new renderer
1 parent 41ca5f5 commit df00fb7

File tree

11 files changed

+233
-217
lines changed

11 files changed

+233
-217
lines changed

libs/soba/misc/src/lib/animations.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
1-
import { computed, effect, ElementRef, Injector, isSignal, Signal, signal, untracked } from '@angular/core';
1+
import { computed, effect, ElementRef, Injector, isSignal, signal, untracked } from '@angular/core';
22
import { injectBeforeRender, resolveRef } from 'angular-three';
33
import { assertInjector } from 'ngxtension/assert-injector';
4-
import { AnimationAction, AnimationClip, AnimationMixer, Object3D } from 'three';
4+
import * as THREE from 'three';
55

66
/**
77
* name: any to allow consumers to pass in type-safe animation clips
88
*/
9-
type NgtsAnimationClipWithoutName = Omit<AnimationClip, 'name'> & { name: any };
9+
type NgtsAnimationClipWithoutName = Omit<THREE.AnimationClip, 'name'> & { name: any };
1010
export type NgtsAnimationClip = Omit<NgtsAnimationClipWithoutName, 'clone'> & { clone: () => NgtsAnimationClip };
1111
export type NgtsAnimationClips<TAnimationNames extends string> = {
1212
[Name in TAnimationNames]: Omit<NgtsAnimationClip, 'name'> & { name: Name };
1313
}[TAnimationNames];
1414
export type NgtsAnimationApi<T extends NgtsAnimationClip> = {
15-
/**
16-
* Whether or not the animations finishes initialized
17-
*
18-
* @deprecated 3.5.0 - use `isReady` getter for better type-narrow instead. Will be removed in 4.0.0
19-
*/
20-
ready: Signal<boolean>;
2115
clips: T[];
22-
mixer: AnimationMixer;
16+
mixer: THREE.AnimationMixer;
2317
names: T['name'][];
24-
} & ({ get isReady(): true; actions: { [key in T['name']]: AnimationAction } } | { get isReady(): false });
18+
} & (
19+
| {
20+
/**
21+
* Whether or not the animations finishes initialized
22+
*/
23+
get isReady(): true;
24+
actions: { [key in T['name']]: THREE.AnimationAction };
25+
}
26+
| {
27+
/**
28+
* Whether or not the animations finishes initialized
29+
*/
30+
get isReady(): false;
31+
}
32+
);
2533

2634
export type NgtsAnimation<TAnimation extends NgtsAnimationClip = NgtsAnimationClip> =
2735
| TAnimation[]
@@ -32,18 +40,21 @@ export type NgtsAnimation<TAnimation extends NgtsAnimationClip = NgtsAnimationCl
3240
*/
3341
export function injectAnimations<TAnimation extends NgtsAnimationClip>(
3442
animations: () => NgtsAnimation<TAnimation> | undefined | null,
35-
object: ElementRef<Object3D> | Object3D | (() => ElementRef<Object3D> | Object3D | undefined | null),
43+
object:
44+
| ElementRef<THREE.Object3D>
45+
| THREE.Object3D
46+
| (() => ElementRef<THREE.Object3D> | THREE.Object3D | undefined | null),
3647
{ injector }: { injector?: Injector } = {},
3748
): NgtsAnimationApi<TAnimation> {
3849
return assertInjector(injectAnimations, injector, () => {
39-
const mixer = new AnimationMixer(null!);
50+
const mixer = new THREE.AnimationMixer(null!);
4051
injectBeforeRender(({ delta }) => {
4152
if (!mixer.getRoot()) return;
4253
mixer.update(delta);
4354
});
4455

45-
let cached = {} as Record<string, AnimationAction>;
46-
const actions = {} as { [key in TAnimation['name']]: AnimationAction };
56+
let cached = {} as Record<string, THREE.AnimationAction>;
57+
const actions = {} as { [key in TAnimation['name']]: THREE.AnimationAction };
4758
const clips = [] as NgtsAnimationApi<TAnimation>['clips'];
4859
const names = [] as NgtsAnimationApi<TAnimation>['names'];
4960

@@ -58,8 +69,9 @@ export function injectAnimations<TAnimation extends NgtsAnimationClip>(
5869
const ready = signal(false);
5970

6071
effect((onCleanup) => {
61-
const obj = actualObject() as Object3D | undefined;
72+
const obj = actualObject() as THREE.Object3D | undefined;
6273
if (!obj) return;
74+
6375
Object.assign(mixer, { _root: obj });
6476

6577
const maybeAnimationClips = animations();
@@ -98,7 +110,7 @@ export function injectAnimations<TAnimation extends NgtsAnimationClip>(
98110
mixer.stopAllAction();
99111
// uncache actions
100112
Object.values(actions).forEach((action) => {
101-
mixer.uncacheAction(action as AnimationClip, obj);
113+
mixer.uncacheAction(action as THREE.AnimationClip, obj);
102114
});
103115
});
104116
});

libs/soba/misc/src/lib/bake-shadows.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import { injectStore } from 'angular-three';
55
export class NgtsBakeShadows {
66
constructor() {
77
const store = injectStore();
8-
const gl = store.select('gl');
98
effect((onCleanup) => {
10-
const _gl = gl();
11-
_gl.shadowMap.autoUpdate = false;
12-
_gl.shadowMap.needsUpdate = true;
9+
const gl = store.gl();
10+
gl.shadowMap.autoUpdate = false;
11+
gl.shadowMap.needsUpdate = true;
1312
onCleanup(() => {
14-
_gl.shadowMap.autoUpdate = _gl.shadowMap.needsUpdate = true;
13+
gl.shadowMap.autoUpdate = gl.shadowMap.needsUpdate = true;
1514
});
1615
});
1716
}

libs/soba/misc/src/lib/computed-attribute.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
input,
88
viewChild,
99
} from '@angular/core';
10-
import { NgtArgs, NgtBufferAttribute, getLocalState } from 'angular-three';
11-
import { BufferAttribute, BufferGeometry } from 'three';
10+
import { NgtArgs, NgtThreeElements, getInstanceState } from 'angular-three';
11+
import * as THREE from 'three';
1212

1313
@Component({
1414
selector: 'ngts-computed-attribute',
@@ -27,22 +27,22 @@ import { BufferAttribute, BufferGeometry } from 'three';
2727
* and attaches the attribute to the geometry.
2828
*/
2929
export class NgtsComputedAttribute {
30-
compute = input.required<(geometry: BufferGeometry) => BufferAttribute>();
30+
compute = input.required<(geometry: THREE.BufferGeometry) => THREE.BufferAttribute>();
3131
name = input.required<string>();
32-
options = input({} as Partial<NgtBufferAttribute>);
32+
options = input({} as Partial<NgtThreeElements['ngt-buffer-geometry']>);
3333

34-
bufferAttribute = new BufferAttribute(new Float32Array(0), 1);
35-
attributeRef = viewChild<ElementRef<BufferAttribute>>('attribute');
34+
protected bufferAttribute = new THREE.BufferAttribute(new Float32Array(0), 1);
35+
attributeRef = viewChild<ElementRef<THREE.BufferAttribute>>('attribute');
3636

3737
constructor() {
3838
effect(() => {
3939
const bufferAttribute = this.attributeRef()?.nativeElement;
4040
if (!bufferAttribute) return;
4141

42-
const localState = getLocalState(bufferAttribute);
43-
if (!localState) return;
42+
const instanceState = getInstanceState(bufferAttribute);
43+
if (!instanceState) return;
4444

45-
const geometry = ((bufferAttribute as any).parent as BufferGeometry) ?? localState.parent();
45+
const geometry = ((bufferAttribute as any).parent as THREE.BufferGeometry) ?? instanceState.parent();
4646

4747
const attribute = this.compute()(geometry);
4848
bufferAttribute.copy(attribute);

libs/soba/misc/src/lib/decal.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import {
77
input,
88
viewChild,
99
} from '@angular/core';
10-
import { applyProps, extend, getLocalState, NgtMesh, omit, pick, resolveRef } from 'angular-three';
10+
import { applyProps, extend, getInstanceState, is, NgtThreeElements, omit, pick, resolveRef } from 'angular-three';
1111
import { mergeInputs } from 'ngxtension/inject-inputs';
12-
import { AxesHelper, BoxGeometry, Euler, Mesh, MeshNormalMaterial, Object3D, Texture, Vector3 } from 'three';
12+
import * as THREE from 'three';
13+
import { AxesHelper, BoxGeometry, Mesh, MeshNormalMaterial } from 'three';
1314
import { DecalGeometry } from 'three-stdlib';
1415

15-
export interface NgtsDecalOptions extends Partial<NgtMesh> {
16+
export interface NgtsDecalOptions extends Partial<NgtThreeElements['ngt-mesh']> {
1617
polygonOffsetFactor: number;
1718
depthTest: boolean;
1819
debug: boolean;
19-
map?: Texture | null;
20+
map?: THREE.Texture | null;
2021
}
2122

2223
const defaultOptions: NgtsDecalOptions = {
@@ -42,7 +43,7 @@ const defaultOptions: NgtsDecalOptions = {
4243
@if (debug()) {
4344
<ngt-mesh #helper>
4445
<ngt-box-geometry />
45-
<ngt-mesh-normal-material [wireframe]="true" />
46+
<ngt-mesh-normal-material wireframe />
4647
<ngt-axes-helper />
4748
</ngt-mesh>
4849
}
@@ -52,9 +53,9 @@ const defaultOptions: NgtsDecalOptions = {
5253
changeDetection: ChangeDetectionStrategy.OnPush,
5354
})
5455
export class NgtsDecal {
55-
mesh = input<ElementRef<Mesh> | Mesh | null>();
56+
mesh = input<ElementRef<THREE.Mesh> | THREE.Mesh | null>();
5657
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
57-
parameters = omit(this.options, [
58+
protected parameters = omit(this.options, [
5859
'debug',
5960
'map',
6061
'depthTest',
@@ -64,8 +65,8 @@ export class NgtsDecal {
6465
'rotation',
6566
]);
6667

67-
meshRef = viewChild.required<ElementRef<Mesh>>('mesh');
68-
helperRef = viewChild<ElementRef<Mesh>>('helper');
68+
meshRef = viewChild.required<ElementRef<THREE.Mesh>>('mesh');
69+
private helperRef = viewChild<ElementRef<THREE.Mesh>>('helper');
6970

7071
protected map = pick(this.options, 'map');
7172
protected depthTest = pick(this.options, 'depthTest');
@@ -80,22 +81,22 @@ export class NgtsDecal {
8081

8182
effect((onCleanup) => {
8283
const thisMesh = this.meshRef().nativeElement;
83-
const localState = getLocalState(thisMesh);
84-
if (!localState) return;
84+
const instanceState = getInstanceState(thisMesh);
85+
if (!instanceState) return;
8586

86-
const parent = resolveRef(this.mesh()) || localState.parent();
87+
const parent = resolveRef(this.mesh()) || instanceState.parent();
8788

88-
if (parent && !(parent as Mesh).isMesh) {
89+
if (parent && !is.three<THREE.Mesh>(parent, 'isMesh')) {
8990
throw new Error('<ngts-decal> must have a Mesh as parent or specify its "mesh" input');
9091
}
9192

9293
if (!parent) return;
9394

94-
const parentLocalState = getLocalState(parent);
95-
if (!parentLocalState) return;
95+
const parentInstanceState = getInstanceState(parent);
96+
if (!parentInstanceState) return;
9697

9798
// track parent's children
98-
const parentNonObjects = parentLocalState.nonObjects();
99+
const parentNonObjects = parentInstanceState.nonObjects();
99100
if (!parentNonObjects || !parentNonObjects.length) {
100101
return;
101102
}
@@ -106,7 +107,7 @@ export class NgtsDecal {
106107
}
107108

108109
const [position, rotation, scale] = [this.position(), this.rotation(), this.scale()];
109-
const state = { position: new Vector3(), rotation: new Euler(), scale: new Vector3(1, 1, 1) };
110+
const state = { position: new THREE.Vector3(), rotation: new THREE.Euler(), scale: new THREE.Vector3(1, 1, 1) };
110111

111112
applyProps(state, { position, scale });
112113

@@ -115,11 +116,11 @@ export class NgtsDecal {
115116
parent.matrixWorld.identity();
116117

117118
if (!rotation || typeof rotation === 'number') {
118-
const o = new Object3D();
119-
o.position.copy(state.position);
120-
o.lookAt(parent.position);
121-
if (typeof rotation === 'number') o.rotateZ(rotation);
122-
applyProps(state, { rotation: o.rotation });
119+
const obj = new THREE.Object3D();
120+
obj.position.copy(state.position);
121+
obj.lookAt(parent.position);
122+
if (typeof rotation === 'number') obj.rotateZ(rotation);
123+
applyProps(state, { rotation: obj.rotation });
123124
} else {
124125
applyProps(state, { rotation });
125126
}

libs/soba/misc/src/lib/deprecated.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { BufferAttribute } from 'three';
1+
import type * as THREE from 'three';
22

33
/**
44
* NOTE: Sets `BufferAttribute.updateRange` since r159.
55
*/
6-
export const setUpdateRange = (attribute: BufferAttribute, updateRange: { offset: number; count: number }): void => {
6+
export const setUpdateRange = (
7+
attribute: THREE.BufferAttribute,
8+
updateRange: { offset: number; count: number },
9+
): void => {
710
if ('updateRanges' in attribute) {
811
// attribute.updateRanges[0] = updateRange;
912
attribute.addUpdateRange(updateRange.offset, updateRange.count);

libs/soba/misc/src/lib/depth-buffer.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { computed, Injector } from '@angular/core';
22
import { injectBeforeRender, injectStore, pick } from 'angular-three';
33
import { assertInjector } from 'ngxtension/assert-injector';
4-
import { DepthFormat, DepthTexture, UnsignedShortType } from 'three';
4+
import * as THREE from 'three';
55
import { injectFBO } from './fbo';
66

77
export function injectDepthBuffer(
@@ -13,17 +13,17 @@ export function injectDepthBuffer(
1313
const frames = computed(() => params().frames || Infinity);
1414

1515
const store = injectStore();
16-
const width = store.select('size', 'width');
17-
const height = store.select('size', 'height');
18-
const dpr = store.select('viewport', 'dpr');
16+
// const width = store.select('size', 'width');
17+
// const height = store.select('size', 'height');
18+
// const dpr = store.select('viewport', 'dpr');
1919

20-
const w = computed(() => size() || width() * dpr());
21-
const h = computed(() => size() || height() * dpr());
20+
const w = computed(() => size() || store.size.width() * store.viewport.dpr());
21+
const h = computed(() => size() || store.size.height() * store.viewport.dpr());
2222

2323
const depthConfig = computed(() => {
24-
const depthTexture = new DepthTexture(w(), h());
25-
depthTexture.format = DepthFormat;
26-
depthTexture.type = UnsignedShortType;
24+
const depthTexture = new THREE.DepthTexture(w(), h());
25+
depthTexture.format = THREE.DepthFormat;
26+
depthTexture.type = THREE.UnsignedShortType;
2727
return { depthTexture };
2828
});
2929

0 commit comments

Comments
 (0)