Blender V5.3
usd_writer_light.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "usd_writer_light.hh"
8
9#include <pxr/usd/usdLux/diskLight.h>
10#include <pxr/usd/usdLux/distantLight.h>
11#include <pxr/usd/usdLux/rectLight.h>
12#include <pxr/usd/usdLux/shapingAPI.h>
13#include <pxr/usd/usdLux/sphereLight.h>
14
15#include "BLI_assert.hh"
16#include "BLI_math_constants.hh"
17
18#include "DNA_light_types.h"
19#include "DNA_object_types.h"
20
21namespace blender::io::usd {
22
24
25bool USDLightWriter::is_supported(const HierarchyContext * /*context*/) const
26{
27 return true;
28}
29
31{
32 pxr::UsdStageRefPtr stage = usd_export_context_.stage;
33 const pxr::SdfPath &usd_path = usd_export_context_.usd_path;
34 pxr::UsdTimeCode time = get_export_time_code();
35
36 const Light *light = id_cast<const Light *>(context.object->data);
37 pxr::UsdLuxLightAPI usd_light_api;
38
39 switch (light->type) {
40 case LA_AREA: {
41 switch (light->area_shape) {
42 case LA_AREA_RECT: {
43 pxr::UsdLuxRectLight rect_light = pxr::UsdLuxRectLight::Define(stage, usd_path);
44 set_attribute(rect_light.CreateWidthAttr(pxr::VtValue(), true),
45 light->area_size,
46 time,
48 set_attribute(rect_light.CreateHeightAttr(pxr::VtValue(), true),
49 light->area_sizey,
50 time,
52 usd_light_api = rect_light.LightAPI();
53 break;
54 }
55 case LA_AREA_SQUARE: {
56 pxr::UsdLuxRectLight rect_light = pxr::UsdLuxRectLight::Define(stage, usd_path);
57 set_attribute(rect_light.CreateWidthAttr(pxr::VtValue(), true),
58 light->area_size,
59 time,
61 set_attribute(rect_light.CreateHeightAttr(pxr::VtValue(), true),
62 light->area_size,
63 time,
65 usd_light_api = rect_light.LightAPI();
66 break;
67 }
68 case LA_AREA_DISK: {
69 pxr::UsdLuxDiskLight disk_light = pxr::UsdLuxDiskLight::Define(stage, usd_path);
70 set_attribute(disk_light.CreateRadiusAttr(pxr::VtValue(), true),
71 light->area_size / 2.0f,
72 time,
74 usd_light_api = disk_light.LightAPI();
75 break;
76 }
77 case LA_AREA_ELLIPSE: {
78 /* An ellipse light deteriorates into a disk light. */
79 pxr::UsdLuxDiskLight disk_light = pxr::UsdLuxDiskLight::Define(stage, usd_path);
80 set_attribute(disk_light.CreateRadiusAttr(pxr::VtValue(), true),
81 (light->area_size + light->area_sizey) / 4.0f,
82 time,
84 usd_light_api = disk_light.LightAPI();
85 break;
86 }
87 }
88 break;
89 }
90 case LA_LOCAL:
91 case LA_SPOT: {
92 pxr::UsdLuxSphereLight sphere_light = pxr::UsdLuxSphereLight::Define(stage, usd_path);
93 set_attribute(sphere_light.CreateRadiusAttr(pxr::VtValue(), true),
94 light->radius,
95 time,
97 set_attribute(sphere_light.CreateTreatAsPointAttr(pxr::VtValue(), true),
98 light->radius == 0.0f,
99 time,
101
102 if (light->type == LA_SPOT) {
103 pxr::UsdLuxShapingAPI shaping_api = pxr::UsdLuxShapingAPI::Apply(sphere_light.GetPrim());
104 if (shaping_api) {
105 set_attribute(shaping_api.CreateShapingConeAngleAttr(pxr::VtValue(), true),
106 RAD2DEGF(light->spotsize) / 2.0f,
107 time,
109 set_attribute(shaping_api.CreateShapingConeSoftnessAttr(pxr::VtValue(), true),
110 light->spotblend,
111 time,
113 }
114 }
115
116 usd_light_api = sphere_light.LightAPI();
117 break;
118 }
119 case LA_SUN: {
120 pxr::UsdLuxDistantLight distant_light = pxr::UsdLuxDistantLight::Define(stage, usd_path);
121 set_attribute(distant_light.CreateAngleAttr(pxr::VtValue(), true),
122 RAD2DEGF(light->sun_angle / 2.0f),
123 time,
125 usd_light_api = distant_light.LightAPI();
126 break;
127 }
128 default:
130 break;
131 }
132
133 float intensity;
134 if (light->type == LA_SUN) {
135 /* Unclear why, but approximately matches Karma. */
136 intensity = light->energy / 4.0f;
137 }
138 else {
139 /* Convert from radiant flux to intensity. */
140 intensity = light->energy / M_PI;
141 }
142
144 usd_light_api.CreateIntensityAttr(pxr::VtValue(), true), intensity, time, usd_value_writer_);
145 set_attribute(usd_light_api.CreateExposureAttr(pxr::VtValue(), true),
146 light->exposure,
147 time,
149
150 set_attribute(usd_light_api.CreateColorAttr(pxr::VtValue(), true),
151 pxr::GfVec3f(light->r, light->g, light->b),
152 time,
154 set_attribute(usd_light_api.CreateEnableColorTemperatureAttr(pxr::VtValue(), true),
155 (light->mode & LA_USE_TEMPERATURE) != 0,
156 time,
158 set_attribute(usd_light_api.CreateColorTemperatureAttr(pxr::VtValue(), true),
159 light->temperature,
160 time,
162
163 set_attribute(usd_light_api.CreateDiffuseAttr(pxr::VtValue(), true),
164 light->diff_fac,
165 time,
167 set_attribute(usd_light_api.CreateSpecularAttr(pxr::VtValue(), true),
168 light->spec_fac,
169 time,
171 set_attribute(usd_light_api.CreateNormalizeAttr(pxr::VtValue(), true),
172 (light->mode & LA_UNNORMALIZED) == 0,
173 time,
175
176 pxr::UsdPrim prim = usd_light_api.GetPrim();
177 add_to_prim_map(prim.GetPath(), &light->id);
178 write_id_properties(prim, light->id, time);
180
181 /* Only a subset of light types are "boundable". */
182 if (auto boundable = pxr::UsdGeomBoundable(prim)) {
183 this->author_extent(boundable, time);
184 }
185}
186
187} // namespace blender::io::usd
#define RAD2DEGF(_rad)
#define M_PI
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
void author_extent(const pxr::UsdGeomBoundable &boundable, const pxr::UsdTimeCode time)
const pxr::SdfPath & usd_path() const
void add_to_prim_map(const pxr::SdfPath &usd_path, const ID *id) const
pxr::UsdTimeCode get_export_time_code() const
pxr::UsdUtilsSparseValueWriter usd_value_writer_
USDAbstractWriter(const USDExporterContext &usd_export_context)
void write_id_properties(const pxr::UsdPrim &prim, const ID &id, pxr::UsdTimeCode=pxr::UsdTimeCode::Default()) const
const USDExporterContext usd_export_context_
bool is_supported(const HierarchyContext *context) const override
USDLightWriter(const USDExporterContext &ctx)
void do_write(HierarchyContext &context) override
void colorspace_apply_to_prim(const pxr::UsdPrim &prim)
void set_attribute(const pxr::UsdAttribute &attr, const USDT value, pxr::UsdTimeCode time, pxr::UsdUtilsSparseValueWriter &value_writer)