Skip to content

Commit a8b11f4

Browse files
authored
Update Babylon Native to the latest (#289)
And improve FPS display
1 parent fa1d029 commit a8b11f4

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

Apps/Playground/App.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ const EngineScreen: FunctionComponent<ViewProps> = (props: ViewProps) => {
141141
<Image style={{flex: 1}} source={{uri: snapshotData }} />
142142
</View>
143143
}
144-
<EngineView camera={camera} onInitialized={onInitialized} />
144+
<EngineView camera={camera} onInitialized={onInitialized} displayFrameRate={true} />
145145
<Slider style={{position: 'absolute', minHeight: 50, margin: 10, left: 0, right: 0, bottom: 0}} minimumValue={0.2} maximumValue={2} step={0.01} value={defaultScale} onValueChange={setScale} />
146-
<Text style={{fontSize: 12, color: 'yellow', position: 'absolute', margin: 10}}>{trackingStateToString(trackingState)}</Text>
146+
<Text style={{color: 'yellow', position: 'absolute', margin: 3}}>{trackingStateToString(trackingState)}</Text>
147147
</View>
148148
}
149149
{ toggleView &&

Modules/@babylonjs/react-native/EngineView.tsx

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component, FunctionComponent, SyntheticEvent, useCallback, useEffect, useState, useRef } from 'react';
22
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';
44
import { ensureInitialized } from './BabylonModule';
55
import { ReactNativeEngine } from './ReactNativeEngine';
66

@@ -25,10 +25,16 @@ export interface EngineViewCallbacks {
2525
takeSnapshot: () => Promise<string>;
2626
}
2727

28+
interface SceneStats {
29+
frameRate: number,
30+
frameTime: number,
31+
}
32+
2833
export const EngineView: FunctionComponent<EngineViewProps> = (props: EngineViewProps) => {
2934
const [initialized, setInitialized] = useState<boolean>();
3035
const [appState, setAppState] = useState(AppState.currentState);
31-
const [fps, setFps] = useState<number>();
36+
//const [fps, setFps] = useState<number>();
37+
const [sceneStats, setSceneStats] = useState<SceneStats>();
3238
const engineViewRef = useRef<Component<NativeEngineViewProps>>(null);
3339
const snapshotPromise = useRef<{ promise: Promise<string>, resolve: (data: string) => void }>();
3440

@@ -74,21 +80,27 @@ export const EngineView: FunctionComponent<EngineViewProps> = (props: EngineView
7480

7581
useEffect(() => {
7682
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;
7885

7986
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+
8192
const timerHandle = setInterval(() => {
82-
setFps(engine.getFps());
93+
setSceneStats({frameRate: engine.getFps(), frameTime: sceneInstrumentation.frameTimeCounter.lastSecAverage});
8394
}, 1000);
8495

8596
return () => {
8697
clearInterval(timerHandle);
98+
setSceneStats(undefined);
99+
sceneInstrumentation.dispose();
87100
};
88101
}
89102
}
90103

91-
setFps(undefined);
92104
return undefined;
93105
}, [props.camera, props.displayFrameRate]);
94106

@@ -137,7 +149,14 @@ export const EngineView: FunctionComponent<EngineViewProps> = (props: EngineView
137149
return (
138150
<View style={[{ flex: 1 }, props.style, { overflow: "hidden" }]}>
139151
{ 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+
}
141160
</View>
142161
);
143162
} else {

0 commit comments

Comments
 (0)