[Bf-blender-cvs] [3c8c0f1094a] master: Gizmo: add gizmo for adjusting spot light blend
Weizhen Huang
noreply at git.blender.org
Fri Feb 3 11:32:23 CET 2023
Commit: 3c8c0f1094a3fad5ee47888ce0507c9707d71772
Author: Weizhen Huang
Date: Tue Jan 31 18:04:15 2023 +0100
Branches: master
https://developer.blender.org/rB3c8c0f1094a3fad5ee47888ce0507c9707d71772
Gizmo: add gizmo for adjusting spot light blend
Ref T104280
Differential Revision: https://developer.blender.org/D17171
===================================================================
M source/blender/editors/space_view3d/view3d_gizmo_light.c
===================================================================
diff --git a/source/blender/editors/space_view3d/view3d_gizmo_light.c b/source/blender/editors/space_view3d/view3d_gizmo_light.c
index df601b4e73b..d967b950bc5 100644
--- a/source/blender/editors/space_view3d/view3d_gizmo_light.c
+++ b/source/blender/editors/space_view3d/view3d_gizmo_light.c
@@ -5,6 +5,7 @@
*/
#include "BLI_math.h"
+#include "BLI_math_base_safe.h"
#include "BLI_utildefines.h"
#include "BKE_context.h"
@@ -34,6 +35,64 @@
/* -------------------------------------------------------------------- */
/** \name Spot Light Gizmos
* \{ */
+/* NOTE: scaling from `overlay_extra.cc`. */
+#define CONE_SCALE 10.0f
+#define INV_CONE_SCALE 0.1f
+
+typedef struct LightSpotWidgetGroup {
+ wmGizmo *spot_angle;
+ wmGizmo *spot_blend;
+} LightSpotWidgetGroup;
+
+static void gizmo_spot_blend_prop_matrix_get(const wmGizmo *UNUSED(gz),
+ wmGizmoProperty *gz_prop,
+ void *value_p)
+{
+ BLI_assert(gz_prop->type->array_length == 16);
+ float(*matrix)[4] = value_p;
+
+ const bContext *C = gz_prop->custom_func.user_data;
+ ViewLayer *view_layer = CTX_data_view_layer(C);
+ BKE_view_layer_synced_ensure(CTX_data_scene(C), view_layer);
+ Light *la = BKE_view_layer_active_object_get(view_layer)->data;
+
+ float a = cosf(la->spotsize * 0.5f);
+ float b = la->spotblend;
+ /* Cosine of the angle where spot attenuation == 1. */
+ float c = (1.0f - a) * b + a;
+ /* Tangent. */
+ float t = sqrtf(1.0f - c * c) / c;
+
+ matrix[0][0] = 2.0f * CONE_SCALE * t * a;
+ matrix[1][1] = 2.0f * CONE_SCALE * t * a;
+}
+
+static void gizmo_spot_blend_prop_matrix_set(const wmGizmo *UNUSED(gz),
+ wmGizmoProperty *gz_prop,
+ const void *value_p)
+{
+ const float(*matrix)[4] = value_p;
+ BLI_assert(gz_prop->type->array_length == 16);
+
+ const bContext *C = gz_prop->custom_func.user_data;
+ Scene *scene = CTX_data_scene(C);
+ ViewLayer *view_layer = CTX_data_view_layer(C);
+ BKE_view_layer_synced_ensure(scene, view_layer);
+ Light *la = BKE_view_layer_active_object_get(view_layer)->data;
+
+ float a = cosf(la->spotsize * 0.5f);
+ float t = matrix[0][0] * 0.5f * INV_CONE_SCALE / a;
+ float c = 1.0f / sqrt(t * t + 1.0f);
+
+ float spot_blend = safe_divide(clamp_f(c - a, 0.0f, 1.0f - a), 1.0f - a);
+
+ PointerRNA light_ptr;
+ RNA_pointer_create(&la->id, &RNA_Light, la, &light_ptr);
+ PropertyRNA *spot_blend_prop = RNA_struct_find_property(&light_ptr, "spot_blend");
+ RNA_property_float_set(&light_ptr, spot_blend_prop, spot_blend);
+
+ RNA_property_update_main(CTX_data_main(C), scene, &light_ptr, spot_blend_prop);
+}
static bool WIDGETGROUP_light_spot_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
{
@@ -59,42 +118,78 @@ static bool WIDGETGROUP_light_spot_poll(const bContext *C, wmGizmoGroupType *UNU
return false;
}
-static void WIDGETGROUP_light_spot_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
+static void WIDGETGROUP_light_spot_setup(const bContext *C, wmGizmoGroup *gzgroup)
{
- wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
+ LightSpotWidgetGroup *ls_gzgroup = MEM_mallocN(sizeof(LightSpotWidgetGroup), __func__);
- wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_arrow_3d", gzgroup, NULL);
- wmGizmo *gz = wwrapper->gizmo;
- RNA_enum_set(gz->ptr, "transform", ED_GIZMO_ARROW_XFORM_FLAG_INVERTED);
+ gzgroup->customdata = ls_gzgroup;
- gzgroup->customdata = wwrapper;
-
- ED_gizmo_arrow3d_set_range_fac(gz, 4.0f);
+ /* Spot angle gizmo. */
+ {
+ ls_gzgroup->spot_angle = WM_gizmo_new("GIZMO_GT_arrow_3d", gzgroup, NULL);
+ wmGizmo *gz = ls_gzgroup->spot_angle;
+ RNA_enum_set(gz->ptr, "transform", ED_GIZMO_ARROW_XFORM_FLAG_INVERTED);
+ ED_gizmo_arrow3d_set_range_fac(gz, 4.0f);
+ UI_GetThemeColor3fv(TH_GIZMO_SECONDARY, gz->color);
+ }
- UI_GetThemeColor3fv(TH_GIZMO_SECONDARY, gz->color);
+ /* Spot blend gizmo. */
+ {
+ ls_gzgroup->spot_blend = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
+ wmGizmo *gz = ls_gzgroup->spot_blend;
+ RNA_enum_set(gz->ptr,
+ "transform",
+ ED_GIZMO_CAGE_XFORM_FLAG_SCALE | ED_GIZMO_CAGE_XFORM_FLAG_SCALE_UNIFORM);
+ RNA_enum_set(gz->ptr, "draw_style", ED_GIZMO_CAGE2D_STYLE_CIRCLE);
+ WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_HOVER, true);
+ UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
+ UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
+
+ WM_gizmo_target_property_def_func(gz,
+ "matrix",
+ &(const struct wmGizmoPropertyFnParams){
+ .value_get_fn = gizmo_spot_blend_prop_matrix_get,
+ .value_set_fn = gizmo_spot_blend_prop_matrix_set,
+ .range_get_fn = NULL,
+ .user_data = (void *)C,
+ });
+ }
}
static void WIDGETGROUP_light_spot_refresh(const bContext *C, wmGizmoGroup *gzgroup)
{
- wmGizmoWrapper *wwrapper = gzgroup->customdata;
- wmGizmo *gz = wwrapper->gizmo;
+ LightSpotWidgetGroup *ls_gzgroup = gzgroup->customdata;
const Scene *scene = CTX_data_scene(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
BKE_view_layer_synced_ensure(scene, view_layer);
Object *ob = BKE_view_layer_active_object_get(view_layer);
Light *la = ob->data;
- float dir[3];
- negate_v3_v3(dir, ob->object_to_world[2]);
+ {
+ PointerRNA lamp_ptr;
+ RNA_pointer_create(&la->id, &RNA_Light, la, &lamp_ptr);
- WM_gizmo_set_matrix_rotation_from_z_axis(gz, dir);
- WM_gizmo_set_matrix_location(gz, ob->object_to_world[3]);
+ wmGizmo *gz = ls_gzgroup->spot_angle;
+ float dir[3];
+ negate_v3_v3(dir, ob->object_to_world[2]);
+ WM_gizmo_set_matrix_rotation_from_z_axis(gz, dir);
+ WM_gizmo_set_matrix_location(gz, ob->object_to_world[3]);
- /* need to set property here for undo. TODO: would prefer to do this in _init. */
- PointerRNA lamp_ptr;
- const char *propname = "spot_size";
- RNA_pointer_create(&la->id, &RNA_Light, la, &lamp_ptr);
- WM_gizmo_target_property_def_rna(gz, "offset", &lamp_ptr, propname, -1);
+ const char *propname = "spot_size";
+ WM_gizmo_target_property_def_rna(gz, "offset", &lamp_ptr, propname, -1);
+ }
+
+ {
+ wmGizmo *gz = ls_gzgroup->spot_blend;
+
+ copy_m4_m4(gz->matrix_basis, ob->object_to_world);
+
+ /* Move center to the cone base plane. */
+ float dir[3];
+ negate_v3_v3(dir, ob->object_to_world[2]);
+ mul_v3_fl(dir, CONE_SCALE * cosf(0.5f * la->spotsize));
+ add_v3_v3(gz->matrix_basis[3], dir);
+ }
}
void VIEW3D_GGT_light_spot(wmGizmoGroupType *gzgt)
More information about the Bf-blender-cvs
mailing list