From a7eefca13a15d34da184ba2190f4a112b6d0cb4a Mon Sep 17 00:00:00 2001 From: Fan-Wu Yang Date: Sun, 10 Sep 2017 16:15:37 +1200 Subject: [PATCH] Work in progress, the perlin noise is now generated, however, a good colour should be selected as well as using a subject3d instead of a imageview #story[1261] --- .../Controllers/RaceController.java | 4 +- .../java/visualiser/layout/SeaSurface.java | 99 +++++++++++++++++++ .../main/java/visualiser/layout/View3D.java | 2 +- .../utils/PerlinNoiseGenerator.java | 87 ++++++++++++++++ 4 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 racevisionGame/src/main/java/visualiser/layout/SeaSurface.java create mode 100644 racevisionGame/src/main/java/visualiser/utils/PerlinNoiseGenerator.java diff --git a/racevisionGame/src/main/java/visualiser/Controllers/RaceController.java b/racevisionGame/src/main/java/visualiser/Controllers/RaceController.java index 195b373a..3826bba3 100644 --- a/racevisionGame/src/main/java/visualiser/Controllers/RaceController.java +++ b/racevisionGame/src/main/java/visualiser/Controllers/RaceController.java @@ -191,13 +191,15 @@ public class RaceController extends Controller { StlMeshImporter importerBurgerBoat = new StlMeshImporter(); importerBurgerBoat.read(alternateBoatAsset); + SeaSurface sea = new SeaSurface(900); + view3D = new View3D(); view3D.setDistance(1050); view3D.setYaw(0); view3D.setPitch(60); view3D.enableTracking(); + canvasBase.add(sea.getSurface(), 0, 0); canvasBase.add(view3D, 0, 0); - // Set up projection from GPS to view RaceDataSource raceData = visualiserRace.getVisualiserRaceState().getRaceDataSource(); final GPSConverter gpsConverter = new GPSConverter(raceData, 450, 450); diff --git a/racevisionGame/src/main/java/visualiser/layout/SeaSurface.java b/racevisionGame/src/main/java/visualiser/layout/SeaSurface.java new file mode 100644 index 00000000..a9395996 --- /dev/null +++ b/racevisionGame/src/main/java/visualiser/layout/SeaSurface.java @@ -0,0 +1,99 @@ +package visualiser.layout; + +import javafx.geometry.Point3D; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +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 javafx.scene.shape.TriangleMesh; +import visualiser.utils.PerlinNoiseGenerator; + +/** + * Created by fwy13 on 10/09/17. + */ +public class SeaSurface { + private float[][] noiseArray; + private ImageView surface; + + public SeaSurface(int size){ + noiseArray = PerlinNoiseGenerator.createNoise(size); + createSurface(); + } + + private void createSurface(){ + Image diffuseMap = createImage(noiseArray.length, noiseArray); + + PhongMaterial material = new PhongMaterial(); + material.setDiffuseMap(diffuseMap); + material.setSpecularColor(Color.WHITE); + + // testing / debugging stuff: show diffuse map on chart + ImageView iv = new ImageView(diffuseMap); + //iv.setTranslateX(-0.5 * noiseArray.length); + //iv.setTranslateY(-0.10 * noiseArray.length); + iv.setRotate(90); + //iv.setRotationAxis(new Point3D(1, 0, 0)); + surface = iv; + } + + /** + * Create texture for uv mapping + * @param size + * @param noise + * @return + */ + private Image createImage(double size, float[][] noise) { + + int width = (int) size; + int height = (int) size; + + WritableImage wr = new WritableImage(width, height); + PixelWriter pw = wr.getPixelWriter(); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + + float value = noise[x][y]; + + double gray = normalizeValue(value, -.5, .5, 0., 1.); + + gray = clamp(gray, 0, 1); + + Color lightBlue = new Color(0.2, 0.2, 1, 1); + Color lighterBlue = new Color(0.25, 0.225, 1, 1); + + Color color = lighterBlue.interpolate(lightBlue, gray); + + pw.setColor(x, y, color); + + } + } + + return wr; + + } + + private static double normalizeValue(double value, double min, double max, double newMin, double newMax) { + + return (value - min) * (newMax - newMin) / (max - min) + newMin; + + } + + 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 ImageView getSurface(){ + return surface; + } + +} diff --git a/racevisionGame/src/main/java/visualiser/layout/View3D.java b/racevisionGame/src/main/java/visualiser/layout/View3D.java index 27fe6086..7b7d9a77 100644 --- a/racevisionGame/src/main/java/visualiser/layout/View3D.java +++ b/racevisionGame/src/main/java/visualiser/layout/View3D.java @@ -98,7 +98,7 @@ public class View3D extends Pane { scene.widthProperty().bind(this.widthProperty()); scene.heightProperty().bind(this.heightProperty()); - scene.setFill(new Color(0.2, 0.6, 1, 1)); + //scene.setFill(new Color(0.2, 0.6, 1, 1)); scene.setCamera(buildCamera()); diff --git a/racevisionGame/src/main/java/visualiser/utils/PerlinNoiseGenerator.java b/racevisionGame/src/main/java/visualiser/utils/PerlinNoiseGenerator.java new file mode 100644 index 00000000..a5093b50 --- /dev/null +++ b/racevisionGame/src/main/java/visualiser/utils/PerlinNoiseGenerator.java @@ -0,0 +1,87 @@ +package visualiser.utils; + +/** + * Created by fwy13 on 10/09/17. + */ +public class PerlinNoiseGenerator { + + + /** + * Create an array of the given size with values of perlin noise + * @param size + * @return + */ + public static float[][] createNoise( int size) { + float[][] noiseArray = new float[(int) size][(int) size]; + + for (int x = 0; x < size; x++) { + for (int y = 0; y < size; y++) { + + double frequency = 20.0 / (double) size; + + double noise = ImprovedNoise.noise(x * frequency, y * frequency, 0); + + noiseArray[x][y] = (float) noise; + } + } + + return noiseArray; + + } + + /** + * Perlin noise generator + * + * // JAVA REFERENCE IMPLEMENTATION OF IMPROVED NOISE - COPYRIGHT 2002 KEN PERLIN. + * // http://mrl.nyu.edu/~perlin/paper445.pdf + * // http://mrl.nyu.edu/~perlin/noise/ + */ + public final static class ImprovedNoise { + static public double noise(double x, double y, double z) { + int X = (int)Math.floor(x) & 255, // FIND UNIT CUBE THAT + Y = (int)Math.floor(y) & 255, // CONTAINS POINT. + Z = (int)Math.floor(z) & 255; + x -= Math.floor(x); // FIND RELATIVE X,Y,Z + y -= Math.floor(y); // OF POINT IN CUBE. + z -= Math.floor(z); + double u = fade(x), // COMPUTE FADE CURVES + v = fade(y), // FOR EACH OF X,Y,Z. + w = fade(z); + int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF + B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z; // THE 8 CUBE CORNERS, + + return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ), // AND ADD + grad(p[BA ], x-1, y , z )), // BLENDED + lerp(u, grad(p[AB ], x , y-1, z ), // RESULTS + grad(p[BB ], x-1, y-1, z ))),// FROM 8 + lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ), // CORNERS + grad(p[BA+1], x-1, y , z-1 )), // OF CUBE + lerp(u, grad(p[AB+1], x , y-1, z-1 ), + grad(p[BB+1], x-1, y-1, z-1 )))); + } + static double fade(double t) { return t * t * t * (t * (t * 6 - 15) + 10); } + static double lerp(double t, double a, double b) { return a + t * (b - a); } + static double grad(int hash, double x, double y, double z) { + int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE + double u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS. + v = h<4 ? y : h==12||h==14 ? x : z; + return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v); + } + static final int p[] = new int[512], permutation[] = { 151,160,137,91,90,15, + 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, + 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, + 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, + 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, + 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, + 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, + 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, + 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, + 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, + 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, + 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, + 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 + }; + static { for (int i=0; i < 256 ; i++) p[256+i] = p[i] = permutation[i]; } + } + +}