From 0da6931f8fd0bc0465c470a65246afe2db581cc9 Mon Sep 17 00:00:00 2001 From: Joseph Gardner Date: Mon, 11 Sep 2017 18:24:21 +1200 Subject: [PATCH] Have structure for skybox, still need to play around with it a bit more to make it fit. #story[1261] --- .../Controllers/RaceController.java | 6 +- .../main/java/visualiser/layout/Plane3D.java | 4 +- .../main/java/visualiser/layout/SkyBox.java | 199 ++++++++++++++++++ 3 files changed, 203 insertions(+), 6 deletions(-) create mode 100644 racevisionGame/src/main/java/visualiser/layout/SkyBox.java diff --git a/racevisionGame/src/main/java/visualiser/Controllers/RaceController.java b/racevisionGame/src/main/java/visualiser/Controllers/RaceController.java index f382b5ff..381d02ba 100644 --- a/racevisionGame/src/main/java/visualiser/Controllers/RaceController.java +++ b/racevisionGame/src/main/java/visualiser/Controllers/RaceController.java @@ -14,9 +14,7 @@ import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.GridPane; import javafx.scene.layout.StackPane; -import javafx.scene.shape.Box; import javafx.scene.shape.MeshView; -import javafx.scene.shape.Sphere; import javafx.scene.transform.Translate; import javafx.util.Callback; import network.Messages.Enums.RaceStatusEnum; @@ -26,7 +24,6 @@ import shared.model.Mark; import visualiser.app.App; import visualiser.gameController.ControllerClient; import visualiser.gameController.Keys.ControlKey; -import visualiser.gameController.Keys.KeyFactory; import visualiser.layout.*; import visualiser.model.*; import visualiser.utils.GPSConverter; @@ -207,7 +204,10 @@ public class RaceController extends Controller { //viewSubjects.add(new Subject3D(new MeshView(new Plane3D(50, 50, 1, 1)))); + SeaSurface sea = new SeaSurface(4000, 200, 250, 210); + SkyBox skybox = new SkyBox(4000, 200, 250, 0, 210); + //viewSubjects.addAll(skybox.getSkyBoxPlanes()); viewSubjects.add(sea.getSurface()); Boundary3D boundary3D = new Boundary3D(visualiserRace.getVisualiserRaceState().getRaceDataSource().getBoundary(), gpsConverter); diff --git a/racevisionGame/src/main/java/visualiser/layout/Plane3D.java b/racevisionGame/src/main/java/visualiser/layout/Plane3D.java index b8e03157..bfdfdc22 100644 --- a/racevisionGame/src/main/java/visualiser/layout/Plane3D.java +++ b/racevisionGame/src/main/java/visualiser/layout/Plane3D.java @@ -16,7 +16,7 @@ import java.util.List; public class Plane3D extends TriangleMesh{ /** - * Lenght is up down, and width is left right + * Length is up down, and width is left right. Drawn on the x-y plane with z kept at 0. * @param width width of the plane * @param length length of the plane * @param subdivisionsWidth number of divisions along the width of the plane @@ -86,8 +86,6 @@ public class Plane3D extends TriangleMesh{ } this.getFaces().setAll(copyListToIntArray(faces)); - - } /** diff --git a/racevisionGame/src/main/java/visualiser/layout/SkyBox.java b/racevisionGame/src/main/java/visualiser/layout/SkyBox.java new file mode 100644 index 00000000..a4135bfd --- /dev/null +++ b/racevisionGame/src/main/java/visualiser/layout/SkyBox.java @@ -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 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 getSkyBoxPlanes() { + return skyBoxPlanes; + } +}