A perfect java blog

Lights in JavaFX 3D

14 June,2013Narayan G. Maharjan

It has been so long day that I have not blogged but today I am here with some new stuffs related to the JavaFX 3D. Before touching in JavaFX it is better to introduce some lights definition. There are many lights types but let not go more we are relating only two lights

Requirement:

JDK 8 EA build ver. 91 or later (or JDK8 General Release Version)

Ambient Light
An ambient light source represents a fixed-intensity and fixed-color light source that affects all objects in the scene equally. Upon rendering, all objects in the scene are brightened with the specified intensity and color. This type of light source is mainly used to provide the scene with a basic view of the different objects in it.

ambient lights javafx

Point Light
Light originates from a single point, and spreads outward in all directions. This is like the torch light in a dark room.

point light

In javafx you can use many Lights like a node because all lights object are inherited from LightBase and the LightBase inherits from Node. All the 3D Objects needs Lights for making it more natural. Currently I have just explained 2 lights because the latest jdk8 early access build 91 contains only two lights PointLight,AmbientLight but I guess there will be more light added like AreaLight, HemisphereLight because JavaFX3D is still in development.

Code:

Before we start our Lights demo you need to know about the Material term which is widely used in 3D stuffs. JavaFX currently supports the PhongMaterial which is very important for 3D Object. Material helps 3D Object to get more natural by the help of lights. Lets see the usage of PhoneMaterial in Shape3D of JavaFX.

Material Adding in Cylinder

1

2

3

4

5

Cylinderc=newCylinder(200,200);

PhongMaterial material=newPhongMaterial();

material.setDiffuseColor(Color.DARKBLUE);

material.setSpecularColor(Color.WHITE);

c.setMaterial(material);

Point Light

1

2

PointLight light=newPointLight();

light.setColor(Color.WHITE);

Ambient Light

1

2

AmbientLight light=newAmbientLight();

light.setColor(Color.WHITE);

Ok Now lets make one small application which makes PointLight to a Sphere.