This chapter describes the Light API included in the JavaFX 3D graphics library.
Light is now also defined as a node in the scene graph. A default light is provided if the set of active lights contained in the scene is empty. Each light contains a set of affected nodes. If a set of nodes is empty, all nodes on the scene (or subscene) are affected. If a Parent node is in that set of nodes, then all of its children are also affected.
Light interacts with the geometry of a Shape3D object and its material to provide rendering result. Currently, there are two types of light sources:
AmbientLight- a light source that seems to come from all directions.
PointLight- a light source that has a fixed point in space and radiates light equally in all directions away from itself.
Example 5-1 shows the Light Class Hierarchy.
Example 5-1 Light Class Hierarchy
javafx.scene.Node javafx.scene.LightBase (abstract) javafx.scene.AmbientLight javafx.scene.PointLight
Creating and Using Light
To create point light and add it to the Scene, do the following:
PointLight light = new PointLight(); light.setColor(Color.RED);
Use the following to add light to the scene graph:
Group lightGroup = new Group(); lightGroup.getChildren().add(light); root.getChildren().add(lightGroup);
Rotate the light 45 degrees with the following line of code:
To move the lightGroup and have light moves with it, use something similar to the following code.
setTranslateZ() method sets the value of the property
translateZ, which is set to -75 in the example code above. This value will be added to any translation defined by the transforms
Example 5-2 MSAAApp.java Code Snippet Using PointLight API
... PointLight light = new PointLight(Color.WHITE); light.setTranslateX(50); light.setTranslateY(-300); light.setTranslateZ(-400); PointLight light2 = new PointLight(Color.color(0.6, 0.3, 0.4)); light2.setTranslateX(400); light2.setTranslateY(0); light2.setTranslateZ(-400); AmbientLight ambientLight = new AmbientLight(Color.color(0.2, 0.2, 0.2)); node.setRotationAxis(new Point3D(2, 1, 0).normalize()); node.setTranslateX(180); node.setTranslateY(180); root.getChildren().addAll(setTitle(title), ambientLight, light, light2, node); ...