Welcome to mirror list, hosted at ThFree Co, Russian Federation.

eevee_shadow_lib.glsl « shaders « eevee « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b08d870311081131c3cc1fedf3f12302b83fd2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

#pragma BLENDER_REQUIRE(eevee_shadow_tilemap_lib.glsl)
#pragma BLENDER_REQUIRE(eevee_shadow_page_lib.glsl)

/* ---------------------------------------------------------------------- */
/** \name Shadow Sampling Functions
 * \{ */

/* Turns local light coordinate into shadow region index. Matches eCubeFace order. */
int shadow_punctual_face_index_get(vec3 lL)
{
  vec3 aP = abs(lL);
  if (all(greaterThan(aP.xx, aP.yz))) {
    return (lL.x > 0.0) ? 1 : 2;
  }
  else if (all(greaterThan(aP.yy, aP.xz))) {
    return (lL.y > 0.0) ? 3 : 4;
  }
  else {
    return (lL.z > 0.0) ? 5 : 0;
  }
}

/* Transform vector to face local coordinate. */
vec3 shadow_punctual_local_position_to_face_local(int face_id, vec3 lL)
{
  switch (face_id) {
    case 1:
      return vec3(-lL.y, lL.z, -lL.x);
    case 2:
      return vec3(lL.y, lL.z, lL.x);
    case 3:
      return vec3(lL.x, lL.z, -lL.y);
    case 4:
      return vec3(-lL.x, lL.z, lL.y);
    case 5:
      return vec3(lL.x, -lL.y, -lL.z);
    default:
      return lL;
  }
}

float shadow_punctual_depth_get(
    sampler2D atlas_tx, usampler2D tilemaps_tx, LightData light, ShadowData shadow, vec3 lL)
{
  lL -= shadow.offset;
  int face_id = shadow_punctual_face_index_get(lL);
  lL = shadow_punctual_local_position_to_face_local(face_id, lL);
  /* UVs in [0..SHADOW_TILEMAP_RES] range. */
  const float lod0_res = float(SHADOW_TILEMAP_RES / 2);
  vec2 uv = (lL.xy / abs(lL.z)) * lod0_res + lod0_res;
  ivec2 tile_co = ivec2(floor(uv));
  int tilemap_index = shadow.tilemap_index + face_id;
  ShadowTileData tile = shadow_tile_load(tilemaps_tx, tile_co, 0, tilemap_index);

  float depth = 1.0;
  if ((tilemap_index <= shadow.tilemap_last) && (tile.is_allocated || tile.lod > 0)) {
    vec2 shadow_uv = shadow_page_uv_transform(tile.page, tile.lod, uv);
    depth = texture(atlas_tx, shadow_uv).r;
  }
  return depth;
}

float shadow_directional_depth_get(sampler2D atlas_tx,
                                   usampler2D tilemaps_tx,
                                   LightData light,
                                   ShadowData shadow,
                                   vec3 camera_P,
                                   vec3 lP,
                                   vec3 P)
{
  int clipmap_lod = shadow_directional_clipmap_level(shadow, distance(P, camera_P));
  int clipmap_lod_relative = clipmap_lod - shadow.clipmap_lod_min;
  int tilemap_index = clamp(
      shadow.tilemap_index + clipmap_lod_relative, shadow.tilemap_index, shadow.tilemap_last);
  /* Compute how many time we need to subdivide. */
  float clipmap_res_mul = float(1 << (shadow.clipmap_lod_max - clipmap_lod));
  /* Compute offset of the clipmap from the largest LOD. */
  vec2 clipmap_offset = vec2(abs(shadow.base_offset) >> clipmap_lod_relative) *
                        sign(shadow.base_offset);

  vec2 uv = (lP.xy * clipmap_res_mul - clipmap_offset) + float(SHADOW_TILEMAP_RES / 2);
  ivec2 tile_co = ivec2(floor(uv));
  ShadowTileData tile = shadow_tile_load(tilemaps_tx, tile_co, 0, tilemap_index);

  float depth = 1.0;
  if (tile.is_allocated) {
    vec2 shadow_uv = shadow_page_uv_transform(tile.page, 0, uv);
    depth = texture(atlas_tx, shadow_uv).r;
  }
  return depth;
}

/* Returns world distance delta from light between shading point and first occluder. */
float shadow_delta_get(sampler2D atlas_tx,
                       usampler2D tilemaps_tx,
                       LightData light,
                       ShadowData shadow,
                       vec3 lL,
                       float receiver_dist,
                       vec3 P)
{
  if (light.type == LIGHT_SUN) {
    /* [-SHADOW_TILEMAP_RES/2..SHADOW_TILEMAP_RES/2] range for highest LOD. */
    vec3 lP = transform_point(shadow.mat, P);
    float occluder_z = shadow_directional_depth_get(
        atlas_tx, tilemaps_tx, light, shadow, cameraPos, lP, P);
    /* Transform to world space distance. */
    return (lP.z - occluder_z) * abs(shadow.clip_far - shadow.clip_near);
  }
  else {
    float occluder_z = shadow_punctual_depth_get(atlas_tx, tilemaps_tx, light, shadow, lL);
    occluder_z = linear_depth(true, occluder_z, shadow.clip_far, shadow.clip_near);
    /* Take into account the cubemap projection. We want the radial distance. */
    float occluder_dist = receiver_dist * occluder_z / max_v3(abs(lL));
    return receiver_dist - occluder_dist;
  }
}

/** \} */