Have structure for skybox, still need to play around with it a bit more to make it fit. #story[1261]
parent
ae75bb86d3
commit
0da6931f8f
@ -0,0 +1,199 @@
|
||||
package visualiser.layout;
|
||||
|
||||
import javafx.geometry.Point3D;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.PixelWriter;
|
||||
import javafx.scene.image.WritableImage;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.paint.PhongMaterial;
|
||||
import javafx.scene.shape.MeshView;
|
||||
import visualiser.utils.PerlinNoiseGenerator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Creates a skyBox
|
||||
*/
|
||||
public class SkyBox {
|
||||
private int size;
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
private double freq;
|
||||
private List<Subject3D> skyBoxPlanes = new ArrayList<>();
|
||||
|
||||
public SkyBox(int size, double freq, double x, double y, double z) {
|
||||
this.size = size;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.freq = freq;
|
||||
makeSkyBox();
|
||||
}
|
||||
|
||||
private void makeSkyBox() {
|
||||
//addTop();
|
||||
//addFront();
|
||||
// addBack();
|
||||
addLeft();
|
||||
// addRight();
|
||||
}
|
||||
|
||||
private void addTop() {
|
||||
MeshView surface = makeSurface();
|
||||
|
||||
Subject3D top = new Subject3D(surface);
|
||||
top.setX(x);
|
||||
top.setY(y);
|
||||
top.setZ(z);
|
||||
skyBoxPlanes.add(top);
|
||||
}
|
||||
|
||||
private void addLeft() {
|
||||
MeshView surface = makeSurface();
|
||||
|
||||
surface.setTranslateX(size/2);
|
||||
surface.setTranslateY(size/2);
|
||||
surface.setRotationAxis(new Point3D(0, 1, 0));
|
||||
surface.setRotate(90);
|
||||
surface.setTranslateX(-size/2);
|
||||
surface.setTranslateY(-size/2);
|
||||
|
||||
surface.setTranslateX(x);
|
||||
surface.setTranslateY(y);
|
||||
surface.setTranslateZ(z);
|
||||
|
||||
|
||||
Subject3D left = new Subject3D(surface);
|
||||
skyBoxPlanes.add(left);
|
||||
}
|
||||
|
||||
private void addBack() {
|
||||
MeshView surface = makeSurface();
|
||||
|
||||
Subject3D back = new Subject3D(surface);
|
||||
back.setX(x);
|
||||
back.setY(y);
|
||||
back.setZ(z + size);
|
||||
skyBoxPlanes.add(back);
|
||||
}
|
||||
|
||||
private void addFront() {
|
||||
MeshView surface = makeSurface();
|
||||
|
||||
Subject3D back = new Subject3D(surface);
|
||||
back.setX(x);
|
||||
back.setY(y);
|
||||
back.setZ(z);
|
||||
skyBoxPlanes.add(back);
|
||||
}
|
||||
|
||||
private void addRight() {
|
||||
MeshView surface = makeSurface();
|
||||
|
||||
surface.setTranslateX(size/2);
|
||||
surface.setTranslateY(size/2);
|
||||
surface.setRotationAxis(new Point3D(1, 0, 0));
|
||||
surface.setRotate(90);
|
||||
surface.setTranslateX(-size/2);
|
||||
surface.setTranslateY(-size/2);
|
||||
|
||||
surface.setTranslateX(x);
|
||||
surface.setTranslateY(y);
|
||||
surface.setTranslateZ(z);
|
||||
|
||||
|
||||
Subject3D left = new Subject3D(surface);
|
||||
skyBoxPlanes.add(left);
|
||||
}
|
||||
|
||||
private MeshView makeSurface() {
|
||||
Image diffuseMap = new Image(getClass().getClassLoader().getResourceAsStream("images/SailIcon.png"));
|
||||
|
||||
PhongMaterial material = new PhongMaterial();
|
||||
material.setDiffuseColor(Color.BLUE);
|
||||
//material.setDiffuseMap(diffuseMap);
|
||||
material.setSpecularColor(Color.WHITE);
|
||||
|
||||
Plane3D plane = new Plane3D(size, size, 10, 10);
|
||||
MeshView surface = new MeshView(plane);
|
||||
surface.setMaterial(material);
|
||||
surface.setMouseTransparent(true);
|
||||
return surface;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create texture for uv mapping
|
||||
* @param size size of the image to make
|
||||
* @return image that is created
|
||||
*/
|
||||
private Image createImage(int size) {
|
||||
float[][] noise = PerlinNoiseGenerator.createNoise(size, freq);
|
||||
|
||||
WritableImage wr = new WritableImage(size, size);
|
||||
PixelWriter pw = wr.getPixelWriter();
|
||||
//interpolate colours based on noise
|
||||
for (int x = 0; x < size; x++) {
|
||||
for (int y = 0; y < size; y++) {
|
||||
|
||||
float value = noise[x][y];
|
||||
|
||||
double gray = normalizeValue(value, -.5, .5, 0., 1.);
|
||||
|
||||
gray = clamp(gray, 0, 1);
|
||||
|
||||
//values to interpolate on
|
||||
Color brightBlue = new Color(0.06, 0.5, .78, 1);
|
||||
Color lightBlue = new Color(0.15, 0.68, .88, 1);
|
||||
Color lighterBlue = new Color(0.28, 0.73, .91, 1);
|
||||
|
||||
Color colour = Color.WHITE.interpolate(brightBlue, gray).interpolate(lighterBlue, gray).interpolate(lightBlue, gray);
|
||||
|
||||
pw.setColor(x, y, colour);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return wr;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Nomalises the values so that the colours are correct
|
||||
* @param value value to normalise
|
||||
* @param min current min
|
||||
* @param max current max
|
||||
* @param newMin new min
|
||||
* @param newMax new max
|
||||
* @return returns normalised value
|
||||
*/
|
||||
private static double normalizeValue(double value, double min, double max, double newMin, double newMax) {
|
||||
|
||||
return (value - min) * (newMax - newMin) / (max - min) + newMin;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* clamps a value between a min and max
|
||||
* @param value value to clamp
|
||||
* @param min minimum value it can be
|
||||
* @param max maximum value it can be
|
||||
* @return result after clamp
|
||||
*/
|
||||
private static double clamp(double value, double min, double max) {
|
||||
|
||||
if (Double.compare(value, min) < 0)
|
||||
return min;
|
||||
|
||||
if (Double.compare(value, max) > 0)
|
||||
return max;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public List<Subject3D> getSkyBoxPlanes() {
|
||||
return skyBoxPlanes;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue