diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2018-06-09 13:21:19 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2018-06-09 13:44:56 +0300 |
commit | a1b42b79b316608a60e811458e7854266ce6ea5f (patch) | |
tree | 2b6f0d6700da78beeae27e0cbd04a4a6c480cb58 /source/blender | |
parent | 7ce81484f8214cd52b000e8d3f47dd3aed56da84 (diff) |
Workbench: Fix incident vector calculation.
This fixes specular in perspective view.
Diffstat (limited to 'source/blender')
5 files changed, 25 insertions, 22 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 39c6863e71c..9a0ec7703c8 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl @@ -71,3 +71,11 @@ vec4 calculate_transparent_accum(vec4 premultiplied) { float w = clamp(a * a * a * 1e8 * b * b * b, 1e-2, 3e2); return premultiplied * w; } + +vec3 view_vector_from_screen_uv(vec2 uv, vec4 viewvecs[3], mat4 proj_mat) +{ + return (proj_mat[3][3] == 0.0) + ? normalize(viewvecs[0].xyz + vec3(uv, 0.0) * viewvecs[1].xyz) + : vec3(0.0, 0.0, 1.0); +} + diff --git a/source/blender/draw/engines/workbench/shaders/workbench_deferred_composite_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_deferred_composite_frag.glsl index e5ee272e7fd..4ae62352e16 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_deferred_composite_frag.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_deferred_composite_frag.glsl @@ -1,5 +1,7 @@ out vec4 fragColor; +uniform mat4 ProjectionMatrix; + uniform usampler2D objectId; uniform sampler2D colorBuffer; uniform sampler2D specularBuffer; @@ -8,6 +10,7 @@ uniform sampler2D normalBuffer; uniform sampler2D cavityBuffer; uniform vec2 invertedViewportSize; +uniform vec4 viewvecs[3]; uniform float shadowMultiplier; uniform float lightMultiplier; uniform float shadowShift = 0.1; @@ -61,6 +64,8 @@ void main() # endif /* WORKBENCH_ENCODE_NORMALS */ #endif + vec3 I_vs = view_vector_from_screen_uv(uv_viewport, viewvecs, ProjectionMatrix); + #ifdef STUDIOLIGHT_ORIENTATION_VIEWNORMAL vec2 matcap_uv = normal_viewport.xy / 2.0 + 0.5; if (world_data.matcap_orientation != 0) { @@ -70,8 +75,6 @@ void main() #endif #ifdef V3D_SHADING_SPECULAR_HIGHLIGHT - /* XXX Should calculate the correct VS Incoming direction */ - vec3 I_vs = vec3(0.0, 0.0, 1.0); vec4 specular_data = texelFetch(specularBuffer, texel, 0); vec3 specular_color = get_world_specular_lights(world_data, specular_data, normal_viewport, I_vs); #else diff --git a/source/blender/draw/engines/workbench/shaders/workbench_forward_transparent_accum_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_forward_transparent_accum_frag.glsl index e04bffdeea5..b26bc396283 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_forward_transparent_accum_frag.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_forward_transparent_accum_frag.glsl @@ -1,8 +1,11 @@ #ifdef OB_TEXTURE uniform sampler2D image; #endif +uniform mat4 ProjectionMatrix; uniform mat3 normalWorldMatrix; uniform float alpha = 0.5; +uniform vec2 invertedViewportSize; +uniform vec4 viewvecs[3]; #ifdef NORMAL_VIEWPORT_PASS_ENABLED in vec3 normal_viewport; @@ -36,12 +39,15 @@ void main() diffuse_color = texture(image, uv_interp); #endif /* OB_TEXTURE */ + vec2 uv_viewport = gl_FragCoord.xy * invertedViewportSize; + vec3 I_vs = view_vector_from_screen_uv(uv_viewport, viewvecs, ProjectionMatrix); + #ifdef V3D_LIGHTING_MATCAP diffuse_light = texture(matcapImage, normal_viewport.xy / 2.0 + 0.5).rgb; #endif #ifdef V3D_SHADING_SPECULAR_HIGHLIGHT - vec3 specular_color = get_world_specular_lights(world_data, vec4(material_data.specular_color.rgb, material_data.roughness), normal_viewport, vec3(0.0, 0.0, 1.0)); + vec3 specular_color = get_world_specular_lights(world_data, vec4(material_data.specular_color.rgb, material_data.roughness), normal_viewport, I_vs; #else vec3 specular_color = vec3(0.0); #endif diff --git a/source/blender/draw/engines/workbench/workbench_deferred.c b/source/blender/draw/engines/workbench/workbench_deferred.c index c4fa82d39b7..e1cb8b482ab 100644 --- a/source/blender/draw/engines/workbench/workbench_deferred.c +++ b/source/blender/draw/engines/workbench/workbench_deferred.c @@ -439,25 +439,7 @@ static void workbench_composite_uniforms(WORKBENCH_PrivateData *wpd, DRWShadingG } if (SPECULAR_HIGHLIGHT_ENABLED(wpd) || MATCAP_ENABLED(wpd)) { DRW_shgroup_uniform_texture_ref(grp, "specularBuffer", &e_data.specular_buffer_tx); - -#if 0 - float invwinmat[4][4]; - DRW_viewport_matrix_get(invwinmat, DRW_MAT_WININV); - - copy_v4_fl4(e_data.screenvecs[0], 1.0f, -1.0f, 0.0f, 1.0f); - copy_v4_fl4(e_data.screenvecs[1], -1.0f, 1.0f, 0.0f, 1.0f); - copy_v4_fl4(e_data.screenvecs[2], -1.0f, -1.0f, 0.0f, 1.0f); - for (int i = 0; i < 3; i++) { - mul_m4_v4(invwinmat, e_data.screenvecs[i]); - e_data.screenvecs[i][0] /= e_data.screenvecs[i][3]; /* perspective divide */ - e_data.screenvecs[i][1] /= e_data.screenvecs[i][3]; /* perspective divide */ - e_data.screenvecs[i][2] /= e_data.screenvecs[i][3]; /* perspective divide */ - e_data.screenvecs[i][3] = 1.0f; - } - sub_v3_v3(e_data.screenvecs[0], e_data.screenvecs[2]); - sub_v3_v3(e_data.screenvecs[1], e_data.screenvecs[2]); - DRW_shgroup_uniform_vec4(grp, "screenvecs[0]", e_data.screenvecs[0], 3); -#endif + DRW_shgroup_uniform_vec4(grp, "viewvecs[0]", (float *)wpd->viewvecs, 3); } DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo); DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1); diff --git a/source/blender/draw/engines/workbench/workbench_forward.c b/source/blender/draw/engines/workbench/workbench_forward.c index 8bd27e18da2..3bc016e139b 100644 --- a/source/blender/draw/engines/workbench/workbench_forward.c +++ b/source/blender/draw/engines/workbench/workbench_forward.c @@ -207,6 +207,10 @@ static WORKBENCH_MaterialData *get_or_create_material_data( BKE_studiolight_ensure_flag(wpd->studio_light, STUDIOLIGHT_EQUIRECTANGULAR_RADIANCE_GPUTEXTURE); DRW_shgroup_uniform_texture(grp, "matcapImage", wpd->studio_light->equirectangular_radiance_gputexture); } + if (SPECULAR_HIGHLIGHT_ENABLED(wpd) || MATCAP_ENABLED(wpd)) { + DRW_shgroup_uniform_vec4(grp, "viewvecs[0]", (float *)wpd->viewvecs, 3); + DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1); + } break; } |