DRW: support clipping for stick & wire bones

This commit is contained in:
Campbell Barton 2019-02-08 22:51:37 +11:00
parent d723249dcc
commit 813800f143
Notes: blender-bot 2023-02-14 06:54:28 +01:00
Referenced by issue #60779, 3D Viewport clipping support
6 changed files with 60 additions and 31 deletions

View File

@ -163,10 +163,12 @@ static void drw_shgroup_bone_box(
}
/* Wire */
static void drw_shgroup_bone_wire(const float (*bone_mat)[4], const float color[4])
static void drw_shgroup_bone_wire(
const float (*bone_mat)[4], const float color[4],
const eGPUShaderConfig shader_cfg)
{
if (g_data.bone_wire == NULL) {
g_data.bone_wire = shgroup_dynlines_flat_color(g_data.passes.bone_wire);
g_data.bone_wire = shgroup_dynlines_flat_color(g_data.passes.bone_wire, shader_cfg);
}
float head[3], tail[3];
mul_v3_m4v3(head, g_data.ob->obmat, bone_mat[3]);
@ -180,10 +182,11 @@ static void drw_shgroup_bone_wire(const float (*bone_mat)[4], const float color[
/* Stick */
static void drw_shgroup_bone_stick(
const float (*bone_mat)[4],
const float col_wire[4], const float col_bone[4], const float col_head[4], const float col_tail[4])
const float col_wire[4], const float col_bone[4], const float col_head[4], const float col_tail[4],
const eGPUShaderConfig shader_cfg)
{
if (g_data.bone_stick == NULL) {
g_data.bone_stick = shgroup_instance_bone_stick(g_data.passes.bone_wire);
g_data.bone_stick = shgroup_instance_bone_stick(g_data.passes.bone_wire, shader_cfg);
}
float final_bonemat[4][4], tail[4];
mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
@ -1322,7 +1325,8 @@ static void draw_bone_envelope(
static void draw_bone_line(
EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
const int boneflag, const short constflag, const int select_id)
const int boneflag, const short constflag,
const eGPUShaderConfig shader_cfg, const int select_id)
{
const float *col_bone = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
@ -1360,20 +1364,20 @@ static void draw_bone_line(
if (select_id == -1) {
/* Not in selection mode, draw everything at once. */
drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, col_head, col_tail);
drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, col_head, col_tail, shader_cfg);
}
else {
/* In selection mode, draw bone, root and tip separately. */
DRW_select_load_id(select_id | BONESEL_BONE);
drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, no_display, no_display);
drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, no_display, no_display, shader_cfg);
if (col_head[3] > 0.0f) {
DRW_select_load_id(select_id | BONESEL_ROOT);
drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, col_head, no_display);
drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, col_head, no_display, shader_cfg);
}
DRW_select_load_id(select_id | BONESEL_TIP);
drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, no_display, col_tail);
drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, no_display, col_tail, shader_cfg);
DRW_select_load_id(-1);
}
@ -1382,7 +1386,7 @@ static void draw_bone_line(
static void draw_bone_wire(
EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
const int boneflag, const short constflag,
const int select_id)
const eGPUShaderConfig shader_cfg, const int select_id)
{
const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
@ -1395,12 +1399,12 @@ static void draw_bone_wire(
BLI_assert(bbones_mat != NULL);
for (int i = pchan->bone->segments; i--; bbones_mat++) {
drw_shgroup_bone_wire(bbones_mat->mat, col_wire);
drw_shgroup_bone_wire(bbones_mat->mat, col_wire, shader_cfg);
}
}
else if (eBone) {
for (int i = 0; i < eBone->segments; i++) {
drw_shgroup_bone_wire(eBone->disp_bbone_mat[i], col_wire);
drw_shgroup_bone_wire(eBone->disp_bbone_mat[i], col_wire, shader_cfg);
}
}
@ -1716,11 +1720,11 @@ static void draw_armature_edit(Object *ob)
}
else if (arm->drawtype == ARM_LINE) {
draw_bone_update_disp_matrix_default(eBone, NULL);
draw_bone_line(eBone, NULL, arm, boneflag, constflag, select_id);
draw_bone_line(eBone, NULL, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
else if (arm->drawtype == ARM_WIRE) {
draw_bone_update_disp_matrix_bbone(eBone, NULL);
draw_bone_wire(eBone, NULL, arm, boneflag, constflag, select_id);
draw_bone_wire(eBone, NULL, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
else if (arm->drawtype == ARM_B_BONE) {
draw_bone_update_disp_matrix_bbone(eBone, NULL);
@ -1831,11 +1835,11 @@ static void draw_armature_pose(Object *ob, const float const_color[4])
}
else if (arm->drawtype == ARM_LINE) {
draw_bone_update_disp_matrix_default(NULL, pchan);
draw_bone_line(NULL, pchan, arm, boneflag, constflag, select_id);
draw_bone_line(NULL, pchan, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
else if (arm->drawtype == ARM_WIRE) {
draw_bone_update_disp_matrix_bbone(NULL, pchan);
draw_bone_wire(NULL, pchan, arm, boneflag, constflag, select_id);
draw_bone_wire(NULL, pchan, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
else if (arm->drawtype == ARM_B_BONE) {
draw_bone_update_disp_matrix_bbone(NULL, pchan);

View File

@ -305,9 +305,9 @@ void DRW_shgroup_world_clip_planes_from_rv3d(DRWShadingGroup *shgrp, const Regio
DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
}
DRWShadingGroup *shgroup_dynlines_flat_color(DRWPass *pass)
DRWShadingGroup *shgroup_dynlines_flat_color(DRWPass *pass, eGPUShaderConfig shader_cfg)
{
GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_FLAT_COLOR);
GPUShader *sh = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_3D_FLAT_COLOR, shader_cfg);
DRW_shgroup_instance_format(g_formats.dynlines_color, {
{"pos", DRW_ATTR_FLOAT, 3},
@ -315,7 +315,9 @@ DRWShadingGroup *shgroup_dynlines_flat_color(DRWPass *pass)
});
DRWShadingGroup *grp = DRW_shgroup_line_batch_create_with_format(sh, pass, g_formats.dynlines_color);
if (shader_cfg == GPU_SHADER_CFG_CLIPPED) {
DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
}
return grp;
}
@ -835,13 +837,17 @@ DRWShadingGroup *shgroup_instance_bone_sphere_outline(DRWPass *pass)
return grp;
}
DRWShadingGroup *shgroup_instance_bone_stick(DRWPass *pass)
DRWShadingGroup *shgroup_instance_bone_stick(DRWPass *pass, eGPUShaderConfig shader_cfg)
{
COMMON_Shaders *sh_data = &g_shaders[GPU_SHADER_CFG_DEFAULT];
COMMON_Shaders *sh_data = &g_shaders[shader_cfg];
if (sh_data->bone_stick == NULL) {
sh_data->bone_stick = DRW_shader_create(
datatoc_armature_stick_vert_glsl, NULL,
datatoc_armature_stick_frag_glsl, NULL);
const char *world_clip_lib_or_empty = (shader_cfg == GPU_SHADER_CFG_CLIPPED) ? datatoc_gpu_shader_cfg_world_clip_lib_glsl : "";
const char *world_clip_def_or_empty = (shader_cfg == GPU_SHADER_CFG_CLIPPED) ? "#define USE_WORLD_CLIP_PLANES\n" : "";
sh_data->bone_stick = GPU_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_armature_stick_vert_glsl, NULL},
.frag = (const char *[]){datatoc_armature_stick_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, NULL},
});
}
DRW_shgroup_instance_format(g_formats.instance_bone_stick, {
@ -859,7 +865,9 @@ DRWShadingGroup *shgroup_instance_bone_stick(DRWPass *pass)
g_formats.instance_bone_stick);
DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
DRW_shgroup_uniform_float_copy(grp, "stickSize", 5.0f * U.pixelsize);
if (shader_cfg == GPU_SHADER_CFG_CLIPPED) {
DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
}
return grp;
}

View File

@ -123,7 +123,7 @@ void DRW_globals_free(void);
void DRW_shgroup_world_clip_planes_from_rv3d(struct DRWShadingGroup *shgrp, const RegionView3D *rv3d);
struct DRWShadingGroup *shgroup_dynlines_flat_color(struct DRWPass *pass);
struct DRWShadingGroup *shgroup_dynlines_flat_color(struct DRWPass *pass, eGPUShaderConfig shader_cfg);
struct DRWShadingGroup *shgroup_dynlines_dashed_uniform_color(struct DRWPass *pass, const float color[4]);
struct DRWShadingGroup *shgroup_dynpoints_uniform_color(struct DRWPass *pass, const float color[4], const float *size, eGPUShaderConfig shader_cfg);
struct DRWShadingGroup *shgroup_groundlines_uniform_color(struct DRWPass *pass, const float color[4], eGPUShaderConfig shader_cfg);
@ -149,7 +149,7 @@ struct DRWShadingGroup *shgroup_instance_bone_shape_outline(struct DRWPass *pass
struct DRWShadingGroup *shgroup_instance_bone_shape_solid(struct DRWPass *pass, struct GPUBatch *geom, bool transp, eGPUShaderConfig shader_cfg);
struct DRWShadingGroup *shgroup_instance_bone_sphere_outline(struct DRWPass *pass);
struct DRWShadingGroup *shgroup_instance_bone_sphere_solid(struct DRWPass *pass, bool transp);
struct DRWShadingGroup *shgroup_instance_bone_stick(struct DRWPass *pass);
struct DRWShadingGroup *shgroup_instance_bone_stick(struct DRWPass *pass, eGPUShaderConfig shader_cfg);
struct DRWShadingGroup *shgroup_instance_bone_dof(struct DRWPass *pass, struct GPUBatch *geom);
struct GPUShader *mpath_line_shader_get(void);

View File

@ -1,6 +1,7 @@
uniform mat4 ProjectionMatrix;
uniform mat4 ViewProjectionMatrix;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform vec2 viewportSize;
@ -49,8 +50,10 @@ void main()
/* Make the color */
colorFac = ((flag & COL_WIRE) == 0u) ? ((flag & COL_BONE) != 0u) ? 1.0 : 2.0 : 0.0;
vec4 v0 = ViewMatrix * vec4(boneStart, 1.0);
vec4 v1 = ViewMatrix * vec4(boneEnd, 1.0);
vec4 boneStart_4d = vec4(boneStart, 1.0);
vec4 boneEnd_4d = vec4(boneEnd, 1.0);
vec4 v0 = ViewMatrix * boneStart_4d;
vec4 v1 = ViewMatrix * boneEnd_4d;
/* Clip the bone to the camera origin plane (not the clip plane)
* to avoid glitches if one end is behind the camera origin (in persp). */
@ -81,6 +84,10 @@ void main()
gl_Position = (is_head) ? p0 : p1;
gl_Position.xy += stickSize * (vpos / viewportSize);
gl_Position.z += (is_bone) ? 0.0 : 1e-6; /* Avoid Z fighting of head/tails. */
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_calc_clip_distance((ModelMatrix * (is_head ? boneStart_4d : boneEnd_4d)).xyz);
#endif
}
else {
gl_Position = vec4(0.0);

View File

@ -1220,7 +1220,9 @@ GPUShader *GPU_shader_get_builtin_shader_with_config(
GPU_SHADER_DISTANCE_LINES,
GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR,
GPU_SHADER_3D_FLAT_SELECT_ID,
GPU_SHADER_3D_UNIFORM_SELECT_ID));
GPU_SHADER_3D_UNIFORM_SELECT_ID) ||
ELEM(shader,
GPU_SHADER_3D_FLAT_COLOR));
const char *world_clip_lib = datatoc_gpu_shader_cfg_world_clip_lib_glsl;
const char *world_clip_def = "#define USE_WORLD_CLIP_PLANES\n";
/* In rare cases geometry shaders calculate clipping themselves. */

View File

@ -1,5 +1,8 @@
uniform mat4 ModelViewProjectionMatrix;
#ifdef USE_WORLD_CLIP_PLANES
uniform mat4 ModelMatrix;
#endif
in vec3 pos;
#if defined(USE_COLOR_U32)
@ -12,7 +15,8 @@ flat out vec4 finalColor;
void main()
{
gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
vec4 pos_4d = vec4(pos, 1.0);
gl_Position = ModelViewProjectionMatrix * pos_4d;
#if defined(USE_COLOR_U32)
finalColor = vec4(
@ -23,4 +27,8 @@ void main()
#else
finalColor = color;
#endif
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_calc_clip_distance((ModelMatrix * pos_4d).xyz);
#endif
}