Anim: Per bone wire width for custom shapes #120176

Merged
Christoph Lendenfeld merged 48 commits from ChrisLend/blender:bone_wire_width into main 2024-05-30 15:51:42 +02:00
6 changed files with 121 additions and 149 deletions
Showing only changes of commit 3b5cab475a - Show all commits

View File

@ -750,6 +750,8 @@ set(GLSL_SRC
engines/overlay/shaders/overlay_armature_shape_solid_frag.glsl
engines/overlay/shaders/overlay_armature_shape_solid_vert.glsl
engines/overlay/shaders/overlay_armature_shape_wire_vert.glsl
engines/overlay/shaders/overlay_armature_shape_wire_frag.glsl
engines/overlay/shaders/overlay_armature_shape_wire_geom.glsl
engines/overlay/shaders/overlay_armature_sphere_outline_vert.glsl
engines/overlay/shaders/overlay_armature_sphere_solid_frag.glsl
engines/overlay/shaders/overlay_armature_sphere_solid_vert.glsl
@ -757,7 +759,6 @@ set(GLSL_SRC
engines/overlay/shaders/overlay_armature_stick_vert.glsl
engines/overlay/shaders/overlay_armature_wire_frag.glsl
engines/overlay/shaders/overlay_armature_wire_vert.glsl
engines/overlay/shaders/overlay_armature_wire_geom.glsl
engines/overlay/shaders/overlay_background_frag.glsl
engines/overlay/shaders/overlay_clipbound_vert.glsl
engines/overlay/shaders/overlay_common_lib.glsl

View File

@ -17,14 +17,6 @@ GPU_SHADER_CREATE_INFO(overlay_armature_common)
.push_constant(Type::FLOAT, "alpha")
.additional_info("draw_view");
GPU_SHADER_INTERFACE_INFO(overlay_armature_wire_geom_iface, "geometry_out")
.flat(Type::VEC4, "finalColor");
GPU_SHADER_INTERFACE_INFO(overlay_armature_wire_geom_flat_iface, "geometry_flat_out")
.flat(Type::VEC4, "finalColorOuter");
GPU_SHADER_INTERFACE_INFO(overlay_armature_wire_geom_noperspective_iface,
"geometry_noperspective_out")
.no_perspective(Type::FLOAT, "edgeCoord");
/* -------------------------------------------------------------------- */
/** \name Armature Sphere
* \{ */
@ -144,21 +136,36 @@ GPU_SHADER_CREATE_INFO(overlay_armature_shape_solid_clipped)
.do_static_compilation(true)
.additional_info("overlay_armature_shape_solid", "drw_clipped");
GPU_SHADER_INTERFACE_INFO(overlay_armature_shape_wire_iface, "geometry_in")
.smooth(Type::VEC4, "finalColor")
.smooth(Type::VEC4, "finalColorOuter_")
.smooth(Type::UINT, "selectOverride_");
GPU_SHADER_INTERFACE_INFO(overlay_armature_shape_wire_geom_iface, "geometry_out")
.smooth(Type::VEC4, "finalColor");
GPU_SHADER_INTERFACE_INFO(overlay_armature_shape_wire_geom_flat_iface, "geometry_flat_out")
.flat(Type::VEC4, "finalColorOuter");
GPU_SHADER_INTERFACE_INFO(overlay_armature_shape_wire_geom_noperspective_iface,
"geometry_noperspective_out")
.no_perspective(Type::FLOAT, "edgeCoord");
GPU_SHADER_CREATE_INFO(overlay_armature_shape_wire)
.do_static_compilation(true)
.push_constant(Type::BOOL, "do_smooth_wire")
.vertex_in(0, Type::VEC3, "pos")
.vertex_in(1, Type::VEC3, "nor")
/* Per instance. */
.vertex_in(2, Type::MAT4, "inst_obmat")
.vertex_out(overlay_armature_wire_iface)
.push_constant(Type::BOOL, "do_smooth_wire")
.geometry_out(overlay_armature_wire_geom_iface)
.geometry_out(overlay_armature_wire_geom_flat_iface)
.geometry_out(overlay_armature_wire_geom_noperspective_iface)
.geometry_layout(PrimitiveIn::LINES, PrimitiveOut::TRIANGLE_STRIP, 4)
.vertex_out(overlay_armature_shape_wire_iface)
.vertex_source("overlay_armature_shape_wire_vert.glsl")
.geometry_source("overlay_armature_wire_geom.glsl")
.fragment_source("overlay_armature_wire_frag.glsl")
.geometry_out(overlay_armature_shape_wire_geom_iface)
.geometry_out(overlay_armature_shape_wire_geom_flat_iface)
.geometry_out(overlay_armature_shape_wire_geom_noperspective_iface)
.geometry_layout(PrimitiveIn::LINES, PrimitiveOut::TRIANGLE_STRIP, 4)
.geometry_source("overlay_armature_shape_wire_geom.glsl")
.fragment_source("overlay_armature_shape_wire_frag.glsl")
.additional_info("overlay_frag_output", "overlay_armature_common", "draw_globals");
GPU_SHADER_CREATE_INFO(overlay_armature_shape_wire_clipped)

View File

@ -0,0 +1,13 @@
/* SPDX-FileCopyrightText: 2019-2023 Blender Authors

if i may, i think 2023should be replaced by 2024

if i may, i think `2023`should be replaced by `2024`

Good catch. I think the currently preferred approach is to just use the year at which the file was first created, so 2019-20232024.

Good catch. I think the currently preferred approach is to just use the year at which the file was first created, so `2019-2023` → `2024`.
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma BLENDER_REQUIRE(common_view_lib.glsl)
#pragma BLENDER_REQUIRE(select_lib.glsl)
void main()
{
lineOutput = vec4(0.0);
fragColor = vec4(geometry_out.finalColor.rgb, geometry_out.finalColor.a * alpha);
select_id_output(select_id);
}

View File

@ -0,0 +1,80 @@
/* SPDX-FileCopyrightText: 2019-2024 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma BLENDER_REQUIRE(common_view_clipping_lib.glsl)
#pragma BLENDER_REQUIRE(common_view_lib.glsl)
void do_vertex(vec4 color, vec4 pos, float coord, vec2 offset)
{
geometry_out.finalColor = color;
geometry_noperspective_out.edgeCoord = coord;
gl_Position = pos;
/* Multiply offset by 2 because gl_Position range is [-1..1]. */

The comment seems a bit incomplete. It's nice that that range is [-1…1], but without knowing what the range of offset is, this doesn't help much.

The comment seems a bit incomplete. It's nice that that range is `[-1…1]`, but without knowing what the range of `offset` is, this doesn't help much.

About this and the comment below. I copied that comment from the _geom.glsl file I used as a reference to construct this.
I see how that isn't clear from the PR but I am still uncertain how I should handle that.
Change the comment only in this file or make a refactor that changes it in all

About this and the comment below. I copied that comment from the _geom.glsl file I used as a reference to construct this. I see how that isn't clear from the PR but I am still uncertain how I should handle that. Change the comment only in this file or make a refactor that changes it in all
gl_Position.xy += offset * 2.0 * pos.w;
/* Correct but fails due to an AMD compiler bug, see: #62792.

Move the comment underneath the #if 0 so that it's clearer that it's scoped to that.

Also issue #62792 has been closed as 'resolved' years ago, so not sure why this is referenced here.

Move the comment underneath the `#if 0` so that it's clearer that it's scoped to that. Also issue #62792 has been closed as 'resolved' years ago, so not sure why this is referenced here.
* Do inline instead. */
#if 0
view_clipping_distances_set(gl_in[i]);
#endif
gpu_EmitVertex();
}
void main()
{
vec2 ss_pos[2];
/* Clip line against near plane to avoid deformed lines. */
vec4 pos0 = gl_in[0].gl_Position;
vec4 pos1 = gl_in[1].gl_Position;
vec2 pz_ndc = vec2(pos0.z / pos0.w, pos1.z / pos1.w);
bvec2 clipped = lessThan(pz_ndc, vec2(-1.0));
if (all(clipped)) {
/* Totally clipped. */
return;
}
vec4 pos01 = pos0 - pos1;
float ofs = abs((pz_ndc.y + 1.0) / (pz_ndc.x - pz_ndc.y));
if (clipped.y) {
pos1 += pos01 * ofs;
}
else if (clipped.x) {
pos0 -= pos01 * (1.0 - ofs);
}
ss_pos[0] = pos0.xy / pos0.w;
ss_pos[1] = pos1.xy / pos1.w;
vec2 line = ss_pos[0] - ss_pos[1];
line = abs(line) * sizeViewport.xy;
geometry_flat_out.finalColorOuter = geometry_in[0].finalColorOuter_;
float half_size = sizeEdge;
/* Enlarge edge for flag display. */
half_size += (geometry_flat_out.finalColorOuter.a > 0.0) ? max(sizeEdge, 1.0) : 0.0;
if (do_smooth_wire) {
/* Add 1px for AA */
half_size += 0.5;
}
vec3 edge_ofs = vec3(half_size * sizeViewportInv, 0.0);
bool horizontal = line.x > line.y;
edge_ofs = (horizontal) ? edge_ofs.zyz : edge_ofs.xzz;
/* Due to an AMD glitch, this line was moved out of the `do_vertex`
* function (see #62792). */
view_clipping_distances_set(gl_in[0]);
do_vertex(geometry_in[0].finalColor, pos0, half_size, edge_ofs.xy);
do_vertex(geometry_in[0].finalColor, pos0, -half_size, -edge_ofs.xy);
view_clipping_distances_set(gl_in[1]);
vec4 final_color = (geometry_in[0].selectOverride_ == 0u) ? geometry_in[1].finalColor :
geometry_in[0].finalColor;
do_vertex(final_color, pos1, half_size, edge_ofs.xy);
do_vertex(final_color, pos1, -half_size, -edge_ofs.xy);
EndPrimitive();
}

View File

@ -13,10 +13,10 @@ void main()
vec3 world_pos = (model_mat * vec4(pos, 1.0)).xyz;
gl_Position = point_world_to_ndc(world_pos);
finalColor.rgb = mix(state_color.rgb, bone_color.rgb, 0.5);
finalColor.a = 1.0;
geometry_in.finalColor.rgb = mix(state_color.rgb, bone_color.rgb, 0.5);
geometry_in.finalColor.a = 1.0;
edgeStart = edgePos = ((gl_Position.xy / gl_Position.w) * 0.5 + 0.5) * sizeViewport.xy;
// edgeStart = edgePos = ((gl_Position.xy / gl_Position.w) * 0.5 + 0.5) * sizeViewport.xy;

AFAIK it should be possible to #include a file in both GLSL and C++, so that this constant can be shared from a single definition. If that's not possible, at least mention WIRE_WIDTH_COMPRESSION here so that it's clear where this value comes from.

AFAIK it should be possible to `#include` a file in both GLSL and C++, so that this constant can be shared from a single definition. If that's not possible, at least mention `WIRE_WIDTH_COMPRESSION` here so that it's clear where this value comes from.
view_clipping_distances(world_pos);
}

View File

@ -1,129 +0,0 @@
/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma USE_SSBO_VERTEX_FETCH(TriangleList, 6)
#pragma BLENDER_REQUIRE(common_view_lib.glsl)
#pragma BLENDER_REQUIRE(overlay_common_lib.glsl)
#define DISCARD_VERTEX \
gl_Position = vec4(0.0); \
return;
void do_vertex(
vec4 pos, float selection_fac, vec2 stipple_start, vec2 stipple_pos, float coord, vec2 offset)
{
geom_out.selectionFac = selection_fac;
geom_noperspective_out.edgeCoord = coord;
geom_flat_out.stippleStart = stipple_start;
geom_noperspective_out.stipplePos = stipple_pos;
gl_Position = pos;
/* Multiply offset by 2 because gl_Position range is [-1..1]. */
gl_Position.xy += offset * 2.0;
}
void main()
{
/* Determine output geometry parameters. */
int quad_id = gl_VertexID / 6;
int quad_vertex_id = gl_VertexID % 6;
int base_vertex_id = 0;
if (vertex_fetch_get_input_prim_type() == GPU_PRIM_LINES) {
base_vertex_id = quad_id * 2;
}
else if (vertex_fetch_get_input_prim_type() == GPU_PRIM_LINE_STRIP) {
base_vertex_id = quad_id;
}
else {
DISCARD_VERTEX
}
/* Read vertex attributes for line prims. */
vec2 root_au0 = vertex_fetch_attribute(base_vertex_id, au, vec2);
vec2 root_au1 = vertex_fetch_attribute(base_vertex_id + 1, au, vec2);
int root_flag0 = vertex_fetch_attribute(base_vertex_id, flag, int);
int root_flag1 = vertex_fetch_attribute(base_vertex_id + 1, flag, int);
/* Vertex shader per input vertex. */
vec3 world_pos0 = point_object_to_world(vec3(root_au0, 0.0));
vec3 world_pos1 = point_object_to_world(vec3(root_au1, 0.0));
vec4 ndc_pos0 = point_world_to_ndc(world_pos0);
vec4 ndc_pos1 = point_world_to_ndc(world_pos1);
/* Snap vertices to the pixel grid to reduce artifacts. */
vec2 half_viewport_res = sizeViewport * 0.5;
vec2 half_pixel_offset = sizeViewportInv * 0.5;
ndc_pos0.xy = floor(ndc_pos0.xy * half_viewport_res) / half_viewport_res + half_pixel_offset;
ndc_pos1.xy = floor(ndc_pos1.xy * half_viewport_res) / half_viewport_res + half_pixel_offset;
#ifdef USE_EDGE_SELECT
bool is_select0 = (root_flag0 & EDGE_UV_SELECT) != 0;
bool is_select1 = (root_flag1 & EDGE_UV_SELECT) != 0;
#else
bool is_select0 = (root_flag0 & VERT_UV_SELECT) != 0;
bool is_select1 = (root_flag1 & VERT_UV_SELECT) != 0;
#endif
float selectionFac0 = is_select0 ? 1.0 : 0.0; // out float selectionFac;
float selectionFac1 = is_select1 ? 1.0 : 0.0; // out float selectionFac;
#ifdef USE_EDGE_SELECT
/* No blending with edge selection. */
selectionFac1 = selectionFac0;
#endif
/* Move selected edges to the top
* Vertices are between 0.0 and 0.2, Edges between 0.2 and 0.4
* actual pixels are at 0.75, 1.0 is used for the background. */
float depth0 = is_select0 ? 0.25 : 0.35;
float depth1 = is_select1 ? 0.25 : 0.35;
ndc_pos0.z = depth0;
ndc_pos1.z = depth1;
/* Avoid precision loss. */
vec2 stipplePos0 = 500.0 + 500.0 * (ndc_pos0.xy / ndc_pos0.w);
vec2 stipplePos1 = 500.0 + 500.0 * (ndc_pos1.xy / ndc_pos1.w);
vec2 stippleStart0 = stipplePos0;
vec2 stippleStart1 = stipplePos1;
/* Geometry shader equivalent calculations. */
vec2 ss_pos[2];
ss_pos[0] = ndc_pos0.xy / ndc_pos0.w;
ss_pos[1] = ndc_pos1.xy / ndc_pos1.w;
float half_size = sizeEdge;
/* Enlarge edge for outline drawing. */
/* Factor of 3.0 out of nowhere! Seems to fix issues with float imprecision. */
half_size += (lineStyle == OVERLAY_UV_LINE_STYLE_OUTLINE) ?
max(sizeEdge * (doSmoothWire ? 1.0 : 3.0), 1.0) :
0.0;
/* Add 1px for AA */
if (doSmoothWire) {
half_size += 0.5;
}
vec2 line = ss_pos[0] - ss_pos[1];
vec2 line_dir = normalize(line);
vec2 line_perp = vec2(-line_dir.y, line_dir.x);
vec2 edge_ofs = line_perp * sizeViewportInv * ceil(half_size);
switch (quad_vertex_id) {
case 1: /* vertex A */
case 3:
do_vertex(ndc_pos1, selectionFac1, stippleStart1, stipplePos1, half_size, edge_ofs.xy);
break;
case 0: /* B */
do_vertex(ndc_pos0, selectionFac0, stippleStart0, stipplePos0, half_size, edge_ofs.xy);
break;
case 2: /* C */
case 4:
do_vertex(ndc_pos0, selectionFac0, stippleStart0, stipplePos0, -half_size, -edge_ofs.xy);
break;
case 5: /* D */
do_vertex(ndc_pos1, selectionFac1, stippleStart1, stipplePos1, -half_size, -edge_ofs.xy);
break;
}
}