Workbench: Draw random object colors

Basic implementation hashes the ob->id.name. In the future we should use
an golden ratio offset algorithm as it can make a better random
palettte.
This commit is contained in:
Jeroen Bakker 2018-04-20 14:38:55 +02:00
parent f18213ae3a
commit c7d3eb2a7e
5 changed files with 25 additions and 4 deletions

@ -3447,6 +3447,7 @@ class VIEW3D_PT_view3d_display(Panel):
col.prop(view, "show_all_objects_origin")
col.prop(view, "show_relationship_lines")
col.prop(view, "show_face_orientation_overlay")
col.prop(view, "show_random_object_colors")
col = layout.column()
col.active = display_all

@ -55,9 +55,15 @@ static uint get_material_hash(const float color[3])
return r + g * 4096 + b * 4096 * 4096;
}
static const float* get_material_solid_color(WORKBENCH_PrivateData *UNUSED(wpd), Object *ob)
static void get_material_solid_color(WORKBENCH_PrivateData *wpd, Object *ob, float *color)
{
return ob->col;
if (wpd->drawtype_options & V3D_DRAWOPTION_RANDOMIZE) {
unsigned int obhash = BLI_ghashutil_strhash(ob->id.name);
cpack_to_rgb(obhash, &color[0], &color[1], &color[2]);
} else {
copy_v3_v3(color, ob->col);
}
}
void workbench_materials_engine_init(void)
@ -111,10 +117,12 @@ void workbench_materials_cache_init(WORKBENCH_Data *vedata)
}
#else
wpd->drawtype_lighting = v3d->drawtype_lighting;
wpd->drawtype_options = v3d->drawtype_options;
#endif
}
else {
wpd->drawtype_lighting = V3D_LIGHTING_STUDIO;
wpd->drawtype_options = 0;
}
}
@ -136,8 +144,9 @@ void workbench_materials_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob
/* Solid */
GPUShader *shader = wpd->drawtype_lighting == V3D_LIGHTING_FLAT?e_data.solid_flat_sh:e_data.solid_studio_sh;
const float *color = get_material_solid_color(wpd, ob);
uint hash = get_material_hash(color);
float color[3];
get_material_solid_color(wpd, ob, color);
unsigned int hash = get_material_hash(color);
material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
if (material == NULL) {

@ -57,6 +57,7 @@ typedef struct WORKBENCH_PrivateData {
struct GHash *material_hash;
short drawtype_lighting;
short drawtype_options;
} WORKBENCH_PrivateData; /* Transient data */
typedef struct WORKBENCH_MaterialData {

@ -80,6 +80,10 @@ enum {
V3D_LIGHTING_SCENE = 2
};
enum {
V3D_DRAWOPTION_RANDOMIZE = (1<<0),
};
enum {
V3D_OVERLAY_FACE_ORIENTATION = (1 << 0),
};

@ -2281,6 +2281,12 @@ static void rna_def_space_view3d(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "Face Orientation", "Show the Face Orientation Overlay");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, "rna_SpaceView3D_viewport_shade_update");
prop = RNA_def_property(srna, "show_random_object_colors", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "drawtype_options", V3D_DRAWOPTION_RANDOMIZE);
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_ui_text(prop, "Random Colors", "Show random object colors");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, "rna_SpaceView3D_viewport_shade_update");
prop = RNA_def_property(srna, "local_view", PROP_POINTER, PROP_NONE);
RNA_def_property_pointer_sdna(prop, NULL, "localvd");
RNA_def_property_ui_text(prop, "Local View",