package javara.world.environment;
import javara.world.VisibleObject;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Matrix4f;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Quad;
public class Sky extends VisibleObject {
protected ColorRGBA horizonColor, groundColor;
protected float horizonScale;
protected Material mat;
protected Geometry geom;
public Sky(AssetManager am, ColorRGBA skyColor, ColorRGBA horizonColor, float horizonScale, ColorRGBA groundColor) {
super(skyColor);
this.horizonColor = horizonColor;
this.groundColor = groundColor;
this.horizonScale = horizonScale;
mat = new Material(am, "Materials/SkyDef.j3md");
mat.setColor("skyColor", skyColor);
mat.setColor("horizonColor", horizonColor);
mat.setColor("groundColor", groundColor);
mat.setFloat("gradientHeight", horizonScale);
spatial = new Node(identifier);
}
protected void addCelestialSpatial(Spatial spat) {
((Node)spatial).attachChild(spat);
}
public void setColor(ColorRGBA color) {
this.color = color;
mat.setColor("skyColor", this.color);
}
public void setHorizonColor(ColorRGBA color) {
horizonColor = color;
mat.setColor("horizonColor", horizonColor);
}
public ColorRGBA getHorizonColor() {
return horizonColor;
}
public void setHorizonScale(float scale) {
horizonScale = scale;
mat.setFloat("gradientHeight", horizonScale);
}
public float getHorizonScale() {
return horizonScale;
}
public void setGroundColor(ColorRGBA color) {
groundColor = color;
mat.setColor("groundColor", groundColor);
}
public ColorRGBA getGroundColor() {
return groundColor;
}
public void worldInitialized() {
CameraNode camNode = world.getPlayer().getCameraNode();
Camera c = camNode.getCamera();
float width = Math.abs(c.getFrustumRight() - c.getFrustumLeft());
float height = Math.abs(c.getFrustumTop() - c.getFrustumBottom());
float distance = c.getFrustumFar();
float scale = distance / c.getFrustumNear();
distance = distance * (255f / 256f);
width *= scale;
height *= scale;
geom = new Geometry("SkyPlane", new Quad(width, height));
geom.setLocalRotation(Quaternion.DIRECTION_Z.opposite());
geom.setLocalTranslation(c.getDirection().mult(-distance).add(new Vector3f(-width / 2, height / 2, 0)));
geom.setMaterial(mat);
camNode.attachChild(geom);
}
public void update(float tpf) {
Matrix4f cameraToWorld = geom.getWorldMatrix();
Vector3f cameraPosition = geom.getParent().getWorldTranslation();
mat.setMatrix4("cameraToWorld", cameraToWorld);
mat.setVector3("cameraPosition", cameraPosition);
Vector3f celestialTranslation = world.getPlayer().getCameraNode().getWorldTranslation();
setLocalTranslation(celestialTranslation);
}
}