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

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