Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we implement our own materials in JavaFX?

JavaFX has the abstract class Material for representing materials of 3D surfaces. The current JDK gives only a PhongMaterial implementation. I was looking into adding my own material (like Lambert or Blinn etc.) by extending Material similar to how PhongMaterial is implemented.

There are 2 problems I encountered:

  1. The behavior (color calculation) specified in the PhongMaterial documentation is nowhere to be found - not in the class itself and not in the internal classes Iv'e looked at. It's somewhere in the JDK, but not so exposed. The PhongMaterial class only holds the properties and not the behavior, which I find strange to begin with.

  2. PhongMaterial appears to be deeply coupled with internal classes:

    • com.sun.javafx.sg.prism.NGPhongMaterial, which according to some documentation is the peer node created by the graphics Toolkit/Pipeline implementation. This class specifies no behavior.
    • com.sun.prism.PhongMaterial, which represents a phong material for retained mode rendering.
    • com.sun.prism.TextureMap is a wrapper class to hold map related information for the PhongMaterial.

    None of these specify the material's behavior.

What are the steps to implement my own material? What classes do I need to write? For example, would I need a new TextureMap class which I will somehow inform Prism about?

like image 366
user1803551 Avatar asked Dec 29 '25 05:12

user1803551


1 Answers

As far as I can tell, the Material class is not (currently) designed for subclassing. This is probably because a fixed public API has not yet been agreed on.

Material contains two abstract methods. In JavaFX 8 these are

public abstract void impl_updatePG(); 

and

public abstract NGPhongMaterial impl_getNGMaterial();

These are marked @Deprecated and @treatAsPrivate implementation detail.

In JavaFX 9 the names change and the visibility changes to package-private:

abstract void updatePG(); 

and

abstract NGPhongMaterial getNGMaterial();

So it looks like the intention here is not to allow subclassing, but to have a design that will allow future (>9) versions to allow subclassing once an API has been decided on. Again, here I am reading between the lines to a large degree: users closer to the JavaFX team may have more complete information. FWIW, the current version of the source code contains the following comment:

Material is not Paint
PhongMaterial maybe the first and only material in FX8 (see 3D conceptual implementation for details)
Bump map: Normal Map and Height Map -- We may generate a Normal Map when given a Height Map
Displacement map? Not in FX8 -- May do Parallex correction mapping to improve quality at performance cost
Support auto generated Mipmap
No plan to support Multi-texture

like image 110
James_D Avatar answered Dec 30 '25 21:12

James_D