[Bf-blender-cvs] [da8963af2dd] soc-2021-uv-editor-improvements: Cleanup and fixes: UV grid types
Siddhartha Jejurkar
noreply at git.blender.org
Mon Aug 16 03:23:57 CEST 2021
Commit: da8963af2dd49201fb214a6e91ea2e251a74257e
Author: Siddhartha Jejurkar
Date: Mon Aug 16 06:44:00 2021 +0530
Branches: soc-2021-uv-editor-improvements
https://developer.blender.org/rBda8963af2dd49201fb214a6e91ea2e251a74257e
Cleanup and fixes: UV grid types
* Code and comment cleanup
* Refactor subdividing grid code to allow creating grid of different
dimensions different from the previous 4x4 grid
* Change subdividing grid dimensions to start from 8x8
* Refactor code used to calculate increment snapping value for the UV
editor
===================================================================
M source/blender/draw/engines/overlay/overlay_grid.c
M source/blender/draw/engines/overlay/overlay_private.h
M source/blender/draw/engines/overlay/shaders/grid_frag.glsl
M source/blender/editors/include/ED_image.h
M source/blender/editors/space_image/image_draw.c
M source/blender/editors/transform/transform.c
M source/blender/makesdna/DNA_space_types.h
===================================================================
diff --git a/source/blender/draw/engines/overlay/overlay_grid.c b/source/blender/draw/engines/overlay/overlay_grid.c
index 3077ddd4ee3..9d0f03b0cfe 100644
--- a/source/blender/draw/engines/overlay/overlay_grid.c
+++ b/source/blender/draw/engines/overlay/overlay_grid.c
@@ -70,19 +70,33 @@ void OVERLAY_grid_init(OVERLAY_Data *vedata)
copy_v3_fl3(
shd->grid_size, (float)sima->tile_grid_shape[0], (float)sima->tile_grid_shape[1], 1.0f);
- /* For a NxN grid. Keep insync with value in initSnapSpatial() inside transform.c */
- int N = 4;
- shd->zoom_factor = ED_space_image_zoom_level(v2d, N);
+ /**
+ * Previously the UV/Image Editor grid was static :
+ * - 0-1 UV space divided into 4x4 divisions marked by thick grid lines (1 grid unit = 0.25 UV
+ * units)
+ * - 0-1 UV space divided into 16x16 divisions marked by thin grid lines (1 grid unit = 0.0625
+ * UV units)
+ *
+ * The new UV/Image Editor grid now supports 2 grid types :
+ * - Subdividing grid : Similar to the 3D viewport grid (zooming in adds more divisions to the
+ * grid) [T89789]
+ * - Dynamic grid : Users create a desired NxN grid by using options exposed in UI [T78389]
+ */
if (sima->flag & SI_DYNAMIC_GRID) {
shd->grid_flag |= DYNAMIC_GRID;
/* Temporary fix : dynamic_grid_size is not using the default value (=1) assignd in RNA */
sima->dynamic_grid_size = (sima->dynamic_grid_size == 0) ? 1 : sima->dynamic_grid_size;
- ED_space_image_grid_steps(sima->dynamic_grid_size, shd->grid_steps, true);
- }
- else {
- ED_space_image_grid_steps(N, shd->grid_steps, false);
}
+ /* N denotes the grid dimension when zoomed out (NxN grid).
+ * While zooming in, each grid division further subdivides into smaller NxN divisions
+ *
+ * If this value is changed, then also update the value in initSnapSpatial()
+ * TODO? : Probably best to move this value to SpaceImage/View2D struct */
+ int N = 8;
+ shd->zoom_factor = ED_space_image_zoom_level(v2d, N);
+
+ ED_space_image_grid_steps(sima, shd->grid_steps, N);
return;
}
diff --git a/source/blender/draw/engines/overlay/overlay_private.h b/source/blender/draw/engines/overlay/overlay_private.h
index 873cbf088a4..6dea1984ff6 100644
--- a/source/blender/draw/engines/overlay/overlay_private.h
+++ b/source/blender/draw/engines/overlay/overlay_private.h
@@ -142,7 +142,7 @@ typedef struct OVERLAY_ShadingData {
float grid_steps[8];
float inv_viewport_size[2];
float grid_line_size;
- float zoom_factor; /* Length per pixel in the UV editor viewport */
+ float zoom_factor; /* Only for UV/Image editor */
int grid_flag;
int zpos_flag;
int zneg_flag;
diff --git a/source/blender/draw/engines/overlay/shaders/grid_frag.glsl b/source/blender/draw/engines/overlay/shaders/grid_frag.glsl
index 81e3f341a4f..93fba194a4c 100644
--- a/source/blender/draw/engines/overlay/shaders/grid_frag.glsl
+++ b/source/blender/draw/engines/overlay/shaders/grid_frag.glsl
@@ -130,8 +130,7 @@ void main()
/* For UV/Image editor use zoomFactor */
if((gridFlag & PLANE_IMAGE) != 0 && (gridFlag & DYNAMIC_GRID) == 0)
- {/* grid begins to appear when the length of one grid unit is at least
- * (N^2) pixels in the UV/Image editor.
+ {/* Grid begins to appear when the length of one grid unit is at least 256/N pixels (for NxN grid)
* Value of N defined in overlay_grid.c */
grid_res = zoomFactor;
}
diff --git a/source/blender/editors/include/ED_image.h b/source/blender/editors/include/ED_image.h
index a0d64037ab9..368acecd8ef 100644
--- a/source/blender/editors/include/ED_image.h
+++ b/source/blender/editors/include/ED_image.h
@@ -45,10 +45,12 @@ struct View2D;
/* image_draw.c (Utility functions - should probably be moved to a BKE header) */
float ED_space_image_zoom_level(const struct View2D *v2d, const int grid_dimension);
-void ED_space_image_grid_steps(const int grid_dimension,
+void ED_space_image_grid_steps(struct SpaceImage *sima,
float grid_steps[8],
- const bool is_dynamic_grid);
-float ED_space_image_increment_snap_value(const float grid_steps[8], const float zoom_factor);
+ const int grid_dimension);
+float ED_space_image_increment_snap_value(const int grid_dimesnions,
+ const float grid_steps[8],
+ const float zoom_factor);
/* image_edit.c, exported for transform */
struct Image *ED_space_image(struct SpaceImage *sima);
diff --git a/source/blender/editors/space_image/image_draw.c b/source/blender/editors/space_image/image_draw.c
index 261ae66248e..4cd891f7c34 100644
--- a/source/blender/editors/space_image/image_draw.c
+++ b/source/blender/editors/space_image/image_draw.c
@@ -578,22 +578,22 @@ void draw_image_cache(const bContext *C, ARegion *region)
float ED_space_image_zoom_level(const View2D *v2d, const int grid_dimension)
{
+ /* UV-space length per pixel */
float xzoom = (v2d->cur.xmax - v2d->cur.xmin) / ((float)(v2d->mask.xmax - v2d->mask.xmin));
float yzoom = (v2d->cur.ymax - v2d->cur.ymin) / ((float)(v2d->mask.ymax - v2d->mask.ymin));
- /* Calculating average of xzoom and yzoom for accuracy. Using only xzoom or yzoom would have
- * been sufficient */
- float zoom_factor = (xzoom + yzoom) / 2.0f;
- /* grid begins to appear when the length of one grid unit is at least (N^2) pixels in the
- * UV/Image editor for a (NxN grid) */
- zoom_factor *= (grid_dimension * grid_dimension);
+
+ /* zoom_factor for UV/Image editor is calculated based on :
+ * - Default grid size on startup, which is 256x256 pixels
+ * - How blend factor for grid lines is set up in the fragment shader (grid_frag.glsl) */
+ float zoom_factor;
+ zoom_factor = (xzoom + yzoom) / 2.0f; /* Average for accuracy */
+ zoom_factor *= 256.0f / (powf(grid_dimension, 2));
return zoom_factor;
}
-void ED_space_image_grid_steps(const int grid_dimension,
- float grid_steps[8],
- const bool is_dynamic_grid)
+void ED_space_image_grid_steps(SpaceImage *sima, float grid_steps[8], const int grid_dimension)
{
- if (is_dynamic_grid) {
+ if (sima->flag & SI_DYNAMIC_GRID) {
for (int step = 0; step < 8; step++) {
grid_steps[step] = powf(1, step) * (1.0f / ((float)grid_dimension));
}
@@ -608,36 +608,24 @@ void ED_space_image_grid_steps(const int grid_dimension,
/* Calculate the increment snapping value for UV/image editor based on the zoom factor
* The code in here (except the offset part) is used in `grid_frag.glsl` (see `grid_res`) for
* drawing the grid overlay for the UV/Image editor */
-float ED_space_image_increment_snap_value(const float grid_steps[8], const float zoom_factor)
+float ED_space_image_increment_snap_value(const int grid_dimesnions,
+ const float grid_steps[8],
+ const float zoom_factor)
{
/* Small offset on each grid_steps[] so that snapping value doesn't change until grid lines are
* significantly visible.
- * Offset = 3/4 * (grid_steps[i] - grid_steps[i+1])
+ * Offset = 3/4 * (grid_steps[i] - (grid_steps[i]/grid_dimesnsions))
*
- * Refer grid_frag.glsl to find out when grid lines actually start
- * appearing */
- if ((grid_steps[0] - ((9 * grid_steps[0]) / (4.0f * 4.0f))) > zoom_factor) {
- return grid_steps[0];
- }
- else if ((grid_steps[1] - ((9 * grid_steps[1]) / (4.0f * 4.0f))) > zoom_factor) {
- return grid_steps[1];
- }
- else if ((grid_steps[2] - ((9 * grid_steps[2]) / (4.0f * 4.0f))) > zoom_factor) {
- return grid_steps[2];
- }
- else if ((grid_steps[3] - ((9 * grid_steps[3]) / (4.0f * 4.0f))) > zoom_factor) {
- return grid_steps[3];
- }
- else if ((grid_steps[4] - ((9 * grid_steps[4]) / (4.0f * 4.0f))) > zoom_factor) {
- return grid_steps[4];
- }
- else if ((grid_steps[5] - ((9 * grid_steps[5]) / (4.0f * 4.0f))) > zoom_factor) {
- return grid_steps[5];
- }
- else if ((grid_steps[6] - ((9 * grid_steps[6]) / (4.0f * 4.0f))) > zoom_factor) {
- return grid_steps[6];
- }
- else {
- return grid_steps[7];
+ * Refer grid_frag.glsl to find out when grid lines actually start appearing */
+
+ for (int step = 0; step < 8; step++) {
+ float offset = (3.0f / 4.0f) * (grid_steps[step] - (grid_steps[step] / grid_dimesnions));
+
+ if ((grid_steps[step] - offset) > zoom_factor) {
+ return grid_steps[step];
+ }
}
+
+ /* Fallback */
+ return grid_steps[0];
}
\ No newline at end of file
diff --git a/source/blender/editors/transform/transform.c b/source/blender/editors/transform/transform.c
index c60f303b540..12e44c4092a 100644
--- a/source/blender/editors/transform/transform.c
+++ b/source/blender/editors/transform/transform.c
@@ -1614,24 +1614,20 @@ static void initSnapSpatial(TransInfo *t, float r_snap[2])
}
}
else if (t->spacetype == SPACE_IMAGE) {
- /* Change the default value of 0.0625 since the UV editor grid is now dynamically subdividing
- */
SpaceImage *sima = t->area->spacedata.first;
View2D *v2d = &t->region->v2d;
- /* For a NxN grid. Keep in sync with value in overay_grid.c. Could be moved to View2D or
- * SpaceImage to make it global indirectly */
- int N = 4;
- float zoom_factor = ED_space_image_zoom_level(v2d, N); /* Use a better name */
+ /* N denotes the grid dimension when zoomed out (NxN grid).
+ * While zooming in, each grid division further subdivides into smaller NxN divisions
+ *
+ * If this value is changed, then also update the value in OVERLAY_grid_init()
+ * TODO? : Probably best to move this value to SpaceImage/View2D struct */
+ int N = 8;
+ float zoom_factor = ED_space_image_zoom_level(v2d, N);
float grid_steps[8];
- if (sima->flag & SI_DYNAMIC_GRID) {
- ED_space_image_grid_steps(sima->dynamic_grid_size, grid_steps, true);
- }
- else {
- ED_space_image_grid_steps(N, grid_steps, false);
- }
-
- r_snap[0] = ED_space_image_increment_snap_value(grid_st
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list