foliage work
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2024-11-22 20:21:26 -05:00
parent eed0170894
commit 8c5874b6a3
6 changed files with 146 additions and 121 deletions

View File

@ -1,7 +1,13 @@
{
"name" : "test1",
"sampler" : {
"name" : "Invoke",
"target" : "test2"
"name" : "Add",
"first" : {
"name" : "Const",
"value" : 0.2
},
"second" : {
"name" : "OpenSimplex"
}
}
}

View File

@ -26,7 +26,7 @@
"name" : "Add",
"first" : {
"name" : "Const",
"value" : 1.0
"value" : 0.2
},
"second" : {
"name" : "OpenSimplex"

View File

@ -1120,6 +1120,11 @@ Add functionality for texture freeing from asset manager
Increase foliage chunk range
Swtich collision engine to use reentrant lock
(11/22/2024)
Foliage manager non-collision engine height lookups
Fix foliage manager not respecting graphics settings
Small noise sampling refactor
# TODO

View File

@ -173,7 +173,7 @@ public class FoliageCellManager {
*/
public void update(){
Globals.profiler.beginCpuSample("FoliageCellManager.update");
if(shouldUpdate && Globals.playerEntity != null){
if(shouldUpdate && Globals.playerEntity != null && Globals.userSettings.getGraphicsPerformanceEnableFoliageManager()){
Vector3d playerPos = EntityUtils.getPosition(Globals.playerEntity);
Vector3i absVoxelPos = Globals.clientWorldData.convertRealToAbsoluteVoxelSpace(playerPos);
int distCache = this.getDistCache(this.lastPlayerPos, absVoxelPos);

View File

@ -66,6 +66,16 @@ public class FoliageModel {
*/
static final int SINGLE_FOLIAGE_DATA_SIZE_BYTES = 3 * 4 + 2 * 4;
/**
* Cutoff to place foliage at, weight-wise
*/
static final double FOLIAGE_CUTOFF = 0.0;
/**
* Cutoff for the cummulative weight to place foliage at
*/
static final double FOLIAGE_CUMMULATIVE_WEIGHT_CUTOFF = 0;
/**
* The target number of foliage to place per cell
*/
@ -184,6 +194,7 @@ public class FoliageModel {
currWorldPos, currRealPos, currVoxelPos,
scale, placementRandomizer,
x, y, z,
data.getType(currVoxelPos.x,currVoxelPos.y,currVoxelPos.z),
floatBufferView, data
);
drawCount = drawCount + numGenerated;
@ -237,38 +248,97 @@ public class FoliageModel {
Vector3i worldPos, Vector3d realPos, Vector3i voxelPos,
int scale, Random placementRandomizer,
int vX, int vY, int vZ,
int targetVoxelType,
FloatBuffer floatBufferView, ChunkData chunkData
){
int rVal = 0;
//construct simple grid to place foliage on
Vector3d sample_00 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add(-SAMPLE_OFFSET,SAMPLE_START_HEIGHT,-SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
Vector3d sample_01 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add(-SAMPLE_OFFSET,SAMPLE_START_HEIGHT, 0), new Vector3d(0,-1,0), RAY_LENGTH);
Vector3d sample_02 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add(-SAMPLE_OFFSET,SAMPLE_START_HEIGHT, SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
Vector3d sample_10 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( 0,SAMPLE_START_HEIGHT,-SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
Vector3d sample_11 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( 0,SAMPLE_START_HEIGHT, 0), new Vector3d(0,-1,0), RAY_LENGTH);
Vector3d sample_12 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( 0,SAMPLE_START_HEIGHT, SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
Vector3d sample_20 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( SAMPLE_OFFSET,SAMPLE_START_HEIGHT,-SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
Vector3d sample_21 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( SAMPLE_OFFSET,SAMPLE_START_HEIGHT, 0), new Vector3d(0,-1,0), RAY_LENGTH);
Vector3d sample_22 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( SAMPLE_OFFSET,SAMPLE_START_HEIGHT, SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_00 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add(-SAMPLE_OFFSET,SAMPLE_START_HEIGHT,-SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_01 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add(-SAMPLE_OFFSET,SAMPLE_START_HEIGHT, 0), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_02 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add(-SAMPLE_OFFSET,SAMPLE_START_HEIGHT, SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_10 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( 0,SAMPLE_START_HEIGHT,-SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_11 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( 0,SAMPLE_START_HEIGHT, 0), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_12 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( 0,SAMPLE_START_HEIGHT, SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_20 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( SAMPLE_OFFSET,SAMPLE_START_HEIGHT,-SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_21 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( SAMPLE_OFFSET,SAMPLE_START_HEIGHT, 0), new Vector3d(0,-1,0), RAY_LENGTH);
// Vector3d sample_22 = Globals.clientSceneWrapper.getCollisionEngine().rayCastPosition(new Vector3d(realPos).add( SAMPLE_OFFSET,SAMPLE_START_HEIGHT, SAMPLE_OFFSET), new Vector3d(0,-1,0), RAY_LENGTH);
boolean sample_11 = true;
boolean sample_12 = true;
boolean sample_21 = true;
boolean sample_22 = true;
//get the heights of each sample
float height_11 = (float)(sample_11 != null ? sample_11.y : 0);
float height_00 = (float)(sample_00 != null ? sample_00.y : height_11);
float height_01 = (float)(sample_01 != null ? sample_01.y : height_11);
float height_02 = (float)(sample_02 != null ? sample_02.y : height_11);
float height_10 = (float)(sample_10 != null ? sample_10.y : height_11);
float height_12 = (float)(sample_12 != null ? sample_12.y : height_11);
float height_20 = (float)(sample_20 != null ? sample_20.y : height_11);
float height_21 = (float)(sample_21 != null ? sample_21.y : height_11);
float height_22 = (float)(sample_22 != null ? sample_22.y : height_11);
float weight_11 = (chunkData.getWeight(voxelPos) + 1) / 2.0f;
float height_11 = weight_11 + voxelPos.y + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
float height_12 = height_11;
float weight_12 = 0;
if(voxelPos.z < ServerTerrainChunk.CHUNK_DATA_GENERATOR_SIZE - 1){
if(voxelPos.y < ServerTerrainChunk.CHUNK_DATA_GENERATOR_SIZE - 1 && chunkData.getWeight(voxelPos.x, voxelPos.y + 1, voxelPos.z + 1) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x, voxelPos.y + 1, voxelPos.z + 1) == targetVoxelType){
weight_12 = (chunkData.getWeight(voxelPos.x, voxelPos.y + 1, voxelPos.z + 1) + 1) / 2.0f;
height_12 = weight_12 + voxelPos.y + 1 + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else if(chunkData.getWeight(voxelPos.x, voxelPos.y, voxelPos.z + 1) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x, voxelPos.y, voxelPos.z + 1) == targetVoxelType){
weight_12 = (chunkData.getWeight(voxelPos.x, voxelPos.y, voxelPos.z + 1) + 1) / 2.0f;
height_12 = weight_12 + voxelPos.y + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else if(voxelPos.y > 0 && chunkData.getWeight(voxelPos.x, voxelPos.y - 1, voxelPos.z + 1) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x, voxelPos.y - 1, voxelPos.z + 1) == targetVoxelType){
weight_12 = (chunkData.getWeight(voxelPos.x, voxelPos.y - 1, voxelPos.z + 1) + 1) / 2.0f;
height_12 = weight_12 + voxelPos.y - 1 + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else {
sample_12 = false;
}
} else {
sample_12 = false;
}
float height_21 = height_11;
float weight_21 = 0;
if(voxelPos.x < ServerTerrainChunk.CHUNK_DATA_GENERATOR_SIZE - 1){
if(voxelPos.y < ServerTerrainChunk.CHUNK_DATA_GENERATOR_SIZE - 1 && chunkData.getWeight(voxelPos.x + 1, voxelPos.y + 1, voxelPos.z) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x + 1, voxelPos.y + 1, voxelPos.z) == targetVoxelType){
weight_21 = (chunkData.getWeight(voxelPos.x + 1, voxelPos.y + 1, voxelPos.z) + 1) / 2.0f;
height_21 = weight_21 + voxelPos.y + 1 + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else if(chunkData.getWeight(voxelPos.x + 1, voxelPos.y, voxelPos.z) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x + 1, voxelPos.y, voxelPos.z) == targetVoxelType){
weight_21 = (chunkData.getWeight(voxelPos.x + 1, voxelPos.y, voxelPos.z) + 1) / 2.0f;
height_21 = weight_21 + voxelPos.y + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else if(voxelPos.y > 0 && chunkData.getWeight(voxelPos.x + 1, voxelPos.y - 1, voxelPos.z) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x + 1, voxelPos.y - 1, voxelPos.z) == targetVoxelType){
weight_21 = (chunkData.getWeight(voxelPos.x + 1, voxelPos.y - 1, voxelPos.z) + 1) / 2.0f;
height_21 = weight_21 + voxelPos.y - 1 + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else {
sample_21 = false;
}
} else {
sample_21 = false;
}
float height_22 = height_11;
float weight_22 = 0;
if(voxelPos.x < ServerTerrainChunk.CHUNK_DATA_GENERATOR_SIZE - 1 && voxelPos.z < ServerTerrainChunk.CHUNK_DATA_GENERATOR_SIZE - 1){
if(voxelPos.y < ServerTerrainChunk.CHUNK_DATA_GENERATOR_SIZE - 1 && chunkData.getWeight(voxelPos.x + 1, voxelPos.y + 1, voxelPos.z + 1) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x + 1, voxelPos.y + 1, voxelPos.z + 1) == targetVoxelType){
weight_22 = (chunkData.getWeight(voxelPos.x + 1, voxelPos.y + 1, voxelPos.z + 1) + 1) / 2.0f;
height_22 = weight_22 + voxelPos.y + 1 + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else if(chunkData.getWeight(voxelPos.x + 1, voxelPos.y, voxelPos.z + 1) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x + 1, voxelPos.y, voxelPos.z + 1) == targetVoxelType){
weight_22 = (chunkData.getWeight(voxelPos.x + 1, voxelPos.y, voxelPos.z + 1) + 1) / 2.0f;
height_22 = weight_22 + voxelPos.y + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else if(voxelPos.y > 0 && chunkData.getWeight(voxelPos.x + 1, voxelPos.y - 1, voxelPos.z + 1) > FOLIAGE_CUTOFF && chunkData.getType(voxelPos.x + 1, voxelPos.y - 1, voxelPos.z + 1) == targetVoxelType){
weight_22 = (chunkData.getWeight(voxelPos.x + 1, voxelPos.y - 1, voxelPos.z + 1) + 1) / 2.0f;
height_22 = weight_22 + voxelPos.y - 1 + worldPos.y * ServerTerrainChunk.CHUNK_DIMENSION;
} else {
sample_22 = false;
}
} else {
sample_22 = false;
}
//each height is in real world coordinates that are absolute
//when rendering, there's already a y offset for the center of the field of grass (based on the model matrix)
//so when offseting the position of the blade of grass RELATIVE to the overall instance being drawn, need to subtract the real world coordinates of the overall instance
//in other words realPos SPECIFICALLY for the y dimension, for x and z you don't need to worry about it
float cummulativeWeight = weight_11 + weight_12 + weight_21 + weight_22;
//if we don't find data for the center sample, can't place grass so don't create entity
if(sample_11 != null){
//generate positions to place
//generate positions to place
if(sample_11 && sample_12 && sample_21 && sample_22 && cummulativeWeight > FOLIAGE_CUMMULATIVE_WEIGHT_CUTOFF){
for(int x = 0; x < TARGET_FOLIAGE_SPACING; x=x+scale){
for(int z = 0; z < TARGET_FOLIAGE_SPACING; z=z+scale){
//get position to place
@ -276,81 +346,39 @@ public class FoliageModel {
double rand2 = placementRandomizer.nextDouble();
double relativePositionOnGridX = x / (1.0 * TARGET_FOLIAGE_SPACING) + rand1 / TARGET_FOLIAGE_SPACING;
double relativePositionOnGridZ = z / (1.0 * TARGET_FOLIAGE_SPACING) + rand2 / TARGET_FOLIAGE_SPACING;
double offsetX = relativePositionOnGridX - 0.5;
double offsetZ = relativePositionOnGridZ - 0.5;
//determine quadrant we're placing in
double offsetY = 0;
boolean addBlade = false;
if(relativePositionOnGridX >=0.5){
if(relativePositionOnGridZ >= 0.5){
relativePositionOnGridX = relativePositionOnGridX - 0.5;
relativePositionOnGridZ = relativePositionOnGridZ - 0.5;
relativePositionOnGridX /= 0.5;
relativePositionOnGridZ /= 0.5;
// System.out.println(relativePositionOnGridX + " " + relativePositionOnGridZ);
//if we have heights for all four surrounding spots, interpolate for y value
if(sample_11 != null && sample_12 != null && sample_21 != null && sample_22 != null){
offsetY =
height_11 * (1-relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_12 * (1-relativePositionOnGridX) * ( relativePositionOnGridZ) +
height_21 * ( relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_22 * ( relativePositionOnGridX) * ( relativePositionOnGridZ);
addBlade = true;
}
} else {
relativePositionOnGridX = relativePositionOnGridX - 0.5;
relativePositionOnGridX /= 0.5;
relativePositionOnGridZ /= 0.5;
//if we have heights for all four surrounding spots, interpolate for y value
if(sample_10 != null && sample_11 != null && sample_20 != null && sample_21 != null){
offsetY =
height_10 * (1-relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_11 * (1-relativePositionOnGridX) * ( relativePositionOnGridZ) +
height_20 * ( relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_21 * ( relativePositionOnGridX) * ( relativePositionOnGridZ);
addBlade = true;
}
}
} else {
if(relativePositionOnGridZ >= 0.5){
relativePositionOnGridZ = relativePositionOnGridZ - 0.5;
relativePositionOnGridX /= 0.5;
relativePositionOnGridZ /= 0.5;
//if we have heights for all four surrounding spots, interpolate for y value
if(sample_01 != null && sample_02 != null && sample_11 != null && sample_12 != null){
offsetY =
height_01 * (1-relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_02 * (1-relativePositionOnGridX) * ( relativePositionOnGridZ) +
height_11 * ( relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_12 * ( relativePositionOnGridX) * ( relativePositionOnGridZ);
addBlade = true;
}
} else {
relativePositionOnGridX /= 0.5;
relativePositionOnGridZ /= 0.5;
//if we have heights for all four surrounding spots, interpolate for y value
if(sample_00 != null && sample_01 != null && sample_10 != null && sample_11 != null){
offsetY =
height_00 * (1-relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_01 * (1-relativePositionOnGridX) * ( relativePositionOnGridZ) +
height_10 * ( relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_11 * ( relativePositionOnGridX) * ( relativePositionOnGridZ);
addBlade = true;
}
}
}
double manualAdjustment = -0.55;
// System.out.println(relativePositionOnGridX + " " + relativePositionOnGridZ);
//if we have heights for all four surrounding spots, interpolate for y value
offsetY =
height_11 * (1-relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_12 * (1-relativePositionOnGridX) * ( relativePositionOnGridZ) +
height_21 * ( relativePositionOnGridX) * (1-relativePositionOnGridZ) +
height_22 * ( relativePositionOnGridX) * ( relativePositionOnGridZ) +
manualAdjustment
;
addBlade = true;
// double percent =
// (1-relativePositionOnGridX) * (1-relativePositionOnGridZ) +
// (1-relativePositionOnGridX) * ( relativePositionOnGridZ) +
// ( relativePositionOnGridX) * (1-relativePositionOnGridZ) +
// ( relativePositionOnGridX) * ( relativePositionOnGridZ);
// if(
// percent < 0.99 || percent > 1.01
// ){
// System.out.println(percent);
// }
if(addBlade){
// if(realPos.y == 20 && sample_11 != null){
// System.out.println("asdf");
// }
//convert y to relative to chunk
offsetY = offsetY - realPos.y;
double rotVar = placementRandomizer.nextDouble() * Math.PI * 2;
double rotVar2 = placementRandomizer.nextDouble();
if(floatBufferView.limit() >= floatBufferView.position() + SINGLE_FOLIAGE_DATA_SIZE_BYTES / 4){
floatBufferView.put((float)offsetX + vX);
floatBufferView.put((float)relativePositionOnGridX + vX);
floatBufferView.put((float)offsetY + vY);
floatBufferView.put((float)offsetZ + vZ);
floatBufferView.put((float)relativePositionOnGridZ + vZ);
floatBufferView.put((float)rotVar);
floatBufferView.put((float)rotVar2);
rVal++;

View File

@ -1,6 +1,5 @@
package electrosphere.server.terrain.generation.voxelphase;
import electrosphere.engine.Globals;
import electrosphere.game.data.biome.BiomeData;
import electrosphere.game.data.voxel.sampler.SamplerFile;
import electrosphere.server.terrain.generation.interfaces.GeneratedVoxel;
@ -53,48 +52,35 @@ public class NoiseVoxelGen implements VoxelGenerator {
) {
double strideMultiplier = Math.pow(2,stride);
double heightDiff = realY - surfaceHeight;
Globals.profiler.endCpuSample();
double sample = this.sampler.getValue(0, realX, realY, realZ);
if(sample <= 0){
voxel.weight = -1.0f;
voxel.type = 0;
return;
}
if(heightDiff < -strideMultiplier * SURFACE_VOXEL_WIDTH){
//below surface
double sample = this.sampler.getValue(0, realX, realY, realZ);
if(sample > 0){
double finalSurface = MathUtils.clamp(sample,0,1);
voxel.weight = (float)finalSurface * 2 - 1;
voxel.type = 1;
} else {
voxel.weight = -1.0f;
voxel.type = 0;
}
double finalSurface = MathUtils.clamp(sample,0,1);
voxel.weight = (float)finalSurface * 2 - 1;
voxel.type = 1;
} else if(heightDiff > 0) {
//above surface
voxel.weight = -1.0f;
voxel.type = 0;
} else if(heightDiff < -strideMultiplier){
//generate full-size surface-type voxel
double sample = this.sampler.getValue(0, realX, realY, realZ);
if(sample > 0){
double finalHeight = MathUtils.clamp(sample,0,1);
voxel.weight = (float)finalHeight * 2 - 1;
voxel.type = 2;
} else {
voxel.weight = -1.0f;
voxel.type = 0;
}
double finalHeight = MathUtils.clamp(sample,0,1);
voxel.weight = (float)finalHeight * 2 - 1;
voxel.type = 2;
} else {
//surface
double sample = this.sampler.getValue(0, realX, realY, realZ);
if(sample > 0){
double surfacePercent = -heightDiff / strideMultiplier;
if(surfacePercent > 1.0 || surfacePercent < 0){
throw new Error("surfacePercent " + surfacePercent + " " + realY + " " + surfaceHeight + " " + heightDiff + " " + strideMultiplier);
}
double finalHeight = MathUtils.clamp(sample,0,1) * surfacePercent;
voxel.weight = (float)finalHeight * 2 - 1;
voxel.type = 2;
} else {
voxel.weight = -1.0f;
voxel.type = 0;
double surfacePercent = -heightDiff / strideMultiplier;
if(surfacePercent > 1.0 || surfacePercent < 0){
throw new Error("surfacePercent " + surfacePercent + " " + realY + " " + surfaceHeight + " " + heightDiff + " " + strideMultiplier);
}
double finalHeight = MathUtils.clamp(sample,0,1) * surfacePercent;
voxel.weight = (float)finalHeight * 2 - 1;
voxel.type = 2;
}
}