Blender V5.3
io/usd/hydra/light.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "light.hh"
6
7#include <pxr/imaging/hd/light.h>
8#include <pxr/imaging/hd/lightSchema.h>
9#include <pxr/imaging/hd/retainedDataSource.h>
10#include <pxr/imaging/hd/tokens.h>
11#include <pxr/imaging/hd/visibilitySchema.h>
12#include <pxr/imaging/hd/xformSchema.h>
13#include <pxr/usd/usdLux/tokens.h>
14
15#include "BLI_math_constants.hh"
16#include "BLI_utildefines.hh"
17
18#include "DNA_light_types.h"
19#include "DNA_object_types.h"
20#include "DNA_view3d_types.h"
21
22#include "populate_context.hh"
23#include "util.hh"
24
25namespace blender::io::hydra {
26
27/* Hydra prim type for a Blender light's type/shape. */
28static pxr::TfToken light_prim_type(const Light *light)
29{
30 switch (light->type) {
31 case LA_AREA:
32 switch (light->area_shape) {
33 case LA_AREA_SQUARE:
34 case LA_AREA_RECT:
35 return pxr::HdPrimTypeTokens->rectLight;
36 case LA_AREA_DISK:
37 case LA_AREA_ELLIPSE:
38 return pxr::HdPrimTypeTokens->diskLight;
39 default:
40 return pxr::HdPrimTypeTokens->rectLight;
41 }
42 case LA_LOCAL:
43 case LA_SPOT:
44 return pxr::HdPrimTypeTokens->sphereLight;
45 case LA_SUN:
46 return pxr::HdPrimTypeTokens->distantLight;
47 default:
49 }
50 return pxr::TfToken();
51}
52
53/* Build the #HdLightSchema container with the light's parameters. */
54static pxr::HdContainerDataSourceHandle build_light_params_data_source(const Object *object)
55{
56 const Light *light = id_cast<const Light *>(object->data);
57
59
60 switch (light->type) {
61 case LA_AREA:
62 switch (light->area_shape) {
63 case LA_AREA_SQUARE:
64 b.add(pxr::HdLightTokens->width, light->area_size);
65 b.add(pxr::HdLightTokens->height, light->area_size);
66 break;
67 case LA_AREA_RECT:
68 b.add(pxr::HdLightTokens->width, light->area_size);
69 b.add(pxr::HdLightTokens->height, light->area_sizey);
70 break;
71 case LA_AREA_DISK:
72 b.add(pxr::HdLightTokens->radius, light->area_size / 2.0f);
73 break;
74 case LA_AREA_ELLIPSE:
75 /* An ellipse light deteriorates into a disk light. */
76 b.add(pxr::HdLightTokens->radius, (light->area_size + light->area_sizey) / 4.0f);
77 break;
78 }
79 break;
80 case LA_LOCAL:
81 case LA_SPOT:
82 b.add(pxr::HdLightTokens->radius, light->radius);
83 if (light->radius == 0.0f) {
84 b.add(pxr::UsdLuxTokens->treatAsPoint, true);
85 }
86 if (light->type == LA_SPOT) {
87 b.add(pxr::UsdLuxTokens->inputsShapingConeAngle, RAD2DEGF(light->spotsize * 0.5f));
88 b.add(pxr::UsdLuxTokens->inputsShapingConeSoftness, light->spotblend);
89 }
90 break;
91 case LA_SUN:
92 b.add(pxr::HdLightTokens->angle, RAD2DEGF(light->sun_angle * 0.5f));
93 break;
94 }
95
96 float intensity;
97 if (light->type == LA_SUN) {
98 /* Unclear why, but approximately matches Karma. */
99 intensity = light->energy / 4.0f;
100 }
101 else {
102 /* Convert from radiant flux to intensity. */
103 intensity = light->energy / M_PI;
104 }
105
106 b.add(pxr::HdLightTokens->color, pxr::GfVec3f(light->r, light->g, light->b));
107 b.add(pxr::HdLightTokens->enableColorTemperature, (light->mode & LA_USE_TEMPERATURE) != 0);
108 b.add(pxr::HdLightTokens->colorTemperature, light->temperature);
109 b.add(pxr::HdLightTokens->intensity, intensity);
110 b.add(pxr::HdLightTokens->exposure, light->exposure);
111 b.add(pxr::HdLightTokens->diffuse, light->diff_fac);
112 b.add(pxr::HdLightTokens->specular, light->spec_fac);
113 b.add(pxr::HdLightTokens->normalize, (light->mode & LA_UNNORMALIZED) == 0);
114
115 return b.build();
116}
117
118pxr::HdContainerDataSourceHandle build_light_prim_data_source(
119 const pxr::HdContainerDataSourceHandle &light_params,
120 const pxr::GfMatrix4d &transform,
121 const bool visible)
122{
123 pxr::HdContainerDataSourceHandle xform =
124 pxr::HdXformSchema::Builder()
125 .SetMatrix(pxr::HdRetainedTypedSampledDataSource<pxr::GfMatrix4d>::New(transform))
126 .SetResetXformStack(pxr::HdRetainedTypedSampledDataSource<bool>::New(false))
127 .Build();
128 pxr::HdContainerDataSourceHandle visibility =
129 pxr::HdVisibilitySchema::Builder()
130 .SetVisibility(pxr::HdRetainedTypedSampledDataSource<bool>::New(visible))
131 .Build();
132
134 b.add(pxr::HdLightSchema::GetSchemaToken(), light_params);
135 b.add(pxr::HdXformSchema::GetSchemaToken(), xform);
136 b.add(pxr::HdVisibilitySchema::GetSchemaToken(), visibility);
137 return b.build();
138}
139
140void emit_light_object(PopulateContext &ctx, const Object *object, EmittedObject &emitted)
141{
142 if (ctx.view3d && !V3D_USES_SCENE_LIGHTS(ctx.view3d)) {
143 /* Lookdev mode where the user disabled scene lights. */
144 return;
145 }
146 const pxr::SdfPath path = ctx.object_prim_id(object);
147 const pxr::TfToken type = light_prim_type(id_cast<const Light *>(object->data));
148 pxr::HdContainerDataSourceHandle params = build_light_params_data_source(object);
149 pxr::HdContainerDataSourceHandle prim_ds = build_light_prim_data_source(
150 params, gf_matrix_from_transform(object->object_to_world().ptr()), true);
151 ctx.emit_object_prim(emitted, path, type, prim_ds);
152}
153
154void emit_light_dupli(PopulateContext &ctx, const Object *source, const float dupli_mat[4][4])
155{
156 if (ctx.view3d && !V3D_USES_SCENE_LIGHTS(ctx.view3d)) {
157 return;
158 }
159 const int idx = ctx.nonmesh_instance_count.lookup_default(source, 0);
160 ctx.nonmesh_instance_count.add_overwrite(source, idx + 1);
161 const pxr::SdfPath path = ctx.instance_clone_prim_id(source, idx);
162 const pxr::TfToken type = light_prim_type(id_cast<const Light *>(source->data));
163 pxr::HdContainerDataSourceHandle params = build_light_params_data_source(source);
164 pxr::HdContainerDataSourceHandle prim_ds = build_light_prim_data_source(
165 params, gf_matrix_from_transform(dupli_mat), true);
166 ctx.emit_prim(path, type, prim_ds);
167}
168
169} // namespace blender::io::hydra
#define RAD2DEGF(_rad)
#define M_PI
Object is a sort of wrapper for general info.
#define V3D_USES_SCENE_LIGHTS(v3d)
#define BLI_assert_unreachable()
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
VecBase< float, D > normalize(VecOp< float, D >) RET
static pxr::HdContainerDataSourceHandle build_light_params_data_source(const Object *object)
static pxr::TfToken light_prim_type(const Light *light)
void emit_light_dupli(PopulateContext &ctx, const Object *source, const float dupli_mat[4][4])
pxr::GfMatrix4d gf_matrix_from_transform(const float m[4][4])
Definition util.hh:20
void emit_light_object(PopulateContext &ctx, const Object *object, EmittedObject &emitted)
pxr::HdContainerDataSourceHandle build_light_prim_data_source(const pxr::HdContainerDataSourceHandle &light_params, const pxr::GfMatrix4d &transform, const bool visible)
Map< const Object *, int > nonmesh_instance_count
pxr::SdfPath object_prim_id(const Object *object) const
void emit_object_prim(EmittedObject &emitted, const pxr::SdfPath &path, const pxr::TfToken &type, const pxr::HdContainerDataSourceHandle &ds, const pxr::GfMatrix4d &geometry_xform=pxr::GfMatrix4d(1.0))
pxr::SdfPath instance_clone_prim_id(const Object *source, int instance_index) const
void emit_prim(const pxr::SdfPath &path, const pxr::TfToken &type, const pxr::HdContainerDataSourceHandle &ds)