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

Cylinder c = new Cylinder(200,200);
PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(Color.DARKBLUE);
material.setSpecularColor(Color.WHITE);
c.setMaterial(material);

 
Point Light

PointLight light = new PointLight();
light.setColor(Color.WHITE);

 
Ambient Light

AmbientLight light = new AmbientLight();
light.setColor(Color.WHITE);

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

 
PointLightDemo.java


import java.io.IOException;
import javafx.application.Application;
import javafx.scene.AmbientLight;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Shape3D;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

/**
 *
 * @author Narayan
 */
public class LightsDemo extends Application {
    
    /**
     * Making 3D Object
     * @param x
     * @param y
     * @param radius
     * @param height
     * @return 
     */
    public Shape3D make3DObject(double x, double y,double radius){
        Cylinder c = new Cylinder(radius,radius);
        //Creating PhongMaterial
        PhongMaterial material = new PhongMaterial();
        //Diffuse Color
        material.setDiffuseColor(Color.ORANGE);
        //Specular Color
        material.setSpecularColor(Color.BLACK);
        c.setMaterial(material);
        c.setLayoutX(y);
        c.setLayoutY(y);
        c.getTransforms().add(new Rotate(20,Rotate.X_AXIS));
        c.getTransforms().add(new Rotate(10,Rotate.Z_AXIS));
        c.getTransforms().add(new Rotate(30,Rotate.Y_AXIS));
        return c;
    }
    
    /**
     * Function for starting the stage and scene components
     * @param primaryStage
     * @throws IOException 
     */
    @Override
    public void start(Stage primaryStage) throws IOException {
        //Container
        Group root = new Group();
        //Creating 3DShape
        Shape3D sh = make3DObject(300,300,200);
        //Creating Ambient Light
        AmbientLight ambient = new AmbientLight();
        ambient.setColor(Color.rgb(0, 255, 0,0.6));
        //Creating Point Light
        PointLight point = new PointLight   ();
        point.setColor(Color.rgb(255, 255, 255,1));
        point.setLayoutX(400);
        point.setLayoutY(100);
        point.setTranslateZ(-1100);
        point.getScope().add(sh);
        
        //Adding nodes inside Container
        root.getChildren().addAll(sh,point,ambient);
        //Adding to scene
        Scene scene = new Scene(root,600,600);
        //Creating Perspective View Camera
        PerspectiveCamera cam=  new PerspectiveCamera(false);
        scene.setCamera(cam );
        primaryStage.setTitle("3D World");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

 
The way it actually outputs like below:

Ambient and Point Light JavaFX


 
The output is pretty fine and I guess it may differ in other system because of the Graphics Card acceleration. Please make sure that you must have at least JDK-8-ea-bin-b91 package to run these codes.

Ok! till then keep coding and have great day geeks :)