DRW: support clipping for octahedral & box bones

This commit is contained in:
Campbell Barton 2019-02-07 07:44:12 +11:00
parent 6152f5cd67
commit 7325035e60
Notes: blender-bot 2023-10-13 01:54:23 +02:00
Referenced by issue #60779, 3D Viewport clipping support
6 changed files with 86 additions and 41 deletions

View File

@ -117,16 +117,18 @@ static struct {
/* Octahedral */
static void drw_shgroup_bone_octahedral(
const float (*bone_mat)[4],
const float bone_color[4], const float hint_color[4], const float outline_color[4])
const float bone_color[4], const float hint_color[4], const float outline_color[4],
const eGPUShaderConfig shader_cfg)
{
if (g_data.bone_octahedral_outline == NULL) {
struct GPUBatch *geom = DRW_cache_bone_octahedral_wire_get();
g_data.bone_octahedral_outline = shgroup_instance_bone_shape_outline(g_data.passes.bone_outline, geom);
g_data.bone_octahedral_outline = shgroup_instance_bone_shape_outline(
g_data.passes.bone_outline, geom, shader_cfg);
}
if (g_data.bone_octahedral_solid == NULL) {
struct GPUBatch *geom = DRW_cache_bone_octahedral_get();
g_data.bone_octahedral_solid = shgroup_instance_bone_shape_solid(g_data.passes.bone_solid, geom,
g_data.transparent);
g_data.bone_octahedral_solid = shgroup_instance_bone_shape_solid(
g_data.passes.bone_solid, geom, g_data.transparent, shader_cfg);
}
float final_bonemat[4][4];
mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
@ -139,15 +141,18 @@ static void drw_shgroup_bone_octahedral(
/* Box / B-Bone */
static void drw_shgroup_bone_box(
const float (*bone_mat)[4],
const float bone_color[4], const float hint_color[4], const float outline_color[4])
const float bone_color[4], const float hint_color[4], const float outline_color[4],
const eGPUShaderConfig shader_cfg)
{
if (g_data.bone_box_wire == NULL) {
struct GPUBatch *geom = DRW_cache_bone_box_wire_get();
g_data.bone_box_outline = shgroup_instance_bone_shape_outline(g_data.passes.bone_outline, geom);
g_data.bone_box_outline = shgroup_instance_bone_shape_outline(
g_data.passes.bone_outline, geom, shader_cfg);
}
if (g_data.bone_box_solid == NULL) {
struct GPUBatch *geom = DRW_cache_bone_box_get();
g_data.bone_box_solid = shgroup_instance_bone_shape_solid(g_data.passes.bone_solid, geom, g_data.transparent);
g_data.bone_box_solid = shgroup_instance_bone_shape_solid(
g_data.passes.bone_solid, geom, g_data.transparent, shader_cfg);
}
float final_bonemat[4][4];
mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
@ -305,7 +310,7 @@ extern void drw_batch_cache_generate_requested(Object *custom);
static void drw_shgroup_bone_custom_solid(
const float (*bone_mat)[4],
const float bone_color[4], const float hint_color[4], const float outline_color[4],
Object *custom)
const eGPUShaderConfig shader_cfg, Object *custom)
{
/* grr, not re-using instances! */
struct GPUBatch *surf = DRW_cache_object_surface_get(custom);
@ -321,13 +326,14 @@ static void drw_shgroup_bone_custom_solid(
}
if (surf) {
DRWShadingGroup *shgrp_geom_solid = shgroup_instance_bone_shape_solid(g_data.passes.bone_solid, surf,
g_data.transparent);
DRWShadingGroup *shgrp_geom_solid = shgroup_instance_bone_shape_solid(
g_data.passes.bone_solid, surf, g_data.transparent, shader_cfg);
DRW_shgroup_call_dynamic_add(shgrp_geom_solid, final_bonemat, bone_color, hint_color);
}
if (edges && outline_color[3] > 0.0f) {
DRWShadingGroup *shgrp_geom_wire = shgroup_instance_bone_shape_outline(g_data.passes.bone_outline, edges);
DRWShadingGroup *shgrp_geom_wire = shgroup_instance_bone_shape_outline(
g_data.passes.bone_outline, edges, shader_cfg);
DRW_shgroup_call_dynamic_add(shgrp_geom_wire, final_bonemat, outline_color);
}
@ -1248,7 +1254,7 @@ static void draw_points(
static void draw_bone_custom_shape(
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_solid = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
@ -1260,7 +1266,7 @@ static void draw_bone_custom_shape(
}
if ((boneflag & BONE_DRAWWIRE) == 0) {
drw_shgroup_bone_custom_solid(disp_mat, col_solid, col_hint, col_wire, pchan->custom);
drw_shgroup_bone_custom_solid(disp_mat, col_solid, col_hint, col_wire, shader_cfg, pchan->custom);
}
else {
drw_shgroup_bone_custom_wire(disp_mat, col_wire, pchan->custom);
@ -1410,7 +1416,7 @@ static void draw_bone_wire(
static void draw_bone_box(
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_solid = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
@ -1425,12 +1431,12 @@ static void draw_bone_box(
BLI_assert(bbones_mat != NULL);
for (int i = pchan->bone->segments; i--; bbones_mat++) {
drw_shgroup_bone_box(bbones_mat->mat, col_solid, col_hint, col_wire);
drw_shgroup_bone_box(bbones_mat->mat, col_solid, col_hint, col_wire, shader_cfg);
}
}
else if (eBone) {
for (int i = 0; i < eBone->segments; i++) {
drw_shgroup_bone_box(eBone->disp_bbone_mat[i], col_solid, col_hint, col_wire);
drw_shgroup_bone_box(eBone->disp_bbone_mat[i], col_solid, col_hint, col_wire, shader_cfg);
}
}
@ -1446,7 +1452,7 @@ static void draw_bone_box(
static void draw_bone_octahedral(
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_solid = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
@ -1456,7 +1462,7 @@ static void draw_bone_octahedral(
DRW_select_load_id(select_id | BONESEL_BONE);
}
drw_shgroup_bone_octahedral(BONE_VAR(eBone, pchan, disp_mat), col_solid, col_hint, col_wire);
drw_shgroup_bone_octahedral(BONE_VAR(eBone, pchan, disp_mat), col_solid, col_hint, col_wire, shader_cfg);
if (select_id != -1) {
DRW_select_load_id(-1);
@ -1718,11 +1724,11 @@ static void draw_armature_edit(Object *ob)
}
else if (arm->drawtype == ARM_B_BONE) {
draw_bone_update_disp_matrix_bbone(eBone, NULL);
draw_bone_box(eBone, NULL, arm, boneflag, constflag, select_id);
draw_bone_box(eBone, NULL, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
else {
draw_bone_update_disp_matrix_default(eBone, NULL);
draw_bone_octahedral(eBone, NULL, arm, boneflag, constflag, select_id);
draw_bone_octahedral(eBone, NULL, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
/* Draw names of bone */
@ -1817,7 +1823,7 @@ static void draw_armature_pose(Object *ob, const float const_color[4])
if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM)) {
draw_bone_update_disp_matrix_custom(pchan);
draw_bone_custom_shape(NULL, pchan, arm, boneflag, constflag, select_id);
draw_bone_custom_shape(NULL, pchan, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
else if (arm->drawtype == ARM_ENVELOPE) {
draw_bone_update_disp_matrix_default(NULL, pchan);
@ -1833,11 +1839,11 @@ static void draw_armature_pose(Object *ob, const float const_color[4])
}
else if (arm->drawtype == ARM_B_BONE) {
draw_bone_update_disp_matrix_bbone(NULL, pchan);
draw_bone_box(NULL, pchan, arm, boneflag, constflag, select_id);
draw_bone_box(NULL, pchan, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
else {
draw_bone_update_disp_matrix_default(NULL, pchan);
draw_bone_octahedral(NULL, pchan, arm, boneflag, constflag, select_id);
draw_bone_octahedral(NULL, pchan, arm, boneflag, constflag, draw_ctx->shader_cfg, select_id);
}
if (!is_pose_select && show_relations &&

View File

@ -728,15 +728,19 @@ DRWShadingGroup *shgroup_instance_mball_handles(DRWPass *pass, eGPUShaderConfig
}
/* Only works with batches with adjacency infos. */
DRWShadingGroup *shgroup_instance_bone_shape_outline(DRWPass *pass, struct GPUBatch *geom)
DRWShadingGroup *shgroup_instance_bone_shape_outline(
DRWPass *pass, struct GPUBatch *geom, eGPUShaderConfig shader_cfg)
{
COMMON_Shaders *sh_data = &g_shaders[GPU_SHADER_CFG_DEFAULT];
COMMON_Shaders *sh_data = &g_shaders[shader_cfg];
if (sh_data->shape_outline == NULL) {
sh_data->shape_outline = DRW_shader_create(
datatoc_armature_shape_outline_vert_glsl,
datatoc_armature_shape_outline_geom_glsl,
datatoc_gpu_shader_flat_color_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->shape_outline = GPU_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_armature_shape_outline_vert_glsl, NULL},
.geom = (const char *[]){world_clip_lib_or_empty, datatoc_armature_shape_outline_geom_glsl, NULL},
.frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, NULL},
});
}
DRW_shgroup_instance_format(g_formats.instance_bone_outline, {
@ -748,17 +752,24 @@ DRWShadingGroup *shgroup_instance_bone_shape_outline(DRWPass *pass, struct GPUBa
sh_data->shape_outline,
pass, geom, g_formats.instance_bone_outline);
DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
if (shader_cfg == GPU_SHADER_CFG_CLIPPED) {
DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
}
return grp;
}
DRWShadingGroup *shgroup_instance_bone_shape_solid(DRWPass *pass, struct GPUBatch *geom, bool transp)
DRWShadingGroup *shgroup_instance_bone_shape_solid(
DRWPass *pass, struct GPUBatch *geom, bool transp, eGPUShaderConfig shader_cfg)
{
COMMON_Shaders *sh_data = &g_shaders[GPU_SHADER_CFG_DEFAULT];
COMMON_Shaders *sh_data = &g_shaders[shader_cfg];
if (sh_data->shape_solid == NULL) {
sh_data->shape_solid = DRW_shader_create(
datatoc_armature_shape_solid_vert_glsl, NULL,
datatoc_armature_shape_solid_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->shape_solid = GPU_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_armature_shape_solid_vert_glsl, NULL},
.frag = (const char *[]){datatoc_armature_shape_solid_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, NULL},
});
}
DRW_shgroup_instance_format(g_formats.instance_bone, {
@ -771,7 +782,9 @@ DRWShadingGroup *shgroup_instance_bone_shape_solid(DRWPass *pass, struct GPUBatc
sh_data->shape_solid,
pass, geom, g_formats.instance_bone);
DRW_shgroup_uniform_float_copy(grp, "alpha", transp ? 0.6f : 1.0f);
if (shader_cfg == GPU_SHADER_CFG_CLIPPED) {
DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
}
return grp;
}

View File

@ -145,8 +145,8 @@ struct DRWShadingGroup *shgroup_instance_bone_axes(struct DRWPass *pass);
struct DRWShadingGroup *shgroup_instance_bone_envelope_distance(struct DRWPass *pass);
struct DRWShadingGroup *shgroup_instance_bone_envelope_outline(struct DRWPass *pass);
struct DRWShadingGroup *shgroup_instance_bone_envelope_solid(struct DRWPass *pass, bool transp);
struct DRWShadingGroup *shgroup_instance_bone_shape_outline(struct DRWPass *pass, struct GPUBatch *geom);
struct DRWShadingGroup *shgroup_instance_bone_shape_solid(struct DRWPass *pass, struct GPUBatch *geom, bool transp);
struct DRWShadingGroup *shgroup_instance_bone_shape_outline(struct DRWPass *pass, struct GPUBatch *geom, eGPUShaderConfig shader_cfg);
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);

View File

@ -27,14 +27,26 @@ void emit_edge(vec2 edge_dir, vec2 hidden_dir, vec2 thick, bool is_persp)
vec2 t = thick * (is_persp ? abs(vPos[1].z) : 1.0);
gl_Position = pPos[1];
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_set_clip_distance(gl_in[1].gl_ClipDistance);
#endif
EmitVertex();
gl_Position.xy += t * edge_dir;
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_set_clip_distance(gl_in[1].gl_ClipDistance);
#endif
EmitVertex();
t = thick * (is_persp ? abs(vPos[2].z) : 1.0);
gl_Position = pPos[2];
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_set_clip_distance(gl_in[2].gl_ClipDistance);
#endif
EmitVertex();
gl_Position.xy += t * edge_dir;
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_set_clip_distance(gl_in[2].gl_ClipDistance);
#endif
EmitVertex();
}
@ -44,6 +56,9 @@ void emit_corner(const int e, vec2 thick, bool is_persp)
vec2 t = thick * (is_persp ? abs(vPos[e].z) : 1.0);
gl_Position = pPos[e] + vec4(t * corner_dir, 0.0, 0.0);
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_set_clip_distance(gl_in[e].gl_ClipDistance);
#endif
EmitVertex();
}

View File

@ -31,7 +31,8 @@ void main()
* doing it per instance on CPU and sending it on via instance attribute. */
mat3 NormalMatrix = transpose(inverse(mat3(ViewMatrix * InstanceModelMatrix)));
vec4 viewpos = ViewMatrix * (InstanceModelMatrix * vec4(pos, 1.0));
vec4 worldPosition = InstanceModelMatrix * vec4(pos, 1.0);
vec4 viewpos = ViewMatrix * worldPosition;
vPos = viewpos.xyz;
pPos = ProjectionMatrix * viewpos;
@ -44,4 +45,8 @@ void main()
ssPos = proj(pPos);
vColSize = outlineColorSize;
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_calc_clip_distance(worldPosition.xyz);
#endif
}

View File

@ -32,5 +32,11 @@ void main()
finalColor.rgb = mix(stateColor, boneColor, fac);
finalColor.a = 1.0;
gl_Position = ViewProjectionMatrix * (InstanceModelMatrix * vec4(pos, 1.0));
vec4 worldPosition = InstanceModelMatrix * vec4(pos, 1.0);
gl_Position = ViewProjectionMatrix * worldPosition;
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_calc_clip_distance(worldPosition.xyz);
#endif
}