@@ -23,29 +23,59 @@ export default withSdk<Props>(({ sdk, entity: entityId }) => {
23
23
const entity = sdk . sceneContext . getEntityOrNull ( entityId )
24
24
const hasAnimator = useHasComponent ( entityId , Animator )
25
25
const [ componentValue , setComponentValue , isComponentEqual ] = useComponentValue < PBAnimator > ( entityId , Animator )
26
+ const [ gltfValue ] = useComponentValue ( entityId , GltfContainer )
26
27
27
28
const [ states , _ , updateStates , _2 , setStates ] = useArrayState < PBAnimationState > (
28
29
componentValue === null ? [ ] : componentValue . states
29
30
)
30
31
31
32
useEffect ( ( ) => {
32
- if ( isComponentEqual ( { states } ) ) return
33
- setComponentValue ( { states } )
34
- } , [ states ] )
33
+ if ( ! entity || ! gltfValue || hasAnimator ) return
34
+
35
+ const initializeComponent = async ( ) => {
36
+ try {
37
+ await initializeAnimatorComponent ( sdk , entityId , [ ] )
38
+ } catch ( error ) {
39
+ // eslint-disable-next-line no-console
40
+ console . warn ( 'Failed to initialize animator component:' , error )
41
+ }
42
+ }
43
+
44
+ void initializeComponent ( )
45
+ } , [ entity , gltfValue , hasAnimator ] )
46
+
47
+ useEffect ( ( ) => {
48
+ if ( ! entity || ! gltfValue || ! hasAnimator ) return
49
+
50
+ const loadAnimations = async ( ) => {
51
+ try {
52
+ const { animationGroups } = await entity . onGltfContainerLoaded ( )
53
+ if ( animationGroups . length && ( ! states . length || states [ 0 ] . clip !== animationGroups [ 0 ] . name ) ) {
54
+ const newStates = animationGroups . map ( ( $ ) => ( {
55
+ clip : $ . name ,
56
+ playing : false ,
57
+ weight : 1 ,
58
+ speed : 1 ,
59
+ loop : false ,
60
+ shouldReset : false
61
+ } ) )
62
+ setStates ( newStates )
63
+ }
64
+ } catch ( error ) {
65
+ // eslint-disable-next-line no-console
66
+ console . warn ( 'Failed to load animations:' , error )
67
+ }
68
+ }
69
+
70
+ void loadAnimations ( )
71
+ } , [ entity , gltfValue , hasAnimator , states ] )
35
72
36
73
useEffect ( ( ) => {
37
- if ( entity ) {
38
- entity
39
- . onGltfContainerLoaded ( )
40
- . then ( async ( { animationGroups } ) => {
41
- if ( animationGroups . length ) {
42
- const { states } = await initializeAnimatorComponent ( sdk , entityId , animationGroups )
43
- setStates ( states )
44
- }
45
- } )
46
- . catch ( ( ) => { } )
74
+ if ( isComponentEqual ( { states } ) ) {
75
+ return
47
76
}
48
- } , [ entity ] )
77
+ setComponentValue ( { states } )
78
+ } , [ states ] )
49
79
50
80
const handleRemove = useCallback ( async ( ) => {
51
81
sdk . operations . removeComponent ( entityId , Animator )
0 commit comments