UNPKG

react-native-filament

Version:

A real-time physically based 3D rendering engine for React Native

115 lines (107 loc) 4.29 kB
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } import React, { useEffect } from 'react'; import { RenderCallbackContext } from './RenderCallbackContext'; import { useAnimator } from '../hooks/useAnimator'; import { useSharedValue } from 'react-native-worklets-core'; import usePrevious from '../hooks/usePrevious'; import { ParentInstancesContext } from './ParentInstancesContext'; /** * Can be used to control the animation of a Model. * For pausing, playing, it is recommended to conditionally render the Animator component. * * @example * ```jsx * <Model source={DroneGlb}> * <Animator animationIndex={2} /> * </Model> * ``` */ export function Animator(props) { const instances = React.useContext(ParentInstancesContext); if (instances == null) { throw new Error('Animator must be used inside a <Model> or <ModelInstance> component.'); } const instance = instances[0]; if (instance == null) { // Should never happen throw new Error('No instances found for the parent Model component. This is a bug.'); } return /*#__PURE__*/React.createElement(AnimatorImpl, _extends({ instance: instance }, props)); } function AnimatorImpl({ instance, animationIndex: animationIndexProp = 0, transitionDuration = 0, onAnimationsLoaded }) { const animator = useAnimator(instance); // State for cross fading animations const prevAnimationIndex = useSharedValue(undefined); const prevAnimationStarted = useSharedValue(undefined); const animationInterpolation = useSharedValue(0); RenderCallbackContext.useRenderCallback(({ passedSeconds }) => { 'worklet'; if (animator == null) { return; } const animationIndex = typeof animationIndexProp === 'number' ? animationIndexProp : animationIndexProp.value; animator.applyAnimation(animationIndex, passedSeconds); // Eventually apply a cross fade if (prevAnimationIndex.value != null && transitionDuration > 0) { if (prevAnimationStarted.value == null) { prevAnimationStarted.value = passedSeconds; } animationInterpolation.value += passedSeconds - prevAnimationStarted.value; const alpha = animationInterpolation.value / transitionDuration; // Blend animations using a cross fade animator.applyCrossFade(prevAnimationIndex.value, prevAnimationStarted.value, alpha); // Reset the prev animation once the transition is completed if (alpha >= 1) { prevAnimationIndex.value = undefined; prevAnimationStarted.value = undefined; animationInterpolation.value = 0; } } animator.updateBoneMatrices(); }, [animator, animationIndexProp]); // Update prevAnimationIndex when animationIndexProp changes const previousAnimationIndexProp = usePrevious(animationIndexProp); useEffect(() => { // Update previous index if the prop is just a number: if (typeof animationIndexProp === 'number') { if (typeof previousAnimationIndexProp !== 'number') return; prevAnimationIndex.value = previousAnimationIndexProp; return; } // Update previous index if the prop is a shared value: let value = animationIndexProp.value; const removeListener = animationIndexProp.addListener(() => { prevAnimationIndex.value = value; value = animationIndexProp.value; }); return () => { removeListener(); }; }, [animationIndexProp, prevAnimationIndex, previousAnimationIndexProp]); // Get all animations and return them using the onAnimationsLoaded callback useEffect(() => { if (animator == null || onAnimationsLoaded == null) { return; } const animations = []; for (let i = 0; i < animator.getAnimationCount(); i++) { animations.push({ index: i, duration: animator.getAnimationDuration(i), name: animator.getAnimationName(i) }); } onAnimationsLoaded(animations); }, [animator, onAnimationsLoaded]); return null; } //# sourceMappingURL=Animator.js.map