Skip to content

Commit

Permalink
SDFDisplacement
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaGupta1 committed Sep 7, 2021
1 parent bba8a27 commit 1b3fa79
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/sdoaj/jimgus/util/math/MathHelper.java
Expand Up @@ -53,6 +53,15 @@ public static float nextFloatAbs(Random random, float min, float max) {
return nextFloatOne(random) * (max - min) + min;
}

public static boolean chance(Random random, float chance) {
return random.nextFloat() < chance;
}

public static boolean chance(Random random, float min, float max) {
float c = random.nextFloat();
return min <= c && c < max;
}

public static float toRadians(float degrees) {
return degrees / 180 * PI;
}
Expand Down
@@ -0,0 +1,25 @@
package org.sdoaj.jimgus.util.sdf.operators;

import org.sdoaj.jimgus.util.math.Vec3f;

import java.util.function.Function;

// doesn't really work with some booleans and any SDFs that aren't linear (e.g. SDFLine with no caps)
public class SDFDisplacement extends SDFUnary {
// negative values expand the shape, positive values shrink the shape
private Function<Vec3f, Float> displacement;

public SDFDisplacement setDisplacement(float displacement) {
return setDisplacement(pos -> displacement);
}

public SDFDisplacement setDisplacement(Function<Vec3f, Float> displacement) {
this.displacement = displacement;
return this;
}

@Override
public float distance(Vec3f pos) {
return this.source.distance(pos) + displacement.apply(pos);
}
}
Expand Up @@ -11,6 +11,7 @@
import org.sdoaj.jimgus.util.Util;
import org.sdoaj.jimgus.util.math.MathHelper;
import org.sdoaj.jimgus.util.sdf.SDF;
import org.sdoaj.jimgus.util.sdf.operators.SDFDisplacement;
import org.sdoaj.jimgus.util.sdf.operators.SDFSubtraction;
import org.sdoaj.jimgus.util.sdf.operators.SDFTransform;
import org.sdoaj.jimgus.util.sdf.operators.SDFUnion;
Expand Down Expand Up @@ -70,6 +71,13 @@ public boolean place(FeaturePlaceContext<NoneFeatureConfiguration> context) {
island = new SDFTransform().scale(1, 1, MathHelper.nextFloat(random, 0.5f, 0.8f)).setSource(island);

island.fill(world, cloudPos);

if (!MathHelper.chance(random, pillarChance)) {
return true;
}

// TODO generate pillar

return true;
}
}

0 comments on commit 1b3fa79

Please sign in to comment.