Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wide SpotLight3D clustering #100236

Merged
merged 1 commit into from
Dec 13, 2024
Merged
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
46 changes: 22 additions & 24 deletions servers/rendering/renderer_rd/cluster_builder_rd.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ class ClusterBuilderRD {

radius *= p_radius;

if (p_type == LIGHT_TYPE_OMNI) {
// Spotlights with wide angle are trated as Omni lights.
// If the spot angle is above the threshold, we need a sphere instead of a cone for building the clusters
// since the cone gets too flat/large (spot angle close to 90 degrees) or
// can't even cover the affected area of the light (spot angle above 90 degrees).
if (p_type == LIGHT_TYPE_OMNI || (p_type == LIGHT_TYPE_SPOT && p_spot_aperture > WIDE_SPOT_ANGLE_THRESHOLD_DEG)) {
radius *= shared->sphere_overfit; // Overfit icosphere.

float depth = -xform.origin.z;
Expand All @@ -269,15 +273,21 @@ class ClusterBuilderRD {
e.scale[0] = radius;
e.scale[1] = radius;
e.scale[2] = radius;
e.type = ELEMENT_TYPE_OMNI_LIGHT;
e.original_index = cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT];
if (p_type == LIGHT_TYPE_OMNI) {
e.type = ELEMENT_TYPE_OMNI_LIGHT;
e.original_index = cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT];
cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT]++;
} else { // LIGHT_TYPE_SPOT with wide angle.
e.type = ELEMENT_TYPE_SPOT_LIGHT;
e.has_wide_spot_angle = true;
e.original_index = cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT];
cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT]++;
}

RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv);

cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT]++;

} else /*LIGHT_TYPE_SPOT */ {
radius *= shared->cone_overfit; // Overfit icosphere
} else /*LIGHT_TYPE_SPOT with no wide angle*/ {
radius *= shared->cone_overfit; // Overfit cone.

real_t len = Math::tan(Math::deg_to_rad(p_spot_aperture)) * radius;
// Approximate, probably better to use a cone support function.
Expand Down Expand Up @@ -310,24 +320,12 @@ class ClusterBuilderRD {
}

e.touches_far = max_d > z_far;

// If the spot angle is above the threshold, use a sphere instead of a cone for building the clusters
// since the cone gets too flat/large (spot angle close to 90 degrees) or
// can't even cover the affected area of the light (spot angle above 90 degrees).
if (p_spot_aperture > WIDE_SPOT_ANGLE_THRESHOLD_DEG) {
e.scale[0] = radius;
e.scale[1] = radius;
e.scale[2] = radius;
e.has_wide_spot_angle = true;
} else {
e.scale[0] = len * shared->cone_overfit;
e.scale[1] = len * shared->cone_overfit;
e.scale[2] = radius;
e.has_wide_spot_angle = false;
}

e.scale[0] = len * shared->cone_overfit;
e.scale[1] = len * shared->cone_overfit;
e.scale[2] = radius;
e.has_wide_spot_angle = false;
e.type = ELEMENT_TYPE_SPOT_LIGHT;
e.original_index = cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT]; // Use omni light since they share index.
e.original_index = cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT];

RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv);

Expand Down