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)];
}
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);
}

View file

@ -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
}

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 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;
}