Skip to content

Commit

Permalink
CameraTransition: invalidate on change, pass options
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Dec 12, 2024
1 parent e4f6c21 commit 8670936
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/r3f/components/CameraTransition.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { forwardRef, useEffect, useMemo } from 'react';
import { useFrame, useThree } from '@react-three/fiber';
import { CameraTransitionManager } from '3d-tiles-renderer';
import { useDeepOptions } from '../utilities/useOptions';

export const CameraTransition = forwardRef( function CameraTransition( props, ref ) {

const { mode = 'perspective', onTransitionStart, onTransitionEnd, perspectiveCamera, orthographicCamera } = props;
const { mode = 'perspective', perspectiveCamera, orthographicCamera, ...options } = props;
const [ set, invalidate, controls, camera, size ] = useThree( state => [ state.set, state.invalidate, state.controls, state.camera, state.size ] );

// create the manager
Expand Down Expand Up @@ -84,29 +85,6 @@ export const CameraTransition = forwardRef( function CameraTransition( props, re

}, [ manager, set ] );

// register for events
useEffect( () => {

if ( onTransitionEnd ) {

manager.addEventListener( 'transition-end', onTransitionEnd );
return () => manager.removeEventListener( 'transition-end', onTransitionEnd );

}

}, [ onTransitionEnd, manager ] );

useEffect( () => {

if ( onTransitionStart ) {

manager.addEventListener( 'transition-start', onTransitionStart );
return () => manager.removeEventListener( 'transition-start', onTransitionStart );

}

}, [ onTransitionStart, manager ] );

// assign cameras
useEffect( () => {

Expand Down Expand Up @@ -156,6 +134,26 @@ export const CameraTransition = forwardRef( function CameraTransition( props, re

}, [ mode, manager, invalidate, controls ] );

// rerender the frame when the transition animates
useEffect( () => {

const callback = () => invalidate();
manager.addEventListener( 'transition-start', callback );
manager.addEventListener( 'transition-change', callback );
manager.addEventListener( 'transition-end', callback );

return () => {

manager.removeEventListener( 'transition-start', callback );
manager.removeEventListener( 'transition-change', callback );
manager.removeEventListener( 'transition-end', callback );

};

}, [ manager, invalidate ] );

useDeepOptions( manager, options );

// update animation
useFrame( () => {

Expand Down

0 comments on commit 8670936

Please sign in to comment.