5f37490603d558c7696adb47412186606f52e69e
[blender.git] / source / blender / draw / engines / workbench / shaders / workbench_deferred_composite_frag.glsl
1 out vec4 fragColor;
2
3 uniform usampler2D objectId;
4 uniform sampler2D colorBuffer;
5 uniform sampler2D specularBuffer;
6 uniform sampler2D normalBuffer;
7 /* normalBuffer contains viewport normals */
8 uniform vec2 invertedViewportSize;
9 uniform float shadowMultiplier;
10 uniform float lightMultiplier;
11 uniform float shadowShift = 0.1;
12 uniform mat3 normalWorldMatrix;
13
14
15 layout(std140) uniform world_block {
16         WorldData world_data;
17 };
18
19 void main()
20 {
21         ivec2 texel = ivec2(gl_FragCoord.xy);
22         vec2 uv_viewport = gl_FragCoord.xy * invertedViewportSize;
23         uint object_id = texelFetch(objectId, texel, 0).r;
24
25 #ifndef V3D_SHADING_OBJECT_OUTLINE
26         if (object_id == NO_OBJECT_ID) {
27                 fragColor = vec4(background_color(world_data, uv_viewport.y), 0.0);
28                 return;
29         }
30 #else /* !V3D_SHADING_OBJECT_OUTLINE */
31         float object_outline = calculate_object_outline(objectId, texel, object_id);
32
33         if (object_id == NO_OBJECT_ID) {
34                 vec3 background = background_color(world_data, uv_viewport.y);
35                 if (object_outline == 0.0) {
36                         fragColor = vec4(background, 0.0);
37                 }
38                 else {
39                         fragColor = vec4(mix(world_data.object_outline_color.rgb, background, object_outline), 1.0-object_outline);
40                 }
41                 return;
42         }
43 #endif /* !V3D_SHADING_OBJECT_OUTLINE */
44
45         vec4 diffuse_color = texelFetch(colorBuffer, texel, 0);
46 /* Do we need normals */
47 #ifdef NORMAL_VIEWPORT_PASS_ENABLED
48 #ifdef WORKBENCH_ENCODE_NORMALS
49         vec3 normal_viewport = normal_decode(texelFetch(normalBuffer, texel, 0).rg);
50         if (diffuse_color.a == 0.0) {
51                 normal_viewport = -normal_viewport;
52         }
53 #else /* WORKBENCH_ENCODE_NORMALS */
54         vec3 normal_viewport = texelFetch(normalBuffer, texel, 0).rgb;
55 #endif /* WORKBENCH_ENCODE_NORMALS */
56 #endif
57
58 #ifdef V3D_SHADING_SPECULAR_HIGHLIGHT
59         /* XXX Should calculate the correct VS Incoming direction */
60         vec3 I_vs = vec3(0.0, 0.0, 1.0);
61         vec4 specular_data = texelFetch(specularBuffer, texel, 0);
62         vec3 specular_color = get_world_specular_lights(world_data, specular_data, normal_viewport, I_vs);
63 #else
64         vec3 specular_color = vec3(0.0);
65 #endif
66
67 #ifdef V3D_LIGHTING_STUDIO
68   #ifdef STUDIOLIGHT_ORIENTATION_CAMERA
69         vec3 diffuse_light = get_camera_diffuse_light(world_data, normal_viewport);
70   #endif
71
72   #ifdef STUDIOLIGHT_ORIENTATION_WORLD
73         vec3 normal_world = normalWorldMatrix * normal_viewport;
74         vec3 diffuse_light = get_world_diffuse_light(world_data, normal_world);
75   #endif
76         vec3 shaded_color = diffuse_light * diffuse_color.rgb + specular_color;
77
78 #else /* V3D_LIGHTING_STUDIO */
79         vec3 shaded_color = diffuse_color.rgb + specular_color;
80
81 #endif /* V3D_LIGHTING_STUDIO */
82
83 #ifdef V3D_SHADING_SHADOW
84         float light_factor = -dot(normal_viewport, world_data.light_direction_vs.xyz);
85         /* The step function might be ok for meshes but it's
86          * clearly not the case for hairs. Do smoothstep in this case. */
87         float shadow_mix = (diffuse_color.a == 1.0 || diffuse_color.a == 0.0)
88                                 ? step(-shadowShift, -light_factor)
89                                 : smoothstep(1.0, shadowShift, light_factor);
90         float light_multiplier = mix(lightMultiplier, shadowMultiplier, shadow_mix);
91
92 #else /* V3D_SHADING_SHADOW */
93         float light_multiplier = 1.0;
94 #endif /* V3D_SHADING_SHADOW */
95
96         shaded_color *= light_multiplier;
97
98 #ifdef V3D_SHADING_OBJECT_OUTLINE
99         shaded_color = mix(world_data.object_outline_color.rgb, shaded_color, object_outline);
100 #endif /* V3D_SHADING_OBJECT_OUTLINE */
101         fragColor = vec4(shaded_color, 1.0);
102 }