@@ -40,7 +40,8 @@ const uniformAdapter = createEffectAdapter<Uniforms>((uniforms, values) => {
4040const objectAdapter = createEffectAdapter < ThreeObject > ( setObjectValues )
4141const animateEffect = createEffectAnimate (
4242 [ uniformAdapter , objectAdapter ] ,
43- "Three.js uniform or object"
43+ "Three.js uniform or object" ,
44+ resolveThreeEffect
4445)
4546
4647/**
@@ -67,7 +68,7 @@ export function objectEffect(
6768}
6869
6970/**
70- * Animates MotionValues registered with a Three.js uniform or object effect .
71+ * Animates Three.js uniforms and flattened object properties .
7172 */
7273export function animate < T extends object > (
7374 subject : ThreeUniforms < T > ,
@@ -99,6 +100,96 @@ const transformMap = {
99100 scaleZ : [ "scale" , "z" ] ,
100101} as const
101102
103+ function resolveThreeEffect (
104+ subject : object ,
105+ key : string ,
106+ _target : unknown ,
107+ adapter : typeof uniformAdapter | typeof objectAdapter | undefined
108+ ) {
109+ const object = subject as ThreeObject
110+ const resolvedAdapter =
111+ adapter ??
112+ ( isUniform ( object [ key ] ) ? uniformAdapter : objectAdapter )
113+ const initial =
114+ resolvedAdapter === uniformAdapter
115+ ? getAnimatableValue ( ( object as Uniforms ) [ key ] ?. value )
116+ : getObjectValue ( object , key )
117+
118+ return initial === undefined
119+ ? undefined
120+ : { adapter : resolvedAdapter , initial }
121+ }
122+
123+ function getObjectValue ( object : ThreeObject , key : string ) {
124+ const transform = transformMap [ key as keyof typeof transformMap ]
125+
126+ if ( transform ) {
127+ const [ name , axis ] = transform
128+ const value = object [ name ] ?. [ axis ]
129+ return key . startsWith ( "rotate" ) && typeof value === "number"
130+ ? value * ( 180 / Math . PI )
131+ : getAnimatableValue ( value )
132+ }
133+
134+ if ( key === "scale" ) {
135+ return getAnimatableValue ( object . scale ?. x )
136+ }
137+
138+ const value =
139+ getProperty ( object , key ) ??
140+ getProperty ( object . material , key ) ??
141+ getUniformValue ( object . material ?. uniforms ?? object . uniforms , key ) ??
142+ getVectorComponent ( object , key ) ??
143+ getVectorComponent ( object . material , key ) ??
144+ getUniformComponent (
145+ object . material ?. uniforms ?? object . uniforms ,
146+ key
147+ )
148+
149+ return value
150+ }
151+
152+ function getProperty ( target : ThreeObject | undefined , key : string ) {
153+ return target && key in target
154+ ? getAnimatableValue ( target [ key ] )
155+ : undefined
156+ }
157+
158+ function getUniformValue ( uniforms : Uniforms | undefined , key : string ) {
159+ return getAnimatableValue ( uniforms ?. [ key ] ?. value )
160+ }
161+
162+ function getVectorComponent ( target : ThreeObject | undefined , key : string ) {
163+ const axis = key . slice ( - 1 ) . toLowerCase ( )
164+ const vector = target ?. [ key . slice ( 0 , - 1 ) ]
165+ return vector && [ "x" , "y" , "z" , "w" ] . includes ( axis )
166+ ? getAnimatableValue ( vector [ axis ] )
167+ : undefined
168+ }
169+
170+ function getUniformComponent ( uniforms : Uniforms | undefined , key : string ) {
171+ const uniform = uniforms ?. [ key . slice ( 0 , - 1 ) ]
172+ return uniform
173+ ? getVectorComponent (
174+ { value : uniform . value } ,
175+ `value${ key . slice ( - 1 ) } `
176+ )
177+ : undefined
178+ }
179+
180+ function getAnimatableValue ( value : unknown ) {
181+ if ( typeof value === "string" || typeof value === "number" ) return value
182+
183+ return value &&
184+ typeof ( value as ThreeObject ) . getStyle === "function"
185+ ? ( value as ThreeObject ) . getStyle ( )
186+ : undefined
187+ }
188+
189+ function isUniform ( value : unknown ) : value is ThreeUniform {
190+ return Boolean ( value && typeof value === "object" && "value" in value )
191+ }
192+
102193function setObjectValues ( object : ThreeObject , values : Record < string , unknown > ) {
103194 for ( const key in values ) {
104195 setObjectValue ( object , key , values [ key ] )
0 commit comments