Blender V5.3
eevee_light_lib.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
12
13/* Attenuation cutoff needs to be the same in the shadow loop and the light eval loop. */
14#define LIGHT_ATTENUATION_THRESHOLD 1e-6f
15
16/* ---------------------------------------------------------------------- */
19
21 /* Unit world space light vector, from the shading point to the light center. */
23 /* Distance from the shading point to the light center. */
24 float dist;
25
26 /* Construct a vector to the light center. */
27 static LightVector get(LightData light, const bool is_directional, float3 P)
28 {
29 if (is_directional) {
30 return LightVector{.L = light.sun.direction, .dist = 1.0f};
31 }
32 LightVector lv;
33 lv.L = normalize_and_get_length(light.position() - P, lv.dist);
34 return lv;
35 }
36
37 /* Construct a vector to the closest point in the light shape. */
38 static LightVector get_shape_closest(LightData light, const bool is_directional, float3 P)
39 {
40 if (!is_directional && is_area_light(light.type)) {
41 LightAreaData area = light.area;
42
43 float3 lP = transform_point_inversed(light.object_to_world, P);
44 float2 ls_closest_point = lP.xy;
45 if (light.type == LIGHT_ELLIPSE) {
46 ls_closest_point /= max(1.0f, length(ls_closest_point / area.size));
47 }
48 else {
49 ls_closest_point = clamp(ls_closest_point, -area.size, area.size);
50 }
51 float3 ws_closest_point = transform_point(light.object_to_world,
52 float3(ls_closest_point, 0.0f));
53
54 LightVector lv;
55 lv.L = normalize_and_get_length(ws_closest_point - P, lv.dist);
56 return lv;
57 }
58
59 /* TODO(@fclem): other light shape? */
60 return LightVector::get(light, is_directional, P);
61 }
62};
63
65{
66 return transform_direction_transposed(light.object_to_world, L);
67}
69{
70 return transform_point_inversed(light.object_to_world, point);
71}
72
73/* From Frostbite PBR Course
74 * Distance based attenuation
75 * http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf */
76float light_influence_attenuation(float dist, float inv_sqr_influence)
77{
78 float factor = square(dist) * inv_sqr_influence;
79 float fac = saturate(1.0f - square(factor));
80 return square(fac);
81}
82
83float light_spot_attenuation(LightData light, float3 L)
84{
85 LightSpotData spot = light.spot;
87 float ellipse = inversesqrt(1.0f + length_squared(lL.xy * spot.spot_size_inv / lL.z));
88 float spotmask = smoothstep(0.0f, 1.0f, ellipse * spot.spot_mul + spot.spot_bias);
89 return (lL.z > 0.0f) ? spotmask : 0.0f;
90}
91
92float light_attenuation_common(LightData light, const bool is_directional, float3 L)
93{
94 if (is_directional) {
95 return 1.0f;
96 }
97 if (is_spot_light(light.type)) {
98 return light_spot_attenuation(light, L);
99 }
100 if (is_area_light(light.type)) {
101 return float(dot(L, light.z_axis()) > 0.0f);
102 }
103 return 1.0f;
104}
105
106float light_shape_radius(LightData light)
107{
108 if (is_sun_light(light.type)) {
109 return light.sun.shape_radius;
110 }
111 if (is_area_light(light.type)) {
112 return length(light.area.size);
113 }
114 return light.local.local.shape_radius;
115}
116
123 LightData light, float3 L, float distance_to_light, float3 Ng, const bool is_transmission)
124{
125 /* Sine of angle between light center and light edge. */
126 float sin_solid_angle = light_shape_radius(light) / distance_to_light;
127 /* Sine of angle between light center and shading plane. */
128 float sin_light_angle = dot(L, is_transmission ? -Ng : Ng);
129 /* Do attenuation after the horizon line to avoid harsh cut
130 * or biasing of surfaces without light bleeding. */
131 float dist = sin_solid_angle + (is_transmission ? -sin_light_angle : sin_light_angle);
132 return saturate((dist + 0.1f) * 10.0f);
133}
134
135float light_attenuation_surface(LightData light, const bool is_directional, LightVector lv)
136{
137 float result = light_attenuation_common(light, is_directional, lv.L);
138 if (!is_directional) {
140 light.local.local.influence_radius_invsqr_surface);
141 }
142 return result;
143}
144
145float light_attenuation_volume(LightData light, const bool is_directional, LightVector lv)
146{
147 float result = light_attenuation_common(light, is_directional, lv.L);
148 if (!is_directional) {
150 light.local.local.influence_radius_invsqr_volume);
151 }
152 return result;
153}
154
155/* Cheaper alternative than evaluating the LTC.
156 * The result needs to be multiplied by BSDF or Phase Function. */
157float light_point_light(LightData light, const bool is_directional, LightVector lv)
158{
159 if (is_directional) {
160 return 1.0f;
161 }
162 /* Using "Point Light Attenuation Without Singularity" from Cem Yuksel
163 * http://www.cemyuksel.com/research/pointlightattenuation/pointlightattenuation.pdf
164 * http://www.cemyuksel.com/research/pointlightattenuation/
165 */
166 float d_sqr = square(lv.dist);
167 float r_sqr = square(light.local.local.shape_radius);
168 /* Using reformulation that has better numerical precision. */
169 float power = 2.0f / (d_sqr + r_sqr + lv.dist * sqrt(d_sqr + r_sqr));
170
171 if (is_area_light(light.type)) {
172 /* Modulate by light plane orientation / solid angle. */
173 power *= saturate(dot(light.z_axis(), lv.L));
174 }
175 return power;
176}
177
183float light_sphere_disk_radius(float sphere_radius, float distance_to_sphere)
184{
185 /* The sine of the half-angle spanned by a sphere light is equal to the tangent of the
186 * half-angle spanned by a disk light with the same radius. */
187 return sphere_radius *
188 inversesqrt(max(1e-8f, 1.0f - square(sphere_radius / distance_to_sphere)));
189}
190
196 /* Corner vertices. */
198 /* Normal defining light plane. */
200 /* Radius of disk encapsulating shape on light plane,
201 * if the disk is projected on the unit sphere. */
203
204 static LightShape get(LightData light, LightVector lv)
205 {
206 LightShape shape;
207
208 float3 Px = light.x_axis();
209 float3 Py = light.y_axis();
210
211 if (light.type == LIGHT_RECT) {
212 LightAreaData area = light.area;
213
214 shape.v[0] = Px * area.size.x + Py * -area.size.y;
215 shape.v[1] = Px * area.size.x + Py * area.size.y;
216 shape.v[2] = -shape.v[0];
217 shape.v[3] = -shape.v[1];
218
219 float3 L = lv.L * lv.dist;
220 shape.v[0] += L;
221 shape.v[1] += L;
222 shape.v[2] += L;
223 shape.v[3] += L;
224
225 shape.N = cross(Px, Py); /* Why N != light.z_axis()? */
226 shape.disk_radius_projected = 0.5f * distance(shape.v[0], shape.v[2]) / lv.dist;
227 }
228 else {
229 if (!is_area_light(light.type)) {
230 make_orthonormal_basis(lv.L, Px, Py);
231 }
232
233 float2 size;
234 if (is_sphere_light(light.type)) {
235 /* Spherical omni or spot light. */
236 size = float2(light_sphere_disk_radius(light.local.local.shape_radius, lv.dist));
237 }
238 else if (is_oriented_disk_light(light.type)) {
239 /* View direction-aligned disk. */
240 size = float2(light.local.local.shape_radius);
241 }
242 else if (is_sun_light(light.type)) {
243 size = float2(light.sun.shape_radius);
244 }
245 else {
246 /* Area light. */
247 size = float2(light.area.size);
248 }
249
250 shape.v[0] = Px * -size.x + Py * -size.y;
251 shape.v[1] = Px * size.x + Py * -size.y;
252 shape.v[2] = -shape.v[0];
253
254 float3 L = lv.L * lv.dist;
255 shape.v[0] += L;
256 shape.v[1] += L;
257 shape.v[2] += L;
258
259 shape.N = cross(Px, Py); /* Why N != light.z_axis()? */
260 shape.disk_radius_projected = max(size.x, size.y) / lv.dist;
261 }
262
263 return shape;
264 }
265};
266
267namespace eevee::light {
268
269float power_get(LightData light, LightingType type)
270{
271 /* Mask anything above 3. See LIGHT_TRANSLUCENT_WITH_THICKNESS. */
272 return light.power_factor[type & 3u] *
273 (type != LIGHT_VOLUME ? light.shape_power : light.point_power);
274}
275
276bool light_linking_affects_receiver(uint2 light_set_membership, uchar receiver_light_set)
277{
278 return bitmask64_test(light_set_membership, receiver_light_set);
279}
280
281} // namespace eevee::light
282
btScalar max
void make_orthonormal_basis(float3 N, float3 &T, float3 &B)
float light_attenuation_facing(LightData light, float3 L, float distance_to_light, float3 Ng, const bool is_transmission)
float light_point_light(LightData light, const bool is_directional, LightVector lv)
float light_shape_radius(LightData light)
float light_spot_attenuation(LightData light, float3 L)
float light_influence_attenuation(float dist, float inv_sqr_influence)
float light_sphere_disk_radius(float sphere_radius, float distance_to_sphere)
float light_attenuation_volume(LightData light, const bool is_directional, LightVector lv)
float light_attenuation_surface(LightData light, const bool is_directional, LightVector lv)
float3 light_world_to_local_point(LightData light, float3 point)
float light_attenuation_common(LightData light, const bool is_directional, float3 L)
float3 light_world_to_local_direction(LightData light, float3 L)
T smoothstep(T, T, T) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T inversesqrt(T) RET
float distance(float, float) RET
#define sqrt
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
unsigned int uchar
float square(float v)
float3 transform_point(float3x3 mat, float3 point)
VecT normalize_and_get_length(VecT vector, float &out_length)
float length_squared(VecT a)
bool bitmask64_test(uint2 bitmask, uint bit_index)
ccl_device_inline float2 power(const float2 v, const float e)
#define L
VecBase< float, 2 > float2
VecBase< float, 3 > float3
float power_get(LightData light, LightingType type)
bool light_linking_affects_receiver(uint2 light_set_membership, uchar receiver_light_set)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
static LightShape get(LightData light, LightVector lv)
static LightVector get(LightData light, const bool is_directional, float3 P)
static LightVector get_shape_closest(LightData light, const bool is_directional, float3 P)
float z
Definition sky_math.h:136
ccl_device_inline T transform_direction_transposed(const ccl_private Transform *t, const T a)
Definition transform.h:176