[Bf-blender-cvs] [95a3c256dca] tmp-eevee-shadowmap-refactor: Eevee: Shadow: Make sun clip distances automatic

Clément Foucault noreply at git.blender.org
Mon Sep 2 16:53:19 CEST 2019


Commit: 95a3c256dcaa298f0c1f81cd1465d453f4e1a055
Author: Clément Foucault
Date:   Wed Aug 28 14:50:18 2019 +0200
Branches: tmp-eevee-shadowmap-refactor
https://developer.blender.org/rB95a3c256dcaa298f0c1f81cd1465d453f4e1a055

Eevee: Shadow: Make sun clip distances automatic

This simplify sun lights setup and matches more cycles behavior.

===================================================================

M	release/scripts/startup/bl_ui/properties_data_light.py
M	source/blender/draw/engines/eevee/eevee_lights.c
M	source/blender/draw/engines/eevee/eevee_private.h
M	source/blender/makesrna/intern/rna_light.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/properties_data_light.py b/release/scripts/startup/bl_ui/properties_data_light.py
index 19775d80636..0ea89e20edc 100644
--- a/release/scripts/startup/bl_ui/properties_data_light.py
+++ b/release/scripts/startup/bl_ui/properties_data_light.py
@@ -175,9 +175,8 @@ class DATA_PT_EEVEE_shadow(DataButtonsPanel, Panel):
 
         col = layout.column()
         sub = col.column(align=True)
-        sub.prop(light, "shadow_buffer_clip_start", text="Clip Start")
-        if light.type == 'SUN':
-            sub.prop(light, "shadow_buffer_clip_end", text="End")
+        if light.type != 'SUN':
+            sub.prop(light, "shadow_buffer_clip_start", text="Clip Start")
 
         col.prop(light, "shadow_buffer_soft", text="Softness")
         col.prop(light, "shadow_buffer_bias", text="Bias")
diff --git a/source/blender/draw/engines/eevee/eevee_lights.c b/source/blender/draw/engines/eevee/eevee_lights.c
index 14385f04949..10da80ece54 100644
--- a/source/blender/draw/engines/eevee/eevee_lights.c
+++ b/source/blender/draw/engines/eevee/eevee_lights.c
@@ -184,6 +184,8 @@ void EEVEE_lights_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
          linfo->shcaster_backbuffer->alloc_count);
   memset(linfo->shcaster_frontbuffer->flags, 0x00, linfo->shcaster_frontbuffer->alloc_count);
 
+  INIT_MINMAX(linfo->shcaster_aabb.min, linfo->shcaster_aabb.max);
+
   psl->shadow_cube_store_pass = NULL;
   psl->shadow_cube_store_high_pass = NULL;
   psl->shadow_cascade_store_pass = NULL;
@@ -394,6 +396,9 @@ void EEVEE_lights_cache_shcaster_object_add(EEVEE_ViewLayerData *sldata, Object
   aabb->halfdim[1] = fabsf(aabb->halfdim[1]);
   aabb->halfdim[2] = fabsf(aabb->halfdim[2]);
 
+  minmax_v3v3_v3(linfo->shcaster_aabb.min, linfo->shcaster_aabb.max, min);
+  minmax_v3v3_v3(linfo->shcaster_aabb.min, linfo->shcaster_aabb.max, max);
+
   oedata->need_update = false;
 }
 
@@ -812,6 +817,33 @@ static void eevee_shadow_cascade_setup(Object *ob,
   copy_m4_m4(sh_data->viewinv, viewmat);
   invert_m4(viewmat);
 
+  /* Compute near and far value based on all shadow casters cumulated AABBs. */
+  float sh_near = -1.0e30f, sh_far = 1.0e30f;
+  BoundBox shcaster_bounds;
+  BKE_boundbox_init_from_minmax(
+      &shcaster_bounds, linfo->shcaster_aabb.min, linfo->shcaster_aabb.max);
+#ifdef DEBUG_CSM
+  float dbg_col1[4] = {1.0f, 0.5f, 0.6f, 1.0f};
+  DRW_debug_bbox(&shcaster_bounds, dbg_col1);
+#endif
+  for (int i = 0; i < 8; i++) {
+    mul_m4_v3(viewmat, shcaster_bounds.vec[i]);
+    sh_near = max_ff(sh_near, shcaster_bounds.vec[i][2]);
+    sh_far = min_ff(sh_far, shcaster_bounds.vec[i][2]);
+  }
+#ifdef DEBUG_CSM
+  float dbg_col2[4] = {0.5f, 1.0f, 0.6f, 1.0f};
+  float pts[2][3] = {{0.0, 0.0, sh_near}, {0.0, 0.0, sh_far}};
+  mul_m4_v3(sh_data->viewinv, pts[0]);
+  mul_m4_v3(sh_data->viewinv, pts[1]);
+  DRW_debug_sphere(pts[0], 1.0f, dbg_col1);
+  DRW_debug_sphere(pts[1], 1.0f, dbg_col2);
+#endif
+  /* The rest of the function is assuming inverted Z. */
+  /* Add a little bias to avoid invalid matrices. */
+  sh_far = -(sh_far - 1e-3);
+  sh_near = -sh_near;
+
   /* The technique consists into splitting
    * the view frustum into several sub-frustum
    * that are individually receiving one shadow map */
@@ -975,8 +1007,8 @@ static void eevee_shadow_cascade_setup(Object *ob,
                     rect_cascade.xmax,
                     rect_cascade.ymin,
                     rect_cascade.ymax,
-                    la->clipsta,
-                    la->clipend);
+                    sh_near,
+                    sh_far);
 
     mul_m4_m4m4(sh_data->viewprojmat[c], projmat, viewmat);
     mul_m4_m4m4(cascade_data->shadowmat[c], texcomat, sh_data->viewprojmat[c]);
@@ -987,8 +1019,8 @@ static void eevee_shadow_cascade_setup(Object *ob,
   }
 
   ubo_data->bias = 0.05f * la->bias;
-  ubo_data->near = la->clipsta;
-  ubo_data->far = la->clipend;
+  ubo_data->near = sh_near;
+  ubo_data->far = sh_far;
 
   evli->shadowid = (float)(sh_data->shadow_id);
   ubo_data->shadow_start = (float)(sh_data->layer_id);
diff --git a/source/blender/draw/engines/eevee/eevee_private.h b/source/blender/draw/engines/eevee/eevee_private.h
index f32d77ac099..ce0c4033987 100644
--- a/source/blender/draw/engines/eevee/eevee_private.h
+++ b/source/blender/draw/engines/eevee/eevee_private.h
@@ -454,6 +454,10 @@ typedef struct EEVEE_LightsInfo {
   /* Pointers only. */
   struct EEVEE_ShadowCasterBuffer *shcaster_frontbuffer;
   struct EEVEE_ShadowCasterBuffer *shcaster_backbuffer;
+  /* AABB of all shadow casters combined. */
+  struct {
+    float min[3], max[3];
+  } shcaster_aabb;
 } EEVEE_LightsInfo;
 
 /* EEVEE_LightsInfo->shadow_casters_flag */
diff --git a/source/blender/makesrna/intern/rna_light.c b/source/blender/makesrna/intern/rna_light.c
index 277005b8863..d1231e6b266 100644
--- a/source/blender/makesrna/intern/rna_light.c
+++ b/source/blender/makesrna/intern/rna_light.c
@@ -297,16 +297,6 @@ static void rna_def_light_shadow(StructRNA *srna, bool sun)
                            "Shadow map clip start, below which objects will not generate shadows");
   RNA_def_property_update(prop, 0, "rna_Light_draw_update");
 
-  prop = RNA_def_property(srna, "shadow_buffer_clip_end", PROP_FLOAT, PROP_DISTANCE);
-  RNA_def_property_float_sdna(prop, NULL, "clipend");
-  RNA_def_property_float_default(prop, 40.0f);
-  RNA_def_property_range(prop, 1e-6f, FLT_MAX);
-  RNA_def_property_ui_range(prop, 0.001f, FLT_MAX, 10, 3);
-  RNA_def_property_ui_text(prop,
-                           "Shadow Buffer Clip End",
-                           "Shadow map clip end, beyond which objects will not generate shadows");
-  RNA_def_property_update(prop, 0, "rna_Light_draw_update");
-
   prop = RNA_def_property(srna, "shadow_buffer_bias", PROP_FLOAT, PROP_NONE);
   RNA_def_property_float_sdna(prop, NULL, "bias");
   RNA_def_property_float_default(prop, 1.0f);



More information about the Bf-blender-cvs mailing list