1

I want to apply some lighting effects using JavaFX onto a canvas GraphicsContext. At first I used the Lighting.bumpInput to pass a static bump-map for lighting. However this only lights a certain area of my canvas correct.
So now I do the lighting manually by first drawing all bump-maps onto the canvas then applying my lighting effect and finally blending all the original images with BlendMode.MULTIPLY together with the lit bump-maps. This does not look as nice as the first approach but it does its job. Now there is still the problem that the lighting effect (Pointlight) is applied across the whole canvas no matter how big the area is.

enter image description here

public void start(Stage primaryStage) {
   Image texture = new Image("...");
   Image bumpMap = new Image("...");

   Light.Point pointLight = new Light.Point(256, 256, 25, Color.WHITE);
   Lighting lighting = new Lighting(pointLight);

   Canvas canvas = new Canvas(1280, 800);
   canvas.getGraphicsContext().setGlobalBlendMode(BlendMode.MULTIPLY);
   canvas.getGraphicsContext().drawImage(bumpMap, 0, 0);
   canvas.getGraphicsContext().applyEffect(lighting);
   canvas.getGraphicsContext().drawImage(texture, 0, 0);

   Scene scene = new Scene(new AnchorPane(canvas), 1280, 800);
   primaryStage.setScene(scene);
   primaryStage.show();
}

Now my question is, what is the proper way in JavaFX to apply the lighting only to a specific area on my canvas? Like to define the radius of my point light?

mrdlink
  • 266
  • 4
  • 15

1 Answers1

0

Well, the intensity of the light is proportional to 1/r³, wheras r is the distance (radius) vom the light source to the object. Hence you can affect the lit area by varying the radius, in your case the z-coordinate (=25) of your light source :

Light.Point pointLight = new Light.Point(256, 256, 25, Color.WHITE);

However, by doing this you will actually still have a steady transition of illumination on your canvas, there is no sharp border of illuminated and non-illuminated areas. No matter, how big you choose the value of r, the intensity will never be exactly zero.

If u want to have illumination only applied to a certain area of your canvas, I would recommend some sort of masking-workaround, i.e. duplicate your image and make for example a hole with Radius = r on it without applying lighting, then place it as overlay-mask onto your original image with all your lighting filters.

Andy
  • 488
  • 4
  • 13