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