1- import React , { Component , FunctionComponent , SyntheticEvent , useCallback , useEffect , useState , useRef } from 'react' ;
2- import { requireNativeComponent , ViewProps , AppState , AppStateStatus , View , Text , findNodeHandle , UIManager } from 'react-native' ;
1+ import React , { Component , FunctionComponent , SyntheticEvent , useCallback , useEffect , useState , useRef , useMemo } from 'react' ;
2+ import { ViewProps , View , Text , findNodeHandle , UIManager } from 'react-native' ;
33import { Camera , SceneInstrumentation } from '@babylonjs/core' ;
4- import { ensureInitialized } from './BabylonModule' ;
54import { ReactNativeEngine } from './ReactNativeEngine' ;
6-
7- declare const global : any ;
8-
9- interface NativeEngineViewProps extends ViewProps {
10- isTransparent : boolean ;
11- onSnapshotDataReturned : ( event : SyntheticEvent ) => void ;
12- }
13-
14- const NativeEngineView : {
15- prototype : Component < NativeEngineViewProps > ;
16- new ( props : Readonly < NativeEngineViewProps > ) : Component < NativeEngineViewProps > ;
17- } = global [ 'EngineView' ] || ( global [ 'EngineView' ] = requireNativeComponent ( 'EngineView' ) ) ;
5+ import { useModuleInitializer , useRenderLoop } from './NativeEngineHook' ;
6+ import { NativeEngineViewProps , NativeEngineView } from './NativeEngineView' ;
187
198export interface EngineViewProps extends ViewProps {
209 camera ?: Camera ;
@@ -33,53 +22,25 @@ interface SceneStats {
3322}
3423
3524export const EngineView : FunctionComponent < EngineViewProps > = ( props : EngineViewProps ) => {
36- const [ initialized , setInitialized ] = useState < boolean > ( ) ;
37- const [ appState , setAppState ] = useState ( AppState . currentState ) ;
3825 //const [fps, setFps] = useState<number>();
3926 const [ sceneStats , setSceneStats ] = useState < SceneStats > ( ) ;
4027 const engineViewRef = useRef < Component < NativeEngineViewProps > > ( null ) ;
4128 const snapshotPromise = useRef < { promise : Promise < string > , resolve : ( data : string ) => void } > ( ) ;
4229 const isTransparent = props . isTransparent || false
4330
44- useEffect ( ( ) => {
45- ( async ( ) => {
46- setInitialized ( await ensureInitialized ( ) ) ;
47- } ) ( ) ;
48- } , [ ] ) ;
31+ const initialized = useModuleInitializer ( ) ;
4932
50- useEffect ( ( ) => {
51- const onAppStateChanged = ( appState : AppStateStatus ) => {
52- setAppState ( appState ) ;
53- } ;
54-
55- AppState . addEventListener ( "change" , onAppStateChanged ) ;
33+ const engine = useMemo ( ( ) => {
34+ return props . camera ?. getScene ( ) . getEngine ( ) as ReactNativeEngine ;
35+ } , [ props . camera ] ) ;
5636
57- return ( ) => {
58- AppState . removeEventListener ( "change" , onAppStateChanged ) ;
37+ const renderLoop = useCallback ( ( ) => {
38+ for ( let scene of engine . scenes ) {
39+ scene . render ( ) ;
5940 }
60- } , [ ] ) ;
61-
62- useEffect ( ( ) => {
63- if ( props . camera && appState === "active" ) {
64- const engine = props . camera . getScene ( ) . getEngine ( ) as ReactNativeEngine ;
41+ } , [ engine ] ) ;
6542
66- if ( ! engine . isDisposed ) {
67- engine . runRenderLoop ( ( ) => {
68- for ( let scene of engine . scenes ) {
69- scene . render ( ) ;
70- }
71- } ) ;
72-
73- return ( ) => {
74- if ( ! engine . isDisposed ) {
75- engine . stopRenderLoop ( ) ;
76- }
77- } ;
78- }
79- }
80-
81- return undefined ;
82- } , [ props . camera , appState ] ) ;
43+ useRenderLoop ( engine , renderLoop ) ;
8344
8445 useEffect ( ( ) => {
8546 if ( props . camera && ( props . displayFrameRate ?? __DEV__ ) ) {
0 commit comments