Stateless simplex

This commit is contained in:
Anuken 2021-06-15 19:28:54 -04:00
parent 210fb349de
commit fe9ff212b2
9 changed files with 30 additions and 39 deletions

View file

@ -44,9 +44,7 @@ public class MenuRenderer implements Disposable{
Seq<Block> ores = content.blocks().select(b -> b instanceof OreBlock && !(b instanceof WallOreBlock));
shadows = new FrameBuffer(width, height);
int offset = Mathf.random(100000);
Simplex s1 = new Simplex(offset);
Simplex s2 = new Simplex(offset + 1);
Simplex s3 = new Simplex(offset + 2);
int s1 = offset, s2 = offset + 1, s3 = offset + 2;
Block[] selected = Structs.select(
new Block[]{Blocks.sand, Blocks.sandWall},
new Block[]{Blocks.shale, Blocks.shaleWall},
@ -85,27 +83,27 @@ public class MenuRenderer implements Disposable{
Block ore = Blocks.air;
Block wall = Blocks.air;
if(s1.octaveNoise2D(3, 0.5, 1/20.0, x, y) > 0.5){
if(Simplex.noise2d(s1, 3, 0.5, 1/20.0, x, y) > 0.5){
wall = walld;
}
if(s3.octaveNoise2D(3, 0.5, 1/20.0, x, y) > 0.5){
if(Simplex.noise2d(s3, 3, 0.5, 1/20.0, x, y) > 0.5){
floor = floord2;
if(wall != Blocks.air){
wall = walld2;
}
}
if(s2.octaveNoise2D(3, 0.3, 1/30.0, x, y) > tr1){
if(Simplex.noise2d(s2, 3, 0.3, 1/30.0, x, y) > tr1){
ore = ore1;
}
if(s2.octaveNoise2D(2, 0.2, 1/15.0, x, y+99999) > tr2){
if(Simplex.noise2d(s2, 2, 0.2, 1/15.0, x, y+99999) > tr2){
ore = ore2;
}
if(doheat){
double heat = s3.octaveNoise2D(4, 0.6, 1 / 50.0, x, y + 9999);
double heat = Simplex.noise2d(s3, 4, 0.6, 1 / 50.0, x, y + 9999);
double base = 0.65;
if(heat > base){
@ -126,7 +124,7 @@ public class MenuRenderer implements Disposable{
if(tech){
int mx = x % secSize, my = y % secSize;
int sclx = x / secSize, scly = y / secSize;
if(s1.octaveNoise2D(2, 1f / 10f, 0.5f, sclx, scly) > 0.4f && (mx == 0 || my == 0 || mx == secSize - 1 || my == secSize - 1)){
if(Simplex.noise2d(s1, 2, 1f / 10f, 0.5f, sclx, scly) > 0.4f && (mx == 0 || my == 0 || mx == secSize - 1 || my == secSize - 1)){
floor = Blocks.darkPanel3;
if(Mathf.dst(mx, my, secSize/2, secSize/2) > secSize/2f + 1){
floor = Blocks.darkPanel4;
@ -140,7 +138,7 @@ public class MenuRenderer implements Disposable{
}
if(tendrils){
if(RidgedPerlin.noise2d(1 + offset, x, y, 1f / 17f) > 0f){
if(Ridged.noise2d(1 + offset, x, y, 1f / 17f) > 0f){
floor = Mathf.chance(0.2) ? Blocks.sporeMoss : Blocks.moss;
if(wall != Blocks.air){

View file

@ -12,7 +12,6 @@ public class SunMesh extends HexMesh{
public SunMesh(Planet planet, int divisions, double octaves, double persistence, double scl, double pow, double mag, float colorScale, Color... colors){
super(planet, new HexMesher(){
Simplex sim = new Simplex();
@Override
public float getHeight(Vec3 position){
@ -21,7 +20,7 @@ public class SunMesh extends HexMesh{
@Override
public Color getColor(Vec3 position){
double height = Math.pow(sim.octaveNoise3D(octaves, persistence, scl, position.x, position.y, position.z), pow) * mag;
double height = Math.pow(Simplex.noise3d(0, octaves, persistence, scl, position.x, position.y, position.z), pow) * mag;
return Tmp.c1.set(colors[Mathf.clamp((int)(height * colors.length), 0, colors.length - 1)]).mul(colorScale);
}
}, divisions, Shaders.unlit);

View file

@ -28,9 +28,6 @@ import static mindustry.Vars.*;
public class LExecutor{
public static final int maxInstructions = 1000;
//for noise operations
public static final Simplex noise = new Simplex();
//special variables
public static final int
varCounter = 0,

View file

@ -2,6 +2,7 @@ package mindustry.logic;
import arc.math.*;
import arc.util.*;
import arc.util.noise.*;
public enum LogicOp{
add("+", (a, b) -> a + b),
@ -32,7 +33,7 @@ public enum LogicOp{
min("min", true, Math::min),
angle("angle", true, (x, y) -> Angles.angle((float)x, (float)y)),
len("len", true, (x, y) -> Mathf.dst((float)x, (float)y)),
noise("noise", true, LExecutor.noise::rawNoise2D),
noise("noise", true, (x, y) -> Simplex.raw2d(0, x, y)),
abs("abs", a -> Math.abs(a)),
log("log", Math::log),
log10("log10", Math::log10),

View file

@ -103,19 +103,19 @@ public abstract class GenerateFilter{
//TODO would be nice if these functions used the seed and ditched "in" completely; simplex should be stateless
protected float noise(GenerateInput in, float scl, float mag){
return (float)in.noise.octaveNoise2D(1f, 0f, 1f / scl, in.x, in.y) * mag;
return (float)Simplex.noise2d(seed, 1f, 0f, 1f / scl, in.x, in.y) * mag;
}
protected float noise(GenerateInput in, float scl, float mag, float octaves, float persistence){
return (float)in.noise.octaveNoise2D(octaves, persistence, 1f / scl, in.x, in.y) * mag;
return (float)Simplex.noise2d(seed, octaves, persistence, 1f / scl, in.x, in.y) * mag;
}
protected float rnoise(float x, float y, float scl, float mag){
return RidgedPerlin.noise2d(seed + 1, (int)(x), (int)(y), 1f / scl) * mag;
return Ridged.noise2d(seed + 1, (int)(x), (int)(y), 1f / scl) * mag;
}
protected float rnoise(float x, float y, int octaves, float scl, float falloff, float mag){
return RidgedPerlin.noise2d(seed + 1, (int)(x), (int)(y), octaves, falloff, 1f / scl) * mag;
return Ridged.noise2d(seed + 1, (int)(x), (int)(y), octaves, falloff, 1f / scl) * mag;
}
protected float chance(int x, int y){
@ -131,7 +131,6 @@ public abstract class GenerateFilter{
/** output parameters */
public Block floor, block, overlay;
Simplex noise = new Simplex();
TileProvider buffer;
public void set(int x, int y, Block block, Block floor, Block overlay){
@ -146,7 +145,6 @@ public abstract class GenerateFilter{
this.buffer = buffer;
this.width = width;
this.height = height;
noise.setSeed(filter.seed);
}
Tile tile(float x, float y){

View file

@ -18,7 +18,6 @@ import static mindustry.Vars.*;
public abstract class PlanetGenerator extends BasicGenerator implements HexMesher{
protected IntSeq ints = new IntSeq();
protected Sector sector;
protected Simplex noise = new Simplex();
/** Should generate sector bases for a planet. */
public void generateSector(Sector sector){
@ -116,7 +115,7 @@ public abstract class PlanetGenerator extends BasicGenerator implements HexMeshe
@Override
protected float noise(float x, float y, double octaves, double falloff, double scl, double mag){
Vec3 v = sector.rect.project(x, y);
return (float)noise.octaveNoise3D(octaves, falloff, 1f / scl, v.x, v.y, v.z) * (float)mag;
return (float)Simplex.noise3d(0, octaves, falloff, 1f / scl, v.x, v.y, v.z) * (float)mag;
}
/** @return the scaling factor for sector rects. */

View file

@ -18,6 +18,8 @@ import mindustry.world.*;
import static mindustry.Vars.*;
public class SerpuloPlanetGenerator extends PlanetGenerator{
static final int seed = 0;
BaseGenerator basegen = new BaseGenerator();
float scl = 5f;
float waterOffset = 0.07f;
@ -55,7 +57,7 @@ public class SerpuloPlanetGenerator extends PlanetGenerator{
float rawHeight(Vec3 position){
position = Tmp.v33.set(position).scl(scl);
return (Mathf.pow((float)noise.octaveNoise3D(7, 0.5f, 1f/3f, position.x, position.y, position.z), 2.3f) + waterOffset) / (1f + waterOffset);
return (Mathf.pow((float)Simplex.noise3d(seed, 7, 0.5f, 1f/3f, position.x, position.y, position.z), 2.3f) + waterOffset) / (1f + waterOffset);
}
@Override
@ -114,7 +116,7 @@ public class SerpuloPlanetGenerator extends PlanetGenerator{
tile.floor = getBlock(position);
tile.block = tile.floor.asFloor().wall;
if(RidgedPerlin.noise3d(1, position.x, position.y, position.z, 2, 22) > 0.31){
if(Ridged.noise3d(1, position.x, position.y, position.z, 2, 22) > 0.31){
tile.block = Blocks.air;
}
}
@ -125,12 +127,12 @@ public class SerpuloPlanetGenerator extends PlanetGenerator{
position = Tmp.v33.set(position).scl(scl);
float rad = scl;
float temp = Mathf.clamp(Math.abs(position.y * 2f) / (rad));
float tnoise = (float)noise.octaveNoise3D(7, 0.56, 1f/3f, position.x, position.y + 999f, position.z);
float tnoise = (float)Simplex.noise3d(seed, 7, 0.56, 1f/3f, position.x, position.y + 999f, position.z);
temp = Mathf.lerp(temp, tnoise, 0.5f);
height *= 1.2f;
height = Mathf.clamp(height);
float tar = (float)noise.octaveNoise3D(4, 0.55f, 1f/2f, position.x, position.y + 999f, position.z) * 0.3f + Tmp.v31.dst(0, 0, 1f) * 0.2f;
float tar = (float)Simplex.noise3d(seed, 4, 0.55f, 1f/2f, position.x, position.y + 999f, position.z) * 0.3f + Tmp.v31.dst(0, 0, 1f) * 0.2f;
Block res = arr[Mathf.clamp((int)(temp * arr.length), 0, arr[0].length - 1)][Mathf.clamp((int)(height * arr[0].length), 0, arr[0].length - 1)];
if(tar > 0.5f){
@ -143,7 +145,7 @@ public class SerpuloPlanetGenerator extends PlanetGenerator{
@Override
protected float noise(float x, float y, double octaves, double falloff, double scl, double mag){
Vec3 v = sector.rect.project(x, y).scl(5f);
return (float)noise.octaveNoise3D(octaves, falloff, 1f / scl, v.x, v.y, v.z) * (float)mag;
return (float)Simplex.noise3d(seed, octaves, falloff, 1f / scl, v.x, v.y, v.z) * (float)mag;
}
@Override
@ -250,15 +252,15 @@ public class SerpuloPlanetGenerator extends PlanetGenerator{
float scl = 1f;
float addscl = 1.3f;
if(noise.octaveNoise3D(2, 0.5, scl, sector.tile.v.x, sector.tile.v.y, sector.tile.v.z)*nmag + poles > 0.25f*addscl){
if(Simplex.noise3d(seed, 2, 0.5, scl, sector.tile.v.x, sector.tile.v.y, sector.tile.v.z)*nmag + poles > 0.25f*addscl){
ores.add(Blocks.oreCoal);
}
if(noise.octaveNoise3D(2, 0.5, scl, sector.tile.v.x + 1, sector.tile.v.y, sector.tile.v.z)*nmag + poles > 0.5f*addscl){
if(Simplex.noise3d(seed, 2, 0.5, scl, sector.tile.v.x + 1, sector.tile.v.y, sector.tile.v.z)*nmag + poles > 0.5f*addscl){
ores.add(Blocks.oreTitanium);
}
if(noise.octaveNoise3D(2, 0.5, scl, sector.tile.v.x + 2, sector.tile.v.y, sector.tile.v.z)*nmag + poles > 0.7f*addscl){
if(Simplex.noise3d(seed, 2, 0.5, scl, sector.tile.v.x + 2, sector.tile.v.y, sector.tile.v.z)*nmag + poles > 0.7f*addscl){
ores.add(Blocks.oreThorium);
}

View file

@ -10,4 +10,4 @@ kapt.include.compile.classpath=false
kotlin.stdlib.default.dependency=false
#needed for android compilation
android.useAndroidX=true
archash=3926b785320fea0cd9ca597f6bfa9071263a5464
archash=07ced971f4c8b8b5a61aa3a84b29c90aa497cb48

View file

@ -176,7 +176,7 @@ public class Generators{
for(int x = 0; x < dim; x++){
for(int y = 0; y < dim; y++){
float dst = Mathf.dst((float)x/dim, (float)y/dim, 0.5f, 0.5f) * 2f;
if(dst < 1.2f && RidgedPerlin.noise2d(1, x, y, 3, 1f / 40f) - dst*(1f-fract) > 0.16f){
if(dst < 1.2f && Ridged.noise2d(1, x, y, 3, 1f / 40f) - dst*(1f-fract) > 0.16f){
image.setRaw(x, y, Color.whiteRgba);
}
}
@ -488,7 +488,7 @@ public class Generators{
image.each((x, y) -> {
//add darker cracks on top
boolean rValue = Math.max(RidgedPerlin.noise2d(1, x, y, 3, 1f / (20f + image.width/8f)), 0) > 0.16f;
boolean rValue = Math.max(Ridged.noise2d(1, x, y, 3, 1f / (20f + image.width/8f)), 0) > 0.16f;
//cut out random chunks with voronoi
boolean vval = vn.noise(x, y, 1f / (14f + image.width/40f)) > 0.47;
@ -601,14 +601,11 @@ public class Generators{
/** Generates a scorch pixmap based on parameters. Thread safe, unless multiple scorch generators are running in parallel. */
public static class ScorchGenerator{
private static final Simplex sim = new Simplex();
public int size = 80, seed = 0, color = Color.whiteRgba;
public double scale = 18, pow = 2, octaves = 4, pers = 0.4, add = 2, nscl = 4.5f;
public Pixmap generate(){
Pixmap pix = new Pixmap(size, size);
sim.setSeed(seed);
pix.each((x, y) -> {
double dst = Mathf.dst(x, y, size/2, size/2) / (size / 2f);
@ -621,7 +618,7 @@ public class Generators{
}
private double noise(float angle){
return Math.pow(sim.octaveNoise2D(octaves, pers, 1 / scale, Angles.trnsx(angle, size/2f) + size/2f, Angles.trnsy(angle, size/2f) + size/2f), pow);
return Math.pow(Simplex.noise2d(seed, octaves, pers, 1 / scale, Angles.trnsx(angle, size/2f) + size/2f, Angles.trnsy(angle, size/2f) + size/2f), pow);
}
}