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

volume.h « geom « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3510a905def45ac2802ad6760947afd2c975c70d (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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

/* Volume Primitive
 *
 * Volumes are just regions inside meshes with the mesh surface as boundaries.
 * There isn't as much data to access as for surfaces, there is only a position
 * to do lookups in 3D voxel or procedural textures.
 *
 * 3D voxel textures can be assigned as attributes per mesh, which means the
 * same shader can be used for volume objects with different densities, etc. */

#pragma once

CCL_NAMESPACE_BEGIN

#ifdef __VOLUME__

/* Return position normalized to 0..1 in mesh bounds */

ccl_device_inline float3 volume_normalized_position(KernelGlobals kg,
                                                    ccl_private const ShaderData *sd,
                                                    float3 P)
{
  /* todo: optimize this so it's just a single matrix multiplication when
   * possible (not motion blur), or perhaps even just translation + scale */
  const AttributeDescriptor desc = find_attribute(kg, sd, ATTR_STD_GENERATED_TRANSFORM);

  object_inverse_position_transform(kg, sd, &P);

  if (desc.offset != ATTR_STD_NOT_FOUND) {
    Transform tfm = primitive_attribute_matrix(kg, sd, desc);
    P = transform_point(&tfm, P);
  }

  return P;
}

ccl_device float volume_attribute_value_to_float(const float4 value)
{
  return average(float4_to_float3(value));
}

ccl_device float volume_attribute_value_to_alpha(const float4 value)
{
  return value.w;
}

ccl_device float3 volume_attribute_value_to_float3(const float4 value)
{
  if (value.w > 1e-6f && value.w != 1.0f) {
    /* For RGBA colors, unpremultiply after interpolation. */
    return float4_to_float3(value) / value.w;
  }
  else {
    return float4_to_float3(value);
  }
}

ccl_device float4 volume_attribute_float4(KernelGlobals kg,
                                          ccl_private const ShaderData *sd,
                                          const AttributeDescriptor desc)
{
  if (desc.element & (ATTR_ELEMENT_OBJECT | ATTR_ELEMENT_MESH)) {
    return kernel_data_fetch(attributes_float4, desc.offset);
  }
  else if (desc.element == ATTR_ELEMENT_VOXEL) {
    /* todo: optimize this so we don't have to transform both here and in
     * kernel_tex_image_interp_3d when possible. Also could optimize for the
     * common case where transform is translation/scale only. */
    float3 P = sd->P;
    object_inverse_position_transform(kg, sd, &P);
    InterpolationType interp = (sd->flag & SD_VOLUME_CUBIC) ? INTERPOLATION_CUBIC :
                                                              INTERPOLATION_NONE;
    return kernel_tex_image_interp_3d(kg, desc.offset, P, interp);
  }
  else {
    return zero_float4();
  }
}

#endif

CCL_NAMESPACE_END