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
This commit is contained in:
parent
fa43886690
commit
6c1a121aef
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
||||
|
|
|
@ -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 */
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue