From 81fbf1f2a9c572c863cb678b2a07faeacc9466ac Mon Sep 17 00:00:00 2001 From: RDW Date: Wed, 28 Feb 2024 03:40:25 +0100 Subject: [PATCH] Client: Enable vertex fogging for water surfaces This is clearly an oversight; only sprites and UI widgets should be exempt. --- .../WebGPU/Shaders/WaterSurfaceShader.wgsl | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Core/NativeClient/WebGPU/Shaders/WaterSurfaceShader.wgsl b/Core/NativeClient/WebGPU/Shaders/WaterSurfaceShader.wgsl index 3fb16867..1366d3fb 100644 --- a/Core/NativeClient/WebGPU/Shaders/WaterSurfaceShader.wgsl +++ b/Core/NativeClient/WebGPU/Shaders/WaterSurfaceShader.wgsl @@ -10,6 +10,7 @@ struct VertexOutput { @builtin(position) position: vec4f, @location(0) color: vec3f, @location(1) diffuseTextureCoords: vec2f, + @location(2) fogFactor: f32, }; // CameraBindGroup: Updated once per frame @@ -23,6 +24,9 @@ struct PerSceneData { unusedPadding: f32, directionalLightDirection: vec4f, directionalLightColor: vec4f, + cameraWorldPosition: vec4f, + fogColor: vec4f, + fogLimits: vec4f, }; @group(0) @binding(0) var uPerSceneData: PerSceneData; @@ -157,6 +161,15 @@ fn vs_main(in: VertexInput) -> VertexOutput { out.color = in.color; out.diffuseTextureCoords = in.diffuseTextureCoords; + + let worldPosition = T1 * S * homogeneousPosition; + let distance = length(worldPosition.xyz - uPerSceneData.cameraWorldPosition.xyz); + + let fogNearLimit = uPerSceneData.fogLimits.x; + let fogFarLimit = uPerSceneData.fogLimits.y; + let fogFactor = (fogFarLimit - distance) / (fogFarLimit - fogNearLimit); + out.fogFactor = 1.0 - clamp(fogFactor, 0.0, 1.0); + return out; } @@ -170,5 +183,6 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4f { let materialColor = vec4f(uMaterialInstanceData.diffuseRed, uMaterialInstanceData.diffuseGreen, uMaterialInstanceData.diffuseBlue, uMaterialInstanceData.materialOpacity); let finalColor = in.color * diffuseTextureColor.rgb * materialColor.rgb; - return vec4f(finalColor.rgb, diffuseTextureColor.a * materialColor.a + DEBUG_ALPHA_OFFSET ); + let foggedColor = mix(finalColor.rgb, uPerSceneData.fogColor.rgb, in.fogFactor); + return vec4f(foggedColor.rgb, diffuseTextureColor.a * materialColor.a + DEBUG_ALPHA_OFFSET ); } \ No newline at end of file