以30 fps运行的3D JavaFX应用程序

时间:2019-02-23 18:12:34

标签: javafx gluon gluon-mobile

我正在开发Gluon中的应用程序。目前,这只是一个简单的多维数据集。

我正在使用AnimationTimer处理游戏循环。当动画计时器可用时,我比较系统的纳秒来计算帧增量时间。我通过以下方式显示fps:1 / DeltaTime。

在桌面上,fpsLabel始终显示60 fps。但是,在移动设备上,我只能接收30 fps。我注意到控制台有时会写setSwapInterval(1),我知道这是OpenGL中的VSYNC设置。我的手机真的没有达到目标60 fps并受到限制吗?通过Gluon获得更直接的OpenGLES支持可能是有益的。

// Content pane
StackPane content = new StackPane();
content.setAlignment(Pos.TOP_LEFT);
content.setPadding(new Insets(8));
content.setPrefSize(Integer.MAX_VALUE, Integer.MAX_VALUE);
this.setCenter(content);

// Holds 3d objects
Group root3D = new Group();

// Scene to view the 3d objects
SubScene subScene = new SubScene(root3D, MobileApplication.getInstance().getScreenWidth(), MobileApplication.getInstance().getScreenHeight(), true, SceneAntialiasing.DISABLED);
content.getChildren().add(subScene);

// Create camera
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setNearClip(0.1);
camera.setFarClip(1000.0);
camera.setTranslateZ(-64);
root3D.getChildren().add(camera);

// Put camera in scene
Platform.runLater(()->{
    subScene.setCamera(camera);
    subScene.widthProperty().bind(MobileApplication.getInstance().getView().widthProperty());
    subScene.heightProperty().bind(MobileApplication.getInstance().getView().heightProperty());
});

// Put box in scene
Box box = new Box(8, 8, 8);
Rotate rxBox = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
boxRot = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
rxBox.setAngle(30);
boxRot.setAngle(50);
box.getTransforms().addAll(rxBox, boxRot);
root3D.getChildren().add(box);

// FPS Label
fpsLabel = new Label("Fps 60");
content.getChildren().add(fpsLabel);

1 个答案:

答案 0 :(得分:1)

在iOS上,CADisplayLink是AnimationTimer equivalent

  

一个计时器对象,允许您的应用程序将其图形与显示器的刷新率同步

如果您在JDK 1.8中检查源代码,则会在图形模块中的iOS的native-glass下找到此计时器的implementation

displayLink = [[UIScreen mainScreen] displayLinkWithTarget:[GlassTimer getDelegate]
                                                                  selector:@selector(displayLinkUpdate:)];
// 1 is 60hz, 2 is 30 Hz, 3 is 20 Hz ...
[displayLink setFrameInterval:2];

如您所见,设计上会限制您获得的FPS

虽然过去可能有该上限的原因,但我不认为它有技术上的原因,除了限制电池消耗外,您还会在移动设备中强制使用这种高速率,在大多数情况下,在这种情况下,30 fps绰绰有余。

我已将帧间隔修改为1(60 Hz),构建了iOS的SDK,并在iPhone上成功对其进行了测试:

但是,这当然不是应该修改的方式。该参数应该以某种方式对开发人员开放,并且需要某种API。

由于此限制也适用于OpenJFX JavaFX 11 code base,因此建议您提交issue,以便可以对其进行适当修改。