4

My goal is to reproduce a gif in JavaFX.

Everything is great, except for when changing so many box heights at once, it slows the machine down.

I have decided to remedy this by scaling by the y axis instead of altering the height so they can be cached. Now it runs perfectly smooth, but the lighting is off. When the boxes stretch out, they seem to get darker.

This only happens during the scaling transformation, as seen here and here, and not the height change.

I've tried a few things: ambient lighting, setting a new PhongMaterial each frame and adding a specular color, as well as changing it's power.

Code (singular grey box)

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();

}
  • I *think* the lighting change effect you are seeing comes from the fact that when you change the scale of the Shape3D object the texture coordinates of the underlying TriangleMesh are not automatically updated. This might be a subtle bug, maybe not. I'm not sure if swapping a new Material in will actually help because the texture coordinates of the underlying Mesh object of the Box object won't get updated. (again I think, I might be wrong). Because of private access issues like this, we created a replacement Box shape in the FXyz3d.org project. You might want to try that as a replacement. – Birdasaur Jun 20 '18 at 13:32
  • Changing the material isn't a bad idea but you might have to update the texture coordinates. Can you supply the code of how you are attempting to change the material? Regarding the light source, I would have thought that assigning the Box Shape3D to an Ambient light source would have mitigated this effect. Can you share how you did that? You might have missed something. – Birdasaur Jun 20 '18 at 13:37
  • I would put: 'box.setMaterial(new PhongMaterial(Color.GREY));' in the animation timer, but it doesn't work. Setting an ambient light would fix the issue, but as soon as the point light is reintroduced, the issues arises again, but fading to the ambient color, not black. I'll give FXyz a try. – Michael Coppola Jun 22 '18 at 00:46

0 Answers0