import com.jme3.app.SimpleApplication;
import com.jme3.environment.EnvironmentProbeControl;
import com.jme3.material.Material;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;

/** A small self-contained image-based lighting example. */
public class PbrScene extends SimpleApplication {
    public static void main(String[] args) {
        new PbrScene().start();
    }

    @Override
    public void simpleInitApp() {
        cam.setLocation(new Vector3f(0, 2, 12));
        cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
        flyCam.setMoveSpeed(8);

        Geometry room = new Geometry("Environment", new Box(25, 25, 25));
        Material roomMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        roomMaterial.setColor("Color", new ColorRGBA(0.12f, 0.16f, 0.24f, 1));
        roomMaterial.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
        room.setMaterial(roomMaterial);
        rootNode.attachChild(room);

        Geometry panel = new Geometry("Bright panel", new Box(4, 1, 0.1f));
        Material panelMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        panelMaterial.setColor("Color", new ColorRGBA(4, 3, 2, 1));
        panel.setMaterial(panelMaterial);
        panel.setLocalTranslation(0, 5, -8);
        rootNode.attachChild(panel);

        EnvironmentProbeControl probe = new EnvironmentProbeControl(assetManager, 256);
        rootNode.addControl(probe);
        probe.tag(room);
        probe.tag(panel);

        for (int i = 0; i < 5; i++) {
            Geometry sphere = new Geometry("Roughness " + i, new Sphere(32, 48, 0.85f));
            Material pbr = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md");
            pbr.setColor("BaseColor", new ColorRGBA(1f, 0.65f, 0.16f, 1));
            pbr.setFloat("Metallic", 1f);
            pbr.setFloat("Roughness", 0.08f + i * 0.2f);
            sphere.setMaterial(pbr);
            sphere.setLocalTranslation((i - 2) * 2.1f, 0, 0);
            rootNode.attachChild(sphere);
        }
    }
}
