voxel texture mapping
This commit is contained in:
parent
4565188f15
commit
0074bf1d1f
7 changed files with 165 additions and 77 deletions
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue