Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace CloudShadows
{
TextureCube<float> CloudShadowsTexture : register(t25);
TextureCube<float> VolumetricCloudShadowsTexture : register(t25);
TextureCube<float> CloudShadowsTexture : register(t26);

const static float CloudHeight = (2e3f / 1.428e-2) * 0.25;
const static float PlanetRadius = (6371e3f / 1.428e-2);
Expand Down
3 changes: 2 additions & 1 deletion package/Shaders/Common/SharedData.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ namespace SharedData
struct CloudShadowsSettings
{
float Opacity;
float3 pad0;
float InnerCloudShadowOpacity;
float2 pad0;
};

struct LODBlendingSettings
Expand Down
30 changes: 30 additions & 0 deletions package/Shaders/Sky.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,36 @@ PS_OUTPUT main(PS_INPUT input)
psout.Color.xyz = Color::Sky(input.Color.xyz) * baseColor.xyz + yyy;
# endif

# if defined(CLOUD_SHADOWS) && defined(CLOUDS)
if (baseColor.w > 0.0 && SharedData::cloudShadowsSettings.InnerCloudShadowOpacity > 0.0) {
float3 viewDir = normalize(input.WorldPosition.xyz);
float rayStep = 1.0 / 32.0;
float rayPos = rayStep * 0.5;
float4 rayShadow = 0.0;

float3 PoissonDisc[] = {
float3(0.460921f, 0.615192f, 0.887539f),
float3(0.757347f, 0.911008f, 0.189581f),
float3(0.548753f, 0.145482f, 0.0548723f),
float3(0.90051f, 0.157048f, 0.623493f)
};

[unroll] for (int i = 0; i < 4; i++)
{
float3 raySample = normalize(lerp(viewDir, SharedData::DirLightDirection.xyz, rayPos) + (PoissonDisc[i] * 2.0 - 1.0) * 0.01);

if (raySample.z < 0.0)
rayShadow[i] += -raySample.z;
else
rayShadow[i] = max(rayShadow[i], CloudShadows::VolumetricCloudShadowsTexture.SampleLevel(SampBaseSampler, raySample, 0).x);

rayPos += rayStep;
}

psout.Color.xyz *= (1.0 - saturate(dot(rayShadow, 0.25)) * SharedData::cloudShadowsSettings.InnerCloudShadowOpacity);
}
# endif

# else
psout.Color = float4(0, 0, 0, 1.0);
# endif // OCCLUSION
Expand Down
31 changes: 25 additions & 6 deletions src/Features/CloudShadows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
CloudShadows::Settings,
Opacity)
Opacity,
InnerCloudShadowOpacity)

void CloudShadows::DrawSettings()
{
ImGui::SliderFloat("Opacity", &settings.Opacity, 0.0f, 1.0f, "%.1f");
ImGui::SliderFloat("Opacity", &settings.Opacity, 0.0f, 1.0f, "%.2f");
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text(
"Higher values make cloud shadows darker.");
ImGui::Text("Controls how dark cloud shadows appear on terrain and objects.");
}

ImGui::SliderFloat("Inner Cloud Shadow Opacity", &settings.InnerCloudShadowOpacity, 0.0f, 1.0f, "%.2f");
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text("Controls shadowing applied within the clouds themselves. Higher values make the underside of clouds darker.");
}
}

Expand Down Expand Up @@ -118,6 +123,8 @@ void CloudShadows::ReflectionsPrepass()
ID3D11ShaderResourceView* srv = texCubemapCloudOccCopy->srv.get();
context->PSSetShaderResources(25, 1, &srv);
context->CSSetShaderResources(25, 1, &srv);
context->PSSetShaderResources(26, 1, &srv);
context->CSSetShaderResources(26, 1, &srv);
}
}

Expand All @@ -129,9 +136,15 @@ void CloudShadows::EarlyPrepass()

auto context = globals::d3d::context;

// t25 = VolumetricCloudShadowsTexture: use previous-frame copy so there is no D3D11 hazard
ID3D11ShaderResourceView* copySrv = texCubemapCloudOccCopy->srv.get();
context->PSSetShaderResources(25, 1, &copySrv);
context->CSSetShaderResources(25, 1, &copySrv);

// t26 = CloudShadowsTexture: live cubemap for terrain ground shadows.
ID3D11ShaderResourceView* srv = texCubemapCloudOcc->srv.get();
context->PSSetShaderResources(25, 1, &srv);
context->CSSetShaderResources(25, 1, &srv);
context->PSSetShaderResources(26, 1, &srv);
context->CSSetShaderResources(26, 1, &srv);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

void CloudShadows::SetupResources()
Expand Down Expand Up @@ -168,6 +181,12 @@ void CloudShadows::SetupResources()
rtvDesc.Format = texDesc.Format;
DX::ThrowIfFailed(device->CreateRenderTargetView(texCubemapCloudOccCopy->resource.get(), &rtvDesc, cubemapCloudOccCopyRTVs + i));
}

// Clear copy cubemap so EarlyPrepass doesn't bind uninitialized data on the first frame.
auto context = globals::d3d::context;
float black[4] = { 0, 0, 0, 0 };
for (int i = 0; i < 6; ++i)
context->ClearRenderTargetView(cubemapCloudOccCopyRTVs[i], black);
}
{
D3D11_BLEND_DESC blendDesc = {};
Expand Down
4 changes: 3 additions & 1 deletion src/Features/CloudShadows.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ struct CloudShadows : Feature
struct alignas(16) Settings
{
float Opacity = 0.8f;
float pad[3];
float InnerCloudShadowOpacity = 0.5f;
float pad[2];
};
STATIC_ASSERT_ALIGNAS_16(Settings);

Settings settings;

Expand Down
Loading