Blender V5.3
blender/light.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/light.h"
6
7#include "DNA_light_types.h"
8
9#include "DNA_world_types.h"
10#include "blender/sync.h"
11#include "blender/util.h"
12#include "scene/object.h"
13
15
16void BlenderSync::sync_light(BObjectInfo &b_ob_info, Light *light)
17{
18 blender::Light &b_light = *blender::id_cast<blender::Light *>(b_ob_info.object_data);
19
20 light->name = b_light.id.name + 2;
21
22 if (PointLight *point_light = dynamic_cast<PointLight *>(light)) {
23 point_light->set_radius(b_light.radius);
24 point_light->set_is_sphere(!(b_light.mode & blender::LA_USE_SOFT_FALLOFF));
25 }
26 else if (AreaLight *area_light = dynamic_cast<AreaLight *>(light)) {
27 area_light->set_sizeu(b_light.area_size);
28 area_light->set_spread(b_light.area_spread);
29 if (b_light.area_shape == blender::LA_AREA_SQUARE ||
30 b_light.area_shape == blender::LA_AREA_DISK)
31 {
32 area_light->set_sizev(area_light->get_sizeu());
33 }
34 else {
35 area_light->set_sizev(b_light.area_sizey);
36 }
37 area_light->set_ellipse(b_light.area_shape == blender::LA_AREA_DISK ||
38 b_light.area_shape == blender::LA_AREA_ELLIPSE);
39 }
40 else if (SunLight *sun_light = dynamic_cast<SunLight *>(light)) {
41 sun_light->set_angle(b_light.sun_angle);
42 }
43 if (SpotLight *spot_light = dynamic_cast<SpotLight *>(light)) {
44 spot_light->set_angle(b_light.spotsize);
45 spot_light->set_smooth(b_light.spotblend);
46 }
47
48 blender::PointerRNA light_rna_ptr = RNA_id_pointer_create(&b_light.id);
49
50 /* Color and strength. */
51 float3 light_color = make_float3(b_light.r, b_light.g, b_light.b);
52 if (b_light.mode & blender::LA_USE_TEMPERATURE) {
53 float color[3];
54 RNA_float_get_array(&light_rna_ptr, "temperature_color", color);
55 light_color *= make_float3(color[0], color[1], color[2]);
56 }
57
58 const float3 strength = light_color * b_light.energy * exp2f(b_light.exposure);
59 light->set_strength(strength);
60
61 /* normalize */
62 light->set_normalize(!(b_light.mode & blender::LA_UNNORMALIZED));
63
64 /* shadow */
65 blender::PointerRNA clight = RNA_pointer_get(&light_rna_ptr, "cycles");
66 light->set_cast_shadow(b_light.mode & blender::LA_SHADOW);
67 light->set_use_mis(get_boolean(clight, "use_multiple_importance_sampling"));
68
69 /* caustics light */
70 light->set_use_caustics(get_boolean(clight, "is_caustics_light"));
71
72 light->set_max_bounces(get_int(clight, "max_bounces"));
73
74 if (AreaLight *area_light = dynamic_cast<AreaLight *>(light)) {
75 area_light->set_is_portal(get_boolean(clight, "is_portal"));
76 }
77
78 /* tag */
79 light->tag_update(scene);
80}
81
82void BlenderSync::sync_background_light(blender::bScreen *b_screen, blender::View3D *b_v3d)
83{
84 blender::World *b_world = view_layer.world_override ? view_layer.world_override : b_scene->world;
85
86 if (b_world) {
87 blender::PointerRNA world_rna_ptr = RNA_id_pointer_create(&b_world->id);
88 blender::PointerRNA cworld = RNA_pointer_get(&world_rna_ptr, "cycles");
89
90 enum SamplingMethod { SAMPLING_NONE = 0, SAMPLING_AUTOMATIC, SAMPLING_MANUAL, SAMPLING_NUM };
91 const int sampling_method = get_enum(
92 cworld, "sampling_method", SAMPLING_NUM, SAMPLING_AUTOMATIC);
93 const bool sample_as_light = (sampling_method != SAMPLING_NONE);
94
95 /* Create object. */
96 Object *object;
97 const ObjectKey object_key(b_world, nullptr, b_world, false);
98 bool update = object_map.add_or_update(&object, &b_world->id, &b_world->id, object_key);
99 if (update) {
100 /* Lights should be shadow catchers by default. */
101 object->set_is_shadow_catcher(true);
102 }
103
104 object->set_lightgroup(
105 ustring((b_world && b_world->lightgroup) ? b_world->lightgroup->name : ""));
106 object->set_asset_name(ustring(b_world->id.name + 2));
107
108 /* Create geometry. */
109 const GeometryKey geom_key{b_world, Geometry::BACKGROUND_LIGHT};
110 Geometry *geom = geometry_map.find(geom_key);
111 if (geom) {
112 update |= geometry_map.update(geom, &b_world->id);
113 }
114 else {
115 geom = scene->create_light_node<BackgroundLight>();
116 geometry_map.add(geom_key, geom);
117 object->set_geometry(geom);
118 update = true;
119 }
120
121 if (update || world_recalc || b_world != world_map) {
122 /* Initialize light geometry. */
123 BackgroundLight *light = static_cast<BackgroundLight *>(geom);
124
125 array<Node *> used_shaders;
126 used_shaders.push_back_slow(scene->default_background);
127 light->set_used_shaders(used_shaders);
128
129 if (sampling_method == SAMPLING_MANUAL) {
130 light->set_map_resolution(get_int(cworld, "sample_map_resolution"));
131 }
132 else {
133 light->set_map_resolution(0);
134 }
135
136 light->set_use_mis(sample_as_light);
137 light->set_max_bounces(get_int(cworld, "max_bounces"));
138
139 /* Caustic light. */
140 light->set_use_caustics(get_boolean(cworld, "is_caustics_light"));
141
142 light->set_cast_shadow(get_boolean(cworld, "use_shadows"));
143
144 light->tag_update(scene);
145
146 geometry_map.set_recalc(b_world);
147 }
148 }
149
150 world_map = b_world;
151 world_recalc = false;
152 viewport_parameters = BlenderViewportParameters(b_screen, b_v3d, use_developer_ui);
153}
154
void tag_update(Scene *scene)
void push_back_slow(const T &t)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
VecBase< float, 3 > float3
PointerRNA RNA_id_pointer_create(ID *id)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
@ LA_USE_SOFT_FALLOFF
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
blender::ID * object_data
ustring name
Definition graph/node.h:178