Consume bounded additional lights in forward-lit
This commit is contained in:
@@ -10,9 +10,20 @@ layout(std140, binding = 0) uniform PerObjectConstants {
|
||||
mat4 gNormalMatrix;
|
||||
};
|
||||
|
||||
const int XC_MAX_ADDITIONAL_LIGHTS = 8;
|
||||
|
||||
struct AdditionalLightData {
|
||||
vec4 colorAndIntensity;
|
||||
vec4 positionAndRange;
|
||||
vec4 directionAndType;
|
||||
vec4 spotAnglesAndFlags;
|
||||
};
|
||||
|
||||
layout(std140, binding = 1) uniform LightingConstants {
|
||||
vec4 gMainLightDirectionAndIntensity;
|
||||
vec4 gMainLightColorAndFlags;
|
||||
vec4 gLightingParams;
|
||||
AdditionalLightData gAdditionalLights[XC_MAX_ADDITIONAL_LIGHTS];
|
||||
};
|
||||
|
||||
layout(std140, binding = 2) uniform MaterialConstants {
|
||||
@@ -57,20 +68,97 @@ float ComputeShadowAttenuation(vec3 positionWS) {
|
||||
return receiverDepth <= shadowDepth ? 1.0 : (1.0 - shadowStrength);
|
||||
}
|
||||
|
||||
float ComputeRangeAttenuation(float distanceSq, float range) {
|
||||
if (range <= 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float clampedRange = max(range, 0.0001);
|
||||
float rangeSq = clampedRange * clampedRange;
|
||||
if (distanceSq >= rangeSq) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float distance = sqrt(max(distanceSq, 0.0));
|
||||
float normalized = clamp(1.0 - distance / clampedRange, 0.0, 1.0);
|
||||
return normalized * normalized;
|
||||
}
|
||||
|
||||
float ComputeSpotAttenuation(AdditionalLightData light, vec3 directionToLightWS) {
|
||||
float cosOuter = light.spotAnglesAndFlags.x;
|
||||
float cosInner = light.spotAnglesAndFlags.y;
|
||||
vec3 spotAxisToLightWS = normalize(light.directionAndType.xyz);
|
||||
float cosTheta = dot(spotAxisToLightWS, directionToLightWS);
|
||||
return clamp((cosTheta - cosOuter) / max(cosInner - cosOuter, 1e-4), 0.0, 1.0);
|
||||
}
|
||||
|
||||
vec3 EvaluateAdditionalLight(AdditionalLightData light, vec3 normalWS, vec3 positionWS) {
|
||||
float lightType = light.directionAndType.w;
|
||||
vec3 lightColor = light.colorAndIntensity.rgb;
|
||||
float lightIntensity = light.colorAndIntensity.w;
|
||||
|
||||
vec3 directionToLightWS = vec3(0.0);
|
||||
float attenuation = 1.0;
|
||||
|
||||
if (lightType < 0.5) {
|
||||
directionToLightWS = normalize(light.directionAndType.xyz);
|
||||
} else {
|
||||
vec3 lightVectorWS = light.positionAndRange.xyz - positionWS;
|
||||
float distanceSq = dot(lightVectorWS, lightVectorWS);
|
||||
if (distanceSq <= 1e-6) {
|
||||
return vec3(0.0);
|
||||
}
|
||||
|
||||
directionToLightWS = normalize(lightVectorWS);
|
||||
attenuation = ComputeRangeAttenuation(distanceSq, light.positionAndRange.w);
|
||||
if (attenuation <= 0.0) {
|
||||
return vec3(0.0);
|
||||
}
|
||||
|
||||
if (lightType > 1.5) {
|
||||
attenuation *= ComputeSpotAttenuation(light, directionToLightWS);
|
||||
if (attenuation <= 0.0) {
|
||||
return vec3(0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
|
||||
if (diffuse <= 0.0) {
|
||||
return vec3(0.0);
|
||||
}
|
||||
|
||||
return lightColor * (diffuse * lightIntensity * attenuation);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec4 baseColor = texture(uBaseColorTexture, vTexCoord) * gBaseColorFactor;
|
||||
if (gMainLightColorAndFlags.w < 0.5) {
|
||||
int additionalLightCount = min(int(gLightingParams.x + 0.5), XC_MAX_ADDITIONAL_LIGHTS);
|
||||
if (gMainLightColorAndFlags.w < 0.5 && additionalLightCount == 0) {
|
||||
fragColor = baseColor;
|
||||
return;
|
||||
}
|
||||
|
||||
vec3 normalWS = normalize(vNormalWS);
|
||||
vec3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
|
||||
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
|
||||
float shadowAttenuation = diffuse > 0.0
|
||||
? ComputeShadowAttenuation(vPositionWS)
|
||||
: 1.0;
|
||||
vec3 lighting = vec3(0.28) +
|
||||
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w * shadowAttenuation);
|
||||
vec3 lighting = gLightingParams.yyy;
|
||||
|
||||
if (gMainLightColorAndFlags.w >= 0.5) {
|
||||
vec3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
|
||||
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
|
||||
float shadowAttenuation = diffuse > 0.0
|
||||
? ComputeShadowAttenuation(vPositionWS)
|
||||
: 1.0;
|
||||
lighting +=
|
||||
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w * shadowAttenuation);
|
||||
}
|
||||
|
||||
for (int lightIndex = 0; lightIndex < XC_MAX_ADDITIONAL_LIGHTS; ++lightIndex) {
|
||||
if (lightIndex >= additionalLightCount) {
|
||||
break;
|
||||
}
|
||||
|
||||
lighting += EvaluateAdditionalLight(gAdditionalLights[lightIndex], normalWS, vPositionWS);
|
||||
}
|
||||
|
||||
fragColor = vec4(baseColor.rgb * lighting, baseColor.a);
|
||||
}
|
||||
|
||||
@@ -12,9 +12,20 @@ layout(set = 0, binding = 0, std140) uniform PerObjectConstants {
|
||||
mat4 gNormalMatrix;
|
||||
};
|
||||
|
||||
const int XC_MAX_ADDITIONAL_LIGHTS = 8;
|
||||
|
||||
struct AdditionalLightData {
|
||||
vec4 colorAndIntensity;
|
||||
vec4 positionAndRange;
|
||||
vec4 directionAndType;
|
||||
vec4 spotAnglesAndFlags;
|
||||
};
|
||||
|
||||
layout(set = 1, binding = 0, std140) uniform LightingConstants {
|
||||
vec4 gMainLightDirectionAndIntensity;
|
||||
vec4 gMainLightColorAndFlags;
|
||||
vec4 gLightingParams;
|
||||
AdditionalLightData gAdditionalLights[XC_MAX_ADDITIONAL_LIGHTS];
|
||||
};
|
||||
|
||||
layout(set = 2, binding = 0, std140) uniform MaterialConstants {
|
||||
@@ -59,20 +70,97 @@ float ComputeShadowAttenuation(vec3 positionWS) {
|
||||
return receiverDepth <= shadowDepth ? 1.0 : (1.0 - shadowStrength);
|
||||
}
|
||||
|
||||
float ComputeRangeAttenuation(float distanceSq, float range) {
|
||||
if (range <= 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float clampedRange = max(range, 0.0001);
|
||||
float rangeSq = clampedRange * clampedRange;
|
||||
if (distanceSq >= rangeSq) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float distance = sqrt(max(distanceSq, 0.0));
|
||||
float normalized = clamp(1.0 - distance / clampedRange, 0.0, 1.0);
|
||||
return normalized * normalized;
|
||||
}
|
||||
|
||||
float ComputeSpotAttenuation(AdditionalLightData light, vec3 directionToLightWS) {
|
||||
float cosOuter = light.spotAnglesAndFlags.x;
|
||||
float cosInner = light.spotAnglesAndFlags.y;
|
||||
vec3 spotAxisToLightWS = normalize(light.directionAndType.xyz);
|
||||
float cosTheta = dot(spotAxisToLightWS, directionToLightWS);
|
||||
return clamp((cosTheta - cosOuter) / max(cosInner - cosOuter, 1e-4), 0.0, 1.0);
|
||||
}
|
||||
|
||||
vec3 EvaluateAdditionalLight(AdditionalLightData light, vec3 normalWS, vec3 positionWS) {
|
||||
float lightType = light.directionAndType.w;
|
||||
vec3 lightColor = light.colorAndIntensity.rgb;
|
||||
float lightIntensity = light.colorAndIntensity.w;
|
||||
|
||||
vec3 directionToLightWS = vec3(0.0);
|
||||
float attenuation = 1.0;
|
||||
|
||||
if (lightType < 0.5) {
|
||||
directionToLightWS = normalize(light.directionAndType.xyz);
|
||||
} else {
|
||||
vec3 lightVectorWS = light.positionAndRange.xyz - positionWS;
|
||||
float distanceSq = dot(lightVectorWS, lightVectorWS);
|
||||
if (distanceSq <= 1e-6) {
|
||||
return vec3(0.0);
|
||||
}
|
||||
|
||||
directionToLightWS = normalize(lightVectorWS);
|
||||
attenuation = ComputeRangeAttenuation(distanceSq, light.positionAndRange.w);
|
||||
if (attenuation <= 0.0) {
|
||||
return vec3(0.0);
|
||||
}
|
||||
|
||||
if (lightType > 1.5) {
|
||||
attenuation *= ComputeSpotAttenuation(light, directionToLightWS);
|
||||
if (attenuation <= 0.0) {
|
||||
return vec3(0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
|
||||
if (diffuse <= 0.0) {
|
||||
return vec3(0.0);
|
||||
}
|
||||
|
||||
return lightColor * (diffuse * lightIntensity * attenuation);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec4 baseColor = texture(sampler2D(uBaseColorTexture, uLinearSampler), vTexCoord) * gBaseColorFactor;
|
||||
if (gMainLightColorAndFlags.w < 0.5) {
|
||||
int additionalLightCount = min(int(gLightingParams.x + 0.5), XC_MAX_ADDITIONAL_LIGHTS);
|
||||
if (gMainLightColorAndFlags.w < 0.5 && additionalLightCount == 0) {
|
||||
fragColor = baseColor;
|
||||
return;
|
||||
}
|
||||
|
||||
vec3 normalWS = normalize(vNormalWS);
|
||||
vec3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
|
||||
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
|
||||
float shadowAttenuation = diffuse > 0.0
|
||||
? ComputeShadowAttenuation(vPositionWS)
|
||||
: 1.0;
|
||||
vec3 lighting = vec3(0.28) +
|
||||
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w * shadowAttenuation);
|
||||
vec3 lighting = gLightingParams.yyy;
|
||||
|
||||
if (gMainLightColorAndFlags.w >= 0.5) {
|
||||
vec3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
|
||||
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
|
||||
float shadowAttenuation = diffuse > 0.0
|
||||
? ComputeShadowAttenuation(vPositionWS)
|
||||
: 1.0;
|
||||
lighting +=
|
||||
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w * shadowAttenuation);
|
||||
}
|
||||
|
||||
for (int lightIndex = 0; lightIndex < XC_MAX_ADDITIONAL_LIGHTS; ++lightIndex) {
|
||||
if (lightIndex >= additionalLightCount) {
|
||||
break;
|
||||
}
|
||||
|
||||
lighting += EvaluateAdditionalLight(gAdditionalLights[lightIndex], normalWS, vPositionWS);
|
||||
}
|
||||
|
||||
fragColor = vec4(baseColor.rgb * lighting, baseColor.a);
|
||||
}
|
||||
|
||||
@@ -11,9 +11,20 @@ cbuffer PerObjectConstants : register(b0) {
|
||||
float4x4 gNormalMatrix;
|
||||
};
|
||||
|
||||
static const int XC_MAX_ADDITIONAL_LIGHTS = 8;
|
||||
|
||||
struct AdditionalLightData {
|
||||
float4 colorAndIntensity;
|
||||
float4 positionAndRange;
|
||||
float4 directionAndType;
|
||||
float4 spotAnglesAndFlags;
|
||||
};
|
||||
|
||||
cbuffer LightingConstants : register(b1) {
|
||||
float4 gMainLightDirectionAndIntensity;
|
||||
float4 gMainLightColorAndFlags;
|
||||
float4 gLightingParams;
|
||||
AdditionalLightData gAdditionalLights[XC_MAX_ADDITIONAL_LIGHTS];
|
||||
};
|
||||
|
||||
cbuffer MaterialConstants : register(b2) {
|
||||
@@ -59,19 +70,97 @@ float ComputeShadowAttenuation(float3 positionWS) {
|
||||
return receiverDepth <= shadowDepth ? 1.0f : (1.0f - shadowStrength);
|
||||
}
|
||||
|
||||
float ComputeRangeAttenuation(float distanceSq, float range) {
|
||||
if (range <= 0.0f) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
const float clampedRange = max(range, 0.0001f);
|
||||
const float rangeSq = clampedRange * clampedRange;
|
||||
if (distanceSq >= rangeSq) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
const float distance = sqrt(max(distanceSq, 0.0f));
|
||||
const float normalized = saturate(1.0f - distance / clampedRange);
|
||||
return normalized * normalized;
|
||||
}
|
||||
|
||||
float ComputeSpotAttenuation(AdditionalLightData light, float3 directionToLightWS) {
|
||||
const float cosOuter = light.spotAnglesAndFlags.x;
|
||||
const float cosInner = light.spotAnglesAndFlags.y;
|
||||
const float3 spotAxisToLightWS = normalize(light.directionAndType.xyz);
|
||||
const float cosTheta = dot(spotAxisToLightWS, directionToLightWS);
|
||||
return saturate((cosTheta - cosOuter) / max(cosInner - cosOuter, 1e-4f));
|
||||
}
|
||||
|
||||
float3 EvaluateAdditionalLight(AdditionalLightData light, float3 normalWS, float3 positionWS) {
|
||||
const float lightType = light.directionAndType.w;
|
||||
const float3 lightColor = light.colorAndIntensity.rgb;
|
||||
const float lightIntensity = light.colorAndIntensity.w;
|
||||
|
||||
float3 directionToLightWS = float3(0.0f, 0.0f, 0.0f);
|
||||
float attenuation = 1.0f;
|
||||
|
||||
if (lightType < 0.5f) {
|
||||
directionToLightWS = normalize(light.directionAndType.xyz);
|
||||
} else {
|
||||
const float3 lightVectorWS = light.positionAndRange.xyz - positionWS;
|
||||
const float distanceSq = dot(lightVectorWS, lightVectorWS);
|
||||
if (distanceSq <= 1e-6f) {
|
||||
return 0.0f.xxx;
|
||||
}
|
||||
|
||||
directionToLightWS = lightVectorWS * rsqrt(distanceSq);
|
||||
attenuation = ComputeRangeAttenuation(distanceSq, light.positionAndRange.w);
|
||||
if (attenuation <= 0.0f) {
|
||||
return 0.0f.xxx;
|
||||
}
|
||||
|
||||
if (lightType > 1.5f) {
|
||||
attenuation *= ComputeSpotAttenuation(light, directionToLightWS);
|
||||
if (attenuation <= 0.0f) {
|
||||
return 0.0f.xxx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const float diffuse = saturate(dot(normalWS, directionToLightWS));
|
||||
if (diffuse <= 0.0f) {
|
||||
return 0.0f.xxx;
|
||||
}
|
||||
|
||||
return lightColor * (diffuse * lightIntensity * attenuation);
|
||||
}
|
||||
|
||||
float4 MainPS(PSInput input) : SV_TARGET {
|
||||
float4 baseColor = gBaseColorTexture.Sample(gLinearSampler, input.texcoord) * gBaseColorFactor;
|
||||
if (gMainLightColorAndFlags.a < 0.5f) {
|
||||
const int additionalLightCount = min((int)gLightingParams.x, XC_MAX_ADDITIONAL_LIGHTS);
|
||||
if (gMainLightColorAndFlags.a < 0.5f && additionalLightCount == 0) {
|
||||
return baseColor;
|
||||
}
|
||||
|
||||
float3 normalWS = normalize(input.normalWS);
|
||||
float3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
|
||||
float diffuse = saturate(dot(normalWS, directionToLightWS));
|
||||
float shadowAttenuation = diffuse > 0.0f
|
||||
? ComputeShadowAttenuation(input.positionWS)
|
||||
: 1.0f;
|
||||
float3 lighting = float3(0.28f, 0.28f, 0.28f) +
|
||||
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w * shadowAttenuation);
|
||||
float3 lighting = gLightingParams.yyy;
|
||||
|
||||
if (gMainLightColorAndFlags.a >= 0.5f) {
|
||||
float3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
|
||||
float diffuse = saturate(dot(normalWS, directionToLightWS));
|
||||
float shadowAttenuation = diffuse > 0.0f
|
||||
? ComputeShadowAttenuation(input.positionWS)
|
||||
: 1.0f;
|
||||
lighting +=
|
||||
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w * shadowAttenuation);
|
||||
}
|
||||
|
||||
[unroll]
|
||||
for (int lightIndex = 0; lightIndex < XC_MAX_ADDITIONAL_LIGHTS; ++lightIndex) {
|
||||
if (lightIndex >= additionalLightCount) {
|
||||
break;
|
||||
}
|
||||
|
||||
lighting += EvaluateAdditionalLight(gAdditionalLights[lightIndex], normalWS, input.positionWS);
|
||||
}
|
||||
|
||||
return float4(baseColor.rgb * lighting, baseColor.a);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user