Debian Codegraph @

src/scene/glsl/source/bump_mapping_steep_parallax_shadowing.fs

raw
1 /* Steep parallax with self-shading bump mapping shader effect.
2 This is added right after bump_mapping.fs and bump_mapping_parallax.fs.
3 Included by EnableShaderBumpMapping in ../castlerendererinternalshader.pas unit.
4 */
5
6 // avoid redeclaring this when no "separate compilation units" (OpenGLES)
7 #ifndef GL_ES
8 uniform float castle_parallax_bm_scale;
9 uniform sampler2D castle_normal_map;
10 float castle_bm_height;
11 vec2 castle_parallax_tex_coord;
12 #endif
13
14 varying vec3 castle_light_direction_tangent_space;
15
16 /* This has to be done after PLUG texture_coord_shift (done from PLUG main_texture_apply),
17 as we depend that global castle_bm_height/castle_parallax_tex_coord
18 are already set correctly. */
19 void PLUG_steep_parallax_shadow_apply(inout vec4 fragment_color)
20 {
21 vec3 light_dir = normalize(castle_light_direction_tangent_space);
22
23 /* We basically do the same thing as when we calculate tex_coord
24 with steep parallax mapping.
25 Only now we increment height, and we use light_dir instead of
26 v_to_eye. */
27 float num_steps = mix(30.0, 10.0, light_dir.z);
28
29 float step = 1.0 / num_steps;
30
31 vec2 delta = light_dir.xy * castle_parallax_bm_scale / (light_dir.z * num_steps);
32
33 /* Do the 1st step always, otherwise initial height = shadow_map_height
34 and we would be considered in our own shadow. */
35 float height = castle_bm_height + step;
36 vec2 shadow_texture_coord = castle_parallax_tex_coord + delta;
37 float shadow_map_height = texture2D(castle_normal_map, shadow_texture_coord).a;
38
39 while (shadow_map_height < height && height < 1.0)
40 {
41 height += step;
42 shadow_texture_coord += delta;
43 shadow_map_height = texture2D(castle_normal_map, shadow_texture_coord).a;
44 }
45
46 if (shadow_map_height >= height)
47 {
48 /* TODO: setting appropriate light contribution to 0 would be more correct.
49 But for now, this self-shadowing is hacky, always from light source 0,
50 and after the light calculation is actually done. */
51 fragment_color.rgb /= 2.0;
52 }
53 }