UI: Change node editor grid into a dot grid

This patch makes the background grid of the node editor a grid of dots
instead of lines. This makes the background look a bit more subtle and
reduces visual complexity. The dots are meant to provide a reference
when panning and zooming. Based on the design of @pablovazquez, and
a patch originally authored by @fabian_schempp.

The "Grid Levels" controls how many levels of dots are drawn. As the
editor zooms in, the higher levels of dots fade in, making them closer
together visually. The zoom factor at which each grid starts and ends
fading in is controllable in the code, and could be tweaked further
in the future. The new default value is 7, out of a range from 0 to 9.

Differential Revision: https://developer.blender.org/D10345
This commit is contained in:
Hans Goudey 2021-10-25 21:46:39 -05:00
parent 6f5bf8aa3b
commit e463d2c16f
Notes: blender-bot 2023-02-14 02:13:08 +01:00
Referenced by issue #92494, Node Snapping is scaled with Interface Resolution Scale
9 changed files with 130 additions and 16 deletions

View File

@ -817,7 +817,7 @@ const bTheme U_theme_default = {
.sub_back = RGBA(0x0000003e),
},
.shade2 = RGBA(0x7f707064),
.grid = RGBA(0x23232300),
.grid = RGBA(0x3d3d3d00),
.wire = RGBA(0x232323ff),
.select = RGBA(0xed5700ff),
.active = RGBA(0xffffffff),
@ -827,7 +827,7 @@ const bTheme U_theme_default = {
.outline_width = 1,
.facedot_size = 4,
.noodle_curving = 4,
.grid_levels = 2,
.grid_levels = 7,
.dash_alpha = 0.5f,
.syntaxl = RGBA(0x565656ff),
.syntaxs = RGBA(0x975b5bff),

View File

@ -939,7 +939,7 @@
</text_editor>
<node_editor>
<ThemeNodeEditor
grid="#353535"
grid="#1B1B1B"
node_selected="#f15800"
node_active="#f15800"
wire="#191919"
@ -955,7 +955,7 @@
matte_node="#977474"
distor_node="#749797"
noodle_curving="4"
grid_levels="2"
grid_levels="7"
dash_alpha="0.5"
input_node="#cb3d4a"
output_node="#cb3d4a"

View File

@ -39,7 +39,7 @@ extern "C" {
/* Blender file format version. */
#define BLENDER_FILE_VERSION BLENDER_VERSION
#define BLENDER_FILE_SUBVERSION 38
#define BLENDER_FILE_SUBVERSION 39
/* Minimum Blender version that supports reading file written with the current
* version. Older Blender versions will test this and show a warning if the file

View File

@ -314,6 +314,11 @@ static void do_versions_theme(const UserDef *userdef, bTheme *btheme)
btheme->space_node.dash_alpha = 0.5f;
}
if (!USER_VERSION_ATLEAST(300, 39)) {
FROM_DEFAULT_V4_UCHAR(space_node.grid);
btheme->space_node.grid_levels = 7;
}
/**
* Versioning code until next subversion bump goes here.
*

View File

@ -49,7 +49,7 @@ typedef enum {
NODE_RIGHT = 8,
} NodeBorder;
#define NODE_GRID_STEPS 5
#define NODE_GRID_STEP_SIZE 10
#define NODE_EDGE_PAN_INSIDE_PAD 2
#define NODE_EDGE_PAN_OUTSIDE_PAD 0 /* Disable clamping for node panning, use whole screen. */
#define NODE_EDGE_PAN_SPEED_RAMP 1

View File

@ -147,6 +147,10 @@ void UI_view2d_view_restore(const struct bContext *C);
/* grid drawing */
void UI_view2d_multi_grid_draw(
const struct View2D *v2d, int colorid, float step, int level_size, int totlevels);
void UI_view2d_dot_grid_draw(const struct View2D *v2d,
int grid_color_id,
float step,
int grid_levels);
void UI_view2d_draw_lines_y__values(const struct View2D *v2d);
void UI_view2d_draw_lines_x__values(const struct View2D *v2d);

View File

@ -32,6 +32,7 @@
#include "DNA_userdef_types.h"
#include "BLI_array.h"
#include "BLI_easing.h"
#include "BLI_link_utils.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
@ -1291,6 +1292,114 @@ void UI_view2d_multi_grid_draw(
immUnbindProgram();
}
static void grid_axis_start_and_count(
const float step, const float min, const float max, float *r_start, int *r_count)
{
*r_start = min;
if (*r_start < 0.0f) {
*r_start += -(float)fmod(min, step);
}
else {
*r_start += step - (float)fabs(fmod(min, step));
}
if (*r_start > max) {
*r_count = 0;
}
else {
*r_count = (max - *r_start) / step + 1;
}
}
typedef struct DotGridLevelInfo {
/* The factor applied to the #min_step argument. This could be easily computed in runtime,
* but seeing it together with the other values is helpful. */
float step_factor;
/* The normalized zoom level at which the grid level starts to fade in.
* At lower zoom levels, the points will not be visible and the level will be skipped. */
float fade_in_start_zoom;
/* The normalized zoom level at which the grid finishes fading in.
* At higher zoom levels, the points will be opaque. */
float fade_in_end_zoom;
} DotGridLevelInfo;
static const DotGridLevelInfo level_info[9] = {
{128.0f, -0.1f, 0.01f},
{64.0f, 0.0f, 0.025f},
{32.0f, 0.025f, 0.15f},
{16.0f, 0.05f, 0.2f},
{8.0f, 0.1f, 0.25f},
{4.0f, 0.125f, 0.3f},
{2.0f, 0.25f, 0.5f},
{1.0f, 0.7f, 0.9f},
{0.5f, 0.6f, 0.9f},
};
/**
* Draw a multi-level grid of dots, with a dynamic number of levels based on the fading.
*
* \param grid_color_id: The theme color used for the points. Faded dynamically based on zoom.
* \param min_step: The base size of the grid. At different zoom levels, the visible grid may have
* a larger step size.
* \param grid_levels: The maximum grid depth. Larger grid levels will subdivide the grid more.
*/
void UI_view2d_dot_grid_draw(const View2D *v2d,
const int grid_color_id,
const float min_step,
const int grid_levels)
{
BLI_assert(grid_levels > 0 && grid_levels < 10);
const float zoom_x = (float)(BLI_rcti_size_x(&v2d->mask) + 1) / BLI_rctf_size_x(&v2d->cur);
const float zoom_normalized = (zoom_x - v2d->minzoom) / (v2d->maxzoom - v2d->minzoom);
GPUVertFormat *format = immVertexFormat();
const uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
const uint color_id = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
GPU_point_size(3.0f * UI_DPI_FAC);
float color[4];
UI_GetThemeColor3fv(grid_color_id, color);
for (int level = 0; level < grid_levels; level++) {
const DotGridLevelInfo *info = &level_info[level];
const float step = min_step * info->step_factor;
const float alpha_factor = (zoom_normalized - info->fade_in_start_zoom) /
(info->fade_in_end_zoom - info->fade_in_start_zoom);
color[3] = clamp_f(BLI_easing_cubic_ease_in_out(alpha_factor, 0.0f, 1.0f, 1.0f), 0.0f, 1.0f);
if (color[3] == 0.0f) {
break;
}
int count_x;
float start_x;
grid_axis_start_and_count(step, v2d->cur.xmin, v2d->cur.xmax, &start_x, &count_x);
int count_y;
float start_y;
grid_axis_start_and_count(step, v2d->cur.ymin, v2d->cur.ymax, &start_y, &count_y);
if (count_x == 0 || count_y == 0) {
continue;
}
immBegin(GPU_PRIM_POINTS, count_x * count_y);
/* Theoretically drawing on top of lower grid levels could be avoided, but it would also
* increase the complexity of this loop, which isn't worth the time at the moment. */
for (int i_y = 0; i_y < count_y; i_y++) {
const float y = start_y + step * i_y;
for (int i_x = 0; i_x < count_x; i_x++) {
const float x = start_x + step * i_x;
immAttr4fv(color_id, color);
immVertex2f(pos, x, y);
}
}
immEnd();
}
immUnbindProgram();
}
/** \} */
/* -------------------------------------------------------------------- */

View File

@ -2227,8 +2227,6 @@ void node_draw_space(const bContext *C, ARegion *region)
snode->runtime->cursor[0] /= UI_DPI_FAC;
snode->runtime->cursor[1] /= UI_DPI_FAC;
int grid_levels = UI_GetThemeValueType(TH_NODE_GRID_LEVELS, SPACE_NODE);
ED_region_draw_cb_draw(C, region, REGION_DRAW_PRE_VIEW);
/* Only set once. */
@ -2259,14 +2257,14 @@ void node_draw_space(const bContext *C, ARegion *region)
copy_v2_v2(snode->edittree->view_center, center);
}
const int grid_levels = UI_GetThemeValueType(TH_NODE_GRID_LEVELS, SPACE_NODE);
UI_view2d_dot_grid_draw(v2d, TH_GRID, NODE_GRID_STEP_SIZE, grid_levels);
/* Top-level edit tree. */
bNodeTree *ntree = path->nodetree;
if (ntree) {
snode_setup_v2d(snode, region, center);
/* Grid. */
UI_view2d_multi_grid_draw(v2d, TH_GRID, ED_node_grid_size(), NODE_GRID_STEPS, grid_levels);
/* Backdrop. */
draw_nodespace_back_pix(C, region, snode, path->parent_key);
@ -2305,8 +2303,6 @@ void node_draw_space(const bContext *C, ARegion *region)
}
}
else {
/* Default grid. */
UI_view2d_multi_grid_draw(v2d, TH_GRID, ED_node_grid_size(), NODE_GRID_STEPS, grid_levels);
/* Backdrop. */
draw_nodespace_back_pix(C, region, snode, NODE_INSTANCE_KEY_NONE);

View File

@ -2911,10 +2911,10 @@ static void rna_def_userdef_theme_space_node(BlenderRNA *brna)
prop = RNA_def_property(srna, "grid_levels", PROP_INT, PROP_NONE);
RNA_def_property_int_sdna(prop, NULL, "grid_levels");
RNA_def_property_int_default(prop, 2);
RNA_def_property_range(prop, 0, 2);
RNA_def_property_int_default(prop, 7);
RNA_def_property_range(prop, 0, 9);
RNA_def_property_ui_text(
prop, "Grid Levels", "Amount of grid lines displayed in the background");
prop, "Grid Levels", "Number of subdivisions for the dot grid displayed in the background");
RNA_def_property_update(prop, 0, "rna_userdef_theme_update");
prop = RNA_def_property(srna, "dash_alpha", PROP_FLOAT, PROP_FACTOR);