Blender  V2.93
usd_writer_light.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 #include "usd_writer_light.h"
20 #include "usd_hierarchy_iterator.h"
21 
22 #include <pxr/usd/usdLux/diskLight.h>
23 #include <pxr/usd/usdLux/distantLight.h>
24 #include <pxr/usd/usdLux/rectLight.h>
25 #include <pxr/usd/usdLux/sphereLight.h>
26 
27 #include "BLI_assert.h"
28 #include "BLI_utildefines.h"
29 
30 #include "DNA_light_types.h"
31 #include "DNA_object_types.h"
32 
33 namespace blender::io::usd {
34 
36 {
37 }
38 
40 {
41  Light *light = static_cast<Light *>(context->object->data);
42  return ELEM(light->type, LA_AREA, LA_LOCAL, LA_SUN);
43 }
44 
46 {
47  pxr::UsdStageRefPtr stage = usd_export_context_.stage;
48  const pxr::SdfPath &usd_path = usd_export_context_.usd_path;
49  pxr::UsdTimeCode timecode = get_export_time_code();
50 
51  Light *light = static_cast<Light *>(context.object->data);
52  pxr::UsdLuxLight usd_light;
53 
54  switch (light->type) {
55  case LA_AREA:
56  switch (light->area_shape) {
57  case LA_AREA_DISK:
58  case LA_AREA_ELLIPSE: { /* An ellipse light will deteriorate into a disk light. */
59  pxr::UsdLuxDiskLight disk_light = pxr::UsdLuxDiskLight::Define(stage, usd_path);
60  disk_light.CreateRadiusAttr().Set(light->area_size, timecode);
61  usd_light = disk_light;
62  break;
63  }
64  case LA_AREA_RECT: {
65  pxr::UsdLuxRectLight rect_light = pxr::UsdLuxRectLight::Define(stage, usd_path);
66  rect_light.CreateWidthAttr().Set(light->area_size, timecode);
67  rect_light.CreateHeightAttr().Set(light->area_sizey, timecode);
68  usd_light = rect_light;
69  break;
70  }
71  case LA_AREA_SQUARE: {
72  pxr::UsdLuxRectLight rect_light = pxr::UsdLuxRectLight::Define(stage, usd_path);
73  rect_light.CreateWidthAttr().Set(light->area_size, timecode);
74  rect_light.CreateHeightAttr().Set(light->area_size, timecode);
75  usd_light = rect_light;
76  break;
77  }
78  }
79  break;
80  case LA_LOCAL: {
81  pxr::UsdLuxSphereLight sphere_light = pxr::UsdLuxSphereLight::Define(stage, usd_path);
82  sphere_light.CreateRadiusAttr().Set(light->area_size, timecode);
83  usd_light = sphere_light;
84  break;
85  }
86  case LA_SUN:
87  usd_light = pxr::UsdLuxDistantLight::Define(stage, usd_path);
88  break;
89  default:
90  BLI_assert(!"is_supported() returned true for unsupported light type");
91  }
92 
93  /* Scale factor to get to somewhat-similar illumination. Since the USDViewer had similar
94  * over-exposure as Blender Internal with the same values, this code applies the reverse of the
95  * versioning code in light_emission_unify(). */
96  float usd_intensity;
97  if (light->type == LA_SUN) {
98  /* Untested, as the Hydra GL viewport of USDViewer doesn't support distant lights. */
99  usd_intensity = light->energy;
100  }
101  else {
102  usd_intensity = light->energy / 100.0f;
103  }
104  usd_light.CreateIntensityAttr().Set(usd_intensity, timecode);
105 
106  usd_light.CreateColorAttr().Set(pxr::GfVec3f(light->r, light->g, light->b), timecode);
107  usd_light.CreateSpecularAttr().Set(light->spec_fac, timecode);
108 }
109 
110 } // namespace blender::io::usd
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define ELEM(...)
#define LA_AREA
#define LA_AREA_SQUARE
#define LA_AREA_ELLIPSE
#define LA_SUN
#define LA_LOCAL
#define LA_AREA_DISK
#define LA_AREA_RECT
Object is a sort of wrapper for general info.
const pxr::SdfPath & usd_path() const
pxr::UsdTimeCode get_export_time_code() const
const USDExporterContext usd_export_context_
virtual bool is_supported(const HierarchyContext *context) const override
USDLightWriter(const USDExporterContext &ctx)
virtual void do_write(HierarchyContext &context) override
EvaluationStage stage
Definition: deg_eval.cc:96
struct SELECTID_Context context
Definition: select_engine.c:47
float r
float energy
float spec_fac
float area_sizey
short area_shape
float g
float area_size
float b
short type