1
1
import React , { Component , FunctionComponent , SyntheticEvent , useCallback , useEffect , useState , useRef } from 'react' ;
2
2
import { requireNativeComponent , ViewProps , AppState , AppStateStatus , View , Text , findNodeHandle , UIManager } from 'react-native' ;
3
- import { Camera } from '@babylonjs/core' ;
3
+ import { Camera , SceneInstrumentation } from '@babylonjs/core' ;
4
4
import { ensureInitialized } from './BabylonModule' ;
5
5
import { ReactNativeEngine } from './ReactNativeEngine' ;
6
6
@@ -25,10 +25,16 @@ export interface EngineViewCallbacks {
25
25
takeSnapshot : ( ) => Promise < string > ;
26
26
}
27
27
28
+ interface SceneStats {
29
+ frameRate : number ,
30
+ frameTime : number ,
31
+ }
32
+
28
33
export const EngineView : FunctionComponent < EngineViewProps > = ( props : EngineViewProps ) => {
29
34
const [ initialized , setInitialized ] = useState < boolean > ( ) ;
30
35
const [ appState , setAppState ] = useState ( AppState . currentState ) ;
31
- const [ fps , setFps ] = useState < number > ( ) ;
36
+ //const [fps, setFps] = useState<number>();
37
+ const [ sceneStats , setSceneStats ] = useState < SceneStats > ( ) ;
32
38
const engineViewRef = useRef < Component < NativeEngineViewProps > > ( null ) ;
33
39
const snapshotPromise = useRef < { promise : Promise < string > , resolve : ( data : string ) => void } > ( ) ;
34
40
@@ -74,21 +80,27 @@ export const EngineView: FunctionComponent<EngineViewProps> = (props: EngineView
74
80
75
81
useEffect ( ( ) => {
76
82
if ( props . camera && ( props . displayFrameRate ?? __DEV__ ) ) {
77
- const engine = props . camera . getScene ( ) . getEngine ( ) as ReactNativeEngine ;
83
+ const scene = props . camera . getScene ( ) ;
84
+ const engine = scene . getEngine ( ) as ReactNativeEngine ;
78
85
79
86
if ( ! engine . isDisposed ) {
80
- setFps ( engine . getFps ( ) ) ;
87
+ setSceneStats ( { frameRate : 0 , frameTime : 0 } ) ;
88
+
89
+ const sceneInstrumentation = new SceneInstrumentation ( scene ) ;
90
+ sceneInstrumentation . captureFrameTime = true ;
91
+
81
92
const timerHandle = setInterval ( ( ) => {
82
- setFps ( engine . getFps ( ) ) ;
93
+ setSceneStats ( { frameRate : engine . getFps ( ) , frameTime : sceneInstrumentation . frameTimeCounter . lastSecAverage } ) ;
83
94
} , 1000 ) ;
84
95
85
96
return ( ) => {
86
97
clearInterval ( timerHandle ) ;
98
+ setSceneStats ( undefined ) ;
99
+ sceneInstrumentation . dispose ( ) ;
87
100
} ;
88
101
}
89
102
}
90
103
91
- setFps ( undefined ) ;
92
104
return undefined ;
93
105
} , [ props . camera , props . displayFrameRate ] ) ;
94
106
@@ -137,7 +149,14 @@ export const EngineView: FunctionComponent<EngineViewProps> = (props: EngineView
137
149
return (
138
150
< View style = { [ { flex : 1 } , props . style , { overflow : "hidden" } ] } >
139
151
{ initialized && < NativeEngineView ref = { engineViewRef } style = { { flex : 1 } } onSnapshotDataReturned = { snapshotDataReturnedHandler } /> }
140
- { fps && < Text style = { { color : 'yellow' , position : 'absolute' , margin : 10 , right : 0 , top : 0 } } > FPS: { Math . round ( fps ) } </ Text > }
152
+ { sceneStats !== undefined &&
153
+ < View style = { { backgroundColor : '#00000040' , opacity : 1 , position : 'absolute' , right : 0 , left : 0 , top : 0 , flexDirection : 'row-reverse' } } >
154
+ < Text style = { { color : 'yellow' , alignSelf : 'flex-end' , margin : 3 , fontVariant : [ 'tabular-nums' ] } } > FPS: { sceneStats . frameRate . toFixed ( 0 ) } </ Text >
155
+ { /* Frame time seems wonky... it goes down when manipulating the scaling slider in the Playground app. Investigate this before showing it so we don't show data that can't be trusted. */ }
156
+ { /* <Text style={{ color: 'yellow', alignSelf: 'flex-end', margin: 3, fontVariant: ['tabular-nums'] }}> | </Text>
157
+ <Text style={{ color: 'yellow', alignSelf: 'flex-end', margin: 3, fontVariant: ['tabular-nums'] }}>Frame Time: {sceneStats.frameTime.toFixed(1)}ms</Text> */ }
158
+ </ View >
159
+ }
141
160
</ View >
142
161
) ;
143
162
} else {
0 commit comments