Skip to content

Commit 41b95c4

Browse files
committed
fix(soba): adjust injectGLTF generics
now you can pass in a type that extends GLTF from three-stdlib that is your custom GLTF type ```ts interface YourGLTF extends GLTF { nodes: { some: Mesh } } gltf = injectGLTF<YourGLTF>(() => './your-model.glb'); gltf().nodes.some; // should be strongly typed ```
1 parent 29c432b commit 41b95c4

File tree

8 files changed

+34
-19
lines changed

8 files changed

+34
-19
lines changed

Diff for: libs/soba/loaders/src/lib/gltf-loader.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,25 @@ function _extensions(useDraco: boolean | string, useMeshOpt: boolean, extensions
2929
};
3030
}
3131

32-
function _injectGLTF<TUrl extends string | string[] | Record<string, string>>(
32+
type InjectGLTFUrl<TGltf extends GLTF | GLTF[] | Record<string, GLTF>> = TGltf extends GLTF
33+
? string
34+
: TGltf extends GLTF[]
35+
? string[]
36+
: TGltf extends Record<string, GLTF>
37+
? Record<string, string>
38+
: never;
39+
type InjectGLTFObjectMap<TGltf extends GLTF | GLTF[] | Record<string, GLTF>> = TGltf extends GLTF
40+
? TGltf & NgtObjectMap
41+
: TGltf extends Array<infer _GLTF extends GLTF>
42+
? Array<_GLTF & NgtObjectMap>
43+
: TGltf extends Record<string, infer _GLTF extends GLTF>
44+
? Record<string, _GLTF & NgtObjectMap>
45+
: never;
46+
47+
function _injectGLTF<
48+
TGltf extends GLTF | GLTF[] | Record<string, GLTF> = GLTF,
49+
TUrl extends string | string[] | Record<string, string> = InjectGLTFUrl<TGltf>,
50+
>(
3351
path: () => TUrl,
3452
{
3553
useDraco = true,
@@ -42,9 +60,9 @@ function _injectGLTF<TUrl extends string | string[] | Record<string, string>>(
4260
useMeshOpt?: boolean;
4361
injector?: Injector;
4462
extensions?: (loader: GLTFLoader) => void;
45-
onLoad?: (data: NgtLoaderResults<TUrl, GLTF & NgtObjectMap>) => void;
63+
onLoad?: (data: InjectGLTFObjectMap<TGltf>) => void;
4664
} = {},
47-
): Signal<NgtLoaderResults<TUrl, GLTF & NgtObjectMap> | null> & { scene: Signal<GLTF['scene'] | null> } {
65+
): Signal<InjectGLTFObjectMap<TGltf> | null> & { scene: Signal<GLTF['scene'] | null> } {
4866
return assertInjector(_injectGLTF, injector, () => {
4967
const result = injectLoader(() => GLTFLoader, path, {
5068
extensions: _extensions(useDraco, useMeshOpt, extensions),
@@ -61,7 +79,7 @@ function _injectGLTF<TUrl extends string | string[] | Record<string, string>>(
6179
});
6280

6381
return result;
64-
}) as Signal<NgtLoaderResults<TUrl, GLTF & NgtObjectMap> | null> & { scene: Signal<GLTF['scene'] | null> };
82+
}) as Signal<InjectGLTFObjectMap<TGltf> | null> & { scene: Signal<GLTF['scene'] | null> };
6583
}
6684

6785
_injectGLTF.preload = <TUrl extends string | string[] | Record<string, string>>(

Diff for: libs/soba/src/abstractions/grid.stories.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
input,
77
output,
88
signal,
9-
Signal,
109
} from '@angular/core';
1110
import { Meta } from '@storybook/angular';
1211
import { NgtAnyRecord, NgtArgs } from 'angular-three';
@@ -42,9 +41,9 @@ type SuzanneGLTF = GLTF & {
4241
changeDetection: ChangeDetectionStrategy.OnPush,
4342
})
4443
class Suzi {
45-
gltf = injectGLTF(
44+
gltf = injectGLTF<SuzanneGLTF>(
4645
() => 'https://vazxmixjsiawhamofees.supabase.co/storage/v1/object/public/models/suzanne-high-poly/model.gltf',
47-
) as Signal<SuzanneGLTF | null>;
46+
);
4847

4948
rotation = input([0, 0, 0]);
5049
scale = input(1);

Diff for: libs/soba/src/materials/mesh-portal-material.stories.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
CUSTOM_ELEMENTS_SCHEMA,
66
ElementRef,
77
input,
8-
Signal,
98
viewChild,
109
} from '@angular/core';
1110
import { Meta } from '@storybook/angular';
@@ -75,7 +74,7 @@ class Side {
7574
index = input.required<number>();
7675
attach = computed(() => ['material', this.index()]);
7776

78-
gltf = injectGLTF(() => './aobox-transformed.glb') as Signal<AOBoxGLTF | null>;
77+
gltf = injectGLTF<AOBoxGLTF>(() => './aobox-transformed.glb');
7978
shapeRef = viewChild<ElementRef<Mesh>>('shape');
8079

8180
constructor() {

Diff for: libs/soba/src/misc/animations.stories.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Component,
55
Directive,
66
ElementRef,
7-
Signal,
87
computed,
98
effect,
109
inject,
@@ -78,7 +77,7 @@ class DefaultAnimationsStory {
7877

7978
animation = input('Strut');
8079

81-
gltf = injectGLTF(() => './ybot.glb') as Signal<BotGLTF | null>;
80+
gltf = injectGLTF<BotGLTF>(() => './ybot.glb');
8281
matcapBody = injectMatcapTexture(() => '293534_B2BFC5_738289_8A9AA7');
8382
matcapJoints = injectMatcapTexture(() => '3A2412_A78B5F_705434_836C47');
8483

Diff for: libs/soba/src/performances/adaptive.stories.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, input, Signal } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, input } from '@angular/core';
22
import { Meta } from '@storybook/angular';
33
import { NgtArgs } from 'angular-three';
44
import { NgtsOrbitControls } from 'angular-three-soba/controls';
@@ -49,7 +49,7 @@ interface ArcherGLTF extends GLTF {
4949
class Archer {
5050
protected readonly Math = Math;
5151

52-
gltf = injectGLTF(() => './archer.glb') as Signal<ArcherGLTF | null>;
52+
gltf = injectGLTF<ArcherGLTF>(() => './archer.glb');
5353
}
5454

5555
@Component({

Diff for: libs/soba/src/performances/instances.stories.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, input, Signal, viewChild } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, input, viewChild } from '@angular/core';
22
import { Meta } from '@storybook/angular';
33
import { injectBeforeRender, injectObjectEvents, omit, pick } from 'angular-three';
44
import { NgtsOrbitControls } from 'angular-three-soba/controls';
@@ -119,7 +119,7 @@ class Shoes {
119119
data = data;
120120
range = input(100);
121121

122-
gltf = injectGLTF(() => './shoe.glb') as Signal<ShoeGLTF | null>;
122+
gltf = injectGLTF<ShoeGLTF>(() => './shoe.glb');
123123
}
124124

125125
@Component({

Diff for: libs/soba/src/staging/matcap-texture.stories.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, Signal, input } from '@angular/core';
1+
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, input } from '@angular/core';
22
import { Meta } from '@storybook/angular';
33
import { NgtArgs } from 'angular-three';
44
import { injectGLTF } from 'angular-three-soba/loaders';
@@ -28,7 +28,7 @@ interface SuzyGLTF extends GLTF {
2828
})
2929
class DefaultMatcapTextureStory {
3030
options = input<NgtsMatcapTextureOptions>();
31-
gltf = injectGLTF(() => './suzanne.glb', { useDraco: true }) as Signal<SuzyGLTF | null>;
31+
gltf = injectGLTF<SuzyGLTF>(() => './suzanne.glb', { useDraco: true });
3232
}
3333

3434
export default {

Diff for: libs/soba/src/staging/normal-texture.stories.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, Signal, computed, input } from '@angular/core';
1+
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, computed, input } from '@angular/core';
22
import { Meta } from '@storybook/angular';
33
import { injectGLTF } from 'angular-three-soba/loaders';
44
import { NgtsNormalTexture, NgtsNormalTextureOptions } from 'angular-three-soba/staging';
@@ -32,7 +32,7 @@ interface SuzyGLTF extends GLTF {
3232
})
3333
class DefaultNormalTextureStory {
3434
options = input<NgtsNormalTextureOptions>();
35-
gltf = injectGLTF(() => './suzanne.glb', { useDraco: true }) as Signal<SuzyGLTF | null>;
35+
gltf = injectGLTF<SuzyGLTF>(() => './suzanne.glb', { useDraco: true });
3636

3737
normalScale = computed(() => {
3838
const repeat = this.options()?.repeat;

0 commit comments

Comments
 (0)