Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void genCrater(double[][] map, double lat, double lon, int size, double craterOffset, double condOffset, double craterStrength, double perturbStrength, double perturbScale, double r, double s, double p1, double p2, Random rng) {
- PerlinNoise3D noise3d = new PerlinNoise3D(rng, 8, 8, 8);
- lat %= 180.0;
- lon %= 360.0;
- double baseHeight = map[(int)((lon + 180.0) / 360.0 * map.length)][(int)((lat + 90.0) / 180.0 * map[0].length)];
- double[] noise = new double[map.length * 2 + 1];
- for(int i = 0; i < map.length * 2 + 1; i++) {
- noise[i] = NoiseUtils.sampleSpherableNoise(noise3d, (double)i / (double)(map.length * 2 + 1) * 12, 6, 12, 12, perturbScale, perturbScale, 0.125) * perturbStrength;
- }
- for(int i = 0; i < map.length; i++) {
- double longitude = (double)(i - map.length / 2) / (map.length / 2.0) * 180.0;
- for(int j = 0; j < map[0].length; j++) {
- double latitude = (double)(j - map[0].length / 2) / (map[0].length / 2.0) * 90.0;
- double diffLong = Math.abs(lon - longitude);
- double a = Math.sin(Math.toRadians(lat)) * Math.sin(Math.toRadians(latitude));
- double b = Math.cos(Math.toRadians(lat)) * Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(diffLong));
- double dist = Math.acos(a + b) / Math.PI;
- dist *= map.length / 2;
- if(dist >= size * 3) continue;
- double crater_funct_x = (dist / (size / 2.0)) * 6;
- double angle = angleFromCoordinate(lat, lon, latitude, longitude);
- if(Double.isNaN(angle)) angle = 0.1; // This always happens exactly in the dead-center of the crater. Workaround is to set the angle to some fixed value.
- double h = noise[(int)(angle / 360.0 * map.length * 2)];
- crater_funct_x += h;
- double func = craterFunct(crater_funct_x, r, s, p1, p2, craterOffset, condOffset) - 1.0;
- func *= craterStrength;
- double baseMul = Math.max(0, Math.min(1.0, (Math.abs(crater_funct_x) - (r - condOffset)) / Math.abs(r - condOffset)));
- double baseVal = baseMul * map[i][j] + (1.0 - baseMul) * baseHeight;
- if(func < 0) {
- map[i][j] = Math.min(map[i][j], baseVal + func);
- }
- else if(lon == 0) map[i][j] = baseVal + func; // Debug thing. I'm only rendering ridges on the left crater.
- }
- }
- }
- public static void genBowlCrater(double[][] map, double lat, double lon, int size, double craterStrength, double perturbStrength, double perturbScale, double r, double s, double p1, double p2, Random rng) {
- genCrater(map, lat, lon, size, 0.0, 0.0, craterStrength, perturbStrength, perturbScale, r, s, p1, p2, rng);
- }
- public static void genFlattenedCrater(double[][] map, double lat, double lon, int size, double craterStrength, double perturbStrength, double perturbScale, double p2, Random rng) {
- double p1 = 7.0;
- genCrater(map, lat, lon, size, 0.5, 1.0 / p1, craterStrength, perturbStrength, perturbScale, 2.2, 0.47, p1, p2, rng);
- }
- private static double angleFromCoordinate(double lat1, double long1, double lat2, double long2) {
- double dLon = Math.toRadians(long2 - long1);
- double y = Math.sin(dLon) * Math.cos(Math.toRadians(lat2));
- double x = Math.cos(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) - Math.sin(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(dLon);
- double brng = Math.atan2(y, x);
- brng = Math.toDegrees(brng);
- brng = (brng + 360) % 360;
- brng = 360 - brng;
- return brng;
- }
- public static double craterFunct(double x, double r, double s, double p1, double p2, double offset, double condOffset) {
- if(Math.abs(x) <= r - condOffset) {
- return offset + Math.pow(Math.abs(s * x), p1);
- }else {
- return 1.0 + (Math.pow(r * s, p1) - 1.0) / Math.pow(Math.abs(x) - (r - 1.0), p2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement