RustToDust/client/terrain_tile_bake.glsl

176 lines
6.4 KiB
GLSL

#version 450
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
struct GpuBrush {
vec4 posRot; // x, z, cosR, sinR
vec4 radDelta; // invRx, invRz, heightDelta, maxRadiusSq
vec4 matColor; // albedoR, albedoG, albedoB, float(kind)
};
layout(set = 0, binding = 0, rgba16f) uniform writeonly image2D u_delta_height;
layout(set = 0, binding = 1, rgba8) uniform writeonly image2D u_delta_albedo;
layout(set = 0, binding = 2, std430) readonly buffer BrushBuffer {
GpuBrush brushes[];
} u_brushes;
layout(push_constant, std430) uniform TileBakeParams {
vec4 tileOriginAndSpan; // x=tileMinX, y=tileMinZ, z=tileSize(192), w=innerSpan(384)
vec4 baseColorAndGutter; // rgb=clearAlbedo, w=gutterStart(2.0)
uvec4 counts; // x=brushStart, y=brushCount, z=texSize(388), w=isSion
} u_params;
void main() {
ivec2 gid = ivec2(gl_GlobalInvocationID.xy);
int texSize = int(u_params.counts.z);
if (gid.x >= texSize || gid.y >= texSize) {
return;
}
float tileMinX = u_params.tileOriginAndSpan.x;
float tileMinZ = u_params.tileOriginAndSpan.y;
float tileSize = u_params.tileOriginAndSpan.z;
float innerSpan = u_params.tileOriginAndSpan.w;
float gutterStart = u_params.baseColorAndGutter.w;
float stepM = tileSize / innerSpan;
float wx = tileMinX + (float(gid.x) - gutterStart) * stepM;
float wz = tileMinZ + (float(gid.y) - gutterStart) * stepM;
bool isSion = u_params.counts.w != 0u;
uint brushStart = u_params.counts.x;
uint brushEnd = brushStart + u_params.counts.y;
float totalDh = 0.0;
float totalDg = 0.0;
float totalScorch = 0.0;
vec3 albedoAcc = u_params.baseColorAndGutter.rgb;
float deltaBlendAcc = isSion ? 0.0 : 1.0;
for (uint i = brushStart; i < brushEnd; i++) {
GpuBrush b = u_brushes.brushes[i];
float dx = wx - b.posRot.x;
float dz = wz - b.posRot.y;
if (dx * dx + dz * dz >= b.radDelta.w) {
continue;
}
float cosR = b.posRot.z;
float sinR = b.posRot.w;
float lx = dx * cosR + dz * sinR;
float lz = -dx * sinR + dz * cosR;
float u = lx * b.radDelta.x;
float v = lz * b.radDelta.y;
float r2 = u * u + v * v;
if (r2 >= 1.0) {
continue;
}
int kind = int(b.matColor.w + 0.5);
float heightDelta = b.radDelta.z;
vec3 bCol = b.matColor.rgb;
float dh = 0.0;
float dg = 0.0;
float dScorch = 0.0;
vec3 col = bCol;
float alpha = 0.0;
if (kind == 0) {
// DuneRidge
float env = 1.0 - r2;
env *= env;
float ridge = max(0.0, 1.0 - u * u);
float asym = 1.0 + 0.32 * u;
dh = heightDelta * env * (0.35 + 0.65 * ridge) * asym;
if (heightDelta > 0.0) {
dg = 0.02 * env;
}
col = mix(bCol * 0.90, bCol * 1.12, ridge);
alpha = env * 0.72;
} else if (kind == 1) {
// Mesa
float r = sqrt(r2);
float t = clamp((1.0 - r) / 0.45, 0.0, 1.0);
float plateau = t * t * (3.0 - 2.0 * t);
dh = heightDelta * plateau;
float cliffBand = clamp(1.0 - abs(r - 0.74) * 5.0, 0.0, 1.0);
col = mix(bCol, bCol * 0.68, cliffBand);
alpha = clamp(plateau + cliffBand * 0.5, 0.0, 0.94);
} else if (kind == 2) {
// SalvagePad
float env = 1.0 - r2;
env *= env;
dh = heightDelta * env;
if (heightDelta > 0.0) {
dg = 0.03 * env;
}
float border = smoothstep(0.75, 0.92, sqrt(r2));
col = mix(bCol, vec3(0.32, 0.28, 0.24), border * 0.45);
alpha = clamp(env * 1.15, 0.0, 0.88);
} else if (kind == 3) {
// Road
float env = 1.0 - r2;
env *= env;
dh = heightDelta * env;
float rut = smoothstep(0.15, 0.45, abs(v)) * (1.0 - smoothstep(0.60, 0.92, abs(v)));
col = mix(bCol, bCol * 0.76, rut);
alpha = env * (0.45 + 0.35 * rut);
} else if (kind == 4) {
// Crater
float r = sqrt(r2);
float depth = abs(heightDelta);
float pitT = clamp(1.0 - r / 0.62, 0.0, 1.0);
float pit = -depth * pitT * pitT * (3.0 - 2.0 * pitT);
float rimT = sin(3.14159265 * clamp((r - 0.55) / 0.45, 0.0, 1.0));
float rim = 0.32 * depth * rimT * rimT;
dh = pit + rim;
dg = 0.04 * (1.0 - r);
dScorch = 0.78 * clamp(1.0 - r * 0.9, 0.0, 1.0);
vec3 ejecta = isSion ? vec3(0.34, 0.31, 0.27) : vec3(0.62, 0.38, 0.22);
col = mix(ejecta, bCol, clamp(pitT * 1.3, 0.0, 1.0));
alpha = clamp(max(pitT * 0.94, rimT * rimT * 0.72), 0.0, 0.95);
} else if (kind == 5) {
// Track
float env = 1.0 - r2;
env *= env;
dh = heightDelta * env;
dScorch = 0.32 * env;
float treads = smoothstep(0.18, 0.45, abs(v)) * (1.0 - smoothstep(0.62, 0.92, abs(v)));
col = bCol;
alpha = env * treads * 0.62;
} else if (kind == 6) {
// Scorch
float r = sqrt(r2);
float mask = clamp(1.0 - r, 0.0, 1.0);
float mask2 = mask * mask;
dg = 0.02 * mask2;
dScorch = 0.85 * mask2;
col = bCol;
alpha = mask2 * 0.88;
} else if (kind == 7) {
// Rubble
float env = 1.0 - r2;
env *= env;
float mound = 0.72 + 0.28 * cos(u * 3.14159265 * 2.5);
dh = heightDelta * env * mound;
dg = 0.08 * env;
dScorch = 0.35 * env;
float chunk = fract(sin(dot(floor(vec2(wx, wz) * 1.5), vec2(12.9898, 78.233))) * 43758.5453);
col = mix(bCol * 0.80, bCol * 1.15, chunk);
alpha = clamp(env * 1.2, 0.0, 0.96);
}
totalDh += dh;
totalDg += dg;
totalScorch += dScorch;
albedoAcc = mix(albedoAcc, col, alpha);
deltaBlendAcc = max(deltaBlendAcc, alpha);
}
float roughness = clamp(0.88 + totalDg, 0.35, 1.0);
float scorch = clamp(totalScorch, 0.0, 1.0);
imageStore(u_delta_height, gid, vec4(totalDh, roughness, scorch, clamp(deltaBlendAcc, 0.0, 1.0)));
imageStore(u_delta_albedo, gid, vec4(clamp(albedoAcc, 0.0, 1.0), 1.0));
}