Workbench: Matcaps T55291
[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 #ifdef STUDIOLIGHT_ORIENTATION_VIEWNORMAL
15 uniform sampler2D matcapImage;
16 #endif
17
18 layout(std140) uniform world_block {
19         WorldData world_data;
20 };
21
22 void main()
23 {
24         ivec2 texel = ivec2(gl_FragCoord.xy);
25         vec2 uv_viewport = gl_FragCoord.xy * invertedViewportSize;
26         uint object_id = texelFetch(objectId, texel, 0).r;
27
28 #ifndef V3D_SHADING_OBJECT_OUTLINE
29         if (object_id == NO_OBJECT_ID) {
30                 fragColor = vec4(background_color(world_data, uv_viewport.y), 0.0);
31                 return;
32         }
33 #else /* !V3D_SHADING_OBJECT_OUTLINE */
34         float object_outline = calculate_object_outline(objectId, texel, object_id);
35
36         if (object_id == NO_OBJECT_ID) {
37                 vec3 background = background_color(world_data, uv_viewport.y);
38                 if (object_outline == 0.0) {
39                         fragColor = vec4(background, 0.0);
40                 }
41                 else {
42                         fragColor = vec4(mix(world_data.object_outline_color.rgb, background, object_outline), 1.0-object_outline);
43                 }
44                 return;
45         }
46 #endif /* !V3D_SHADING_OBJECT_OUTLINE */
47
48         vec4 diffuse_color = texelFetch(colorBuffer, texel, 0);
49
50 /* Do we need normals */
51 #ifdef NORMAL_VIEWPORT_PASS_ENABLED
52 #  ifdef WORKBENCH_ENCODE_NORMALS
53         vec3 normal_viewport = normal_decode(texelFetch(normalBuffer, texel, 0).rg);
54         if (diffuse_color.a == 0.0) {
55                 normal_viewport = -normal_viewport;
56         }
57 #  else /* WORKBENCH_ENCODE_NORMALS */
58         vec3 normal_viewport = texelFetch(normalBuffer, texel, 0).rgb;
59 #  endif /* WORKBENCH_ENCODE_NORMALS */
60 #endif
61
62 #ifdef STUDIOLIGHT_ORIENTATION_VIEWNORMAL
63         diffuse_color = texture(matcapImage, normal_viewport.xy / 2.0 + 0.5);
64 #endif
65
66 #ifdef V3D_SHADING_SPECULAR_HIGHLIGHT
67         /* XXX Should calculate the correct VS Incoming direction */
68         vec3 I_vs = vec3(0.0, 0.0, 1.0);
69         vec4 specular_data = texelFetch(specularBuffer, texel, 0);
70         vec3 specular_color = get_world_specular_lights(world_data, specular_data, normal_viewport, I_vs);
71 #else
72         vec3 specular_color = vec3(0.0);
73 #endif
74
75 #ifdef V3D_LIGHTING_FLAT
76         vec3 diffuse_light = vec3(1.0);
77 #endif
78
79 #ifdef V3D_LIGHTING_MATCAP
80         /* TODO: if pixel data is matcap. then */
81         vec3 diffuse_light = texelFetch(specularBuffer, texel, 0).rgb;
82 #endif
83
84 #ifdef V3D_LIGHTING_STUDIO
85 #  ifdef STUDIOLIGHT_ORIENTATION_CAMERA
86         vec3 diffuse_light = get_camera_diffuse_light(world_data, normal_viewport);
87 #  endif
88
89 #  ifdef STUDIOLIGHT_ORIENTATION_WORLD
90         vec3 normal_world = normalWorldMatrix * normal_viewport;
91         vec3 diffuse_light = get_world_diffuse_light(world_data, normal_world);
92 #  endif
93 #endif
94         vec3 shaded_color = diffuse_light * diffuse_color.rgb + specular_color;
95
96 #ifdef V3D_SHADING_SHADOW
97         float light_factor = -dot(normal_viewport, world_data.light_direction_vs.xyz);
98         /* The step function might be ok for meshes but it's
99          * clearly not the case for hairs. Do smoothstep in this case. */
100         float shadow_mix = (diffuse_color.a == 1.0 || diffuse_color.a == 0.0)
101                                 ? step(-shadowShift, -light_factor)
102                                 : smoothstep(1.0, shadowShift, light_factor);
103         float light_multiplier = mix(lightMultiplier, shadowMultiplier, shadow_mix);
104
105 #else /* V3D_SHADING_SHADOW */
106         float light_multiplier = 1.0;
107 #endif /* V3D_SHADING_SHADOW */
108
109         shaded_color *= light_multiplier;
110
111 #ifdef V3D_SHADING_OBJECT_OUTLINE
112         shaded_color = mix(world_data.object_outline_color.rgb, shaded_color, object_outline);
113 #endif /* V3D_SHADING_OBJECT_OUTLINE */
114         fragColor = vec4(shaded_color, 1.0);
115 }