In this chapter, we will see how to add lighting to the JavaFX 3D. Lighting is one of the most important parts in 3D application development since it decides what should be visible at a point of time.
JavaFX provides two options for lighting. One is AmbientLight and another one is PointLight.
Ambient Light
An ambient light radiates lights equally in all directions. When it is used, all the surfaces of the 3D object get equal amount of light. It can be considered as a natural default light.
Let’s see how we can add Ambient Light. You can give the color of the light by using setColor() method.
private LightBase prepareLightSource() { //Create light object AmbientLight light = new AmbientLight(); //Set light color light.setColor(Color.DEEPSKYBLUE); return light; }
Screenshot:-
Point Light
A point light can be considered as a light bulb that emits light in all directions. You can think of a point light as a sphere of light filling an area. Objects closer to the light will be brighter, and objects further away will be darker. JavaFX allows to add point light using javafx.scene.PointLight class.
private LightBase prepareLightSource() { //Create point light PointLight pointLight = new PointLight(); //Set light color pointLight.setColor(Color.RED); //Set location of light source pointLight.getTransforms().add(new Translate(0,-50,100)); }
Screenshot:-