Blender V5.3
eevee_light_shape_display.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
12
13namespace eevee::light {
14
22
26
28{
29 if (is_area_light(light.type)) {
30 return transform_point(light.object_to_world, float3(quad_pos * light.area.size, 0.0f));
31 }
32
33 float radius = light.local.local.shape_radius;
34 float3 center = light.position();
35 float3 view_right = view.viewinv[0].xyz;
36 float3 view_up = view.viewinv[1].xyz;
37 float3 L = center - view.position();
38 float dist = length(L);
39 if (dist > 1e-8f) {
40 L /= dist;
41 make_orthonormal_basis(L, view_right, view_up);
42 }
43
44 if (is_sphere_light(light.type)) {
45 if (dist > radius) {
46 radius = light_sphere_disk_radius(radius, dist);
47 }
48 else {
49 radius = view.far();
50 }
51 }
52
53 return center + (view_right * quad_pos.x + view_up * quad_pos.y) * radius;
54}
55
57{
58 return view.point_ndc_to_world(float3(ndc_pos, 1.0f));
59}
60
62{
63 int quad_vertex_id = vertex_id % 6;
64 float x = (quad_vertex_id == 0 || quad_vertex_id == 2 || quad_vertex_id == 5) ? -1.0f : 1.0f;
65 float y = (quad_vertex_id == 0 || quad_vertex_id == 1 || quad_vertex_id == 3) ? -1.0f : 1.0f;
66 return float2(x, y);
67}
68
69[[vertex]] [[clip_control]]
71 [[resource_table]] const LightRenderData &lrd,
72 [[vertex_id]] const int vertex_id,
73 [[out]] ShapeDisplayVertOut &v_out,
74 [[position]] float4 &out_position)
75{
77 v_out.P = float3(0.0f);
78 v_out.radiance = float3(0.0f);
79 v_out.light_index = 0;
80 v_out.light_type = uint(LIGHT_RECT);
81 out_position = float4(0.0f, 0.0f, 0.0f, 1.0f);
82
83 int draw_index = vertex_id / 6;
84 int visible_count = int(lrd.light_cull_buf.visible_count + lrd.light_cull_buf.sun_lights_len);
85
86 if (draw_index >= visible_count) {
87 return;
88 }
89
90 int light_index;
91 if (draw_index < int(lrd.light_cull_buf.visible_count)) {
92 light_index = draw_index;
93 }
94 else {
95 int offset = draw_index - int(lrd.light_cull_buf.visible_count);
96 light_index = int(lrd.light_cull_buf.local_lights_len) + offset;
97 }
98
99 LightData light = lrd.light_buf[light_index];
100 if ((light.flags & LIGHT_CAMERA_HIDDEN) != 0u) {
101 return;
102 }
103
104 v_out.light_type = uint(light.type);
105 v_out.light_index = light_index;
106 v_out.radiance = light.color * light.shape_power;
107
108 const ViewMatrices view = views.get(0);
109 if (is_sun_light(light.type)) {
110 v_out.P = shape_display_far_plane_position_get(view, v_out.lP);
111 out_position = reverse_z::transform(float4(v_out.lP, 1.0f, 1.0f));
112 return;
113 }
114
115 v_out.P = shape_display_light_position_get(view, light, v_out.lP);
116 out_position = reverse_z::transform(view.point_world_to_homogenous(v_out.P));
117}
118
119[[fragment]]
121 [[resource_table]] const LightRenderData &lrd,
122 [[resource_table]] const UnifiedVolumeData &volumes,
123 [[resource_table]] const Uniform &uni,
124 [[frag_coord]] const float4 frag_co,
125 [[in]] const ShapeDisplayVertOut &v_out,
126 [[out]] ShapeDisplayFragOut &frag_out)
127{
128 eLightType light_type = eLightType(v_out.light_type);
129 LightData light = lrd.light_buf[v_out.light_index];
130 float3 P = v_out.P;
131
132 const ViewMatrices view = views.get(0);
134
135 if (is_sun_light(light_type)) {
136 float3 sun_direction = light.sun.direction;
137 float sun_cos = cos_from_tan(light.sun.shape_radius);
138 if (dot(-V, sun_direction) < sun_cos) {
140 return;
141 }
142 }
143 else {
144 if (is_area_light(light_type) && dot(V, light.z_axis()) > 0.0f) {
146 return;
147 }
148
149 bool is_circle = light_type == LIGHT_ELLIPSE || is_point_light(light_type);
150 if (is_circle && dot(v_out.lP, v_out.lP) > 1.0f) {
152 return;
153 }
154 }
155
156 float depth = reverse_z::read(frag_co.z);
157
158 float2 uvs = frag_co.xy * uni.uniform_buf.volumes.main_view_extent_inv;
159 float3 radiance = v_out.radiance;
160 if (is_spot_light(light_type)) {
161 radiance *= light_spot_attenuation(light, -V);
162 }
163
164 VolumeResolveSample vol = volumes.resolve(float3(uvs, depth));
165 frag_out.out_color = float4(radiance * vol.transmittance, 1.0f);
166}
167
169
170} // namespace eevee::light
void make_orthonormal_basis(float3 N, float3 &T, float3 &B)
float light_spot_attenuation(LightData light, float3 L)
float light_sphere_disk_radius(float sphere_radius, float distance_to_sphere)
#define position
#define flat
#define frag_coord
#define smooth
#define resource_table
#define fragment
#define vertex_id
#define clip_control
#define vertex
#define frag_color(slot)
void gpu_discard_fragment()
float dot(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
float cos_from_tan(float t)
float3 transform_point(float3x3 mat, float3 point)
#define L
void shape_display_frag(const draw::View &views, const LightRenderData &lrd, const UnifiedVolumeData &volumes, const Uniform &uni, const float4 frag_co, const ShapeDisplayVertOut &v_out, ShapeDisplayFragOut &frag_out)
float3 shape_display_far_plane_position_get(const ViewMatrices view, float2 ndc_pos)
void shape_display_vert(const draw::View &views, const LightRenderData &lrd, const int vertex_id, ShapeDisplayVertOut &v_out, float4 &out_position)
float3 shape_display_light_position_get(const ViewMatrices view, LightData light, float2 quad_pos)
PipelineGraphic shape_display(shape_display_vert, shape_display_frag)
float2 shape_display_quad_position_get(int vertex_id)
T read(T depth_buffer_value)
float4 transform(float4 hs_position)
float3 sun_direction(float sun_cos_theta)
Definition sky_math.h:457
float4 point_world_to_homogenous(float3 P) const
float3 point_ndc_to_world(float3 ssP) const
float far() const
float3 position() const
float3 world_incident_vector(float3 P) const
float x
float y
CCL_NAMESPACE_BEGIN struct Window V