In this chapter, we will talk about applying textures with diffuse map. In 3D programming, texture is one of the most important parts because it provides that actual look and feel of an object by adding details on its surface.

JavaFX and PhongMaterial

In the javafx.scene.paint package, you can see a class that extends Material abstract class, the PhongMaterial. PhongMaterial allows to create virtual materials that can be applied over your 3D object to create real-world like objects.

Before diving directly in to the code, let’s have a look at what exactly is the Diffuse Map.

What is a Diffuse Map ?

According to the definition, Diffuse mapping (also know as texture mapping) refers to a computer graphics method that simply wrapped and mapped pixels from a texture to a 3D surface. It defines the color property of the surface. For example, if you want to color your object to red, you simply apply red diffuse map over it, it will become red.

If you would like to read more about it, have a look at cryengine doc.

Apply Color Over The Surface

JavaFX PhongMaterial provides two option to apply a diffuse map. Either using a color or using an image. If you use a color, then the whole object will be painted with that color.

Let’s see how setting a color works.

//Create Material
PhongMaterial material = new PhongMaterial();
//Applying JavaFX Diffuse Map with Color Royal Blue[/caption] Royal blue color
material.setDiffuseColor(Color.ROYALBLUE));
Box box = new Box(100, 20, 50);
//Apply material for the box
box.setMaterial(material);

 

JavaFX: Royal blue color applied as Diffuse Map

So, using the PhongMaterial’s setDiffuseColor(Color.ROYALBLUE), we set the color of the surface. You can set any color you want.

Apply Texture Over The Surface

Colors are suitable in some cases. But, in most cases, we need something more complex than some simple colors. JavaFX provides option to set diffuse map using an Image. You can simply apply any image as texture for your object.

Let’s see how a wooden texture works on our box.

//Create Material
PhongMaterial material = new PhongMaterial();
//Applying wooden texture
material.setDiffuseMap(new Image(getClass().getResourceAsStream("wood.jpg")));
Box box = new Box(100, 20, 50);
//Apply material for the box
box.setMaterial(material);

And it looks as follows. Pretty awesome, right?

JavaFX Diffuse Map with Woode Image

and that’s it. It is just 3 lines of code to apply a complex texture on your object. If you look at the set* methods of PhongMaterial, you will find a lot more methods. The thing is, diffuse map is just a start. You can make your objects with great amount of detail with these options. We will discover those methods in the upcoming chapters.

Visit JavaFX 3D Course Index Page