Warren Nocos
Warren Nocos

Reputation: 1282

Visible light source in JavaFX 3D

I am currently studying JavaFX 3D capabilities, and I would like to model the solar system. I am quite successful in creating a light source of type PointLight, and set its coordinates bound to sun's coordinates. Now I would like to see the sun glows with its radiance as the light source. How would I do that?

@Component
public class RootPane extends StackPane {

    @Inject
    protected Scene scene;

    @Inject
    protected PerspectiveCamera camera;

    @Inject
    protected LightBase lightBase;

    @PostConstruct
    public void init() {
        setBackground(new Background(new BackgroundFill(
                Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
        setAlignment(Pos.CENTER);
        setPadding(new Insets(20));

        Sphere sunSphere = new Sphere(80);
        sunSphere.setEffect(new Glow(5));

        Sphere mercurySphere = new Sphere(40);
        mercurySphere.translateZProperty().bind(sunSphere.translateZProperty()
                .add(200));

        Sphere venusSphere = new Sphere(40);
        venusSphere.translateXProperty().bind(sunSphere.translateXProperty()
                .subtract(300));
        venusSphere.translateZProperty().bind(sunSphere.translateZProperty()
                .subtract(300));

        Sphere earthSphere = new Sphere(40);
        earthSphere.translateXProperty().bind(sunSphere.translateXProperty()
                .add(300));
        earthSphere.translateZProperty().bind(sunSphere.translateZProperty()
                .subtract(400));

        Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
        Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
        Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);

        Translate translate = new Translate(0, 0, -3000);

        camera.getTransforms().addAll(rotateX, rotateY, rotateZ, translate);

        scene.addEventHandler(
                KeyEvent.KEY_PRESSED,
                event -> {
                    if (Objects.equals(event.getCode(), KeyCode.D)) {
                        rotateY.setAngle(rotateY.getAngle() - 
                                (event.isShiftDown() ? 5 : 0.1));
                    } else if (Objects.equals(event.getCode(), KeyCode.A)) {
                        rotateY.setAngle(rotateY.getAngle() + 
                                (event.isShiftDown() ? 5 : 0.1));
                    }
                });
        scene.addEventHandler(
                KeyEvent.KEY_PRESSED,
                event -> {
                    if (Objects.equals(event.getCode(), KeyCode.W)) {
                        rotateX.setAngle(rotateX.getAngle() - 
                                (event.isShiftDown() ? 5 : 0.1));
                    } else if (Objects.equals(event.getCode(), KeyCode.S)) {
                        rotateX.setAngle(rotateX.getAngle() + 
                                (event.isShiftDown() ? 5 : 0.1));
                    }
                });

        lightBase.translateXProperty().bind(sunSphere.translateXProperty());
        lightBase.translateYProperty().bind(sunSphere.translateYProperty());
        lightBase.translateZProperty().bind(sunSphere.translateZProperty());

        getChildren().addAll(sunSphere, mercurySphere, venusSphere,
                earthSphere, camera, lightBase);
    }

}

Upvotes: 4

Views: 1276

Answers (2)

IceGlow
IceGlow

Reputation: 636

From PhongMaterial JavaDoc

The color is computed by the following equation:

for each ambient light source i {
    ambient += lightColor[i]
}
for each point light source i {
    diffuse += (L[i] . N) * lightColor[i]
    specular += ((R[i] . V) ^ (specularPower * intensity(specularMap))) * lightColor[i]
}
color = (ambient + diffuse) * diffuseColor * diffuseMap
         + specular * specularColor * specularMap
         + selfIlluminationMap

where:

lightColor[i] is the color of light source i,

L[i] is the vector from the surface to light source i,

N is the normal vector (taking into the account the bumpMap if present),

R[i] is the normalized reflection vector for L[i] about the surface normal,

and V is the normalized view vector.

If your PointLight coordinates match your Sun's Sphere coordinates, then L[i] . N < 0 and you have no diffuse contribution. So, in the absence of ambient light it'll look black.

Try to add that selfIlluminationMap they have mentioned using sun texture. It should help.

A brief search on google gave me this link, might be useful: planets and sun maps. Be careful with the licenses, though, there ought to be free sources.

Upvotes: 1

dreamwagon
dreamwagon

Reputation: 1289

Without an example of the effect you are looking to achieve it is hard to provide an answer but here are Two options:

Create a particle effect emitting from the sun with additive blending

Create a material with a specular map.

Upvotes: 1

Related Questions