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