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.Box; 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 Subject3D surface; public SeaSurface(int size, double freq, double x, double z){ noiseArray = PerlinNoiseGenerator.createNoise(size, freq); createSurface(); surface.setZ(z); surface.setX(x); } private void createSurface(){ Image diffuseMap = createImage(noiseArray.length, noiseArray); PhongMaterial material = new PhongMaterial(); material.setDiffuseMap(diffuseMap); material.setSpecularColor(Color.WHITE); Plane3D seaPlane = new Plane3D(noiseArray.length, noiseArray.length, 10, 10); MeshView seaSurface = new MeshView(seaPlane); // Box seaSurface = new Box(noiseArray.length, 0.1, noiseArray.length); seaSurface.setMaterial(material); //seaSurface.setRotationAxis(new Point3D(1, 0, 0)); //seaSurface.setRotate(90); surface = new Subject3D(seaSurface); } /** * 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 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 color = Color.WHITE.interpolate(brightBlue, gray).interpolate(lighterBlue, gray).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 Subject3D getSurface(){ return surface; } }