Lighting

Lighting effects in JavaFX create bright areas and shadows on objects producing a more realistic, 3D effect.

There are three main lighting types:
DistantLight A distant light source that produces a similar effect across the entire scene. (Think: the Sun)
PointLight A localized light source that emits light from a particular location in the 3D world. Shadows may vary between objects depending on each objects position relative to the light. (Think: a light in a room)
SpotLight The same as a PointLight but more directional. (Think: a desk light or a spotlight)

Key Variables

Apply to all lighting types:

light The lighting type to use (If not set, it will act as a DistantLight by default)
diffuseConstant ( See demo below )
specularConstant
specularExponent
surfaceScale

DistantLight variables

azimuth Orientation angle with respect to the light source. (Think: Facing the Sun or back to the Sun)
elevation Elevation angle of light source (Think: Position of the Sun on the horizon, time of day)
color Color of light (Think: Looking through colored glasses)

PointLight variables

x Position of light source, right/left on screen
y Position of light source, up/down on screen
z Position of light source, into/out of screen
color Color of light (Think: Looking through colored glasses)

SpotLight variables

x Position of light source, right/left on screen
y Position of light source, up/down on screen
z Position of light source, into/out of screen
pointsAtX Position to which light source is pointing, right/left on screen
pointsAtY Position to which light source is pointing, up/down on screen
pointsAtZ Position to which light source is pointing, into/out of screen
specularExponent Sharpness of the shadow at the lights edge
color Color of light (Think: Looking through colored glasses)

Distant Light Example

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.Lighting;
import javafx.scene.effect.light.DistantLight;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
 
 
Stage {
    title: "Lighting"
    scene: Scene{
        width: 300  height: 300
        content: [
            Rectangle {
                x: 40  y: 50
                width: 200  height: 200
                fill: Color.GREEN;
 
                effect: Lighting{
 
                    surfaceScale: 10 // Makes it "stick out" more
 
                    light: DistantLight{
                        azimuth: 90  // 90 degrees = light from bottom
                    }
                }
 
            }
        ]
    }
}



Lighting With Group

To apply the same lighting effect across multiple objects (like in the demo) put them in a Group and apply the lighting effect to the group.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.effect.Lighting;
import javafx.scene.effect.light.PointLight;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
 
 
Stage {
    title: "Lighting"
    scene: Scene{
        width: 300  height: 300
        content: [
            Group{
 
                effect: Lighting{
                    surfaceScale: 10 // Makes it "stick out" more
                    light: PointLight{
                        x:20  y:50  z:60
                    }
                }
 
                content: [
                    Rectangle {
                        x: 10  y: 10
                        width: 110  height: 150
                        fill: Color.GREEN;
                    }
                    Rectangle {
                        x: 190  y: 130
                        width: 80  height: 150
                        fill: Color.GREEN;
                    }
                ]
            }
        ]
    }
}




Interactive Demo

This interactive demo is designed to simplify the process of choosing lighting variable values. It shows the effect of changing each value and will let you tweak them to get exactly the lighting effect you are looking for.

JavaFX Lighting Demo