Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve performance of facet providers #16

Merged
merged 6 commits into from Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/main/java/org/terasology/caves/CaveFacetProvider.java
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.caves;

import org.joml.Math;
import org.joml.Vector3ic;
import org.terasology.utilities.procedural.BrownianNoise;
import org.terasology.utilities.procedural.SimplexNoise;
Expand Down Expand Up @@ -41,15 +42,16 @@ public void process(GeneratingRegion region) {
CaveFacet facet = new CaveFacet(region.getRegion(), region.getBorderForFacet(CaveFacet.class));

// get noise in batch for performance reasons. Getting it by individual position takes 10 times as long
float[][] caveNoiseValues = new float[][]{caveNoise[0].noise(facet.getWorldRegion()),caveNoise[1].noise(facet.getWorldRegion())};
float[][] caveNoiseValues = new float[][]{caveNoise[0].noise(facet.getWorldRegion()), caveNoise[1].noise(facet.getWorldRegion())};

for (Vector3ic pos : facet.getWorldRegion()) {
float depth = elevationFacet.getWorld(pos.x(), pos.z()) - pos.y();
float frequencyReduction = (float) Math.max(0, 0.3 - Math.max(depth, 0) / 400); //0: no reduction, 0.7: pretty much no caves. Also somewhat increases the tendency of caves to loop rather than continuing indefinitely.
int i = facet.getWorldIndex(pos);
float noiseValue = (float) Math.hypot(caveNoiseValues[0][i], caveNoiseValues[1][i]+frequencyReduction);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is significantly slower.


facet.setWorld(pos, noiseValue < 0.06 + depth/2000f);
float xx = caveNoiseValues[0][i];
float yy = caveNoiseValues[1][i] + frequencyReduction;
float freqDepth = 0.06f + depth / 2000f;
facet.setWorld(pos, (xx * xx + yy * yy) < freqDepth * freqDepth);
}

region.setRegionFacet(CaveFacet.class, facet);
Expand Down
52 changes: 31 additions & 21 deletions src/main/java/org/terasology/caves/CaveToSurfaceProvider.java
Expand Up @@ -3,6 +3,7 @@

package org.terasology.caves;

import com.google.common.collect.Sets;
import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.terasology.world.generation.Facet;
Expand All @@ -16,7 +17,6 @@
import org.terasology.world.generation.facets.SurfacesFacet;
import org.terasology.world.generator.plugin.RegisterPlugin;

import java.util.HashSet;
import java.util.Set;

/**
Expand Down Expand Up @@ -47,26 +47,26 @@ public void process(GeneratingRegion region) {
SurfacesFacet surfacesFacet = region.getRegionFacet(SurfacesFacet.class);
SeaLevelFacet seaLevel = region.getRegionFacet(SeaLevelFacet.class);

Set<Vector3i> cavePositions = new HashSet<Vector3i>();
Set<Vector3ic> cavePositions = Sets.newHashSetWithExpectedSize((int) (0.33f * caveFacet.getWorldRegion().volume()));

for (Vector3ic pos : caveFacet.getWorldRegion()) {
if (caveFacet.getWorld(pos) && densityFacet.getWorldRegion().contains(pos) && surfacesFacet.getWorldRegion().contains(pos)) {
cavePositions.add(new Vector3i(pos));
}
}

Vector3i belowPos = new Vector3i();
// Ensure that the ocean can't immediately fall into a cave.
for (Vector3ic position : densityFacet.getWorldRegion()) {
Vector3i pos = new Vector3i(position);
for (Vector3ic pos : densityFacet.getWorldRegion()) {
if (densityFacet.getWorld(pos) <= 0) {
if (cavePositions.contains(pos)) {
cavePositions.remove(pos);
caveFacet.setWorld(pos, false);
}
if (pos.y <= seaLevel.getSeaLevel() + 1) {
if (pos.y() <= seaLevel.getSeaLevel() + 1) {
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
Vector3i belowPos = new Vector3i(pos.x + x, pos.y - 1, pos.z + z);
belowPos.set(pos.x() + x, pos.y() - 1, pos.z() + z);
if (cavePositions.contains(belowPos)) {
cavePositions.remove(belowPos);
caveFacet.setWorld(belowPos, false);
Expand All @@ -78,13 +78,13 @@ public void process(GeneratingRegion region) {
}

// Mark any cave floors exposed to the sky as surface.
Set<Vector3i> newSurfaces = new HashSet<>();
for (Vector3i pos : cavePositions) {
Set<Vector3i> newSurfaces = Sets.newHashSet();
for (Vector3ic pos : cavePositions) {
if (surfacesFacet.getWorld(pos)) {
surfacesFacet.setWorld(pos, false);
Vector3i newSurface = new Vector3i(pos);
while (cavePositions.contains(newSurface)) {
newSurface.add(0,-1,0);
newSurface.add(0, -1, 0);
}
if (newSurface.y >= surfacesFacet.getWorldRegion().minY()) {
newSurfaces.add(newSurface);
Expand All @@ -93,37 +93,47 @@ public void process(GeneratingRegion region) {
}
}

Vector3i a1 = new Vector3i();
Vector3i a2 = new Vector3i();
Vector3i a3 = new Vector3i();
Vector3i a4 = new Vector3i();


Set<Vector3i> newerSurfaces = Sets.newHashSetWithExpectedSize(newSurfaces.size());
// Mark cave floors near to those exposed to the sky as surface.
for (int i = 0; i < SURFACE_SPREAD; i++) {
Set<Vector3i> newerSurfaces = new HashSet<>();
newerSurfaces.clear();
for (Vector3i surface : newSurfaces) {
for (Vector3i adjacent : new Vector3i[]{
new Vector3i(surface).sub(1,0,0),
new Vector3i(surface).add(1,0,0),
new Vector3i(surface).sub(0,0,1),
new Vector3i(surface).add(0,0,1)
a1.set(surface).sub(1, 0, 0),
a2.set(surface).add(1, 0, 0),
a3.set(surface).sub(0, 0, 1),
a4.set(surface).add(0, 0, 1)
}) {
while (!cavePositions.contains(adjacent) && densityFacet.getWorldRegion().contains(adjacent) && densityFacet.getWorld(adjacent) > 0) {
adjacent.add(0,1,0);
adjacent.add(0, 1, 0);
}
// Only continue if the selected position is actually in a cave, rather than on the surface or above the selected region.
if (cavePositions.contains(adjacent)) {
while (cavePositions.contains(adjacent)) {
adjacent.sub(0,1,0);
adjacent.sub(0, 1, 0);
}
if (
surfacesFacet.getWorldRegion().contains(adjacent) &&
densityFacet.getWorldRegion().contains(adjacent) &&
densityFacet.getWorld(adjacent) > 0 &&
!surfacesFacet.getWorld(adjacent)
surfacesFacet.getWorldRegion().contains(adjacent) &&
densityFacet.getWorldRegion().contains(adjacent) &&
densityFacet.getWorld(adjacent) > 0 &&
!surfacesFacet.getWorld(adjacent)
) {
newerSurfaces.add(adjacent);
surface.set(adjacent); // reuse vector from last set
newerSurfaces.add(surface);
surfacesFacet.setWorld(adjacent, true);
}
}
}
}
Set<Vector3i> temp = newSurfaces;
newSurfaces = newerSurfaces;
newerSurfaces = temp;
}
}
}