Advertisement
TheGhastModding

CraterGenerator

Apr 3rd, 2020
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1.     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) {
  2.         PerlinNoise3D noise3d = new PerlinNoise3D(rng, 8, 8, 8);
  3.        
  4.         lat %= 180.0;
  5.         lon %= 360.0;
  6.        
  7.         double baseHeight = map[(int)((lon + 180.0) / 360.0 * map.length)][(int)((lat + 90.0) / 180.0 * map[0].length)];
  8.        
  9.         double[] noise = new double[map.length * 2 + 1];
  10.         for(int i = 0; i < map.length * 2 + 1; i++) {
  11.             noise[i] = NoiseUtils.sampleSpherableNoise(noise3d, (double)i / (double)(map.length * 2 + 1) * 12, 6, 12, 12, perturbScale, perturbScale, 0.125) * perturbStrength;
  12.         }
  13.        
  14.         for(int i = 0; i < map.length; i++) {
  15.             double longitude = (double)(i - map.length / 2) / (map.length / 2.0) * 180.0;
  16.             for(int j = 0; j < map[0].length; j++) {
  17.                 double latitude = (double)(j - map[0].length / 2) / (map[0].length / 2.0) * 90.0;
  18.                
  19.                 double diffLong = Math.abs(lon - longitude);
  20.                
  21.                 double a = Math.sin(Math.toRadians(lat)) * Math.sin(Math.toRadians(latitude));
  22.                 double b = Math.cos(Math.toRadians(lat)) * Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(diffLong));
  23.                 double dist = Math.acos(a + b) / Math.PI;
  24.                 dist *= map.length / 2;
  25.                 if(dist >= size * 3) continue;
  26.                
  27.                 double crater_funct_x = (dist / (size / 2.0)) * 6;
  28.                
  29.                 double angle = angleFromCoordinate(lat, lon, latitude, longitude);
  30.                 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.
  31.                 double h = noise[(int)(angle / 360.0 * map.length * 2)];
  32.                
  33.                 crater_funct_x += h;
  34.                
  35.                 double func = craterFunct(crater_funct_x, r, s, p1, p2, craterOffset, condOffset) - 1.0;
  36.                 func *= craterStrength;
  37.                
  38.                 double baseMul = Math.max(0, Math.min(1.0, (Math.abs(crater_funct_x) - (r - condOffset)) / Math.abs(r - condOffset)));
  39.                 double baseVal = baseMul * map[i][j] + (1.0 - baseMul) * baseHeight;
  40.                
  41.                 if(func < 0) {
  42.                     map[i][j] = Math.min(map[i][j], baseVal + func);
  43.                 }
  44.                 else if(lon == 0) map[i][j] = baseVal + func; // Debug thing. I'm only rendering ridges on the left crater.
  45.             }
  46.         }
  47.     }
  48.    
  49.     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) {
  50.         genCrater(map, lat, lon, size, 0.0, 0.0, craterStrength, perturbStrength, perturbScale, r, s, p1, p2, rng);
  51.     }
  52.    
  53.     public static void genFlattenedCrater(double[][] map, double lat, double lon, int size, double craterStrength, double perturbStrength, double perturbScale, double p2, Random rng) {
  54.         double p1 = 7.0;
  55.         genCrater(map, lat, lon, size, 0.5, 1.0 / p1, craterStrength, perturbStrength, perturbScale, 2.2, 0.47, p1, p2, rng);
  56.     }
  57.    
  58.     private static double angleFromCoordinate(double lat1, double long1, double lat2, double long2) {
  59.         double dLon = Math.toRadians(long2 - long1);
  60.        
  61.         double y = Math.sin(dLon) * Math.cos(Math.toRadians(lat2));
  62.         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);
  63.        
  64.         double brng = Math.atan2(y, x);
  65.        
  66.         brng = Math.toDegrees(brng);
  67.         brng = (brng + 360) % 360;
  68.         brng = 360 - brng;
  69.        
  70.         return brng;
  71.     }
  72.    
  73.     public static double craterFunct(double x, double r, double s, double p1, double p2, double offset, double condOffset) {
  74.         if(Math.abs(x) <= r - condOffset) {
  75.             return offset + Math.pow(Math.abs(s * x), p1);
  76.         }else {
  77.             return 1.0 + (Math.pow(r * s, p1) - 1.0) / Math.pow(Math.abs(x) - (r - 1.0), p2);
  78.         }
  79.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement