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

draw_resource_finalize_comp.glsl « shaders « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 511d4e496510e1645ddcf8578b437b40cf609f65 (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

/**
 * Finish computation of a few draw resource after sync.
 */

#pragma BLENDER_REQUIRE(common_math_lib.glsl)

void main()
{
  uint resource_id = gl_GlobalInvocationID.x;
  if (resource_id >= resource_len) {
    return;
  }

  mat4 model_mat = matrix_buf[resource_id].model;
  ObjectInfos infos = infos_buf[resource_id];
  ObjectBounds bounds = bounds_buf[resource_id];

  if (bounds.bounding_sphere.w != -1.0) {
    /* Convert corners to origin + sides in world space. */
    vec3 p0 = bounds.bounding_corners[0].xyz;
    vec3 p01 = bounds.bounding_corners[1].xyz - p0;
    vec3 p02 = bounds.bounding_corners[2].xyz - p0;
    vec3 p03 = bounds.bounding_corners[3].xyz - p0;
    /* Avoid flat box. */
    p01.x = max(p01.x, 1e-4);
    p02.y = max(p02.y, 1e-4);
    p03.z = max(p03.z, 1e-4);
    vec3 diagonal = p01 + p02 + p03;
    vec3 center = p0 + diagonal * 0.5;
    float min_axis = min_v3(abs(diagonal));
    bounds_buf[resource_id].bounding_sphere.xyz = transform_point(model_mat, center);
    /* We have to apply scaling to the diagonal. */
    bounds_buf[resource_id].bounding_sphere.w = length(transform_direction(model_mat, diagonal)) *
                                                0.5;
    bounds_buf[resource_id]._inner_sphere_radius = min_axis;
    bounds_buf[resource_id].bounding_corners[0].xyz = transform_point(model_mat, p0);
    bounds_buf[resource_id].bounding_corners[1].xyz = transform_direction(model_mat, p01);
    bounds_buf[resource_id].bounding_corners[2].xyz = transform_direction(model_mat, p02);
    bounds_buf[resource_id].bounding_corners[3].xyz = transform_direction(model_mat, p03);
    /* Always have correct handedness in the corners vectors. */
    if (flag_test(infos.flag, OBJECT_NEGATIVE_SCALE)) {
      bounds_buf[resource_id].bounding_corners[0].xyz +=
          bounds_buf[resource_id].bounding_corners[1].xyz;
      bounds_buf[resource_id].bounding_corners[1].xyz =
          -bounds_buf[resource_id].bounding_corners[1].xyz;
    }

    /* TODO: Bypass test for very large objects (see T67319). */
    if (bounds_buf[resource_id].bounding_sphere.w > 1e12) {
      bounds_buf[resource_id].bounding_sphere.w = -1.0;
    }
  }

  vec3 loc = infos.orco_add;  /* Box center. */
  vec3 size = infos.orco_mul; /* Box half-extent. */
  /* This is what the original computation looks like.
   * Simplify to a nice MADD in shading code. */
  // orco = (pos - loc) / size;
  // orco = pos * (1.0 / size) + (-loc / size);
  vec3 size_inv = safe_rcp(size);
  infos_buf[resource_id].orco_add = -loc * size_inv;
  infos_buf[resource_id].orco_mul = size_inv;
}