Okay here is the test class.
Please just change the file location for your own file structure.
You also need to run the class twice.
The first run will create the file and the second run will load it.
import com.jme3.app.SimpleApplication;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.export.binary.BinaryImporter;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jme3utilities.sky.SkyControl;
import jme3utilities.sky.StarsOption;
import jme3utilities.sky.Updater;
/**
*
* @author nicki
*/
public class TestSkyControl extends SimpleApplication {
private DirectionalLight directionalLight;
private AmbientLight ambientLight;
private Node sceneNode;
private File file = new File("/home/ndebruyn/Personal/GameDevelopment/scenes/sky.j3o");
public static void main(String[] args) {
TestSkyControl app = new TestSkyControl();
app.start();
}
@Override
public void simpleInitApp() {
loadScene();
loadCamera();
}
private void loadScene() {
if (file.exists()) {
//Load existing scene
Spatial loaded = openSpatial(file);
sceneNode = (Node) loaded;
rootNode.attachChild(sceneNode);
System.out.println("LOADED SCENE FROM FILE");
} else {
//Create a new scene
sceneNode = new Node("Scene");
rootNode.attachChild(sceneNode);
Spatial box = addBox(sceneNode, 10, 0.1f, 10);
addColor(box, ColorRGBA.Orange, 0);
box = addBox(sceneNode, 0.2f, 5f, 0.2f);
addColor(box, ColorRGBA.Cyan, 1);
directionalLight = new DirectionalLight(new Vector3f(1, 1, 1), ColorRGBA.White);
sceneNode.addLight(directionalLight);
ambientLight = new AmbientLight(ColorRGBA.LightGray);
sceneNode.addLight(ambientLight);
loadSky();
try {
saveSpatial(sceneNode, file);
} catch (Exception ex) {
Logger.getLogger(TestSkyControl.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("CREATED SCENE AND SAVE TO FILE");
}
}
private void loadSky() {
//Add the sky control stuff
SkyControl skyControl = new SkyControl(assetManager, cam, 0.8f, StarsOption.Cube, true);
sceneNode.addControl(skyControl);
skyControl.setCloudiness(0.2f);
skyControl.setCloudsYOffset(0.4f);
skyControl.setCloudsRate(10);
skyControl.getSunAndStars().setHour(13f);
skyControl.setEnabled(true);
Updater updater = skyControl.getUpdater();
updater.setAmbientLight(ambientLight);
updater.setMainLight(directionalLight);
}
private void loadCamera() {
cam.setLocation(new Vector3f(-10, 4, 10));
cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
flyCam.setMoveSpeed(10);
}
private Spatial addBox(Node parent, float xExtend, float yExtend, float zExtend) {
Box box = new Box(xExtend, yExtend, zExtend);
Geometry geometry = new Geometry("box", box);
parent.attachChild(geometry);
return geometry;
}
private Material addColor(Spatial spatial, ColorRGBA colorRGBA, int type) {
Material material = null;
if (type == 1) {
material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
material.setBoolean("UseMaterialColors", true);
material.setColor("Ambient", colorRGBA);
material.setColor("Diffuse", colorRGBA);
} else if (type == 2) {
material = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md");
material.setColor("BaseColor", colorRGBA);
material.setFloat("Metallic", 0f);
material.setFloat("Roughness", 0.5f);
spatial.setMaterial(material);
} else {
material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
material.setColor("Color", colorRGBA);
}
spatial.setMaterial(material);
return material;
}
private void saveSpatial(Spatial spatial, File file) throws Exception {
BinaryExporter exporter = BinaryExporter.getInstance();
if (file != null && !file.getName().endsWith(".j3o")) {
throw new Exception("Invalid file extension, must be .j3o");
}
try {
exporter.save(spatial, file);
System.out.println("Spatial " + file.getName() + " successfully saved! Location: " + file.getPath());
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Opens a .j3o on the file system
*
* @param file
* @return
*/
private Spatial openSpatial(File file) {
if (file == null || !file.exists()) {
return null;
}
BinaryImporter importer = BinaryImporter.getInstance();
importer.setAssetManager(assetManager);
try {
Spatial spatial = (Spatial) importer.load(file);
System.out.println("Found spatial: " + spatial.getName());
return spatial;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}