Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/workbench/workbench_materials.c
| Show All 25 Lines | |||||
| #include "BKE_image.h" | #include "BKE_image.h" | ||||
| #include "BKE_node.h" | #include "BKE_node.h" | ||||
| #include "BLI_dynstr.h" | #include "BLI_dynstr.h" | ||||
| #include "BLI_hash.h" | #include "BLI_hash.h" | ||||
| #include "DNA_node_types.h" | #include "DNA_node_types.h" | ||||
| #include "DNA_mesh_types.h" | |||||
| #include "ED_uvedit.h" | #include "ED_uvedit.h" | ||||
| #define HSV_SATURATION 0.5 | #define HSV_SATURATION 0.5 | ||||
| #define HSV_VALUE 0.8 | #define HSV_VALUE 0.8 | ||||
| void workbench_material_update_data(WORKBENCH_PrivateData *wpd, | void workbench_material_update_data(WORKBENCH_PrivateData *wpd, | ||||
| Object *ob, | Object *ob, | ||||
| Show All 20 Lines | if (ob->id.lib) { | ||||
| hash = (hash * 13) ^ BLI_ghashutil_strhash_p_murmur(ob->id.lib->name); | hash = (hash * 13) ^ BLI_ghashutil_strhash_p_murmur(ob->id.lib->name); | ||||
| } | } | ||||
| float hue = BLI_hash_int_01(hash); | float hue = BLI_hash_int_01(hash); | ||||
| float hsv[3] = {hue, HSV_SATURATION, HSV_VALUE}; | float hsv[3] = {hue, HSV_SATURATION, HSV_VALUE}; | ||||
| hsv_to_rgb_v(hsv, data->diffuse_color); | hsv_to_rgb_v(hsv, data->diffuse_color); | ||||
| copy_v3_v3(data->base_color, data->diffuse_color); | copy_v3_v3(data->base_color, data->diffuse_color); | ||||
| } | } | ||||
| else if (color_type == V3D_SHADING_OBJECT_COLOR) { | else if (ELEM(color_type, V3D_SHADING_OBJECT_COLOR, V3D_SHADING_VERTEX_COLOR)) { | ||||
| copy_v3_v3(data->diffuse_color, ob->color); | copy_v3_v3(data->diffuse_color, ob->color); | ||||
| copy_v3_v3(data->base_color, data->diffuse_color); | copy_v3_v3(data->base_color, data->diffuse_color); | ||||
| } | } | ||||
| else { | else { | ||||
| /* V3D_SHADING_MATERIAL_COLOR */ | /* V3D_SHADING_MATERIAL_COLOR */ | ||||
| if (mat) { | if (mat) { | ||||
| if (SPECULAR_HIGHLIGHT_ENABLED(wpd)) { | if (SPECULAR_HIGHLIGHT_ENABLED(wpd)) { | ||||
| copy_v3_v3(data->base_color, &mat->r); | copy_v3_v3(data->base_color, &mat->r); | ||||
| mul_v3_v3fl(data->diffuse_color, &mat->r, 1.0f - mat->metallic); | mul_v3_v3fl(data->diffuse_color, &mat->r, 1.0f - mat->metallic); | ||||
| mul_v3_v3fl(data->specular_color, &mat->r, mat->metallic); | mul_v3_v3fl(data->specular_color, &mat->r, mat->metallic); | ||||
| add_v3_fl(data->specular_color, 0.05f * (1.0f - mat->metallic)); | add_v3_fl(data->specular_color, 0.05f * (1.0f - mat->metallic)); | ||||
| data->metallic = mat->metallic; | data->metallic = mat->metallic; | ||||
| data->roughness = sqrtf(mat->roughness); /* Remap to disney roughness. */ | data->roughness = sqrtf(mat->roughness); /* Remap to disney roughness. */ | ||||
| } | } | ||||
| else { | else { | ||||
| copy_v3_v3(data->base_color, &mat->r); | copy_v3_v3(data->base_color, &mat->r); | ||||
| copy_v3_v3(data->diffuse_color, &mat->r); | copy_v3_v3(data->diffuse_color, &mat->r); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| char *workbench_material_build_defines(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair) | char *workbench_material_build_defines(WORKBENCH_PrivateData *wpd, | ||||
| bool is_primary_shader, | |||||
| bool is_hair) | |||||
| { | { | ||||
| char *str = NULL; | char *str = NULL; | ||||
| bool use_textures = is_primary_shader && (wpd->shading.color_type == V3D_SHADING_TEXTURE_COLOR); | |||||
| bool use_vertex_colors = is_primary_shader && | |||||
| (wpd->shading.color_type == V3D_SHADING_VERTEX_COLOR); | |||||
| DynStr *ds = BLI_dynstr_new(); | DynStr *ds = BLI_dynstr_new(); | ||||
| if (wpd->shading.flag & V3D_SHADING_OBJECT_OUTLINE) { | if (wpd->shading.flag & V3D_SHADING_OBJECT_OUTLINE) { | ||||
| BLI_dynstr_append(ds, "#define V3D_SHADING_OBJECT_OUTLINE\n"); | BLI_dynstr_append(ds, "#define V3D_SHADING_OBJECT_OUTLINE\n"); | ||||
| } | } | ||||
| if (wpd->shading.flag & V3D_SHADING_SHADOW) { | if (wpd->shading.flag & V3D_SHADING_SHADOW) { | ||||
| BLI_dynstr_append(ds, "#define V3D_SHADING_SHADOW\n"); | BLI_dynstr_append(ds, "#define V3D_SHADING_SHADOW\n"); | ||||
| Show All 17 Lines | if (OBJECT_ID_PASS_ENABLED(wpd)) { | ||||
| BLI_dynstr_append(ds, "#define OBJECT_ID_PASS_ENABLED\n"); | BLI_dynstr_append(ds, "#define OBJECT_ID_PASS_ENABLED\n"); | ||||
| } | } | ||||
| if (MATDATA_PASS_ENABLED(wpd)) { | if (MATDATA_PASS_ENABLED(wpd)) { | ||||
| BLI_dynstr_append(ds, "#define MATDATA_PASS_ENABLED\n"); | BLI_dynstr_append(ds, "#define MATDATA_PASS_ENABLED\n"); | ||||
| } | } | ||||
| if (NORMAL_VIEWPORT_PASS_ENABLED(wpd)) { | if (NORMAL_VIEWPORT_PASS_ENABLED(wpd)) { | ||||
| BLI_dynstr_append(ds, "#define NORMAL_VIEWPORT_PASS_ENABLED\n"); | BLI_dynstr_append(ds, "#define NORMAL_VIEWPORT_PASS_ENABLED\n"); | ||||
| } | } | ||||
| if (use_vertex_colors) { | |||||
| BLI_dynstr_append(ds, "#define V3D_SHADING_VERTEX_COLOR\n"); | |||||
| } | |||||
| if (use_textures) { | if (use_textures) { | ||||
| BLI_dynstr_append(ds, "#define V3D_SHADING_TEXTURE_COLOR\n"); | BLI_dynstr_append(ds, "#define V3D_SHADING_TEXTURE_COLOR\n"); | ||||
| } | } | ||||
| if (NORMAL_ENCODING_ENABLED()) { | if (NORMAL_ENCODING_ENABLED()) { | ||||
| BLI_dynstr_append(ds, "#define WORKBENCH_ENCODE_NORMALS\n"); | BLI_dynstr_append(ds, "#define WORKBENCH_ENCODE_NORMALS\n"); | ||||
| } | } | ||||
| if (is_hair) { | if (is_hair) { | ||||
| BLI_dynstr_append(ds, "#define HAIR_SHADER\n"); | BLI_dynstr_append(ds, "#define HAIR_SHADER\n"); | ||||
| ▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | int workbench_material_get_composite_shader_index(WORKBENCH_PrivateData *wpd) | ||||
| SET_FLAG_FROM_TEST(index, wpd->shading.flag & V3D_SHADING_CAVITY, 1 << 3); | SET_FLAG_FROM_TEST(index, wpd->shading.flag & V3D_SHADING_CAVITY, 1 << 3); | ||||
| SET_FLAG_FROM_TEST(index, wpd->shading.flag & V3D_SHADING_OBJECT_OUTLINE, 1 << 4); | SET_FLAG_FROM_TEST(index, wpd->shading.flag & V3D_SHADING_OBJECT_OUTLINE, 1 << 4); | ||||
| SET_FLAG_FROM_TEST(index, MATDATA_PASS_ENABLED(wpd), 1 << 5); | SET_FLAG_FROM_TEST(index, MATDATA_PASS_ENABLED(wpd), 1 << 5); | ||||
| BLI_assert(index < MAX_COMPOSITE_SHADERS); | BLI_assert(index < MAX_COMPOSITE_SHADERS); | ||||
| return index; | return index; | ||||
| } | } | ||||
| int workbench_material_get_prepass_shader_index(WORKBENCH_PrivateData *wpd, | int workbench_material_get_prepass_shader_index(WORKBENCH_PrivateData *wpd, | ||||
| bool use_textures, | bool is_primary_shader, | ||||
| bool is_hair) | bool is_hair) | ||||
| { | { | ||||
| bool use_textures = is_primary_shader && (wpd->shading.color_type == V3D_SHADING_TEXTURE_COLOR); | |||||
| bool use_vertex_colors = is_primary_shader && | |||||
| (wpd->shading.color_type == V3D_SHADING_VERTEX_COLOR); | |||||
| /* NOTE: change MAX_PREPASS_SHADERS accordingly when modifying this function. */ | /* NOTE: change MAX_PREPASS_SHADERS accordingly when modifying this function. */ | ||||
| int index = 0; | int index = 0; | ||||
| SET_FLAG_FROM_TEST(index, is_hair, 1 << 0); | SET_FLAG_FROM_TEST(index, is_hair, 1 << 0); | ||||
| SET_FLAG_FROM_TEST(index, MATDATA_PASS_ENABLED(wpd), 1 << 1); | SET_FLAG_FROM_TEST(index, MATDATA_PASS_ENABLED(wpd), 1 << 1); | ||||
| SET_FLAG_FROM_TEST(index, OBJECT_ID_PASS_ENABLED(wpd), 1 << 2); | SET_FLAG_FROM_TEST(index, OBJECT_ID_PASS_ENABLED(wpd), 1 << 2); | ||||
| SET_FLAG_FROM_TEST(index, NORMAL_VIEWPORT_PASS_ENABLED(wpd), 1 << 3); | SET_FLAG_FROM_TEST(index, NORMAL_VIEWPORT_PASS_ENABLED(wpd), 1 << 3); | ||||
| SET_FLAG_FROM_TEST(index, MATCAP_ENABLED(wpd), 1 << 4); | SET_FLAG_FROM_TEST(index, MATCAP_ENABLED(wpd), 1 << 4); | ||||
| SET_FLAG_FROM_TEST(index, use_textures, 1 << 5); | SET_FLAG_FROM_TEST(index, use_textures, 1 << 5); | ||||
| SET_FLAG_FROM_TEST(index, use_vertex_colors, 1 << 6); | |||||
| BLI_assert(index < MAX_PREPASS_SHADERS); | BLI_assert(index < MAX_PREPASS_SHADERS); | ||||
| return index; | return index; | ||||
| } | } | ||||
| int workbench_material_get_accum_shader_index(WORKBENCH_PrivateData *wpd, | int workbench_material_get_accum_shader_index(WORKBENCH_PrivateData *wpd, | ||||
| bool use_textures, | bool is_primary_shader, | ||||
| bool is_hair) | bool is_hair) | ||||
| { | { | ||||
| bool use_textures = is_primary_shader && (wpd->shading.color_type == V3D_SHADING_TEXTURE_COLOR); | |||||
| bool use_vertex_colors = is_primary_shader && | |||||
| (wpd->shading.color_type == V3D_SHADING_VERTEX_COLOR); | |||||
| /* NOTE: change MAX_ACCUM_SHADERS accordingly when modifying this function. */ | /* NOTE: change MAX_ACCUM_SHADERS accordingly when modifying this function. */ | ||||
| int index = 0; | int index = 0; | ||||
| /* 2 bits FLAT/STUDIO/MATCAP + Specular highlight */ | /* 2 bits FLAT/STUDIO/MATCAP + Specular highlight */ | ||||
| index = SPECULAR_HIGHLIGHT_ENABLED(wpd) ? 3 : wpd->shading.light; | index = SPECULAR_HIGHLIGHT_ENABLED(wpd) ? 3 : wpd->shading.light; | ||||
| SET_FLAG_FROM_TEST(index, use_textures, 1 << 2); | SET_FLAG_FROM_TEST(index, use_textures, 1 << 2); | ||||
| SET_FLAG_FROM_TEST(index, is_hair, 1 << 3); | SET_FLAG_FROM_TEST(index, use_vertex_colors, 1 << 3); | ||||
| SET_FLAG_FROM_TEST(index, is_hair, 1 << 4); | |||||
| /* 1 bits SHADOWS (only facing factor) */ | /* 1 bits SHADOWS (only facing factor) */ | ||||
| SET_FLAG_FROM_TEST(index, SHADOW_ENABLED(wpd), 1 << 4); | SET_FLAG_FROM_TEST(index, SHADOW_ENABLED(wpd), 1 << 5); | ||||
| BLI_assert(index < MAX_ACCUM_SHADERS); | BLI_assert(index < MAX_ACCUM_SHADERS); | ||||
| return index; | return index; | ||||
| } | } | ||||
| int workbench_material_determine_color_type(WORKBENCH_PrivateData *wpd, Image *ima, Object *ob) | int workbench_material_determine_color_type(WORKBENCH_PrivateData *wpd, | ||||
| Image *ima, | |||||
| Object *ob, | |||||
| bool is_sculpt_mode) | |||||
| { | { | ||||
| int color_type = wpd->shading.color_type; | int color_type = wpd->shading.color_type; | ||||
| if ((color_type == V3D_SHADING_TEXTURE_COLOR && ima == NULL) || (ob->dt < OB_TEXTURE)) { | const Mesh *me = (ob->type == OB_MESH) ? ob->data : NULL; | ||||
| if ((color_type == V3D_SHADING_TEXTURE_COLOR && (ima == NULL || is_sculpt_mode)) || | |||||
| (ob->dt < OB_TEXTURE)) { | |||||
| color_type = V3D_SHADING_MATERIAL_COLOR; | color_type = V3D_SHADING_MATERIAL_COLOR; | ||||
| } | } | ||||
| if (color_type == V3D_SHADING_VERTEX_COLOR && (me == NULL || me->mloopcol == NULL)) { | |||||
| color_type = V3D_SHADING_OBJECT_COLOR; | |||||
| } | |||||
| return color_type; | return color_type; | ||||
| } | } | ||||
| void workbench_material_get_image_and_mat( | void workbench_material_get_image_and_mat( | ||||
| Object *ob, int mat_nr, Image **r_image, ImageUser **r_iuser, int *r_interp, Material **r_mat) | Object *ob, int mat_nr, Image **r_image, ImageUser **r_iuser, int *r_interp, Material **r_mat) | ||||
| { | { | ||||
| bNode *node; | bNode *node; | ||||
| *r_mat = give_current_material(ob, mat_nr); | *r_mat = give_current_material(ob, mat_nr); | ||||
| Show All 27 Lines | void workbench_material_shgroup_uniform(WORKBENCH_PrivateData *wpd, | ||||
| const bool use_metallic, | const bool use_metallic, | ||||
| const bool deferred, | const bool deferred, | ||||
| const int interp) | const int interp) | ||||
| { | { | ||||
| if (deferred && !MATDATA_PASS_ENABLED(wpd)) { | if (deferred && !MATDATA_PASS_ENABLED(wpd)) { | ||||
| return; | return; | ||||
| } | } | ||||
| if (workbench_material_determine_color_type(wpd, material->ima, ob) == | if (workbench_material_determine_color_type(wpd, material->ima, ob, false) == | ||||
| V3D_SHADING_TEXTURE_COLOR) { | V3D_SHADING_TEXTURE_COLOR) { | ||||
| ImBuf *ibuf = BKE_image_acquire_ibuf(material->ima, material->iuser, NULL); | ImBuf *ibuf = BKE_image_acquire_ibuf(material->ima, material->iuser, NULL); | ||||
| const bool do_color_correction = wpd->use_color_management && | const bool do_color_correction = wpd->use_color_management && | ||||
| (ibuf && | (ibuf && | ||||
| (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA) == 0); | (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA) == 0); | ||||
| BKE_image_release_ibuf(material->ima, ibuf, NULL); | BKE_image_release_ibuf(material->ima, ibuf, NULL); | ||||
| GPUTexture *tex = GPU_texture_from_blender( | GPUTexture *tex = GPU_texture_from_blender( | ||||
| material->ima, material->iuser, GL_TEXTURE_2D, false); | material->ima, material->iuser, GL_TEXTURE_2D, false); | ||||
| Show All 39 Lines | |||||