4aa471f70d8b91b7eb6e58704511e50c7b026132
[blender.git] / source / blender / draw / engines / workbench / shaders / workbench_deferred_composite_frag.glsl
1 out vec4 fragColor;
2
3 uniform mat4 ProjectionMatrix;
4 uniform mat4 ViewMatrixInverse;
5
6 uniform usampler2D objectId;
7 uniform sampler2D colorBuffer;
8 uniform sampler2D specularBuffer;
9 uniform sampler2D normalBuffer;
10 /* normalBuffer contains viewport normals */
11 uniform sampler2D cavityBuffer;
12
13 uniform vec2 invertedViewportSize;
14 uniform vec4 viewvecs[3];
15 uniform float shadowMultiplier;
16 uniform float lightMultiplier;
17 uniform float shadowShift = 0.1;
18
19 #ifdef STUDIOLIGHT_ORIENTATION_VIEWNORMAL
20 uniform sampler2D matcapImage;
21 #endif
22
23 layout(std140) uniform world_block {
24         WorldData world_data;
25 };
26
27 void main()
28 {
29         ivec2 texel = ivec2(gl_FragCoord.xy);
30         vec2 uv_viewport = gl_FragCoord.xy * invertedViewportSize;
31         uint object_id = texelFetch(objectId, texel, 0).r;
32
33 #ifndef V3D_SHADING_OBJECT_OUTLINE
34         if (object_id == NO_OBJECT_ID) {
35                 fragColor = vec4(background_color(world_data, uv_viewport.y), world_data.background_alpha);
36                 return;
37         }
38 #else /* !V3D_SHADING_OBJECT_OUTLINE */
39         float object_outline = calculate_object_outline(objectId, texel, object_id);
40
41         if (object_id == NO_OBJECT_ID) {
42                 vec3 background = background_color(world_data, uv_viewport.y);
43                 if (object_outline == 0.0) {
44                         fragColor = vec4(background, world_data.background_alpha);
45                 }
46                 else {
47                         /* Do correct alpha blending. */
48                         vec4 background_color = vec4(background, 1.0) * world_data.background_alpha;
49                         vec4 outline_color = vec4(world_data.object_outline_color.rgb, 1.0);
50                         fragColor = mix(outline_color, background_color, object_outline);
51                         fragColor = vec4(fragColor.rgb / max(1e-8, fragColor.a), fragColor.a);
52                 }
53                 return;
54         }
55 #endif /* !V3D_SHADING_OBJECT_OUTLINE */
56
57         vec4 diffuse_color = texelFetch(colorBuffer, texel, 0);
58
59 /* Do we need normals */
60 #ifdef NORMAL_VIEWPORT_PASS_ENABLED
61 #  ifdef WORKBENCH_ENCODE_NORMALS
62         vec3 normal_viewport = normal_decode(texelFetch(normalBuffer, texel, 0).rg);
63         if (diffuse_color.a == 0.0) {
64                 normal_viewport = -normal_viewport;
65         }
66 #  else /* WORKBENCH_ENCODE_NORMALS */
67         vec3 normal_viewport = texelFetch(normalBuffer, texel, 0).rgb;
68 #  endif /* WORKBENCH_ENCODE_NORMALS */
69 #endif
70
71         vec3 I_vs = view_vector_from_screen_uv(uv_viewport, viewvecs, ProjectionMatrix);
72
73         /* -------- SHADING --------- */
74 #ifdef V3D_LIGHTING_FLAT
75         vec3 shaded_color = diffuse_color.rgb;
76
77 #elif defined(V3D_LIGHTING_MATCAP)
78         bool flipped = world_data.matcap_orientation != 0;
79         vec2 matcap_uv = matcap_uv_compute(I_vs, normal_viewport, flipped);
80         vec3 object_color = texelFetch(specularBuffer, texel, 0).rgb;
81         vec3 matcap = textureLod(matcapImage, matcap_uv, 0.0).rgb;
82         vec3 shaded_color = matcap * object_color;
83
84 #elif defined(V3D_LIGHTING_STUDIO)
85
86 #  ifdef V3D_SHADING_SPECULAR_HIGHLIGHT
87         vec4 specular_data = texelFetch(specularBuffer, texel, 0);
88 #  else
89         vec4 specular_data = vec4(0.0);
90 #  endif
91         vec3 shaded_color = get_world_lighting(world_data,
92                                                diffuse_color.rgb, specular_data.rgb, specular_data.a,
93                                                normal_viewport, I_vs);
94 #endif
95
96         /* -------- POST EFFECTS --------- */
97 #ifdef V3D_SHADING_SSAO
98         vec2 cavity = texelFetch(cavityBuffer, texel, 0).rg;
99         shaded_color *= 1.0 - cavity.x;
100         shaded_color *= 1.0 + cavity.y;
101 #endif
102
103 #ifdef V3D_SHADING_CURVATURE
104         float curvature = calculate_curvature(objectId, normalBuffer, texel, world_data.curvature_ridge, world_data.curvature_valley);
105         shaded_color *= curvature + 1.0;
106 #endif
107
108 #ifdef V3D_SHADING_SHADOW
109         float light_factor = -dot(normal_viewport, world_data.shadow_direction_vs.xyz);
110         /* The step function might be ok for meshes but it's
111          * clearly not the case for hairs. Do smoothstep in this case. */
112         float shadow_mix = smoothstep(1.0, shadowShift, light_factor);
113         shaded_color *= mix(lightMultiplier, shadowMultiplier, shadow_mix);
114 #endif
115
116 #ifdef V3D_SHADING_OBJECT_OUTLINE
117         shaded_color = mix(world_data.object_outline_color.rgb, shaded_color, object_outline);
118 #endif
119
120         fragColor = vec4(shaded_color, 1.0);
121 }