voxel texture mapping

This commit is contained in:
Halbear 2026-09-17 21:48:04 +01:00
parent 4565188f15
commit 0074bf1d1f
7 changed files with 165 additions and 77 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

Before After
Before After

View file

@ -52,14 +52,68 @@ uint getVoxel(ivec3 pos) {
return voxels[flatten(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) { uint random_1_to_3(vec2 uv, uint customSeed) {
return (voxelPos.x & 0x1Fu) | uvec2 p = uvec2(floatBitsToUint(uv.x), floatBitsToUint(uv.y));
((voxelPos.y & 0x1Fu) << 5u) | p.x ^= customSeed;
((voxelPos.z & 0x1Fu) << 10u) | uint randInt = hash21(p);
((faceIndex & 0x7u) << 15u) | return (randInt % 3u) + 1u;
((materialId & 0xFFu) << 18u) | }
((ao & 0x3u) << 26u);
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) { void emitFace(ivec3 voxelPos, uint faceIndex, uint voxelType) {
@ -69,7 +123,7 @@ void emitFace(ivec3 voxelPos, uint faceIndex, uint voxelType) {
return; 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); atomicAdd(drawCmds[indirectCommandIndex].vertexCount, VERTICES_PER_FACE);
} }
@ -87,10 +141,16 @@ void main() {
return; return;
} }
if (getVoxel(pos + ivec3( 0, 1, 0)) == 0u) emitFace(pos, 0u, current); if (current == 4u) {
if (getVoxel(pos + ivec3( 0, -1, 0)) == 0u) emitFace(pos, 1u, current); emitFace(pos, 6u, current);
if (getVoxel(pos + ivec3( 1, 0, 0)) == 0u) emitFace(pos, 2u, current); emitFace(pos, 7u, current);
if (getVoxel(pos + ivec3(-1, 0, 0)) == 0u) emitFace(pos, 3u, current); return;
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 (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);
} }

View file

@ -101,7 +101,6 @@ bool isCave(vec3 worldPos) {
void main() { void main() {
ivec3 localPos = ivec3(gl_GlobalInvocationID.xyz); ivec3 localPos = ivec3(gl_GlobalInvocationID.xyz);
ivec3 worldPos = chunkPos * CHUNK_SIZE + localPos; ivec3 worldPos = chunkPos * CHUNK_SIZE + localPos;
float YZone = floor(worldPos.y/150.0)*150.0; 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 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; 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 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 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 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 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; //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) { if (float(worldPos.y) <= height && float(worldPos.y) >= islandheight) {
float depthBelowSurface = height - float(worldPos.y); float depthBelowSurface = height - float(worldPos.y);
if (depthBelowSurface < 0.5) {
if (depthBelowSurface < 1.5) { if(grassNoise < 0.65) voxelType = 4u; // Grass foliage
else voxelType = 0u;
}else if (depthBelowSurface < 2.5) {
voxelType = 1u; // Grass voxelType = 1u; // Grass
} else if (depthBelowSurface < 5.0) { } else if (depthBelowSurface < 6.0) {
voxelType = 2u; // Dirt(also stone rn) voxelType = 2u; // Dirt(also stone rn)
} else { } else {
voxelType = 3u; // Stone voxelType = 3u; // Stone
@ -129,10 +131,13 @@ void main() {
else if (float(worldPos.y) <= height2 && float(worldPos.y) >= islandheight2) { else if (float(worldPos.y) <= height2 && float(worldPos.y) >= islandheight2) {
float depthBelowSurface = height2 - float(worldPos.y); float depthBelowSurface = height2 - float(worldPos.y);
if (depthBelowSurface < 1.5) { if (depthBelowSurface < 0.5) {
voxelType = 1u; // Grass/topsoil if(grassNoise < 0.65) voxelType = 4u; // Grass foliage
} else if (depthBelowSurface < 5.0) { else voxelType = 0u;
voxelType = 2u; // Dirt }else if (depthBelowSurface < 1.5) {
voxelType = 1u; // Grass
} else if (depthBelowSurface < 6.0) {
voxelType = 2u; // Dirt(also stone rn)
} else { } else {
voxelType = 3u; // Stone voxelType = 3u; // Stone
} }

View file

@ -23,47 +23,67 @@ layout(push_constant) uniform pc {
const uint TRI_TO_CORNER[6] = uint[6](0u, 1u, 2u, 0u, 2u, 3u); const uint TRI_TO_CORNER[6] = uint[6](0u, 1u, 2u, 0u, 2u, 3u);
const vec3 NORMALS[6] = vec3[6]( const vec3 NORMALS[8] = vec3[8](
vec3( 0.0, 1.0, 0.0), vec3( 0.0, 1.0, 0.0), // Face 0 (+Y)
vec3( 0.0, -1.0, 0.0), vec3( 0.0, -1.0, 0.0), // Face 1 (-Y)
vec3( 1.0, 0.0, 0.0), vec3( 1.0, 0.0, 0.0), // Face 2 (+X)
vec3(-1.0, 0.0, 0.0), vec3(-1.0, 0.0, 0.0), // Face 3 (-X)
vec3( 0.0, 0.0, 1.0), vec3( 0.0, 0.0, 1.0), // Face 4 (+Z)
vec3( 0.0, 0.0, -1.0) 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]( const vec3 TANGENTS[8] = vec3[8](
vec3( 1.0, 0.0, 0.0), vec3( 1.0, 0.0, 0.0), // Face 0
vec3( 1.0, 0.0, 0.0), vec3( 1.0, 0.0, 0.0), // Face 1
vec3( 0.0, 0.0, -1.0), vec3( 0.0, 0.0, -1.0), // Face 2
vec3( 0.0, 0.0, 1.0), vec3( 0.0, 0.0, 1.0), // Face 3
vec3( 1.0, 0.0, 0.0), vec3( 1.0, 0.0, 0.0), // Face 4
vec3(-1.0, 0.0, 0.0) 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]( const vec3 BITANGENTS[8] = vec3[8](
vec3( 0.0, 0.0, 1.0), vec3( 0.0, 0.0, 1.0), // Face 0
vec3( 0.0, 0.0, -1.0), vec3( 0.0, 0.0, -1.0), // Face 1
vec3( 0.0, 1.0, 0.0), vec3( 0.0, 1.0, 0.0), // Face 2
vec3( 0.0, 1.0, 0.0), vec3( 0.0, 1.0, 0.0), // Face 3
vec3( 0.0, 1.0, 0.0), vec3( 0.0, 1.0, 0.0), // Face 4
vec3( 0.0, 1.0, 0.0) 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]( const vec2 CORNER_UVS[5][4] = vec2[5][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(0.0, 0.5), vec2(0.0, 1.0), vec2(0.5, 1.0), vec2(0.5, 0.5), vec2[4](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[4](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) 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, 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(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(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, 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, 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() { void main() {
@ -84,9 +104,14 @@ void main() {
uint voxelZ = (packedFace >> 10u) & 0x1Fu; uint voxelZ = (packedFace >> 10u) & 0x1Fu;
uint faceId = (packedFace >> 15u) & 0x7u; uint faceId = (packedFace >> 15u) & 0x7u;
uint matId = (packedFace >> 18u) & 0xFFu; uint matId = (packedFace >> 18u) & 0xFFu;
uint texIdxX = (packedFace >> 26u) & 0x7u;
uint texIdxY = (packedFace >> 29u) & 0x7u;
faceId = min(faceId, 5u); vec2 texID = vec2((texIdxX)/4.0,(texIdxY)/4.0);
matId = min(matId, 3u); 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 localPos = vec3(voxelX, voxelY, voxelZ) + FACE_CORNERS[faceId][cornerIndex];
vec3 worldPos = localPos + push_constants.ModelOffset.xyz * 16.0; vec3 worldPos = localPos + push_constants.ModelOffset.xyz * 16.0;
@ -99,5 +124,5 @@ void main() {
outNormal = NORMALS[faceId]; outNormal = NORMALS[faceId];
outTangent = TANGENTS[faceId]; outTangent = TANGENTS[faceId];
outBitangent = BITANGENTS[faceId]; outBitangent = BITANGENTS[faceId];
outTextCoords = CORNER_UVS[cornerIndex + (matId << 2u)]; outTextCoords = CORNER_UVS_GRASS_BLOCK[faceId][cornerIndex] * scalar + texID;
} }

View file

@ -656,22 +656,12 @@ public class GameCore implements GameLogic {
return imGuiIO.getWantCaptureKeyboard(); return imGuiIO.getWantCaptureKeyboard();
} }
Vector3f CamPos = new Vector3f(); Vector3f CamPos = new Vector3f();
Vector3f LastCamRotation = new Vector3f();
@Override @Override
public void Update(EngineInstance engineInstance, long FrameDiffNanoSeconds) { public void Update(EngineInstance engineInstance, long FrameDiffNanoSeconds) {
if(!PrimaryRuntime.IsServer && !RenderThread.Headless) { if(!PrimaryRuntime.IsServer && !RenderThread.Headless) {
Camera cam = engineInstance.scene().GetCamera(); Camera cam = engineInstance.scene().GetCamera();
Project3D projection = engineInstance.scene().GetProjection();
CamPos.set(cam.GetPosition()); 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++){ for(int i = 0; i < Actor3DElement.ElementNames.size(); i++){
if( Actor3DElement.ActorElementRegistry.get(Actor3DElement.ElementNames.get(i)) == null) continue; if( Actor3DElement.ActorElementRegistry.get(Actor3DElement.ElementNames.get(i)) == null) continue;
Actor3DElement.ActorElementRegistry.get(Actor3DElement.ElementNames.get(i)).UpdateModelMatrix(); Actor3DElement.ActorElementRegistry.get(Actor3DElement.ElementNames.get(i)).UpdateModelMatrix();
@ -688,9 +678,16 @@ public class GameCore implements GameLogic {
float DeltaTime = (float)FrameDiffNanoSeconds/(PhysicsFrameTime*1000000f); float DeltaTime = (float)FrameDiffNanoSeconds/(PhysicsFrameTime*1000000f);
Camera cam = engineInstance.scene().GetCamera(); Camera cam = engineInstance.scene().GetCamera();
Project3D projection = engineInstance.scene().GetProjection(); 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)); 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()); 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.GenerateChunks(ChunkPos, new Vector3i(12,6,12),cam.GetViewMatrix(),projection.GetProjectionMatrix()); 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(PrimaryRuntime.IsServer){
/* if(UseLegacyNetworking) Server.LegacyCollectIncomingPackets(); /* if(UseLegacyNetworking) Server.LegacyCollectIncomingPackets();
else*/ //Server.CollectIncomingPackets(); else*/ //Server.CollectIncomingPackets();

View file

@ -70,7 +70,7 @@ public class VoxelWorldManager {
private static Vector3f min = new Vector3f(); private static Vector3f min = new Vector3f();
private static Vector3f min2 = 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); projectionMatrix.mul(cameraView, viewProjectionMatrix);
frustumIntersection.set(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; int maxDistSq = unloadRadius * unloadRadius;
projectionMatrix.mul(cameraView, viewProjectionMatrix);
frustumIntersection.set(viewProjectionMatrix);
RenderChunks.entrySet().removeIf(entry -> { RenderChunks.entrySet().removeIf(entry -> {
Vector3i pos = entry.getKey(); 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); 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); 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 dx = pos.x - centerChunk.x;
int dy = pos.y - centerChunk.y; int dy = pos.y - centerChunk.y;
int dz = pos.z - centerChunk.z; int dz = pos.z - centerChunk.z;
int distSq = dx * dx + dy * dy + dz * dz; 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) {
if (distSq > maxDistSq || !InView) {
RenderChunk chunk = entry.getValue(); RenderChunk chunk = entry.getValue();
Resources.meshPool().free(chunk); Resources.meshPool().free(chunk);

View file

@ -201,12 +201,10 @@ public class LightRenderer {
private static ShaderModule[] CreateShaderModules(VulkanContext VkCtx, LightSpecConsts lightSpecConsts, boolean shadowPipeline) { private static ShaderModule[] CreateShaderModules(VulkanContext VkCtx, LightSpecConsts lightSpecConsts, boolean shadowPipeline) {
if (EngineConfig.getInstance().RecompileShaders()) { if (EngineConfig.getInstance().RecompileShaders()) {
ShaderCompiler.CompileGLSLShaderOnChange(VERTEX_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_vertex_shader); 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); ShaderCompiler.CompileGLSLShaderOnChange(shadowPipeline ? FRAGMENT_SHADER_FILE_GLSL : NO_SHADOWS_FRAGMENT_SHADER_FILE_GLSL, Shaderc.shaderc_glsl_fragment_shader);
} }
return new ShaderModule[]{ return new ShaderModule[]{
new ShaderModule(VkCtx, VK_SHADER_STAGE_VERTEX_BIT, VERTEX_SHADER_FILE_SPV, null), 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), new ShaderModule(VkCtx, VK_SHADER_STAGE_FRAGMENT_BIT, shadowPipeline ? FRAGMENT_SHADER_FILE_SPV : NO_SHADOWS_FRAGMENT_SHADER_FILE_SPV, shadowPipeline ? lightSpecConsts.getSpecInfo() : null),
}; };
} }