4

我的目标JavaFX.

一切都很好,除了一次改变这么多盒子高度时,它会减慢机器速度。

我决定通过按 y 轴缩放而不是改变高度来解决这个问题,以便可以缓存它们。现在它运行得非常顺畅,但是灯光关闭了。当盒子伸出时,它们似乎变暗了。

这只发生在缩放转换期间,如这里这里所示,而不是高度变化

我尝试了一些事情:环境照明,设置新的PhongMaterial每一帧并添加镜面反射颜色,以及改变它的力量。

代码(单灰色框)

public void start(Stage primaryStage) throws Exception{
    Group root= new Group();
    Scene scene = new Scene(root,400,400,true, SceneAntialiasing.BALANCED);
    PerspectiveCamera camera = new PerspectiveCamera(true);
    scene.setCamera(camera);
    camera.setFarClip(1000);
    Group trans = new Group();
    root.getChildren().add(trans);

    Box box = new Box(10,10,10);
    box.setCache(true);
    box.setCacheHint(CacheHint.SCALE);


    PhongMaterial material = new PhongMaterial(Color.GRAY);
    box.setMaterial(material);
    trans.getChildren().add(box);
    Scale scale = new Scale();
    box.getTransforms().add(scale);

    PointLight light = new PointLight(Color.WHITE);
    light.setTranslateZ(50);
    light.setTranslateX(-50);
    root.getChildren().add(light);
    light.setTranslateY(-25);



    new AnimationTimer(){
        @Override
        public void handle(long now) {
            scale.setY(Math.sin(angle)+2);
            angle += .05;
            //the line of code that works with the lighting, but doesn't support caching.
            //box.setHeight(40*Math.sin(angle)+50);
        }
    }.start();

    Rotate rotateX = new Rotate(0,Rotate.X_AXIS);
    Rotate rotateY = new Rotate(0,Rotate.Y_AXIS);

    trans.getTransforms().addAll(rotateX,rotateY);
    trans.setTranslateZ(200);
    rotateY.setAngle(45);
    rotateX.setAngle(35);

    primaryStage.setScene(scene);
    primaryStage.show();

}
4

0 回答 0