diff --git a/resources/EngineResources/Texture/test_cube.png b/resources/EngineResources/Texture/test_cube.png index bc5571f..ee50980 100644 Binary files a/resources/EngineResources/Texture/test_cube.png and b/resources/EngineResources/Texture/test_cube.png differ diff --git a/resources/EngineResources/VoxelComputeShaders/meshGenerationShared.glsl b/resources/EngineResources/VoxelComputeShaders/meshGenerationShared.glsl index 95ea105..0ed1040 100644 --- a/resources/EngineResources/VoxelComputeShaders/meshGenerationShared.glsl +++ b/resources/EngineResources/VoxelComputeShaders/meshGenerationShared.glsl @@ -52,14 +52,68 @@ uint getVoxel(ivec3 pos) { return voxels[flatten(pos)]; } +uint hash21(uvec2 p) { + p = p * 1664525u + 1013904223u; + p.x += p.y * 1664525u; + p.y += p.x * 1664525u; + return p.x ^ (p.x >> 16); +} -uint packFace(uvec3 voxelPos, uint faceIndex, uint materialId, uint ao) { - return (voxelPos.x & 0x1Fu) | - ((voxelPos.y & 0x1Fu) << 5u) | - ((voxelPos.z & 0x1Fu) << 10u) | - ((faceIndex & 0x7u) << 15u) | - ((materialId & 0xFFu) << 18u) | - ((ao & 0x3u) << 26u); +uint random_1_to_3(vec2 uv, uint customSeed) { + uvec2 p = uvec2(floatBitsToUint(uv.x), floatBitsToUint(uv.y)); + p.x ^= customSeed; + uint randInt = hash21(p); + return (randInt % 3u) + 1u; +} + +uint packFace(uvec3 voxelPos, uint faceIndex, uint materialId) { + uint randomFaceValue = random_1_to_3(vec2(voxelPos.x, voxelPos.z),faceOffset); + uint textureIndexX = 0u; + uint textureIndexY = 0u; + if(materialId == 1u){ + if(faceIndex == 0u){ + textureIndexX = 0u; + textureIndexY = randomFaceValue; + } else if(faceIndex == 1u){ + textureIndexX = 1u; + textureIndexY = 0u; + } + else{ + textureIndexX = 0u; + textureIndexY = 0u; + } + } else if(materialId == 2u){ + textureIndexX = 1u; + textureIndexY = 0u; + }else if(materialId == 3u){ + textureIndexX = 1u; + textureIndexY = 0u; + }else if(materialId == 4u){ + textureIndexX = 1u; + textureIndexY = randomFaceValue; + }else if(materialId == 5u){ + textureIndexX = 2u; + textureIndexY = 3u; + }else if(materialId == 5u){ + if (faceIndex == 0u || faceIndex == 1u){ + textureIndexX = 2u; + textureIndexY = 1u; + } else{ + textureIndexX = 2u; + textureIndexY = 2u; + } + }else if(materialId == 6u){ + textureIndexX = 2u; + textureIndexY = 0u; + } + + return (voxelPos.x & 0x1Fu) | + ((voxelPos.y & 0x1Fu) << 5u) | + ((voxelPos.z & 0x1Fu) << 10u) | + ((faceIndex & 0x7u) << 15u) | + ((materialId & 0xFFu) << 18u) | + ((textureIndexX & 0x7u) << 26u) | + ((textureIndexY & 0x7u) << 29u); } void emitFace(ivec3 voxelPos, uint faceIndex, uint voxelType) { @@ -69,7 +123,7 @@ void emitFace(ivec3 voxelPos, uint faceIndex, uint voxelType) { return; } - faces[faceOffset + localFace] = packFace(uvec3(voxelPos), faceIndex, voxelType, 0u); + faces[faceOffset + localFace] = packFace(uvec3(voxelPos), faceIndex, voxelType); atomicAdd(drawCmds[indirectCommandIndex].vertexCount, VERTICES_PER_FACE); } @@ -87,10 +141,16 @@ void main() { return; } - if (getVoxel(pos + ivec3( 0, 1, 0)) == 0u) emitFace(pos, 0u, current); - if (getVoxel(pos + ivec3( 0, -1, 0)) == 0u) emitFace(pos, 1u, current); - if (getVoxel(pos + ivec3( 1, 0, 0)) == 0u) emitFace(pos, 2u, current); - if (getVoxel(pos + ivec3(-1, 0, 0)) == 0u) emitFace(pos, 3u, current); - if (getVoxel(pos + ivec3( 0, 0, 1)) == 0u) emitFace(pos, 4u, current); - if (getVoxel(pos + ivec3( 0, 0, -1)) == 0u) emitFace(pos, 5u, current); + if (current == 4u) { + emitFace(pos, 6u, current); + emitFace(pos, 7u, current); + return; + } + + if (getVoxel(pos + ivec3( 0, 1, 0)) == 0u || getVoxel(pos + ivec3( 0, 1, 0)) == 4u) emitFace(pos, 0u, current); + if (getVoxel(pos + ivec3( 0, -1, 0)) == 0u || getVoxel(pos + ivec3( 0, -1, 0)) == 4u) emitFace(pos, 1u, current); + if (getVoxel(pos + ivec3( 1, 0, 0)) == 0u || getVoxel(pos + ivec3( 1, 0, 0)) == 4u) emitFace(pos, 2u, current); + if (getVoxel(pos + ivec3(-1, 0, 0)) == 0u || getVoxel(pos + ivec3( -1, 0, 0)) == 4u) emitFace(pos, 3u, current); + if (getVoxel(pos + ivec3( 0, 0, 1)) == 0u|| getVoxel(pos + ivec3( 0, 0, 1)) == 4u) emitFace(pos, 4u, current); + if (getVoxel(pos + ivec3( 0, 0, -1)) == 0u || getVoxel(pos + ivec3( 0, 0, -1)) == 4u) emitFace(pos, 5u, current); } \ No newline at end of file diff --git a/resources/EngineResources/VoxelComputeShaders/voxelGenerationShared.glsl b/resources/EngineResources/VoxelComputeShaders/voxelGenerationShared.glsl index 493f74c..66011bb 100644 --- a/resources/EngineResources/VoxelComputeShaders/voxelGenerationShared.glsl +++ b/resources/EngineResources/VoxelComputeShaders/voxelGenerationShared.glsl @@ -101,7 +101,6 @@ bool isCave(vec3 worldPos) { void main() { ivec3 localPos = ivec3(gl_GlobalInvocationID.xyz); ivec3 worldPos = chunkPos * CHUNK_SIZE + localPos; - float YZone = floor(worldPos.y/150.0)*150.0; float islandheight = valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2) * 0.005) * 100 + valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2) * 0.1) * 20 + valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2) * 0.1) * 5+ valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2)) * 0.5 - 15 + YZone; float height = valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2) * 0.005) * 100 + valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2) * 0.01) * 10 + valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2) * 0.1) * 5+ valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2)) * 0.5 + YZone; @@ -109,6 +108,7 @@ void main() { float YZone2 = floor(worldPos.y/75.0)*75.0; float islandheight2 = valueNoise(vec2(worldPos.x - YZone2 * 2, worldPos.z -YZone2 * 2) * 0.005) * 60 + valueNoise(vec2(worldPos.x + YZone2 * 2, worldPos.z + YZone2 * 2) * 0.1) * 20 + valueNoise(vec2(worldPos.x + YZone2 * 2, worldPos.z + YZone2 * 2) * 0.1) * 10+ valueNoise(vec2(worldPos.x + YZone2 * 2, worldPos.z + YZone2 * 2)) * 0.5 - 5 + YZone2; float height2 = valueNoise(vec2(worldPos.x + YZone2 * 2, worldPos.z + YZone2 * 2) * 0.005) * 50 + valueNoise(vec2(worldPos.x + YZone2 * 2, worldPos.z + YZone2 * 2) * 0.01) * 10 + valueNoise(vec2(worldPos.x + YZone2 * 2, worldPos.z + YZone2 * 2) * 0.1) * 5+ valueNoise(vec2(worldPos.x + YZone2 * 2, worldPos.z + YZone2 * 2)) * 0.5 + YZone2; + float grassNoise = valueNoise(vec2(worldPos.z + YZone * 2, worldPos.x + YZone * 2)); //float height2 = valueNoise(vec2(-worldPos.x, -worldPos.z) * 0.005) * 100 + valueNoise(vec2(-worldPos.x, -worldPos.z) * 0.01) * 10 + valueNoise(vec2(-worldPos.x, -worldPos.z) * 0.1) * 5+ valueNoise(vec2(-worldPos.x, -worldPos.z)) * 0.5 + 100; //float islandheight2 = 100 + valueNoise(vec2(-worldPos.z, -worldPos.x) * 0.005) * 100 + valueNoise(vec2(-worldPos.x, -worldPos.z) * 0.1) * 20 + valueNoise(vec2(worldPos.x, worldPos.z) * 0.1) * 5+ valueNoise(vec2(worldPos.x, worldPos.z)) * 0.5 - 5; @@ -117,10 +117,12 @@ void main() { if (float(worldPos.y) <= height && float(worldPos.y) >= islandheight) { float depthBelowSurface = height - float(worldPos.y); - - if (depthBelowSurface < 1.5) { + if (depthBelowSurface < 0.5) { + if(grassNoise < 0.65) voxelType = 4u; // Grass foliage + else voxelType = 0u; + }else if (depthBelowSurface < 2.5) { voxelType = 1u; // Grass - } else if (depthBelowSurface < 5.0) { + } else if (depthBelowSurface < 6.0) { voxelType = 2u; // Dirt(also stone rn) } else { voxelType = 3u; // Stone @@ -129,10 +131,13 @@ void main() { else if (float(worldPos.y) <= height2 && float(worldPos.y) >= islandheight2) { float depthBelowSurface = height2 - float(worldPos.y); - if (depthBelowSurface < 1.5) { - voxelType = 1u; // Grass/topsoil - } else if (depthBelowSurface < 5.0) { - voxelType = 2u; // Dirt + if (depthBelowSurface < 0.5) { + if(grassNoise < 0.65) voxelType = 4u; // Grass foliage + else voxelType = 0u; + }else if (depthBelowSurface < 1.5) { + voxelType = 1u; // Grass + } else if (depthBelowSurface < 6.0) { + voxelType = 2u; // Dirt(also stone rn) } else { voxelType = 3u; // Stone } diff --git a/resources/EngineResources/shaders/packed_scene_vertex.glsl b/resources/EngineResources/shaders/packed_scene_vertex.glsl index 61b5eda..6e125f4 100644 --- a/resources/EngineResources/shaders/packed_scene_vertex.glsl +++ b/resources/EngineResources/shaders/packed_scene_vertex.glsl @@ -23,47 +23,67 @@ layout(push_constant) uniform pc { const uint TRI_TO_CORNER[6] = uint[6](0u, 1u, 2u, 0u, 2u, 3u); -const vec3 NORMALS[6] = vec3[6]( - vec3( 0.0, 1.0, 0.0), - vec3( 0.0, -1.0, 0.0), - vec3( 1.0, 0.0, 0.0), - vec3(-1.0, 0.0, 0.0), - vec3( 0.0, 0.0, 1.0), - vec3( 0.0, 0.0, -1.0) +const vec3 NORMALS[8] = vec3[8]( + vec3( 0.0, 1.0, 0.0), // Face 0 (+Y) + vec3( 0.0, -1.0, 0.0), // Face 1 (-Y) + vec3( 1.0, 0.0, 0.0), // Face 2 (+X) + vec3(-1.0, 0.0, 0.0), // Face 3 (-X) + vec3( 0.0, 0.0, 1.0), // Face 4 (+Z) + vec3( 0.0, 0.0, -1.0), // Face 5 (-Z) + vec3(-0.7071, 0.0, 0.7071), // Face 6 + vec3( 0.7071, 0.0, 0.7071) // Face 7 ); -const vec3 TANGENTS[6] = vec3[6]( - vec3( 1.0, 0.0, 0.0), - vec3( 1.0, 0.0, 0.0), - vec3( 0.0, 0.0, -1.0), - vec3( 0.0, 0.0, 1.0), - vec3( 1.0, 0.0, 0.0), - vec3(-1.0, 0.0, 0.0) +const vec3 TANGENTS[8] = vec3[8]( + vec3( 1.0, 0.0, 0.0), // Face 0 + vec3( 1.0, 0.0, 0.0), // Face 1 + vec3( 0.0, 0.0, -1.0), // Face 2 + vec3( 0.0, 0.0, 1.0), // Face 3 + vec3( 1.0, 0.0, 0.0), // Face 4 + vec3(-1.0, 0.0, 0.0), // Face 5 + vec3( 0.7071, 0.0, 0.7071), // Face 6 + vec3( 0.7071, 0.0,-0.7071) // Face 7 ); -const vec3 BITANGENTS[6] = vec3[6]( - vec3( 0.0, 0.0, 1.0), - vec3( 0.0, 0.0, -1.0), - vec3( 0.0, 1.0, 0.0), - vec3( 0.0, 1.0, 0.0), - vec3( 0.0, 1.0, 0.0), - vec3( 0.0, 1.0, 0.0) +const vec3 BITANGENTS[8] = vec3[8]( + vec3( 0.0, 0.0, 1.0), // Face 0 + vec3( 0.0, 0.0, -1.0), // Face 1 + vec3( 0.0, 1.0, 0.0), // Face 2 + vec3( 0.0, 1.0, 0.0), // Face 3 + vec3( 0.0, 1.0, 0.0), // Face 4 + vec3( 0.0, 1.0, 0.0), // Face 5 + vec3( 0.0, 1.0, 0.0), // Face 6 + vec3( 0.0, 1.0, 0.0) // Face 7 ); -const vec2 CORNER_UVS[16] = vec2[16]( - vec2(0.5, 0.0), vec2(0.5, 0.5), vec2(1.0, 0.5), vec2(1.0, 0.0), - vec2(0.0, 0.5), vec2(0.0, 1.0), vec2(0.5, 1.0), vec2(0.5, 0.5), - vec2(0.5, 0.0), vec2(0.5, 0.5), vec2(1.0, 0.5), vec2(1.0, 0.0), - vec2(0.5, 0.0), vec2(0.5, 0.5), vec2(1.0, 0.5), vec2(1.0, 0.0) +const vec2 CORNER_UVS[5][4] = vec2[5][4]( + vec2[4](vec2(0.5, 0.0), vec2(0.5, 0.5), vec2(1.0, 0.5), vec2(1.0, 0.0)), + vec2[4](vec2(0.0, 0.5), vec2(0.0, 1.0), vec2(0.5, 1.0), vec2(0.5, 0.5)), + vec2[4](vec2(0.5, 0.0), vec2(0.5, 0.5), vec2(1.0, 0.5), vec2(1.0, 0.0)), + vec2[4](vec2(0.5, 0.0), vec2(0.5, 0.5), vec2(1.0, 0.5), vec2(1.0, 0.0)), + vec2[4](vec2(1.0, 1.0), vec2(1.0, 0.5), vec2(0.5, 0.5), vec2(0.5, 1.0)) +); +const vec2 CORNER_UVS_GRASS_BLOCK[8][4] = vec2[8][4]( + vec2[4](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0)), + vec2[4](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0)), + vec2[4](vec2(1.0, 1.0), vec2(1.0, 0.0),vec2(0.0, 0.0), vec2(0.0, 1.0)), + vec2[4]( vec2(0.0, 1.0),vec2(1.0, 1.0), vec2(1.0, 0.0),vec2(0.0, 0.0)), + vec2[4]( vec2(0.0, 1.0),vec2(1.0, 1.0), vec2(1.0, 0.0),vec2(0.0, 0.0)), + vec2[4](vec2(1.0, 1.0), vec2(1.0, 0.0),vec2(0.0, 0.0), vec2(0.0, 1.0)), + vec2[4](vec2(1.0, 1.0), vec2(1.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 1.0)), + vec2[4](vec2(1.0, 1.0), vec2(1.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 1.0)) + ); -const vec3 FACE_CORNERS[6][4] = vec3[6][4]( +const vec3 FACE_CORNERS[8][4] = vec3[8][4]( vec3[4](vec3(0.0, 1.0, 0.0), vec3(0.0, 1.0, 1.0), vec3(1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0)), // Face 0 (+Y) vec3[4](vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), vec3(1.0, 0.0, 1.0), vec3(0.0, 0.0, 1.0)), // Face 1 (-Y) vec3[4](vec3(1.0, 0.0, 0.0), vec3(1.0, 1.0, 0.0), vec3(1.0, 1.0, 1.0), vec3(1.0, 0.0, 1.0)), // Face 2 (+X) vec3[4](vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0), vec3(0.0, 1.0, 1.0), vec3(0.0, 1.0, 0.0)), // Face 3 (-X) vec3[4](vec3(0.0, 0.0, 1.0), vec3(1.0, 0.0, 1.0), vec3(1.0, 1.0, 1.0), vec3(0.0, 1.0, 1.0)), // Face 4 (+Z) - vec3[4](vec3(0.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(1.0, 1.0, 0.0), vec3(1.0, 0.0, 0.0)) // Face 5 (-Z) + vec3[4](vec3(0.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(1.0, 1.0, 0.0), vec3(1.0, 0.0, 0.0)), // Face 5 (-Z) + vec3[4](vec3(0.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(1.0, 1.0, 1.0), vec3(1.0, 0.0, 1.0)), // Face 6 (cross model pane 1) + vec3[4](vec3(0.0, 0.0, 1.0), vec3(0.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0), vec3(1.0, 0.0, 0.0)) // Face 7 (cross model pane 2) ); void main() { @@ -84,9 +104,14 @@ void main() { uint voxelZ = (packedFace >> 10u) & 0x1Fu; uint faceId = (packedFace >> 15u) & 0x7u; uint matId = (packedFace >> 18u) & 0xFFu; + uint texIdxX = (packedFace >> 26u) & 0x7u; + uint texIdxY = (packedFace >> 29u) & 0x7u; - faceId = min(faceId, 5u); - matId = min(matId, 3u); + vec2 texID = vec2((texIdxX)/4.0,(texIdxY)/4.0); + vec2 scalar = vec2(1.0/4.0,1.0/4.0); + + faceId = min(faceId, 7u); + matId = min(matId, 4u); vec3 localPos = vec3(voxelX, voxelY, voxelZ) + FACE_CORNERS[faceId][cornerIndex]; vec3 worldPos = localPos + push_constants.ModelOffset.xyz * 16.0; @@ -99,5 +124,5 @@ void main() { outNormal = NORMALS[faceId]; outTangent = TANGENTS[faceId]; outBitangent = BITANGENTS[faceId]; - outTextCoords = CORNER_UVS[cornerIndex + (matId << 2u)]; + outTextCoords = CORNER_UVS_GRASS_BLOCK[faceId][cornerIndex] * scalar + texID; } \ No newline at end of file diff --git a/src/main/java/net/halbear/Terrain4J/EngineCore/Main/GameCore.java b/src/main/java/net/halbear/Terrain4J/EngineCore/Main/GameCore.java index 6f3b29d..6ea59bc 100644 --- a/src/main/java/net/halbear/Terrain4J/EngineCore/Main/GameCore.java +++ b/src/main/java/net/halbear/Terrain4J/EngineCore/Main/GameCore.java @@ -656,22 +656,12 @@ public class GameCore implements GameLogic { return imGuiIO.getWantCaptureKeyboard(); } Vector3f CamPos = new Vector3f(); + Vector3f LastCamRotation = new Vector3f(); @Override public void Update(EngineInstance engineInstance, long FrameDiffNanoSeconds) { if(!PrimaryRuntime.IsServer && !RenderThread.Headless) { Camera cam = engineInstance.scene().GetCamera(); - Project3D projection = engineInstance.scene().GetProjection(); CamPos.set(cam.GetPosition()); - Vector3i ChunkPos = new Vector3i((int)Math.floor(CamPos.x/ (float) VoxelChunkGenerator.CHUNK_SIZE),(int)Math.floor(CamPos.y/ (float)VoxelChunkGenerator.CHUNK_SIZE),(int)Math.floor(CamPos.z/ (float)VoxelChunkGenerator.CHUNK_SIZE)); - // if(ChunkPos.x != VoxelWorldManager.LastPosition.x || ChunkPos.y != VoxelWorldManager.LastPosition.y || ChunkPos.z != VoxelWorldManager.LastPosition.z) { - // VoxelWorldManager.GenerateChunks(ChunkPos, new Vector3i(5, 5, 5)); - // VoxelWorldManager.UnloadFarChunks(ChunkPos, 10); - //} else { - //} - - for (int i = 0; i < VisualisedCollisionActor3D.CollisionVisuals.size(); i++) { - VisualisedCollisionActor3D.CollisionVisuals.get(i).Update(); - } for(int i = 0; i < Actor3DElement.ElementNames.size(); i++){ if( Actor3DElement.ActorElementRegistry.get(Actor3DElement.ElementNames.get(i)) == null) continue; Actor3DElement.ActorElementRegistry.get(Actor3DElement.ElementNames.get(i)).UpdateModelMatrix(); @@ -688,9 +678,16 @@ public class GameCore implements GameLogic { float DeltaTime = (float)FrameDiffNanoSeconds/(PhysicsFrameTime*1000000f); Camera cam = engineInstance.scene().GetCamera(); Project3D projection = engineInstance.scene().GetProjection(); + Vector3f CamRot = cam.GetRotation(); Vector3i ChunkPos = new Vector3i((int)Math.floor(CamPos.x/ (float) VoxelChunkGenerator.CHUNK_SIZE),(int)Math.floor(CamPos.y/ (float)VoxelChunkGenerator.CHUNK_SIZE),(int)Math.floor(CamPos.z/ (float)VoxelChunkGenerator.CHUNK_SIZE)); - VoxelWorldManager.UnloadFarChunks(ChunkPos, 12,cam.GetViewMatrix(),projection.GetProjectionMatrix()); - VoxelWorldManager.GenerateChunks(ChunkPos, new Vector3i(12,6,12),cam.GetViewMatrix(),projection.GetProjectionMatrix()); + if(ChunkPos.x != VoxelWorldManager.LastPosition.x || ChunkPos.y != VoxelWorldManager.LastPosition.y || ChunkPos.z != VoxelWorldManager.LastPosition.z || CamRot.x != LastCamRotation.x || CamRot.y != LastCamRotation.y || CamRot.z != LastCamRotation.z) { + VoxelWorldManager.UnloadFarChunks(ChunkPos, 20,cam.GetViewMatrix(),projection.GetProjectionMatrix()); + VoxelWorldManager.GenerateChunks(ChunkPos, new Vector3i(20,8,20),cam.GetViewMatrix(),projection.GetProjectionMatrix()); + } else { + VoxelWorldManager.UnloadFarChunks(ChunkPos, 64,cam.GetViewMatrix(),projection.GetProjectionMatrix()); + VoxelWorldManager.GenerateChunks(ChunkPos, new Vector3i(32,12,32),cam.GetViewMatrix(),projection.GetProjectionMatrix()); + } + LastCamRotation.set(CamRot); if(PrimaryRuntime.IsServer){ /* if(UseLegacyNetworking) Server.LegacyCollectIncomingPackets(); else*/ //Server.CollectIncomingPackets(); diff --git a/src/main/java/net/halbear/Terrain4J/EngineCore/RenderingAPI/Vulkan/Computing/VoxelTerrainGeneration/VoxelWorldManager.java b/src/main/java/net/halbear/Terrain4J/EngineCore/RenderingAPI/Vulkan/Computing/VoxelTerrainGeneration/VoxelWorldManager.java index a7d0562..ca06519 100644 --- a/src/main/java/net/halbear/Terrain4J/EngineCore/RenderingAPI/Vulkan/Computing/VoxelTerrainGeneration/VoxelWorldManager.java +++ b/src/main/java/net/halbear/Terrain4J/EngineCore/RenderingAPI/Vulkan/Computing/VoxelTerrainGeneration/VoxelWorldManager.java @@ -70,7 +70,7 @@ public class VoxelWorldManager { private static Vector3f min = new Vector3f(); private static Vector3f min2 = new Vector3f(); - public static void GenerateChunks(Vector3i Position, Vector3i Radius, Matrix4f cameraView,Matrix4f projectionMatrix) { + public static synchronized void GenerateChunks(Vector3i Position, Vector3i Radius, Matrix4f cameraView,Matrix4f projectionMatrix) { projectionMatrix.mul(cameraView, viewProjectionMatrix); frustumIntersection.set(viewProjectionMatrix); @@ -190,25 +190,28 @@ public class VoxelWorldManager { } - public static void UnloadFarChunks(Vector3i centerChunk, int unloadRadius,Matrix4f cameraView,Matrix4f projectionMatrix) { + public static synchronized void UnloadFarChunks(Vector3i centerChunk, int unloadRadius,Matrix4f cameraView,Matrix4f projectionMatrix) { int maxDistSq = unloadRadius * unloadRadius; - + projectionMatrix.mul(cameraView, viewProjectionMatrix); + frustumIntersection.set(viewProjectionMatrix); RenderChunks.entrySet().removeIf(entry -> { Vector3i pos = entry.getKey(); - - projectionMatrix.mul(cameraView, viewProjectionMatrix); - frustumIntersection.set(viewProjectionMatrix); min.set(pos.x * CHUNK_SIZE, pos.y * CHUNK_SIZE, pos.z * CHUNK_SIZE); max.set(min.x + CHUNK_SIZE, min.y + CHUNK_SIZE, min.z + CHUNK_SIZE); - + boolean InView = frustumIntersection.testAab(min.x, min.y, min.z, max.x, max.y, max.z); + if (!InView) { + RenderChunk chunk = entry.getValue(); + Resources.meshPool().free(chunk); + KnownChunks.remove(pos); + return true; + } int dx = pos.x - centerChunk.x; int dy = pos.y - centerChunk.y; int dz = pos.z - centerChunk.z; int distSq = dx * dx + dy * dy + dz * dz; - boolean InView = frustumIntersection.testAab(min.x, min.y, min.z, max.x, max.y, max.z); - if (distSq > maxDistSq || !InView) { + if (distSq > maxDistSq) { RenderChunk chunk = entry.getValue(); Resources.meshPool().free(chunk); diff --git a/src/main/java/net/halbear/Terrain4J/EngineCore/RenderingAPI/Vulkan/Rendering/RenderPasses/Lighting/LightRenderer.java b/src/main/java/net/halbear/Terrain4J/EngineCore/RenderingAPI/Vulkan/Rendering/RenderPasses/Lighting/LightRenderer.java index f55ed77..8470029 100644 --- a/src/main/java/net/halbear/Terrain4J/EngineCore/RenderingAPI/Vulkan/Rendering/RenderPasses/Lighting/LightRenderer.java +++ b/src/main/java/net/halbear/Terrain4J/EngineCore/RenderingAPI/Vulkan/Rendering/RenderPasses/Lighting/LightRenderer.java @@ -201,12 +201,10 @@ public class LightRenderer { private static ShaderModule[] CreateShaderModules(VulkanContext VkCtx, LightSpecConsts lightSpecConsts, boolean shadowPipeline) { if (EngineConfig.getInstance().RecompileShaders()) { ShaderCompiler.CompileGLSLShaderOnChange(VERTEX_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_vertex_shader); - //ShaderCompiler.CompileGLSLShaderOnChange( FRAGMENT_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_fragment_shader); ShaderCompiler.CompileGLSLShaderOnChange(shadowPipeline ? FRAGMENT_SHADER_FILE_GLSL : NO_SHADOWS_FRAGMENT_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_fragment_shader); } return new ShaderModule[]{ new ShaderModule(VkCtx, VK_SHADER_STAGE_VERTEX_BIT, VERTEX_SHADER_FILE_SPV, null), - //new ShaderModule(VkCtx, VK_SHADER_STAGE_FRAGMENT_BIT, FRAGMENT_SHADER_FILE_SPV, lightSpecConsts.getSpecInfo()), new ShaderModule(VkCtx, VK_SHADER_STAGE_FRAGMENT_BIT, shadowPipeline ? FRAGMENT_SHADER_FILE_SPV : NO_SHADOWS_FRAGMENT_SHADER_FILE_SPV, shadowPipeline ? lightSpecConsts.getSpecInfo() : null), }; }