Theme

alex/beta-minecraft-rust

Minecraft Beta 1.7.3 Rewrite

BRANCH main
main
SNAPSHOT Draw terrain GPU-driven from packed per-section buffers with GPU occlusion culling, add a render distance slider (2-128) with a quad/disk shape, cut chunk bookkeeping at high render distances, and fix a light underflow at y=0
DOWNLOAD
Download file

root / crates / client / shaders / scene.slang

struct Vertex
{
    float3 position;
    float2 uv;
    uint color;
    int texture;
};

struct Push
{
    float4x4 viewProjection;
    Vertex* vertices;
    uint fog;
    float alphaCutoff;
    float4 fogColor;
    float4 camera;
    float fogStart;
    float fogEnd;
    float opacity;
};

[[vk::push_constant]] Push push;
[[vk::binding(0, 0)]] Texture2D textures[];
[[vk::binding(1, 0)]] SamplerState samplers[];

struct Varyings
{
    float4 position : SV_Position;
    float2 uv : TEXCOORD0;
    float4 color : COLOR0;
    nointerpolation int texture : TEXCOORD1;
    float distance : TEXCOORD2;
};

[shader("vertex")]
Varyings vertexMain(uint index : SV_VulkanVertexID)
{
    Vertex v = push.vertices[index];
    Varyings o;
    o.position = mul(push.viewProjection, float4(v.position, 1.0));
    o.uv = v.uv;
    o.color = unpackUnorm4x8ToFloat(v.color);
    o.texture = v.texture;
    o.distance = length(v.position - push.camera.xyz);
    return o;
}

[shader("fragment")]
float4 fragmentMain(Varyings i) : SV_Target
{
    float4 color = i.color;
    // Texture flag 1 << 16 samples smoothly and clamped
    if (i.texture >= 0)
    {
        uint sampler = (i.texture & (1 << 16)) != 0 ? 1 : 0;
        color *= textures[NonUniformResourceIndex(i.texture & 0xffff)].Sample(samplers[NonUniformResourceIndex(sampler)], i.uv);
    }
    // Untextured overlays and flag 1 << 17 (spider eyes) are blended without alpha testing
    if (i.texture >= 0 && (i.texture & (1 << 17)) == 0 && color.a <= push.alphaCutoff) discard;
    if (push.fog != 0)
    {
        // A negative start selects GL_EXP fog with the negated start as density
        float fog = push.fogStart < 0.0
            ? exp(push.fogStart * i.distance)
            : saturate((push.fogEnd - i.distance) / (push.fogEnd - push.fogStart));
        color.rgb = lerp(push.fogColor.rgb, color.rgb, fog);
    }
    color.a *= push.opacity;
    return color;
}