voxel biomes and CPU pushed blocks

This commit is contained in:
Halbear 2026-09-20 17:57:19 +01:00
parent 7fda2614d7
commit a0b00b0f1c
24 changed files with 1007 additions and 91 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -32,11 +32,26 @@ layout(std430, binding = 3) buffer Counters {
uint faceCount;
} counters;
struct Voxel {
uint MaterialIndex;
uint BlockType;
uvec2[2] UpIndex;
uvec2[2] DownIndex;
uvec2[2] NorthIndex;
uvec2[2] SouthIndex;
uvec2[2] EastIndex;
uvec2[2] WestIndex;
};
layout(std430, set = 1, binding = 0) readonly buffer VoxelUniform {
Voxel materials[];
} voxelReg;
layout(push_constant) uniform ChunkInfo {
ivec3 chunkPos;
int slot;
uint faceOffset;
uint unused0;
uint voxelTypeCount;
uint indirectCommandIndex;
uint padding0;
};
@ -44,7 +59,10 @@ layout(push_constant) uniform ChunkInfo {
uint flatten(ivec3 pos) {
return uint((pos.x * CHUNK_AREA) + (pos.y * CHUNK_SIZE) + pos.z);
}
bool isSeeThrough(Voxel voxel, uint type) {
if (type == 0u || voxel.BlockType == 3u || type > voxelTypeCount) return true;
return voxelReg.materials[type - 1u].BlockType >= 1u;
}
uint getVoxel(ivec3 pos) {
if (pos.x < 0 || pos.x >= CHUNK_SIZE) return 0u;
if (pos.y < 0 || pos.y >= CHUNK_SIZE) return 0u;
@ -58,7 +76,16 @@ uint hash21(uvec2 p) {
p.y += p.x * 1664525u;
return p.x ^ (p.x >> 16);
}
uint pcg_hash(uint seed) {
uint state = seed * 747796405u + 289133645u;
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
uint randomRangeUint(uint seed, uint minVal, uint maxVal) {
uint range = maxVal - minVal + 1u;
return minVal + (pcg_hash(seed) % range);
}
uint random_1_to_3(vec2 uv, uint customSeed) {
uvec2 p = uvec2(floatBitsToUint(uv.x), floatBitsToUint(uv.y));
p.x ^= customSeed;
@ -66,6 +93,31 @@ uint random_1_to_3(vec2 uv, uint customSeed) {
return (randInt % 3u) + 1u;
}
uint packFace(uvec3 voxelPos, uint faceIndex, Voxel voxel) {
uint randomSeed = uint(voxelPos.x) * 73856093u ^ uint(voxelPos.z) * 19349663u;
uvec2 UpIndex = uvec2(randomRangeUint(randomSeed,voxel.UpIndex[0].x,voxel.UpIndex[1].x),randomRangeUint(randomSeed,voxel.UpIndex[0].y,voxel.UpIndex[1].y));
uvec2 DownIndex = uvec2(randomRangeUint(randomSeed,voxel.DownIndex[0].x,voxel.DownIndex[1].x),randomRangeUint(randomSeed,voxel.DownIndex[0].y,voxel.DownIndex[1].y));
uvec2 NorthIndex = uvec2(randomRangeUint(randomSeed,voxel.NorthIndex[0].x,voxel.NorthIndex[1].x),randomRangeUint(randomSeed,voxel.NorthIndex[0].y,voxel.NorthIndex[1].y));
uvec2 SouthIndex = uvec2(randomRangeUint(randomSeed,voxel.SouthIndex[0].x,voxel.SouthIndex[1].x),randomRangeUint(randomSeed,voxel.SouthIndex[0].y,voxel.SouthIndex[1].y));
uvec2 EastIndex = uvec2(randomRangeUint(randomSeed,voxel.EastIndex[0].x,voxel.EastIndex[1].x),randomRangeUint(randomSeed,voxel.EastIndex[0].y,voxel.EastIndex[1].y));
uvec2 WestIndex = uvec2(randomRangeUint(randomSeed,voxel.WestIndex[0].x,voxel.WestIndex[1].x),randomRangeUint(randomSeed,voxel.WestIndex[0].y,voxel.WestIndex[1].y));
uvec2 textureIndices[8] = uvec2[8](UpIndex, DownIndex, NorthIndex,
SouthIndex, EastIndex, WestIndex, NorthIndex,SouthIndex);
uvec2 tex = textureIndices[faceIndex];
uint textureIndexX = tex.x;
uint textureIndexY = tex.y;
uint materialId = voxel.MaterialIndex;
return (voxelPos.x & 0xFu) |
((voxelPos.y & 0xFu) << 4u) |
((voxelPos.z & 0xFu) << 8u) |
((faceIndex & 0x7u) << 12u) |
((materialId & 0x7FFu) << 15u) |
((textureIndexX & 0x7u) << 26u) |
((textureIndexY & 0x7u) << 29u);
}
uint packFace(uvec3 voxelPos, uint faceIndex, uint materialId) {
uint randomFaceValue = random_1_to_3(vec2(voxelPos.x, voxelPos.z),faceOffset);
uint textureIndexX = 0u;
@ -107,23 +159,28 @@ uint packFace(uvec3 voxelPos, uint faceIndex, uint materialId) {
textureIndexY = 0u;
}
return (voxelPos.x & 0x1Fu) |
((voxelPos.y & 0x1Fu) << 5u) |
((voxelPos.z & 0x1Fu) << 10u) |
((faceIndex & 0x7u) << 15u) |
((materialId & 0xFFu) << 18u) |
return (voxelPos.x & 0xFu) |
((voxelPos.y & 0xFu) << 4u) |
((voxelPos.z & 0xFu) << 8u) |
((faceIndex & 0x7u) << 12u) |
((materialId & 0x7FFu) << 15u) |
((textureIndexX & 0x7u) << 26u) |
((textureIndexY & 0x7u) << 29u);
}
void emitFace(ivec3 voxelPos, uint faceIndex, uint voxelType) {
if (voxelType == 0u || voxelType > voxelTypeCount) {
return;
}
uint localFace = atomicAdd(counters.faceCount, 1u);
if (localFace >= MAX_VISIBLE_FACES_PER_CHUNK) {
return;
}
faces[faceOffset + localFace] = packFace(uvec3(voxelPos), faceIndex, voxelType);
Voxel voxel = voxelReg.materials[voxelType - 1u];
faces[faceOffset + localFace] = packFace(uvec3(voxelPos), faceIndex, voxel);
atomicAdd(drawCmds[indirectCommandIndex].vertexCount, VERTICES_PER_FACE);
}
@ -136,21 +193,20 @@ void main() {
}
uint current = getVoxel(pos);
if (current == 0u) {
if (current == 0u || current > voxelTypeCount) {
return;
}
if (current == 4u) {
Voxel voxel = voxelReg.materials[current - 1u];
if (voxel.BlockType == 1u) {
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);
if (isSeeThrough(voxel,getVoxel(pos + ivec3( 0, 1, 0)))) emitFace(pos, 0u, current);
if (isSeeThrough(voxel,getVoxel(pos + ivec3( 0, -1, 0)))) emitFace(pos, 1u, current);
if (isSeeThrough(voxel,getVoxel(pos + ivec3( 1, 0, 0)))) emitFace(pos, 2u, current);
if (isSeeThrough(voxel,getVoxel(pos + ivec3(-1, 0, 0)))) emitFace(pos, 3u, current);
if (isSeeThrough(voxel,getVoxel(pos + ivec3( 0, 0, 1)))) emitFace(pos, 4u, current);
if (isSeeThrough(voxel,getVoxel(pos + ivec3( 0, 0, -1)))) emitFace(pos, 5u, current);
}

View file

@ -8,6 +8,19 @@ const int CHUNK_AREA = CHUNK_SIZE * CHUNK_SIZE;
layout(std430, binding = 0) buffer VoxelData {
uint voxels[];
};
struct Biome {
uint surfaceBlock;
uint dirtBlock;
uint stoneBlock;
uint grassFoliage;
uint[4] flowers;
uint[4] tallBlocks;
uint[4] otherFoliage;
};
layout(std430, set = 1, binding = 0) readonly buffer BiomeUniform {
Biome biomes[];
} BiomeReg;
layout(push_constant) uniform ChunkInfo {
ivec3 chunkPos;
@ -15,15 +28,8 @@ layout(push_constant) uniform ChunkInfo {
uint faceOffset;
uint unused0;
uint indirectCommandIndex;
uint padding0;
uint biomeCount;
};
vec3 random3(vec3 st) {
return fract(sin(vec3(
dot(st, vec3(127.1, 311.7, 74.7)),
dot(st, vec3(269.5, 183.3, 246.1)),
dot(st, vec3(113.5, 271.9, 124.6))
)) * 43758.5453123);
}
float random3to1(vec3 st) {
return fract(sin(dot(st, vec3(127.1, 311.7, 74.7))) * 43758.5453123);
@ -88,6 +94,29 @@ float valueNoise(vec2 st) {
(c - a) * u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
uint pcg_hash(uint seed) {
uint state = seed * 747796405u + 289133645u;
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
uint randomRangeUint(uint seed, uint minVal, uint maxVal) {
uint range = maxVal - minVal + 1u;
return minVal + (pcg_hash(seed) % range);
}
uint GetBiome(vec3 worldPos, uint MaxBiomes) {
if (MaxBiomes == 0u) return 0u;
vec3 X = vec3(worldPos);
X = mat3(
0.788675134594813, -0.211324865405187, -0.577350269189626,
-0.211324865405187, 0.788675134594813, -0.577350269189626,
0.577350269189626, 0.577350269189626, 0.577350269189626) * X;
float n = clamp(cubicNoise(X), 0.0, 0.9999);
uint Biome = uint(floor(n * float(MaxBiomes)));
return min(Biome, MaxBiomes - 1u);
}
bool isCave(vec3 worldPos) {
vec3 X = vec3(worldPos);
X = mat3(
@ -101,33 +130,41 @@ bool isCave(vec3 worldPos) {
void main() {
ivec3 localPos = ivec3(gl_GlobalInvocationID.xyz);
ivec3 worldPos = chunkPos * CHUNK_SIZE + localPos;
uint biome = GetBiome(vec3(worldPos.z, worldPos.y, worldPos.x) * 0.05, biomeCount);
Biome currentBiome = BiomeReg.biomes[biome];
float YZone = floor(worldPos.y/150.0)*150.0;
float SurfaceHeight = valueNoise(vec2(worldPos.x + YZone * 2, worldPos.z + YZone * 2) * 0.005) * 100 +
float SurfaceHeight = 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;
uint RandomBlockVal = randomRangeUint(uint(worldPos.x) * 73856093u ^ uint(worldPos.z) * 19349663u,0u ,3u);
float IslandUnderneathHeight = 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 grassNoise = valueNoise(vec2(worldPos.z + YZone * 2, worldPos.x + YZone * 2));
float grassNoise = valueNoise(vec2(worldPos.z * 0.5 - YZone * 2, worldPos.x * 0.5 - YZone * 2));
uint voxelType = 0u;
if (float(worldPos.y) <= SurfaceHeight && float(worldPos.y) >= IslandUnderneathHeight) {
float depthBelowSurface = SurfaceHeight - float(worldPos.y);
if (depthBelowSurface < 0.5) {
if(grassNoise < 0.65) voxelType = 4u; // Grass foliage
if(grassNoise < 0.65) voxelType = currentBiome.grassFoliage; // Grass foliage
else if (grassNoise < 0.75) voxelType = currentBiome.flowers[RandomBlockVal];
else if (grassNoise < 0.85) voxelType = currentBiome.otherFoliage[RandomBlockVal];
else voxelType = 0u;
}else if (depthBelowSurface < 2.5) {
voxelType = 1u; // Grass
voxelType = currentBiome.surfaceBlock; // Grass
} else if (depthBelowSurface < 6.0) {
voxelType = 2u; // Dirt(also stone rn)
voxelType = currentBiome.dirtBlock; // Dirt(also stone rn)
} else {
voxelType = 3u; // Stone
voxelType = currentBiome.stoneBlock; // Stone
}
}
else{
@ -145,14 +182,16 @@ void main() {
float depthBelowSurface = height2 - float(worldPos.y);
if (depthBelowSurface < 0.5) {
if (grassNoise < 0.65) voxelType = 4u; // Grass foliage
if (grassNoise < 0.65) voxelType = currentBiome.grassFoliage; // Grass foliage
else if (grassNoise < 0.75) voxelType = currentBiome.flowers[RandomBlockVal];
else if (grassNoise < 0.85) voxelType = currentBiome.otherFoliage[RandomBlockVal];
else voxelType = 0u;
} else if (depthBelowSurface < 1.5) {
voxelType = 1u; // Grass
voxelType = currentBiome.surfaceBlock; // Grass
} else if (depthBelowSurface < 6.0) {
voxelType = 2u; // Dirt(also stone rn)
voxelType = currentBiome.dirtBlock; // Dirt(also stone rn)
} else {
voxelType = 3u; // Stone
voxelType = currentBiome.stoneBlock; // Stone
}
}
}

View file

@ -0,0 +1,88 @@
#version 450
#extension GL_EXT_nonuniform_qualifier : require
const int MAX_TEXTURES = 128;
layout(location = 0) in vec4 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec3 inTangent;
layout(location = 3) in vec3 inBitangent;
layout(location = 4) in vec2 inTextCoords;
layout(location = 5) in mat4 viewMatrix;
layout(location = 9) in flat uint textureIndex;
layout(location = 0) out vec4 outAlbedo;
layout(location = 1) out vec4 outViewPos;
struct Material {
vec4 diffuseColor; //16
uint hasTexture; //20
uint textureIdx; //24
uint hasNormalMap; //28
uint normalMapIdx; //32
uint hasRoughMap; //36
uint roughMapIdx; //40
float roughnessFactor; //44
float metallicFactor; //48
vec4 emissiveColour; //64
uint hasEmissiveMap; //68
uint emissiveMapIdx; //72
uint hasTranslucencyMap; //76
uint translucencyMapIdx; //80
float translucencyFactor; //84
uint hasOpacityMap; //88
uint OpacityMapIdx; //92
float OpacityFactor; //96
float reflectiveness; //100
float refractiveness; //104
float Padding1; //108
float Padding2; //112
};
layout(set = 2, binding = 0) readonly buffer MaterialUniform{
Material materials[];
} matUniform;
layout(set = 3, binding = 0) uniform sampler2D textSampler[MAX_TEXTURES];
layout(push_constant) uniform pc{
layout(offset = 64) uint materialIdx;
} push_constants;
void main()
{
vec4 viewPos = vec4(viewMatrix * inPos);
outViewPos = viewPos;
Material material = matUniform.materials[textureIndex];
uint albedoTextureIndex = material.textureIdx;
if (albedoTextureIndex >= MAX_TEXTURES){
outAlbedo = vec4(0.0,0.0,1.0,1.0);
return;
}
if(material.hasTexture == 1){
vec4 texColor = texture(textSampler[nonuniformEXT(albedoTextureIndex)], inTextCoords);
outAlbedo = texColor;
} else{
outAlbedo = material.diffuseColor;
}
vec4 Opacity = vec4(outAlbedo.a, outAlbedo.a, outAlbedo.a, 1.0);
if (material.OpacityFactor > 0.0 && material.OpacityFactor < 1.0) {
Opacity = vec4(material.OpacityFactor, material.OpacityFactor, material.OpacityFactor, 1.0);
}
if (material.hasOpacityMap > 0 && material.OpacityMapIdx < MAX_TEXTURES) {
Opacity = texture(textSampler[nonuniformEXT(material.OpacityMapIdx)], inTextCoords);
}
float opacityf = Opacity.x + Opacity.y + Opacity.z;
opacityf = opacityf / 3;
if(opacityf < 0.4){ discard; }
outAlbedo = vec4(outAlbedo.rgb, opacityf);
}

View file

@ -0,0 +1,149 @@
#version 450
#extension GL_EXT_nonuniform_qualifier : require
const int MAX_TEXTURES = 128;
layout(location = 0) in vec4 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec3 inTangent;
layout(location = 3) in vec3 inBitangent;
layout(location = 4) in vec2 inTextCoords;
layout(location = 5) in mat4 viewMatrix;
layout(location = 9) in flat uint MaterialID;
layout(location = 1) out vec4 outAlbedo ;
layout(location = 0) out vec4 outPos;
layout(location = 2) out vec4 outNormal;
layout(location = 3) out vec4 outPBR;
layout(location = 4) out vec4 outEmissive;
layout(location = 5) out vec4 outTranslucency;
layout(location = 6) out vec4 outOpacity;
layout(location = 7) out vec4 outViewPos;
struct Material {
vec4 diffuseColor; //16
uint hasTexture; //20
uint textureIdx; //24
uint hasNormalMap; //28
uint normalMapIdx; //32
uint hasRoughMap; //36
uint roughMapIdx; //40
float roughnessFactor; //44
float metallicFactor; //48
vec4 emissiveColour; //64
uint hasEmissiveMap; //68
uint emissiveMapIdx; //72
uint hasTranslucencyMap; //76
uint translucencyMapIdx; //80
float translucencyFactor; //84
uint hasOpacityMap; //88
uint OpacityMapIdx; //92
float OpacityFactor; //96
float reflectiveness; //100
float refractiveness; //104
float Padding1; //108
float Padding2; //112
};
layout(set = 2, binding = 0) readonly buffer MaterialUniform {
Material materials[];
} matUniform;
layout(set = 3, binding = 0) uniform sampler2D textSampler[MAX_TEXTURES];
vec3 calcNormal(Material material, vec3 normal, vec2 textCoords, mat3 TBN)
{
vec3 newNormal = normal;
if (material.hasNormalMap > 0 && material.normalMapIdx < MAX_TEXTURES)
{
newNormal = texture(textSampler[nonuniformEXT(material.normalMapIdx)], textCoords).rgb;
newNormal = normalize(newNormal * 2.0 - 1.0);
newNormal = normalize(TBN * newNormal);
}
return newNormal;
}
layout(push_constant) uniform pc {
layout(offset = 64) uint materialIdx;
} push_constants;
void main()
{
outPos = inPos;
vec4 viewPos = vec4(viewMatrix * vec4(inPos.xyz, 1.0));
outViewPos = viewPos;
Material material = matUniform.materials[MaterialID];
uint albedoTextureIndex = material.textureIdx;
if (albedoTextureIndex >= MAX_TEXTURES) {
outAlbedo = vec4(0.0,0.0,1.0,1.0);
outOpacity = vec4(0.0,0.0,1.0,1.0);
outNormal = vec4(0.0,0.0,1.0,1.0);
outEmissive = vec4(0.0,0.0,1.0,1.0);
outTranslucency = vec4(0.0,0.0,1.0,1.0);
outPBR = vec4(0.0,0.0,1.0,1.0);
return;
}
if (material.hasTexture == 1) {
outAlbedo = texture(textSampler[nonuniformEXT(albedoTextureIndex)], inTextCoords);
} else {
outAlbedo = material.diffuseColor;
}
vec4 Opacity = vec4(outAlbedo.a, outAlbedo.a, outAlbedo.a, 1.0);
if (material.OpacityFactor > 0.0 && material.OpacityFactor < 1.0) {
Opacity = vec4(material.OpacityFactor, material.OpacityFactor, material.OpacityFactor, 1.0);
}
if (material.hasOpacityMap > 0 && material.OpacityMapIdx < MAX_TEXTURES) {
outOpacity = texture(textSampler[nonuniformEXT(material.OpacityMapIdx)], inTextCoords);
} else {
outOpacity = Opacity;
}
Opacity = outOpacity;
float opacityf = Opacity.x + Opacity.y + Opacity.z;
opacityf = opacityf / 3;
outAlbedo = vec4(outAlbedo.rgb, opacityf);
if(outAlbedo.a < 0.5) discard;
mat3 TBN = mat3(inTangent, inBitangent, inNormal);
vec3 newNormal = calcNormal(material, inNormal, inTextCoords, TBN);
float ao = 0.5f;
float roughnessFactor = 0.0f;
float metallicFactor = 0.0f;
if (material.hasRoughMap > 0 && material.roughMapIdx < MAX_TEXTURES) {
vec4 metRoughValue = texture(textSampler[nonuniformEXT(material.roughMapIdx)], inTextCoords);
roughnessFactor = metRoughValue.g;
metallicFactor = metRoughValue.b;
} else {
roughnessFactor = material.roughnessFactor;
metallicFactor = material.metallicFactor;
}
vec4 emissive = material.emissiveColour;
if (material.hasEmissiveMap > 0 && material.emissiveMapIdx < MAX_TEXTURES) {
emissive = texture(textSampler[nonuniformEXT(material.emissiveMapIdx)], inTextCoords);
}
outEmissive = emissive;
vec4 Translucency = vec4(material.translucencyFactor, material.translucencyFactor, material.translucencyFactor, 1);
if(material.hasTranslucencyMap > 0 && material.translucencyMapIdx < MAX_TEXTURES){
Translucency = texture(textSampler[nonuniformEXT(material.translucencyMapIdx)], inTextCoords);
}
outTranslucency = Translucency;
float Refractiveness = material.refractiveness;
float Reflectiveness = material.reflectiveness;
outNormal = vec4(newNormal, Refractiveness);
outPBR = vec4(ao, roughnessFactor, metallicFactor, Reflectiveness);
}

View file

@ -6,6 +6,7 @@ layout(location = 2) out vec3 outTangent;
layout(location = 3) out vec3 outBitangent;
layout(location = 4) out vec2 outTextCoords;
layout(location = 5) out mat4 viewMatrix;
layout(location = 9) out flat uint outMaterialID;
layout(set = 0, binding = 0) uniform ProjUniform { mat4 matrix; } projUniform;
layout(set = 1, binding = 0) uniform ViewUniform { mat4 matrix; } viewUniform;
@ -93,17 +94,13 @@ void main() {
uint triangleVertex = (uint(gl_VertexIndex))% 6u;
uint cornerIndex = TRI_TO_CORNER[triangleVertex];
//uint faceRecordIndex = uint(gl_VertexIndex) / 6u;
//uint triangleVertex = uint(gl_VertexIndex) % 6u;
//uint cornerIndex = TRI_TO_CORNER[triangleVertex];
uint packedFace = faces[faceRecordIndex];
uint voxelX = packedFace & 0x1Fu;
uint voxelY = (packedFace >> 5u) & 0x1Fu;
uint voxelZ = (packedFace >> 10u) & 0x1Fu;
uint faceId = (packedFace >> 15u) & 0x7u;
uint matId = (packedFace >> 18u) & 0xFFu;
uint voxelX = packedFace & 0xFu;
uint voxelY = (packedFace >> 4u) & 0xFu;
uint voxelZ = (packedFace >> 8u) & 0xFu;
uint faceId = (packedFace >> 12u) & 0x7u;
uint matId = (packedFace >> 15u) & 0x7FFu;
uint texIdxX = (packedFace >> 26u) & 0x7u;
uint texIdxY = (packedFace >> 29u) & 0x7u;
@ -111,7 +108,6 @@ void main() {
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;
@ -119,10 +115,14 @@ void main() {
vec4 worldPosVec4 = vec4(worldPos, 1.0);
gl_Position = projUniform.matrix * viewUniform.matrix * worldPosVec4;
outPos = worldPosVec4;
outNormal = NORMALS[faceId];
outTangent = TANGENTS[faceId];
outBitangent = BITANGENTS[faceId];
outTextCoords = CORNER_UVS_GRASS_BLOCK[faceId][cornerIndex] * scalar + texID;
vec2 atlasSize = vec2(64, 64);
vec2 texel = 0.5 / atlasSize;
vec2 tileMin = vec2(texIdxX, texIdxY) * scalar + texel;
vec2 tileMax = vec2(texIdxX + 1u, texIdxY + 1u) * scalar - texel;
outTextCoords = mix(tileMin, tileMax, CORNER_UVS_GRASS_BLOCK[faceId][cornerIndex]);
outMaterialID = matId;
}

View file

@ -17,6 +17,69 @@ layout(location = 1) out flat uint outMaterialIdx;
const uint TRI_TO_CORNER[6] = uint[6](0u, 1u, 2u, 0u, 2u, 3u);
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[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[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[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[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, 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)
);
vec3 buildFaceCornerPosition(uvec3 voxelPos, uint faceId, uint cornerIndex) {
vec3 p = vec3(voxelPos);
@ -63,21 +126,40 @@ vec3 buildFaceCornerPosition(uvec3 voxelPos, uint faceId, uint cornerIndex) {
void main() {
uint faceRecordIndex = uint(gl_VertexIndex) / 6u;
uint triangleVertex = uint(gl_VertexIndex) % 6u;
uint triangleVertex = (uint(gl_VertexIndex))% 6u;
uint cornerIndex = TRI_TO_CORNER[triangleVertex];
//uint faceRecordIndex = uint(gl_VertexIndex) / 6u;
//uint triangleVertex = uint(gl_VertexIndex) % 6u;
//uint cornerIndex = TRI_TO_CORNER[triangleVertex];
uint packedFace = faces[faceRecordIndex];
uint voxelX = packedFace & 0x1Fu;
uint voxelY = (packedFace >> 5u) & 0x1Fu;
uint voxelZ = (packedFace >> 10u) & 0x1Fu;
uint faceId = (packedFace >> 15u) & 0x7u;
uint matId = (packedFace >> 18u) & 0xFFu;
uint voxelX = packedFace & 0xFu;
uint voxelY = (packedFace >> 4u) & 0xFu;
uint voxelZ = (packedFace >> 8u) & 0xFu;
uint faceId = (packedFace >> 12u) & 0x7u;
uint matId = (packedFace >> 15u) & 0x7FFu;
uint texIdxX = (packedFace >> 26u) & 0x7u;
uint texIdxY = (packedFace >> 29u) & 0x7u;
vec3 localPos = buildFaceCornerPosition(uvec3(voxelX, voxelY, voxelZ), faceId, cornerIndex);
// (voxelPos.x & 0xFu) |
// ((voxelPos.y & 0xFu) << 4u) |
// ((voxelPos.z & 0xFu) << 8u) |
// ((faceIndex & 0x7u) << 12u) |
// ((materialId & 0x7FFu) << 15u) |
// ((textureIndexX & 0x7u) << 26u) |
// ((textureIndexY & 0x7u) << 29u);
vec2 texID = vec2((texIdxX)/4.0,(texIdxY)/4.0);
vec2 scalar = vec2(1.0/4.0,1.0/4.0);
faceId = min(faceId, 7u);
vec3 localPos = vec3(voxelX, voxelY, voxelZ) + FACE_CORNERS[faceId][cornerIndex];
vec3 worldPos = localPos + push_constants.ModelOffset.xyz * 16.0;
outTextCoord = vec2(0.0);
outTextCoord = CORNER_UVS_GRASS_BLOCK[faceId][cornerIndex] * scalar + texID;
outMaterialIdx = matId;
gl_Position = vec4(worldPos, 1.0);

View file

@ -0,0 +1,51 @@
#version 460 core
in vec3 FragPos;
in vec2 TexCoords;
in vec3 Normal;
out vec4 FragColor;
uniform vec3 cameraPos;
uniform vec3 lightPos;
// Material constants
const vec3 waterShallowColor = vec3(0.0, 0.6, 0.7);
const vec3 waterDeepColor = vec3(0.05, 0.15, 0.3);
const vec3 sunColor = vec3(1.0, 0.95, 0.8);
void main() {
// Normalize vectors
vec3 n = normalize(Normal);
vec3 viewDir = normalize(cameraPos - FragPos);
vec3 lightDir = normalize(lightPos - FragPos);
// 1. Ambient Lighting
vec3 ambient = 0.3 * waterShallowColor;
// 2. Diffuse Shading
float diff = max(dot(n, lightDir), 0.0);
vec3 diffuse = diff * sunColor * 0.4;
// 3. Specular Highlights (Blinn-Phong)
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(n, halfwayDir), 0.0), 64.0); // High shininess for water glaze
vec3 specular = spec * sunColor * 0.8;
// 4. Fresnel Approximation (Schlick's approximation)
// Water has a base reflectivity of roughly 0.02 at a perpendicular view angle
float F0 = 0.02;
float fresnel = F0 + (1.0 - F0) * pow(1.0 - max(dot(n, viewDir), 0.0), 5.0);
// Mix deep and shallow water colors based on the normal angle
vec3 baseWaterColor = mix(waterShallowColor, waterDeepColor, max(dot(n, vec3(0.0, 1.0, 0.0)), 0.0));
// Combine lighting results
vec3 lightingResult = ambient + diffuse + specular;
// Blend final look with Fresnel reflection dominance
vec3 finalColor = mix(baseWaterColor + specular, lightingResult + vec3(fresnel * 0.5), fresnel);
// Output final color with subtle translucency opacity
FragColor = vec4(finalColor, 0.85);
}

View file

@ -0,0 +1,46 @@
#version 450
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords;
out vec3 FragPos;
out vec2 TexCoords;
out vec3 Normal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform float uTime;
// Wave configuration constants
const float AMPLITUDE = 0.15;
const float FREQUENCY = 1.5;
const float SPEED = 2.0;
// Simple wave function that modifies elevation based on position and time
float calculateWave(vec3 pos, float time, vec2 direction) {
return AMPLITUDE * sin(dot(pos.xz, direction) * FREQUENCY + time * SPEED);
}
// Derivative of the wave function to calculate accurate dynamic surface normals
vec3 calculateWaveNormal(vec3 pos, float time) {
float dx = AMPLITUDE * FREQUENCY * cos(pos.x * FREQUENCY + time * SPEED);
float dz = AMPLITUDE * FREQUENCY * cos(pos.z * FREQUENCY + time * SPEED);
return normalize(vec3(-dx, 1.0, -dz));
}
void main() {
vec3 displacedPos = aPos;
// Combine two wave directions for a less predictable, more organic look
displacedPos.y += calculateWave(aPos, uTime, vec2(1.0, 0.0));
displacedPos.y += calculateWave(aPos, uTime * 1.2, vec2(0.5, 0.8));
FragPos = vec3(model * vec4(displacedPos, 1.0));
TexCoords = aTexCoords;
// Pass transformed normals and position to the fragment shader
Normal = mat3(transpose(inverse(model))) * calculateWaveNormal(displacedPos, uTime);
gl_Position = projection * view * model * vec4(displacedPos, 1.0);
}