@@ -3,17 +3,16 @@ import {
33 Directive ,
44 EmbeddedViewRef ,
55 Injector ,
6- Signal ,
6+ ResourceRef ,
77 TemplateRef ,
88 ViewContainerRef ,
99 computed ,
1010 effect ,
1111 inject ,
1212 input ,
13- output ,
1413 signal ,
1514} from '@angular/core' ;
16- import { injectTexture } from 'angular-three-soba/loaders' ;
15+ import { injectTexture , textureResource } from 'angular-three-soba/loaders' ;
1716import { assertInjector } from 'ngxtension/assert-injector' ;
1817import * as THREE from 'three' ;
1918
@@ -26,6 +25,10 @@ interface NgtsNormalTextureSettings {
2625 offset ?: number [ ] ;
2726}
2827
28+ /**
29+ * @deprecated Use normalTexture instead. Will be removed in v5.0.0
30+ * @since v4.0.0
31+ */
2932export function injectNormalTexture (
3033 id : ( ) => string | number = ( ) => 0 ,
3134 {
@@ -79,14 +82,68 @@ export function injectNormalTexture(
7982 } ) ;
8083}
8184
85+ export function normalTextureResource (
86+ id : ( ) => string | number = ( ) => 0 ,
87+ {
88+ settings = ( ) => ( { } ) ,
89+ onLoad,
90+ injector,
91+ } : { settings ?: ( ) => NgtsNormalTextureSettings ; onLoad ?: ( texture : THREE . Texture ) => void ; injector ?: Injector } ,
92+ ) {
93+ return assertInjector ( normalTextureResource , injector , ( ) => {
94+ const normalList = signal < Record < string , string > > ( { } ) ;
95+
96+ fetch ( LIST_URL )
97+ . then ( ( res ) => res . json ( ) )
98+ . then ( ( list ) => {
99+ normalList . set ( list ) ;
100+ } ) ;
101+
102+ const DEFAULT_NORMAL = computed ( ( ) => normalList ( ) [ 0 ] ) ;
103+ const numTot = computed ( ( ) => Object . keys ( normalList ( ) ) . length ) ;
104+
105+ const fileHash = computed ( ( ) => {
106+ const idValue = id ( ) ;
107+ if ( typeof idValue === 'string' ) {
108+ return idValue ;
109+ }
110+
111+ if ( typeof idValue === 'number' ) {
112+ return normalList ( ) [ idValue ] ;
113+ }
114+
115+ return null ;
116+ } ) ;
117+
118+ const imageName = computed ( ( ) => fileHash ( ) || DEFAULT_NORMAL ( ) ) ;
119+ const url = computed ( ( ) => `${ NORMAL_ROOT } /normals/${ imageName ( ) } ` ) ;
120+
121+ const resource = textureResource ( url , { onLoad } ) ;
122+
123+ effect ( ( ) => {
124+ if ( ! resource . hasValue ( ) ) return ;
125+
126+ const texture = resource . value ( ) ;
127+ const { anisotropy = 1 , repeat = [ 1 , 1 ] , offset = [ 0 , 0 ] } = settings ( ) ;
128+
129+ texture . wrapS = texture . wrapT = THREE . RepeatWrapping ;
130+ texture . repeat = new THREE . Vector2 ( repeat [ 0 ] , repeat [ 1 ] ) ;
131+ texture . offset = new THREE . Vector2 ( offset [ 0 ] , offset [ 1 ] ) ;
132+ texture . anisotropy = anisotropy ;
133+ } ) ;
134+
135+ return { url, resource, numTot } ;
136+ } ) ;
137+ }
138+
82139export interface NgtsNormalTextureOptions extends NgtsNormalTextureSettings {
83140 id ?: number | string ;
84141}
85142
86143@Directive ( { selector : 'ng-template[normalTexture]' } )
87144export class NgtsNormalTexture {
88145 normalTexture = input < NgtsNormalTextureOptions > ( ) ;
89- normalTextureLoaded = output < THREE . Texture [ ] > ( ) ;
146+ normalTextureLoaded = input < ( texture : THREE . Texture ) => void > ( ) ;
90147
91148 private template = inject ( TemplateRef ) ;
92149 private vcr = inject ( ViewContainerRef ) ;
@@ -97,16 +154,16 @@ export class NgtsNormalTexture {
97154 return settings ;
98155 } ) ;
99156
100- private ref ?: EmbeddedViewRef < { $implicit : Signal < THREE . Texture | null > } > ;
157+ private ref ?: EmbeddedViewRef < { $implicit : ResourceRef < THREE . Texture | undefined > } > ;
101158
102159 constructor ( ) {
103- const { texture } = injectNormalTexture ( this . id , {
160+ const { resource } = normalTextureResource ( this . id , {
104161 settings : this . settings ,
105- onLoad : this . normalTextureLoaded . emit . bind ( this . normalTextureLoaded ) ,
162+ onLoad : this . normalTextureLoaded ( ) ,
106163 } ) ;
107164
108165 effect ( ( ) => {
109- this . ref = this . vcr . createEmbeddedView ( this . template , { $implicit : texture } ) ;
166+ this . ref = this . vcr . createEmbeddedView ( this . template , { $implicit : resource } ) ;
110167 this . ref . detectChanges ( ) ;
111168 } ) ;
112169
@@ -118,7 +175,7 @@ export class NgtsNormalTexture {
118175 static ngTemplateContextGuard (
119176 _ : NgtsNormalTexture ,
120177 ctx : unknown ,
121- ) : ctx is { $implicit : Signal < THREE . Texture | null > } {
178+ ) : ctx is { $implicit : ResourceRef < THREE . Texture | undefined > } {
122179 return true ;
123180 }
124181}
0 commit comments