Blender V5.3
usd_reader_light.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Tangent Animation. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "usd_reader_light.hh"
7
9
10#include "BKE_light.h"
11#include "BKE_object.hh"
12
13#include "DNA_light_types.h"
14#include "DNA_object_types.h"
15
16#include <pxr/usd/usdLux/diskLight.h>
17#include <pxr/usd/usdLux/distantLight.h>
18#include <pxr/usd/usdLux/rectLight.h>
19#include <pxr/usd/usdLux/shapingAPI.h>
20#include <pxr/usd/usdLux/sphereLight.h>
21
22namespace blender::io::usd {
23
25{
26 Light *blight = BKE_light_add(bmain, name_.c_str());
27
29 object_->data = id_cast<ID *>(blight);
30}
31
32void USDLightReader::read_object_data(Main *bmain, const pxr::UsdTimeCode time)
33{
34 Light *blight = id_cast<Light *>(object_->data);
35
36 if (blight == nullptr) {
37 return;
38 }
39
40 pxr::UsdLuxLightAPI light_api(prim_);
41 if (!light_api) {
42 return;
43 }
44
45 if (prim_.IsA<pxr::UsdLuxDiskLight>()) {
46 /* Disk area light. */
47 blight->type = LA_AREA;
48 blight->area_shape = LA_AREA_DISK;
49
50 pxr::UsdLuxDiskLight disk_light(prim_);
51 if (disk_light) {
52 if (pxr::UsdAttribute radius_attr = disk_light.GetRadiusAttr()) {
53 float radius = 0.0f;
54 if (radius_attr.Get(&radius, time)) {
55 blight->area_size = radius * 2.0f;
56 }
57 }
58 }
59 }
60 else if (prim_.IsA<pxr::UsdLuxRectLight>()) {
61 /* Rectangular area light. */
62 blight->type = LA_AREA;
63 blight->area_shape = LA_AREA_RECT;
64
65 pxr::UsdLuxRectLight rect_light(prim_);
66 if (rect_light) {
67 if (pxr::UsdAttribute width_attr = rect_light.GetWidthAttr()) {
68 float width = 0.0f;
69 if (width_attr.Get(&width, time)) {
70 blight->area_size = width;
71 }
72 }
73
74 if (pxr::UsdAttribute height_attr = rect_light.GetHeightAttr()) {
75 float height = 0.0f;
76 if (height_attr.Get(&height, time)) {
77 blight->area_sizey = height;
78 }
79 }
80 }
81 }
82 else if (prim_.IsA<pxr::UsdLuxSphereLight>()) {
83 /* Point and spot light. */
84 blight->type = LA_LOCAL;
85
86 pxr::UsdLuxSphereLight sphere_light(prim_);
87 if (sphere_light) {
88 pxr::UsdAttribute treatAsPoint_attr = sphere_light.GetTreatAsPointAttr();
89 bool treatAsPoint;
90 if (treatAsPoint_attr && treatAsPoint_attr.Get(&treatAsPoint, time) && treatAsPoint) {
91 blight->radius = 0.0f;
92 }
93 else if (pxr::UsdAttribute radius_attr = sphere_light.GetRadiusAttr()) {
94 float radius = 0.0f;
95 if (radius_attr.Get(&radius, time)) {
96 blight->radius = radius;
97 }
98 }
99 }
100
101 pxr::UsdLuxShapingAPI shaping_api = pxr::UsdLuxShapingAPI(prim_);
102 if (shaping_api && shaping_api.GetShapingConeAngleAttr().IsAuthored()) {
103 blight->type = LA_SPOT;
104
105 if (pxr::UsdAttribute cone_angle_attr = shaping_api.GetShapingConeAngleAttr()) {
106 float cone_angle = 0.0f;
107 if (cone_angle_attr.Get(&cone_angle, time)) {
108 blight->spotsize = DEG2RADF(cone_angle) * 2.0f;
109 }
110 }
111
112 if (pxr::UsdAttribute cone_softness_attr = shaping_api.GetShapingConeSoftnessAttr()) {
113 float cone_softness = 0.0f;
114 if (cone_softness_attr.Get(&cone_softness, time)) {
115 blight->spotblend = cone_softness;
116 }
117 }
118 }
119 }
120 else if (prim_.IsA<pxr::UsdLuxDistantLight>()) {
121 blight->type = LA_SUN;
122
123 pxr::UsdLuxDistantLight distant_light(prim_);
124 if (distant_light) {
125 if (pxr::UsdAttribute angle_attr = distant_light.GetAngleAttr()) {
126 float angle = 0.0f;
127 if (angle_attr.Get(&angle, time)) {
128 blight->sun_angle = DEG2RADF(angle * 2.0f);
129 }
130 }
131 }
132 }
133
134 /* Intensity */
135 if (pxr::UsdAttribute intensity_attr = light_api.GetIntensityAttr()) {
136 float intensity = 0.0f;
137 if (intensity_attr.Get(&intensity, time)) {
138 if (blight->type == LA_SUN) {
139 /* Unclear why, but approximately matches Karma. */
140 blight->energy = intensity * 4.0f;
141 }
142 else {
143 /* Convert from intensity to radiant flux. */
144 blight->energy = intensity * M_PI;
145 }
146 blight->energy *= this->import_params_.light_intensity_scale;
147 }
148 }
149
150 /* Exposure. */
151 if (pxr::UsdAttribute exposure_attr = light_api.GetExposureAttr()) {
152 float exposure = 0.0f;
153 if (exposure_attr.Get(&exposure, time)) {
154 blight->exposure = exposure;
155 }
156 }
157
158 /* Color. */
159 if (pxr::UsdAttribute color_attr = light_api.GetColorAttr()) {
160 pxr::GfVec3f color;
161 if (color_attr.Get(&color, time)) {
163 blight->r = color[0];
164 blight->g = color[1];
165 blight->b = color[2];
166 }
167 }
168
169 /* Temperature */
170 if (pxr::UsdAttribute enable_temperature_attr = light_api.GetEnableColorTemperatureAttr()) {
171 bool enable_temperature = false;
172 if (enable_temperature_attr.Get(&enable_temperature, time)) {
173 if (enable_temperature) {
174 blight->mode |= LA_USE_TEMPERATURE;
175 }
176 }
177 }
178
179 if (pxr::UsdAttribute color_temperature_attr = light_api.GetColorTemperatureAttr()) {
180 float color_temperature = 6500.0f;
181 if (color_temperature_attr.Get(&color_temperature, time)) {
182 blight->temperature = color_temperature;
183 }
184 }
185
186 /* Diffuse and Specular. */
187 if (pxr::UsdAttribute diff_attr = light_api.GetDiffuseAttr()) {
188 float diff_fac = 1.0f;
189 if (diff_attr.Get(&diff_fac, time)) {
190 blight->diff_fac = diff_fac;
191 }
192 }
193 if (pxr::UsdAttribute spec_attr = light_api.GetSpecularAttr()) {
194 float spec_fac = 1.0f;
195 if (spec_attr.Get(&spec_fac, time)) {
196 blight->spec_fac = spec_fac;
197 }
198 }
199
200 /* Normalize */
201 if (pxr::UsdAttribute normalize_attr = light_api.GetNormalizeAttr()) {
202 bool normalize = false;
203 if (normalize_attr.Get(&normalize, time)) {
204 if (!normalize) {
205 blight->mode |= LA_UNNORMALIZED;
206 }
207 }
208 }
209
211}
212
213} // namespace blender::io::usd
General operations, lookup, etc. for blender lights.
General operations, lookup, etc. for blender objects.
#define DEG2RADF(_deg)
#define M_PI
Object is a sort of wrapper for general info.
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr const char * c_str() const
void create_object(Main *bmain) override
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
const USDImportParams & import_params_
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
VecBase< float, D > normalize(VecOp< float, D >) RET
void colorspace_attr_to_scene_linear(const pxr::UsdAttribute &attr, pxr::GfVec3f &color)
Light * BKE_light_add(Main *bmain, const char *name) ATTR_WARN_UNUSED_RESULT
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265