@@ -21,6 +21,11 @@ export type UniformAnimationTarget<T extends object> = {
2121 [ K in keyof T ] ?: string | number | Array < string | number | null >
2222}
2323
24+ export type UniformValueAnimationTarget =
25+ | string
26+ | number
27+ | Array < string | number | null >
28+
2429export interface ObjectEffectValues {
2530 [ key : string ] : MotionValue | undefined
2631}
@@ -68,8 +73,13 @@ export function objectEffect(
6873}
6974
7075/**
71- * Animates Three.js uniforms and flattened object properties.
76+ * Animates Three.js uniform nodes, uniforms and flattened object properties.
7277 */
78+ export function animate (
79+ subject : ThreeUniform ,
80+ keyframes : UniformValueAnimationTarget ,
81+ options ?: AnimationOptions
82+ ) : AnimationPlaybackControlsWithThen
7383export function animate < T extends object > (
7484 subject : ThreeUniforms < T > ,
7585 keyframes : UniformAnimationTarget < T > ,
@@ -82,10 +92,16 @@ export function animate(
8292) : AnimationPlaybackControlsWithThen
8393export function animate (
8494 subject : object ,
85- keyframes : ObjectAnimationTarget ,
95+ keyframes : UniformValueAnimationTarget | ObjectAnimationTarget ,
8696 options ?: AnimationOptions
8797) : AnimationPlaybackControlsWithThen {
88- return animateEffect ( subject , keyframes , options )
98+ return animateEffect (
99+ subject ,
100+ typeof keyframes === "object" && ! Array . isArray ( keyframes )
101+ ? keyframes
102+ : { value : keyframes } ,
103+ options
104+ )
89105}
90106
91107const transformMap = {
@@ -108,8 +124,7 @@ function resolveThreeEffect(
108124) {
109125 const object = subject as ThreeObject
110126 const resolvedAdapter =
111- adapter ??
112- ( isUniform ( object [ key ] ) ? uniformAdapter : objectAdapter )
127+ adapter ?? ( isUniform ( object [ key ] ) ? uniformAdapter : objectAdapter )
113128 const initial =
114129 resolvedAdapter === uniformAdapter
115130 ? getAnimatableValue ( ( object as Uniforms ) [ key ] ?. value )
@@ -135,24 +150,32 @@ function getObjectValue(object: ThreeObject, key: string) {
135150 return getAnimatableValue ( object . scale ?. x )
136151 }
137152
153+ const node = getNodeUniform ( object , key )
154+ if ( node ) return getAnimatableValue ( node . value )
155+
138156 const value =
139157 getProperty ( object , key ) ??
140158 getProperty ( object . material , key ) ??
141159 getUniformValue ( object . material ?. uniforms ?? object . uniforms , key ) ??
142160 getVectorComponent ( object , key ) ??
143161 getVectorComponent ( object . material , key ) ??
144- getUniformComponent (
145- object . material ?. uniforms ?? object . uniforms ,
146- key
147- )
162+ getUniformComponent ( object . material ?. uniforms ?? object . uniforms , key )
148163
149164 return value
150165}
151166
167+ /**
168+ * Resolves TSL uniform nodes assigned to node material slots, e.g.
169+ * material.colorNode = uniform(color). Non-uniform nodes are compiled
170+ * into the shader so can't be animated via their value.
171+ */
172+ function getNodeUniform ( object : ThreeObject , key : string ) {
173+ const node = object [ key + "Node" ] ?? object . material ?. [ key + "Node" ]
174+ return node ?. isUniformNode ? ( node as ThreeUniform ) : undefined
175+ }
176+
152177function getProperty ( target : ThreeObject | undefined , key : string ) {
153- return target && key in target
154- ? getAnimatableValue ( target [ key ] )
155- : undefined
178+ return target && key in target ? getAnimatableValue ( target [ key ] ) : undefined
156179}
157180
158181function getUniformValue ( uniforms : Uniforms | undefined , key : string ) {
@@ -170,18 +193,14 @@ function getVectorComponent(target: ThreeObject | undefined, key: string) {
170193function getUniformComponent ( uniforms : Uniforms | undefined , key : string ) {
171194 const uniform = uniforms ?. [ key . slice ( 0 , - 1 ) ]
172195 return uniform
173- ? getVectorComponent (
174- { value : uniform . value } ,
175- `value${ key . slice ( - 1 ) } `
176- )
196+ ? getVectorComponent ( { value : uniform . value } , `value${ key . slice ( - 1 ) } ` )
177197 : undefined
178198}
179199
180200function getAnimatableValue ( value : unknown ) {
181201 if ( typeof value === "string" || typeof value === "number" ) return value
182202
183- return value &&
184- typeof ( value as ThreeObject ) . getStyle === "function"
203+ return value && typeof ( value as ThreeObject ) . getStyle === "function"
185204 ? ( value as ThreeObject ) . getStyle ( )
186205 : undefined
187206}
@@ -212,6 +231,12 @@ function setObjectValue(object: ThreeObject, key: string, value: unknown) {
212231 return
213232 }
214233
234+ const node = getNodeUniform ( object , key )
235+ if ( node ) {
236+ setProperty ( node , "value" , value )
237+ return
238+ }
239+
215240 if ( setProperty ( object , key , value ) ) return
216241
217242 const material = object . material
0 commit comments