diff options
author | Jeroen Bakker <j.bakker@atmind.nl> | 2018-04-25 22:33:59 +0300 |
---|---|---|
committer | Jeroen Bakker <j.bakker@atmind.nl> | 2018-04-25 22:33:59 +0300 |
commit | 6c1a121aef0e2a1fb06121a44d3d57eca20a003e (patch) | |
tree | d15f56b811585167a29a2cc5893c66d5f5ad1186 | |
parent | fa4388669005b9ca0cf0bc3855ee0c0e183c3b3a (diff) |
Workbench: normal packing
Normal packing. The sign is stored in the A of the color buffer.
if the A == 1.0 the normal should be inverted. before use.
The reason is that packing has more precision for frontfaces, than for
backfaces
4 files changed, 27 insertions, 21 deletions
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl b/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl index 008ab90d7a3..e9d9b8c02e8 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl @@ -15,7 +15,7 @@ float bayer_dither_noise() { /* From http://aras-p.info/texts/CompactNormalStorage.html * Using Method #4: Spheremap Transform */ -vec3 normal_decode(vec3 enc) +vec3 normal_decode(vec2 enc) { vec2 fenc = enc.xy * 4.0 - 2.0; float f = dot(fenc, fenc); @@ -23,19 +23,13 @@ vec3 normal_decode(vec3 enc) vec3 n; n.xy = fenc*g; n.z = 1 - f / 2; - - if (enc.z > 0.5) { - n = -n; - } return n; } /* From http://aras-p.info/texts/CompactNormalStorage.html * Using Method #4: Spheremap Transform */ -vec3 normal_encode(vec3 n) +vec2 normal_encode(vec3 n) { float p = sqrt(n.z * 8.0 + 8.0); - vec3 result = vec3(n.xyz / p + 0.5); - result.z = (gl_FrontFacing)? 0.0: 1.0; - return result; + return vec2(n.xy / p + 0.5); } diff --git a/source/blender/draw/engines/workbench/shaders/workbench_composite_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_composite_frag.glsl index 89e5b882536..6a5d6c71aba 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_composite_frag.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_composite_frag.glsl @@ -40,18 +40,22 @@ void main() } #endif /* !V3D_DRAWOPTION_OBJECT_OVERLAP */ - vec3 diffuse_color = texelFetch(colorBuffer, texel, 0).rgb; #ifdef V3D_LIGHTING_STUDIO + vec4 diffuse_color = texelFetch(colorBuffer, texel, 0); #ifdef WORKBENCH_ENCODE_NORMALS - vec3 normal_viewport = normal_decode(texelFetch(normalBuffer, texel, 0).rgb); -#else + vec3 normal_viewport = normal_decode(texelFetch(normalBuffer, texel, 0).rg); + if (diffuse_color.a == 1.0) { + normal_viewport = - normal_viewport; + } +#else /* WORKBENCH_ENCODE_NORMALS */ vec3 normal_viewport = texelFetch(normalBuffer, texel, 0).rgb; -#endif +#endif /* WORKBENCH_ENCODE_NORMALS */ vec3 diffuse_light = get_world_diffuse_light(world_data, normal_viewport); - vec3 shaded_color = diffuse_light * diffuse_color; + vec3 shaded_color = diffuse_light * diffuse_color.rgb; #else /* V3D_LIGHTING_STUDIO */ + vec3 diffuse_color = texelFetch(colorBuffer, texel, 0).rgb; vec3 shaded_color = diffuse_color; #endif /* V3D_LIGHTING_STUDIO */ diff --git a/source/blender/draw/engines/workbench/shaders/workbench_prepass_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_prepass_frag.glsl index 32133914e65..1535ab2e36f 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_prepass_frag.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_prepass_frag.glsl @@ -4,17 +4,25 @@ uniform vec3 object_color = vec3(1.0, 0.0, 1.0); in vec3 normal_viewport; out uint objectId; -out vec3 diffuseColor; +out vec4 diffuseColor; +#ifdef WORKBENCH_ENCODE_NORMALS +out vec2 normalViewport; +#else out vec3 normalViewport; - +#endif void main() { objectId = uint(object_id); - diffuseColor = object_color; + diffuseColor = vec4(object_color, 0.0); #ifdef WORKBENCH_ENCODE_NORMALS - normalViewport = normal_encode(normal_viewport); -#else + if (!gl_FrontFacing) { + normalViewport = normal_encode(-normal_viewport); + diffuseColor.a = 1.0; + } else { + normalViewport = normal_encode(normal_viewport); + } +#else /* WORKBENCH_ENCODE_NORMALS */ normalViewport = normal_viewport; -#endif +#endif /* WORKBENCH_ENCODE_NORMALS */ } diff --git a/source/blender/draw/engines/workbench/workbench_materials.c b/source/blender/draw/engines/workbench/workbench_materials.c index 6fc416c068e..2ee87c9f2ce 100644 --- a/source/blender/draw/engines/workbench/workbench_materials.c +++ b/source/blender/draw/engines/workbench/workbench_materials.c @@ -206,7 +206,7 @@ void workbench_materials_engine_init(WORKBENCH_Data *vedata) e_data.object_id_tx = DRW_texture_pool_query_2D(size[0], size[1], DRW_TEX_R_32U, &draw_engine_workbench_solid); e_data.color_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], DRW_TEX_RGBA_8, &draw_engine_workbench_solid); #ifdef WORKBENCH_ENCODE_NORMALS - e_data.normal_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], DRW_TEX_RGBA_8, &draw_engine_workbench_solid); + e_data.normal_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], DRW_TEX_RG_8, &draw_engine_workbench_solid); #else e_data.normal_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], DRW_TEX_RGBA_32, &draw_engine_workbench_solid); #endif |