Workbench: Option to use Object color
authorJeroen Bakker <j.bakker@atmind.nl>
Thu, 19 Apr 2018 07:45:52 +0000 (09:45 +0200)
committerJeroen Bakker <j.bakker@atmind.nl>
Thu, 19 Apr 2018 07:45:52 +0000 (09:45 +0200)
- added `object_color_type` where the user can set if the collection
determines the color, or the object will be used for the color.
Implemented it as an enum as later this can have a random color option.
- moved OB_LIGHTING_* to DNA_view3d_types and renamed it.
- Fixed some DRY in workbench_materials.c. Can remove more DRY's but
will need to discuss the responsibility of the workbench engine as it
might become part of the eevee renderer.

14 files changed:
release/scripts/startup/bl_ui/properties_collection.py
release/scripts/startup/bl_ui/properties_render.py
source/blender/blenloader/intern/versioning_280.c
source/blender/draw/engines/workbench/solid_flat_mode.c
source/blender/draw/engines/workbench/solid_studio_mode.c
source/blender/draw/engines/workbench/workbench_engine.c
source/blender/draw/engines/workbench/workbench_materials.c
source/blender/draw/engines/workbench/workbench_private.h
source/blender/draw/intern/draw_manager.c
source/blender/editors/space_view3d/space_view3d.c
source/blender/makesdna/DNA_object_types.h
source/blender/makesdna/DNA_view3d_types.h
source/blender/makesrna/intern/rna_layer.c
source/blender/makesrna/intern/rna_space.c

index e0ac7786b50b60217e5d0c341ef1e0ccbafd61fb..25b4664ab831314cd95e4179976f720acee962b8 100644 (file)
@@ -96,6 +96,7 @@ class COLLECTION_PT_workbench_settings(CollectionButtonsPanel, Panel):
         collection_props = collection.engine_overrides['BLENDER_WORKBENCH']
 
         col = layout.column()
+        col.template_override_property(collection_props, scene_props, "object_color_type")
         col.template_override_property(collection_props, scene_props, "object_color")
 
 
index 9b9cfee5dc77f99136dbb285fcfefe3e5197f3c2..a0d7e9b10262ebca5da4d728840731baa5e1794c 100644 (file)
@@ -907,7 +907,9 @@ class RENDER_PT_workbench_collection_settings(RenderButtonsPanel, Panel):
         props = context.scene.collection_properties['BLENDER_WORKBENCH']
 
         col = layout.column()
-        col.prop(props, "object_color")
+        col.prop(props, "object_color_type")
+        if props.object_color_type == 'COLLECTION':
+            col.prop(props, "object_color")
 
 
 classes = (
index b2bb2fd792b37a2973f8be34fdab1f1ff819a48c..d6cf411c31cae1d9c3107c6b43c9b512644dbae4 100644 (file)
@@ -913,8 +913,8 @@ void blo_do_versions_280(FileData *fd, Library *UNUSED(lib), Main *main)
                                for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
                                        if (sl->spacetype == SPACE_VIEW3D) {
                                                View3D *v3d = (View3D *)sl;
-                                               v3d->drawtype_solid = OB_LIGHTING_STUDIO;
-                                               v3d->drawtype_wireframe = OB_LIGHTING_STUDIO;
+                                               v3d->drawtype_solid = V3D_LIGHTING_STUDIO;
+                                               v3d->drawtype_wireframe = V3D_LIGHTING_STUDIO;
 
                                                /* Assume (demo) files written with 2.8 want to show
                                                 * Eevee renders in the viewport. */
index bed87518f397f9812c99e19753be454f41fac95f..1d8eb3b436230e6c0375a039d1c28984e9db5fe6 100644 (file)
@@ -68,24 +68,7 @@ static void workbench_solid_flat_cache_init(void *vedata)
 static void workbench_solid_flat_cache_populate(void *vedata, Object *ob)
 {
        WORKBENCH_Data * data = (WORKBENCH_Data *)vedata;
-       WORKBENCH_StorageList *stl = data->stl;
-
-       IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
-       const float *color = BKE_collection_engine_property_value_get_float_array(props, "object_color");
-
-       if (!DRW_object_is_renderable(ob))
-               return;
-
-       struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
-       WORKBENCH_MaterialData *material;
-       if (geom) {
-               /* Depth */
-               DRW_shgroup_call_add(stl->g_data->depth_shgrp, geom, ob->obmat);
-
-               /* Solid */
-               material = workbench_get_or_create_solid_flat_material_data(data, color);
-               DRW_shgroup_call_add(material->shgrp, geom, ob->obmat);
-       }
+       workbench_materials_solid_cache_populate(data, ob, V3D_LIGHTING_FLAT);
 }
 
 static void workbench_solid_flat_cache_finish(void *vedata)
index 6ac63fbca67c0bf27d787b3549e1a12b9bab5be0..e687debe3193135c2c3d16bbe9fa9eeac640b166 100644 (file)
@@ -68,24 +68,7 @@ static void workbench_solid_studio_cache_init(void *vedata)
 static void workbench_solid_studio_cache_populate(void *vedata, Object *ob)
 {
        WORKBENCH_Data * data = (WORKBENCH_Data *)vedata;
-       WORKBENCH_StorageList *stl = data->stl;
-
-       IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
-       const float* color = BKE_collection_engine_property_value_get_float_array(props, "object_color");
-
-       if (!DRW_object_is_renderable(ob))
-               return;
-
-       struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
-       WORKBENCH_MaterialData *material;
-       if (geom) {
-               /* Depth */
-               DRW_shgroup_call_add(stl->g_data->depth_shgrp, geom, ob->obmat);
-
-               /* Solid */
-               material = workbench_get_or_create_solid_studio_material_data(data, color);
-               DRW_shgroup_call_add(material->shgrp, geom, ob->obmat);
-       }
+       workbench_materials_solid_cache_populate(data, ob, V3D_LIGHTING_STUDIO);
 }
 
 static void workbench_solid_studio_cache_finish(void *vedata)
index 2b6c95e301f2e4c4c82f535c05e9c274c50c7bbd..5a2c70ce810eb54ee3a213b8b54a514dc4d1b7ce 100644 (file)
@@ -49,6 +49,7 @@ static void workbench_layer_collection_settings_create(RenderEngine *UNUSED(engi
                   props->type == IDP_GROUP &&
                   props->subtype == IDP_GROUP_SUB_ENGINE_RENDER);
        float default_object_color[3] = {1.0, 1.0, 1.0};
+       BKE_collection_engine_property_add_int(props, "object_color_type", V3D_OBJECT_COLOR_COLLECTION);
        BKE_collection_engine_property_add_float_array(props, "object_color", default_object_color, 3);
 }
 
index 873d24116a1744ade415d3e87db0da553065be70..eafeda324d70672680cd779181820d8327ba98ba 100644 (file)
@@ -55,48 +55,19 @@ static uint get_material_hash(const float color[3])
        return r + g * 4096 + b * 4096 * 4096;
 }
 
-WORKBENCH_MaterialData *workbench_get_or_create_solid_flat_material_data(WORKBENCH_Data *vedata, const float color[3])
+static const float* get_material_solid_color(Object *ob)
 {
-       WORKBENCH_StorageList *stl = vedata->stl;
-       WORKBENCH_PassList *psl = vedata->psl;
-       WORKBENCH_PrivateData *wpd = stl->g_data;
-
-       uint hash = get_material_hash(color);
-       WORKBENCH_MaterialData *material;
-
-       material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
-       if (material == NULL) {
-               material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), "WORKBENCH_MaterialData");
-               material->shgrp = DRW_shgroup_create(e_data.solid_flat_sh, psl->solid_pass);
-               material->color[0] = color[0];
-               material->color[1] = color[1];
-               material->color[2] = color[2];
-               DRW_shgroup_uniform_vec3(material->shgrp, "color", material->color, 1);
-               BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
+       IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
+       int object_color_option = BKE_collection_engine_property_value_get_int(props, "object_color_type");
+       switch (object_color_option)
+       {
+               default:
+               case V3D_OBJECT_COLOR_COLLECTION:
+                       return BKE_collection_engine_property_value_get_float_array(props, "object_color");
+
+               case V3D_OBJECT_COLOR_OBJECT:
+                       return ob->col;
        }
-       return material;
-}
-
-WORKBENCH_MaterialData *workbench_get_or_create_solid_studio_material_data(WORKBENCH_Data *vedata, const float color[3])
-{
-       WORKBENCH_StorageList *stl = vedata->stl;
-       WORKBENCH_PassList *psl = vedata->psl;
-       WORKBENCH_PrivateData *wpd = stl->g_data;
-
-       uint hash = get_material_hash(color);
-       WORKBENCH_MaterialData *material;
-
-       material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
-       if (material == NULL) {
-               material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), "WORKBENCH_MaterialData");
-               material->shgrp = DRW_shgroup_create(e_data.solid_studio_sh, psl->solid_pass);
-               material->color[0] = color[0];
-               material->color[1] = color[1];
-               material->color[2] = color[2];
-               DRW_shgroup_uniform_vec3(material->shgrp, "color", material->color, 1);
-               BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
-       }
-       return material;
 }
 
 void workbench_materials_engine_init(void)
@@ -127,6 +98,44 @@ void workbench_materials_cache_init(WORKBENCH_Data *vedata)
        wpd->material_hash = BLI_ghash_ptr_new("Workbench material_hash");
 }
 
+void workbench_materials_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob, int lighting_mode)
+{
+       WORKBENCH_StorageList *stl = vedata->stl;
+       WORKBENCH_PassList *psl = vedata->psl;
+       WORKBENCH_PrivateData *wpd = stl->g_data;
+
+       if (!DRW_object_is_renderable(ob))
+               return;
+
+       struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
+       WORKBENCH_MaterialData *material;
+       if (geom) {
+               /* Depth */
+               DRW_shgroup_call_add(stl->g_data->depth_shgrp, geom, ob->obmat);
+
+               /* Solid */
+               GPUShader *shader = lighting_mode == V3D_LIGHTING_FLAT?e_data.solid_flat_sh:e_data.solid_studio_sh;
+
+               const float *color = get_material_solid_color(ob);
+               uint hash = get_material_hash(color);
+               WORKBENCH_MaterialData *material;
+
+               material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
+               if (material == NULL) {
+                       material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), "WORKBENCH_MaterialData");
+                       material->shgrp = DRW_shgroup_create(shader, psl->solid_pass);
+                       material->color[0] = color[0];
+                       material->color[1] = color[1];
+                       material->color[2] = color[2];
+                       DRW_shgroup_uniform_vec3(material->shgrp, "color", material->color, 1);
+                       BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
+               }
+
+               DRW_shgroup_call_add(material->shgrp, geom, ob->obmat);
+       }
+
+}
+
 void workbench_materials_cache_finish(WORKBENCH_Data *vedata)
 {
        WORKBENCH_StorageList *stl = vedata->stl;
index e45ee9bd043054d7c51ac5325c4d8ec2402d2481..f6702a9f51187e68f49638881bd76aa69fbf2b6a 100644 (file)
@@ -28,6 +28,7 @@
 
 
 #include "DRW_render.h"
+#include "DNA_view3d_types.h"
 
 #define WORKBENCH_ENGINE "BLENDER_WORKBENCH"
 
@@ -78,8 +79,7 @@ void workbench_solid_materials_free(void);
 void workbench_materials_engine_init(void);
 void workbench_materials_engine_finish(void);
 void workbench_materials_cache_init(WORKBENCH_Data *vedata);
-WORKBENCH_MaterialData *workbench_get_or_create_solid_flat_material_data(WORKBENCH_Data *vedata, const float color[3]);
-WORKBENCH_MaterialData *workbench_get_or_create_solid_studio_material_data(WORKBENCH_Data *vedata, const float color[3]);
+void workbench_materials_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob, int lighting_mode);
 void workbench_materials_cache_finish(WORKBENCH_Data *vedata);
 
 
index c99cd25df6776f53ab6d6dc173db2769d898c80e..2c1e8612d510f947875009f81af43c03a582fa4f 100644 (file)
@@ -903,10 +903,11 @@ static void drw_engines_enable_from_engine(RenderEngineType *engine_type, int dr
                        break;
 
                case OB_SOLID:
-                       if (drawtype_solid == OB_LIGHTING_FLAT) {
+                       if (drawtype_solid == V3D_LIGHTING_FLAT) {
                                use_drw_engine(&draw_engine_workbench_solid_flat);
-                       }
-                       else if (drawtype_solid == OB_LIGHTING_STUDIO) {
+
+                       } 
+                       else if (drawtype_solid == V3D_LIGHTING_STUDIO) {
                                use_drw_engine(&draw_engine_workbench_solid_studio);
 
                        }
@@ -1948,6 +1949,7 @@ void DRW_engines_register(void)
        RE_engines_register(NULL, &DRW_engine_viewport_workbench_type);
 
        DRW_engine_register(&draw_engine_workbench_solid_flat);
+       DRW_engine_register(&draw_engine_workbench_solid_studio);
 
        DRW_engine_register(&draw_engine_object_type);
        DRW_engine_register(&draw_engine_edit_armature_type);
index 883722105ee0ab606ba01c8a833734d682107acc..5e9d6f4ea1cb89fd7c86238785ecf06a5295ac54 100644 (file)
@@ -333,8 +333,8 @@ static SpaceLink *view3d_new(const bContext *C)
        v3d->gridlines = 16;
        v3d->gridsubdiv = 10;
        v3d->drawtype = OB_SOLID;
-       v3d->drawtype_solid = OB_LIGHTING_STUDIO;
-       v3d->drawtype_texture = OB_LIGHTING_STUDIO;
+       v3d->drawtype_solid = V3D_LIGHTING_STUDIO;
+       v3d->drawtype_texture = V3D_LIGHTING_STUDIO;
 
        v3d->gridflag = V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_FLOOR;
        
index e292e1a8456997c006068f3e5bdfb1bee2ab92fc..bb17b10b299968955527ce0e5c0cb8460c2b7a4e 100644 (file)
@@ -446,12 +446,6 @@ enum {
        OB_PAINT     = 100,  /* temporary used in draw code */
 };
 
-enum {
-       OB_LIGHTING_FLAT   = 0,
-       OB_LIGHTING_STUDIO = 1,
-       OB_LIGHTING_SCENE  = 2
-};
-
 /* dtx: flags (short) */
 enum {
        OB_DRAWBOUNDOX    = 1 << 0,
index 2b82638a6b83ac4ff772a574875d6eb329522839..ed42a2e8b71b398b7cdea00071b5f48f9cd178db 100644 (file)
@@ -74,6 +74,16 @@ typedef struct View3DDebug {
 } View3DDebug;
 
 /* ********************************* */
+enum {
+       V3D_LIGHTING_FLAT   = 0,
+       V3D_LIGHTING_STUDIO = 1,
+       V3D_LIGHTING_SCENE  = 2
+};
+
+enum {
+       V3D_OBJECT_COLOR_COLLECTION = 0,
+       V3D_OBJECT_COLOR_OBJECT     = 1,
+};
 
 typedef struct RegionView3D {
        
index dce85f0e91c6e6e7a14cfacad920323212f12bf7..245f368ce3024e67c9c9ee6bbdaa39436d0f2072 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "DNA_scene_types.h"
 #include "DNA_layer_types.h"
+#include "DNA_view3d_types.h"
 
 #include "BLI_math.h"
 #include "BLI_string_utils.h"
@@ -312,6 +313,9 @@ static void rna_LayerEngineSettings_##_ENGINE_##_##_NAME_##_set(PointerRNA *ptr,
 #define RNA_LAYER_ENGINE_WORKBENCH_GET_SET_FLOAT_ARRAY(_NAME_, _LEN_) \
        RNA_LAYER_ENGINE_GET_SET_ARRAY(float, Workbench, COLLECTION_MODE_NONE, _NAME_, _LEN_)
 
+#define RNA_LAYER_ENGINE_WORKBENCH_GET_SET_INT(_NAME_) \
+       RNA_LAYER_ENGINE_GET_SET(int, Workbench, COLLECTION_MODE_NONE, _NAME_)
+
 /* mode engines */
 
 #define RNA_LAYER_MODE_OBJECT_GET_SET_FLOAT(_NAME_) \
@@ -359,6 +363,7 @@ RNA_LAYER_ENGINE_CLAY_GET_SET_FLOAT(hair_brightness_randomness)
 /* workbench engine */
 /* LayerCollection settings. */
 RNA_LAYER_ENGINE_WORKBENCH_GET_SET_FLOAT_ARRAY(object_color, 3)
+RNA_LAYER_ENGINE_WORKBENCH_GET_SET_INT(object_color_type)
 
 /* eevee engine */
 /* ViewLayer settings. */
@@ -1662,21 +1667,35 @@ static void rna_def_layer_collection_engine_settings_clay(BlenderRNA *brna)
 }
 #endif /* WITH_CLAY_ENGINE */
 
+/* Workbench engine */
 static void rna_def_layer_collection_engine_settings_workbench(BlenderRNA *brna)
 {
        StructRNA *srna;
        PropertyRNA *prop;
-       
+
+       static const EnumPropertyItem object_color_type_items[] = {
+               {V3D_OBJECT_COLOR_COLLECTION, "COLLECTION", 0, "Collection", ""},
+               {V3D_OBJECT_COLOR_OBJECT,     "OBJECT",     0, "Object",     ""},
+               {0, NULL, 0, NULL, NULL}
+       };
+
        srna = RNA_def_struct(brna, "LayerCollectionEngineSettingsWorkbench", "LayerCollectionSettings");
-       RNA_def_struct_ui_text(srna, "Collections Workbench Engine Settings", "Engine specific settings for this collection");
+       RNA_def_struct_ui_text(srna, "Collections Workbench Engine Settings", "Workbench specific settings for this collection");
 
        RNA_define_verify_sdna(0); /* not in sdna */
 
+       prop = RNA_def_property(srna, "object_color_type", PROP_ENUM, PROP_COLOR);
+       RNA_def_property_enum_funcs(prop, "rna_LayerEngineSettings_Workbench_object_color_type_get", "rna_LayerEngineSettings_Workbench_object_color_type_set", NULL);
+       RNA_def_property_enum_items(prop, object_color_type_items);
+       RNA_def_property_ui_text(prop, "Object Color", "Way colors are given to the Object");
+       RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
+       RNA_def_property_update(prop, 0, "rna_LayerCollectionEngineSettings_update");
+
        prop = RNA_def_property(srna, "object_color", PROP_FLOAT, PROP_COLOR);
        RNA_def_property_array(prop, 3);
        RNA_def_property_float_funcs(prop, "rna_LayerEngineSettings_Workbench_object_color_get",
                                     "rna_LayerEngineSettings_Workbench_object_color_set", NULL);
-       RNA_def_property_ui_text(prop, "Object Color", "Color for Drawing Objects");
+       RNA_def_property_ui_text(prop, "Collection Color", "Color for Drawing Objects in this Collection");
        RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
        RNA_def_property_update(prop, 0, "rna_LayerCollectionEngineSettings_update");
 }
index 47ae2cb8951aba410e8cac1663e9ecf312c08f32..80d23e1494976e8f82360b236b8509a10a26ddf2 100644 (file)
@@ -183,15 +183,15 @@ const EnumPropertyItem rna_enum_viewport_shade_items[] = {
 };
 
 const EnumPropertyItem rna_enum_viewport_shade_solid_items[] = {
-       {OB_LIGHTING_FLAT, "FLAT", ICON_SOLID, "Flat Lighting", "Display using flat lighting"},
-       {OB_LIGHTING_STUDIO, "STUDIO", ICON_SOLID, "Studio Lighting", "Display using studio lighting"},
-       // {OB_LIGHTING_SCENE, "SCENE", ICON_SOLID, "Scene Lighting", "Display using scene lighting"},
+       {V3D_LIGHTING_FLAT, "FLAT", ICON_SOLID, "Flat Lighting", "Display using flat lighting"},
+       {V3D_LIGHTING_STUDIO, "STUDIO", ICON_SOLID, "Studio Lighting", "Display using studio lighting"},
+       /* {V3D_LIGHTING_SCENE, "SCENE", ICON_SOLID, "Scene Lighting", "Display using scene lighting"}, */
        {0, NULL, 0, NULL, NULL}
 };
 const EnumPropertyItem rna_enum_viewport_shade_texture_items[] = {
-       {OB_LIGHTING_FLAT, "FLAT", ICON_POTATO, "Flat Lighting", "Display using flat lighting"},
-       {OB_LIGHTING_STUDIO, "STUDIO", ICON_POTATO, "Studio Lighting", "Display using studio lighting"},
-       // {OB_LIGHTING_SCENE, "SCENE", ICON_POTATO, "Scene Lighting", "Display using scene lighting"},
+       {V3D_LIGHTING_FLAT, "FLAT", ICON_POTATO, "Flat Lighting", "Display using flat lighting"},
+       {V3D_LIGHTING_STUDIO, "STUDIO", ICON_POTATO, "Studio Lighting", "Display using studio lighting"},
+       /* {V3D_LIGHTING_SCENE, "SCENE", ICON_POTATO, "Scene Lighting", "Display using scene lighting"}, */
        {0, NULL, 0, NULL, NULL}
 };