Workbench: Transparent textures

Worknemch now supports transparent textures.
As the main engine is a deferred shading only a cutoff is supported
(draw or don't draw this pixel)
This commit is contained in:
Jeroen Bakker 2018-06-29 12:05:23 +02:00
parent 15ebe30bc5
commit 04f8929271
4 changed files with 43 additions and 21 deletions

View File

@ -1,7 +1,20 @@
uniform int object_id = 0;
layout(location=0) out uint objectId;
uniform float ImageTransparencyCutoff = 0.1;
#ifdef V3D_SHADING_TEXTURE_COLOR
uniform sampler2D image;
in vec2 uv_interp;
#endif
void main()
{
#ifdef V3D_SHADING_TEXTURE_COLOR
vec4 diffuse_color = texture(image, uv_interp);
if (diffuse_color.a < ImageTransparencyCutoff) {
discard;
}
#endif
objectId = uint(object_id);
}

View File

@ -1,5 +1,7 @@
#ifdef V3D_SHADING_TEXTURE_COLOR
uniform sampler2D image;
uniform float ImageTransparencyCutoff = 0.1;
#endif
uniform mat4 ProjectionMatrix;
uniform mat3 normalWorldMatrix;
@ -35,6 +37,9 @@ void main()
#ifdef V3D_SHADING_TEXTURE_COLOR
diffuse_color = texture(image, uv_interp);
if (diffuse_color.a < ImageTransparencyCutoff) {
discard;
}
#else
diffuse_color = materialDiffuseColor;
#endif /* V3D_SHADING_TEXTURE_COLOR */

View File

@ -6,6 +6,8 @@ uniform float materialRoughness;
#ifdef V3D_SHADING_TEXTURE_COLOR
uniform sampler2D image;
uniform float ImageTransparencyCutoff = 0.1;
#endif
#ifdef NORMAL_VIEWPORT_PASS_ENABLED
@ -42,6 +44,9 @@ void main()
#ifdef V3D_SHADING_TEXTURE_COLOR
diffuseColor = texture(image, uv_interp);
if (diffuseColor.a < ImageTransparencyCutoff) {
discard;
}
#else
diffuseColor = vec4(materialDiffuseColor.rgb, 0.0);
# ifdef STUDIOLIGHT_ORIENTATION_VIEWNORMAL

View File

@ -51,6 +51,7 @@ static struct {
struct GPUShader *composite_sh_cache[MAX_SHADERS];
struct GPUShader *transparent_accum_sh_cache[MAX_SHADERS];
struct GPUShader *object_outline_sh;
struct GPUShader *object_outline_texture_sh;
struct GPUShader *object_outline_hair_sh;
struct GPUShader *checker_depth_sh;
@ -78,20 +79,6 @@ extern char datatoc_workbench_common_lib_glsl[];
extern char datatoc_workbench_world_light_lib_glsl[];
/* static functions */
static char *workbench_build_forward_depth_frag(void)
{
char *str = NULL;
DynStr *ds = BLI_dynstr_new();
BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
BLI_dynstr_append(ds, datatoc_workbench_forward_depth_frag_glsl);
str = BLI_dynstr_get_cstring(ds);
BLI_dynstr_free(ds);
return str;
}
static char *workbench_build_forward_vert(void)
{
char *str = NULL;
@ -189,7 +176,15 @@ static WORKBENCH_MaterialData *get_or_create_material_data(
material->shgrp = grp;
/* Depth */
material->shgrp_object_outline = DRW_shgroup_create(e_data.object_outline_sh, psl->object_outline_pass);
if (workbench_material_determine_color_type(wpd, material->ima) == V3D_SHADING_TEXTURE_COLOR)
{
material->shgrp_object_outline = DRW_shgroup_create(e_data.object_outline_texture_sh, psl->object_outline_pass);
GPUTexture *tex = GPU_texture_from_blender(material->ima, NULL, GL_TEXTURE_2D, false, 0.0f);
DRW_shgroup_uniform_texture(material->shgrp_object_outline, "image", tex);
}
else {
material->shgrp_object_outline = DRW_shgroup_create(e_data.object_outline_sh, psl->object_outline_pass);
}
material->object_id = engine_object_data->object_id;
DRW_shgroup_uniform_int(material->shgrp_object_outline, "object_id", &material->object_id, 1);
BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
@ -266,23 +261,26 @@ void workbench_forward_engine_init(WORKBENCH_Data *vedata)
memset(e_data.composite_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
memset(e_data.transparent_accum_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
char *defines = workbench_material_build_defines(wpd, OB_SOLID, false);
char *defines_hair = workbench_material_build_defines(wpd, OB_SOLID, true);
char *defines = workbench_material_build_defines(wpd, false, false);
char *defines_texture = workbench_material_build_defines(wpd, true, false);
char *defines_hair = workbench_material_build_defines(wpd, false, true);
char *forward_vert = workbench_build_forward_vert();
char *forward_depth_frag = workbench_build_forward_depth_frag();
e_data.object_outline_sh = DRW_shader_create(
forward_vert, NULL,
forward_depth_frag, defines);
datatoc_workbench_forward_depth_frag_glsl, defines);
e_data.object_outline_texture_sh = DRW_shader_create(
forward_vert, NULL,
datatoc_workbench_forward_depth_frag_glsl, defines_texture);
e_data.object_outline_hair_sh = DRW_shader_create(
forward_vert, NULL,
forward_depth_frag, defines_hair);
datatoc_workbench_forward_depth_frag_glsl, defines_hair);
e_data.checker_depth_sh = DRW_shader_create_fullscreen(
datatoc_workbench_checkerboard_depth_frag_glsl, NULL);
MEM_freeN(forward_vert);
MEM_freeN(forward_depth_frag);
MEM_freeN(defines);
MEM_freeN(defines_texture);
MEM_freeN(defines_hair);
}
workbench_fxaa_engine_init();
@ -369,6 +367,7 @@ void workbench_forward_engine_free()
DRW_SHADER_FREE_SAFE(e_data.transparent_accum_sh_cache[index]);
}
DRW_SHADER_FREE_SAFE(e_data.object_outline_sh);
DRW_SHADER_FREE_SAFE(e_data.object_outline_texture_sh);
DRW_SHADER_FREE_SAFE(e_data.object_outline_hair_sh);
DRW_SHADER_FREE_SAFE(e_data.checker_depth_sh);