package org.pollux3d.editor.app;
import java.io.File;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.pollux3d.editor.AssetLogger;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.UrlLocator;
import com.jme3.input.ChaseCamera;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.BloomFilter;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.control.LodControl;
import jme3test.post.BloomUI;
/**
*
* @author Mawi
*/
public class EditorApp extends SimpleApplication {
private FilterPostProcessor bloomFilter;
private AssetLogger assetLogger;
private static final Logger logger = Logger.getLogger(EditorApp.class.getName());
private AmbientLight al = null;
private DirectionalLight dl = null;
private DirectionalLight dl2 = null;
private PointLight ll = null;
private PointLight ll2 = null;
private PointLight ll3 = null;
private PointLight ll4 = null;
private PointLight ll5 = null;
private PointLight ll6 = null;
private Node camNode;
private void loadEnviroment() {
flyCam.setEnabled(false);
viewPort.setBackgroundColor(ColorRGBA.Black);
setPointLight(true);
guiNode.detachChildNamed("Statistics View");
camNode = new Node("Cam Node");
flyCam.setEnabled(false);
ChaseCamera chaseCam = new ChaseCamera(cam, camNode, inputManager);
chaseCam.setSmoothMotion(true);
chaseCam.setMaxDistance(100000);
chaseCam.setMinVerticalRotation(-FastMath.PI / 2);
rootNode.attachChild(camNode);
}
public void setAssetsPath(String path) {
if (!path.endsWith(File.separator)) {
path += File.separator;
}
assetManager.registerLocator("file:"+path, UrlLocator.class.getName());
}
public void setDirectionalLight(final boolean on) {
if (dl == null) {
Vector3f lightDir = new Vector3f(-0.8719428f, -0.46824604f, 0.14304268f);
dl = new DirectionalLight();
dl.setColor(new ColorRGBA(1.0f, 0.92f, 0.75f, 1f));
dl.setDirection(lightDir);
Vector3f lightDir2 = new Vector3f(0.70518064f, 0.5902297f, -0.39287305f);
dl2 = new DirectionalLight();
dl2.setColor(new ColorRGBA(0.7f, 0.85f, 1.0f, 1f));
dl2.setDirection(lightDir2);
}
this.enqueue(new Callable<Integer>(){
public Integer call() throws Exception {
if (on) {
rootNode.addLight(dl);
rootNode.addLight(dl2);
} else {
rootNode.removeLight(dl);
rootNode.removeLight(dl2);
}
return 0;
}
});
}
public void setAmbientLight(final boolean on) {
if (al == null) {
al = new AmbientLight();
al.setColor(new ColorRGBA(1.0f, 0.92f, 0.75f, 1f));
}
this.enqueue(new Callable<Integer>(){
public Integer call() throws Exception {
if (on) {
rootNode.addLight(al);
} else {
rootNode.removeLight(al);
}
return 0;
}
});
}
private PointLight getPointLight() {
PointLight ll = new PointLight();
ll.setColor(new ColorRGBA(1.0f, 0.92f, 0.75f, 1f));
ll.setRadius(20f);
return ll;
}
public void setPointLight(final boolean on) {
int dis = 10;
if (ll == null) {
ll = getPointLight();
ll.setPosition(new Vector3f(0,dis,0));
ll2 = getPointLight();
ll2.setPosition(new Vector3f(0,-dis,0));
ll3 = getPointLight();
ll3.setPosition(new Vector3f(dis,0,0));
ll4 = getPointLight();
ll4.setPosition(new Vector3f(-dis,0,0));
ll5 = getPointLight();
ll5.setPosition(new Vector3f(0,0,dis));
ll6 = getPointLight();
ll6.setPosition(new Vector3f(0,0,-dis));
}
this.enqueue(new Callable<Integer>(){
public Integer call() throws Exception {
if (on) {
rootNode.addLight(ll);
rootNode.addLight(ll2);
rootNode.addLight(ll3);
rootNode.addLight(ll4);
rootNode.addLight(ll5);
rootNode.addLight(ll6);
} else {
rootNode.removeLight(ll);
rootNode.removeLight(ll2);
rootNode.removeLight(ll3);
rootNode.removeLight(ll4);
rootNode.removeLight(ll5);
rootNode.removeLight(ll6);
}
return 0;
}
});
}
public void loadModel(final String path) {
this.enqueue(new Callable<String>(){
public String call() throws Exception {
assetLogger.setModel(path);
String relPath;
if (path.startsWith(File.separator)) {
relPath = path.substring(1);
} else {
relPath = path;
}
Node model = (Node) assetManager.loadModel(relPath);
if (model == null) {
assetLogger.modelFinished();
logger.log(Level.SEVERE, "Couldn't load model "+path);
return null;
}
Geometry modelGeom = (Geometry) model.getChild(0);
try {
// trows an Exception if lod is not supported
modelGeom.setLodLevel(0);
LodControl control = new LodControl();
modelGeom.addControl(control);
} catch (Exception e) {}
assetLogger.modelFinished();
camNode.detachAllChildren();
camNode.attachChild(model);
return null;
}
});
}
@Override
public void simpleInitApp() {
assetLogger = new AssetLogger();
assetManager.setAssetEventListener(assetLogger);
loadEnviroment();
loadModel("Models/tank/Tank.mesh.xml");
}
public AssetLogger getAssetLogger() {
return assetLogger;
}
public void addBloomFilter() {
this.enqueue(new Callable<Integer>(){
public Integer call() throws Exception {
if (bloomFilter == null) {
bloomFilter = new FilterPostProcessor(assetManager);
BloomFilter bf = new BloomFilter(BloomFilter.GlowMode.Objects);
bf.setBloomIntensity(2.0f);
bf.setExposurePower(1.3f);
bloomFilter.addFilter(bf);
@SuppressWarnings("unused")
BloomUI bui = new BloomUI(inputManager, bf);
}
viewPort.addProcessor(bloomFilter);
return 0;
}
});
}
public void removeBloomFilter() {
this.enqueue(new Callable<Integer>(){
public Integer call() throws Exception {
viewPort.removeProcessor(bloomFilter);
return 0;
}
});
}
}