Blender  V3.3
usd_reader_light.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Tangent Animation. All rights reserved. */
3 
4 #include "usd_reader_light.h"
5 
6 #include "BKE_light.h"
7 #include "BKE_object.h"
8 
9 #include "DNA_light_types.h"
10 #include "DNA_object_types.h"
11 
12 #include <pxr/usd/usdLux/diskLight.h>
13 #include <pxr/usd/usdLux/distantLight.h>
14 #include <pxr/usd/usdLux/rectLight.h>
15 #include <pxr/usd/usdLux/shapingAPI.h>
16 #include <pxr/usd/usdLux/sphereLight.h>
17 
18 #include <iostream>
19 
20 namespace blender::io::usd {
21 
22 void USDLightReader::create_object(Main *bmain, const double /* motionSampleTime */)
23 {
24  Light *blight = static_cast<Light *>(BKE_light_add(bmain, name_.c_str()));
25 
27  object_->data = blight;
28 }
29 
30 void USDLightReader::read_object_data(Main *bmain, const double motionSampleTime)
31 {
32  Light *blight = (Light *)object_->data;
33 
34  if (blight == nullptr) {
35  return;
36  }
37 
38  if (!prim_) {
39  return;
40  }
41 #if PXR_VERSION >= 2111
42  pxr::UsdLuxLightAPI light_api(prim_);
43 #else
44  pxr::UsdLuxLight light_api(prim_);
45 #endif
46 
47  if (!light_api) {
48  return;
49  }
50 
51  pxr::UsdLuxShapingAPI shaping_api;
52 
53  /* Set light type. */
54 
55  if (prim_.IsA<pxr::UsdLuxDiskLight>()) {
56  blight->type = LA_AREA;
57  blight->area_shape = LA_AREA_DISK;
58  /* Ellipse lights are not currently supported */
59  }
60  else if (prim_.IsA<pxr::UsdLuxRectLight>()) {
61  blight->type = LA_AREA;
62  blight->area_shape = LA_AREA_RECT;
63  }
64  else if (prim_.IsA<pxr::UsdLuxSphereLight>()) {
65  blight->type = LA_LOCAL;
66 
67  shaping_api = pxr::UsdLuxShapingAPI(prim_);
68 
69  if (shaping_api && shaping_api.GetShapingConeAngleAttr().IsAuthored()) {
70  blight->type = LA_SPOT;
71  }
72  }
73  else if (prim_.IsA<pxr::UsdLuxDistantLight>()) {
74  blight->type = LA_SUN;
75  }
76 
77  /* Set light values. */
78 
79  if (pxr::UsdAttribute intensity_attr = light_api.GetIntensityAttr()) {
80  float intensity = 0.0f;
81  if (intensity_attr.Get(&intensity, motionSampleTime)) {
82  blight->energy = intensity * this->import_params_.light_intensity_scale;
83  }
84  }
85 
86  /* TODO(makowalsk): Not currently supported. */
87 #if 0
88  pxr::VtValue exposure;
89  light_prim.GetExposureAttr().Get(&exposure, motionSampleTime);
90 #endif
91 
92  /* TODO(makowalsk): Not currently supported */
93 #if 0
94  pxr::VtValue diffuse;
95  light_prim.GetDiffuseAttr().Get(&diffuse, motionSampleTime);
96 #endif
97 
98  if (pxr::UsdAttribute spec_attr = light_api.GetSpecularAttr()) {
99  float spec = 0.0f;
100  if (spec_attr.Get(&spec, motionSampleTime)) {
101  blight->spec_fac = spec;
102  }
103  }
104 
105  if (pxr::UsdAttribute color_attr = light_api.GetColorAttr()) {
106  pxr::GfVec3f color;
107  if (color_attr.Get(&color, motionSampleTime)) {
108  blight->r = color[0];
109  blight->g = color[1];
110  blight->b = color[2];
111  }
112  }
113 
114  /* TODO(makowalski): Not currently supported. */
115 #if 0
116  pxr::VtValue use_color_temp;
117  light_prim.GetEnableColorTemperatureAttr().Get(&use_color_temp, motionSampleTime);
118 #endif
119 
120  /* TODO(makowalski): Not currently supported. */
121 #if 0
122  pxr::VtValue color_temp;
123  light_prim.GetColorTemperatureAttr().Get(&color_temp, motionSampleTime);
124 #endif
125 
126  switch (blight->type) {
127  case LA_AREA:
128  if (blight->area_shape == LA_AREA_RECT && prim_.IsA<pxr::UsdLuxRectLight>()) {
129 
130  pxr::UsdLuxRectLight rect_light(prim_);
131 
132  if (!rect_light) {
133  break;
134  }
135 
136  if (pxr::UsdAttribute width_attr = rect_light.GetWidthAttr()) {
137  float width = 0.0f;
138  if (width_attr.Get(&width, motionSampleTime)) {
139  blight->area_size = width;
140  }
141  }
142 
143  if (pxr::UsdAttribute height_attr = rect_light.GetHeightAttr()) {
144  float height = 0.0f;
145  if (height_attr.Get(&height, motionSampleTime)) {
146  blight->area_sizey = height;
147  }
148  }
149  }
150  else if (blight->area_shape == LA_AREA_DISK && prim_.IsA<pxr::UsdLuxDiskLight>()) {
151 
152  pxr::UsdLuxDiskLight disk_light(prim_);
153 
154  if (!disk_light) {
155  break;
156  }
157 
158  if (pxr::UsdAttribute radius_attr = disk_light.GetRadiusAttr()) {
159  float radius = 0.0f;
160  if (radius_attr.Get(&radius, motionSampleTime)) {
161  blight->area_size = radius * 2.0f;
162  }
163  }
164  }
165  break;
166  case LA_LOCAL:
167  if (prim_.IsA<pxr::UsdLuxSphereLight>()) {
168 
169  pxr::UsdLuxSphereLight sphere_light(prim_);
170 
171  if (!sphere_light) {
172  break;
173  }
174 
175  if (pxr::UsdAttribute radius_attr = sphere_light.GetRadiusAttr()) {
176  float radius = 0.0f;
177  if (radius_attr.Get(&radius, motionSampleTime)) {
178  blight->area_size = radius;
179  }
180  }
181  }
182  break;
183  case LA_SPOT:
184  if (prim_.IsA<pxr::UsdLuxSphereLight>()) {
185 
186  pxr::UsdLuxSphereLight sphere_light(prim_);
187 
188  if (!sphere_light) {
189  break;
190  }
191 
192  if (pxr::UsdAttribute radius_attr = sphere_light.GetRadiusAttr()) {
193  float radius = 0.0f;
194  if (radius_attr.Get(&radius, motionSampleTime)) {
195  blight->area_size = radius;
196  }
197  }
198 
199  if (!shaping_api) {
200  break;
201  }
202 
203  if (pxr::UsdAttribute cone_angle_attr = shaping_api.GetShapingConeAngleAttr()) {
204  float cone_angle = 0.0f;
205  if (cone_angle_attr.Get(&cone_angle, motionSampleTime)) {
206  blight->spotsize = cone_angle * ((float)M_PI / 180.0f) * 2.0f;
207  }
208  }
209 
210  if (pxr::UsdAttribute cone_softness_attr = shaping_api.GetShapingConeSoftnessAttr()) {
211  float cone_softness = 0.0f;
212  if (cone_softness_attr.Get(&cone_softness, motionSampleTime)) {
213  blight->spotblend = cone_softness;
214  }
215  }
216  }
217  break;
218  case LA_SUN:
219  if (prim_.IsA<pxr::UsdLuxDistantLight>()) {
220  pxr::UsdLuxDistantLight distant_light(prim_);
221 
222  if (!distant_light) {
223  break;
224  }
225 
226  if (pxr::UsdAttribute angle_attr = distant_light.GetAngleAttr()) {
227  float angle = 0.0f;
228  if (angle_attr.Get(&angle, motionSampleTime)) {
229  blight->sun_angle = angle * (float)M_PI / 180.0f;
230  }
231  }
232  }
233  break;
234  }
235 
236  USDXformReader::read_object_data(bmain, motionSampleTime);
237 }
238 
239 } // namespace blender::io::usd
typedef float(TangentPoint)[2]
General operations, lookup, etc. for blender lights.
struct Light * BKE_light_add(struct Main *bmain, const char *name) ATTR_WARN_UNUSED_RESULT
Definition: light.c:203
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_add_only_object(struct Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
Definition: object.cc:2241
#define M_PI
Definition: BLI_math_base.h:20
#define LA_AREA
#define LA_SPOT
#define LA_SUN
#define LA_LOCAL
#define LA_AREA_DISK
#define LA_AREA_RECT
Object is a sort of wrapper for general info.
@ OB_LAMP
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
void create_object(Main *bmain, double motionSampleTime) override
void read_object_data(Main *bmain, double motionSampleTime) override
const USDImportParams & import_params_
void read_object_data(Main *bmain, double motionSampleTime) override
float sun_angle
float r
float energy
float spec_fac
float area_sizey
short area_shape
float spotblend
float g
float spotsize
float area_size
float b
short type
Definition: BKE_main.h:121
void * data
float light_intensity_scale
Definition: usd.h:66