Workbench: Fix xray normal not being normalized.
[blender.git] / source / blender / draw / engines / workbench / shaders / workbench_forward_transparent_accum_frag.glsl
1 #ifdef OB_TEXTURE
2 uniform sampler2D image;
3 #endif
4 uniform mat4 ProjectionMatrix;
5 uniform mat3 normalWorldMatrix;
6 uniform float alpha = 0.5;
7 uniform vec2 invertedViewportSize;
8 uniform vec4 viewvecs[3];
9
10 #ifdef NORMAL_VIEWPORT_PASS_ENABLED
11 in vec3 normal_viewport;
12 #endif /* NORMAL_VIEWPORT_PASS_ENABLED */
13 #ifdef OB_TEXTURE
14 in vec2 uv_interp;
15 #endif
16 #ifdef STUDIOLIGHT_ORIENTATION_VIEWNORMAL
17 uniform sampler2D matcapImage;
18 #endif
19
20 layout(std140) uniform world_block {
21         WorldData world_data;
22 };
23
24 layout(std140) uniform material_block {
25         MaterialData material_data;
26 };
27
28 layout(location=0) out vec4 transparentAccum;
29
30
31 void main()
32 {
33         vec4 diffuse_color;
34         vec3 diffuse_light = vec3(1.0);
35 #ifdef OB_SOLID
36         diffuse_color = material_data.diffuse_color;
37 #endif /* OB_SOLID */
38 #ifdef OB_TEXTURE
39         diffuse_color = texture(image, uv_interp);
40 #endif /* OB_TEXTURE */
41
42         vec2 uv_viewport = gl_FragCoord.xy * invertedViewportSize;
43         vec3 I_vs = view_vector_from_screen_uv(uv_viewport, viewvecs, ProjectionMatrix);
44
45         vec3 nor = normalize(normal_viewport);
46
47 #ifdef V3D_LIGHTING_MATCAP
48         bool flipped = world_data.matcap_orientation != 0;
49         vec2 matcap_uv = matcap_uv_compute(I_vs, nor, flipped);
50         diffuse_light = texture(matcapImage, matcap_uv).rgb;
51 #endif
52
53 #ifdef V3D_SHADING_SPECULAR_HIGHLIGHT
54         vec3 specular_color = get_world_specular_lights(world_data, vec4(material_data.specular_color.rgb, material_data.roughness), nor, I_vs;
55 #else
56         vec3 specular_color = vec3(0.0);
57 #endif
58
59 #ifdef V3D_LIGHTING_STUDIO
60 #  ifdef STUDIOLIGHT_ORIENTATION_CAMERA
61         diffuse_light = get_camera_diffuse_light(world_data, nor);
62 #  endif
63 #  ifdef STUDIOLIGHT_ORIENTATION_WORLD
64         vec3 normal_world = normalWorldMatrix * nor;
65         diffuse_light = get_world_diffuse_light(world_data, normal_world);
66 #  endif
67 #endif
68
69         vec3 shaded_color = diffuse_light * diffuse_color.rgb + specular_color;
70
71         vec4 premultiplied = vec4(shaded_color.rgb * alpha, alpha);
72         transparentAccum = calculate_transparent_accum(premultiplied);
73 }
74