73 lines
2.3 KiB
HLSL
73 lines
2.3 KiB
HLSL
// XC_BUILTIN_SKYBOX_D3D12_PS
|
|
Texture2D gSkyboxTexture : register(t0);
|
|
SamplerState gLinearSampler : register(s0);
|
|
|
|
cbuffer EnvironmentConstants : register(b0) {
|
|
float4 gSkyboxTopColor;
|
|
float4 gSkyboxHorizonColor;
|
|
float4 gSkyboxBottomColor;
|
|
float4 gCameraRightAndTanHalfFov;
|
|
float4 gCameraUpAndAspect;
|
|
float4 gCameraForwardAndUnused;
|
|
}
|
|
|
|
cbuffer MaterialConstants : register(b1) {
|
|
float4 gSkyboxTintAndExposure;
|
|
float4 gSkyboxRotationAndMode;
|
|
}
|
|
|
|
struct PSInput {
|
|
float4 position : SV_POSITION;
|
|
float2 ndc : TEXCOORD0;
|
|
};
|
|
|
|
static const float XC_PI = 3.14159265358979323846f;
|
|
static const float XC_INV_PI = 0.31830988618379067154f;
|
|
static const float XC_INV_TWO_PI = 0.15915494309189533577f;
|
|
|
|
float3 EvaluateProceduralSkybox(float3 viewRay) {
|
|
const float vertical = clamp(viewRay.y, -1.0f, 1.0f);
|
|
float3 color = gSkyboxHorizonColor.rgb;
|
|
if (vertical >= 0.0f) {
|
|
color = lerp(gSkyboxHorizonColor.rgb, gSkyboxTopColor.rgb, pow(saturate(vertical), 0.65f));
|
|
} else {
|
|
color = lerp(gSkyboxHorizonColor.rgb, gSkyboxBottomColor.rgb, pow(saturate(-vertical), 0.55f));
|
|
}
|
|
|
|
return color;
|
|
}
|
|
|
|
float2 ComputePanoramicUv(float3 viewRay) {
|
|
const float rotation = gSkyboxRotationAndMode.x;
|
|
const float sinTheta = sin(rotation);
|
|
const float cosTheta = cos(rotation);
|
|
const float3 rotatedRay = normalize(float3(
|
|
viewRay.x * cosTheta - viewRay.z * sinTheta,
|
|
viewRay.y,
|
|
viewRay.x * sinTheta + viewRay.z * cosTheta));
|
|
|
|
const float u = frac(atan2(rotatedRay.z, rotatedRay.x) * XC_INV_TWO_PI + 0.5f);
|
|
const float v = acos(clamp(rotatedRay.y, -1.0f, 1.0f)) * XC_INV_PI;
|
|
return float2(u, saturate(v));
|
|
}
|
|
|
|
float4 MainPS(PSInput input) : SV_Target {
|
|
const float tanHalfFov = gCameraRightAndTanHalfFov.w;
|
|
const float aspect = gCameraUpAndAspect.w;
|
|
|
|
float3 viewRay = normalize(
|
|
gCameraForwardAndUnused.xyz +
|
|
input.ndc.x * aspect * tanHalfFov * gCameraRightAndTanHalfFov.xyz +
|
|
input.ndc.y * tanHalfFov * gCameraUpAndAspect.xyz);
|
|
|
|
float3 color = EvaluateProceduralSkybox(viewRay);
|
|
if (gSkyboxRotationAndMode.y > 0.5f) {
|
|
const float2 uv = ComputePanoramicUv(viewRay);
|
|
color = gSkyboxTexture.Sample(gLinearSampler, uv).rgb *
|
|
gSkyboxTintAndExposure.rgb *
|
|
gSkyboxTintAndExposure.w;
|
|
}
|
|
|
|
return float4(color, 1.0f);
|
|
}
|