14 #include <pxr/imaging/hd/sceneDelegate.h>
15 #include <pxr/usd/sdf/assetPath.h>
28 : HdLight(sprimId), _lightType(lightType)
38 return DirtyBits::DirtyTransform | DirtyBits::DirtyParams;
42 HdRenderParam *renderParam,
43 HdDirtyBits *dirtyBits)
45 if (*dirtyBits == DirtyBits::Clean) {
49 Initialize(renderParam);
54 const SdfPath &
id = GetId();
56 if (*dirtyBits & DirtyBits::DirtyTransform) {
57 const float metersPerUnit =
61 #if PXR_VERSION >= 2011
65 sceneDelegate->GetLightParamValue(
id, HdTokens->transform)
73 if (_lightType == HdPrimTypeTokens->diskLight || _lightType == HdPrimTypeTokens->rectLight) {
79 if (*dirtyBits & DirtyBits::DirtyParams) {
82 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->color);
83 if (!value.IsEmpty()) {
84 const auto color = value.Get<GfVec3f>();
88 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->exposure);
89 if (!value.IsEmpty()) {
90 strength *= exp2(value.Get<
float>());
93 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->intensity);
94 if (!value.IsEmpty()) {
95 strength *= value.Get<
float>();
99 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->normalize);
100 const bool normalize = value.IsHolding<
bool>() && value.UncheckedGet<
bool>();
102 value = sceneDelegate->GetLightParamValue(
id, _tokens->visibleInPrimaryRay);
103 if (!value.IsEmpty()) {
104 _light->set_use_camera(value.Get<
bool>());
107 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->shadowEnable);
108 if (!value.IsEmpty()) {
109 _light->set_cast_shadow(value.Get<
bool>());
112 if (_lightType == HdPrimTypeTokens->distantLight) {
113 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->angle);
114 if (!value.IsEmpty()) {
115 _light->set_angle(GfDegreesToRadians(value.Get<
float>()));
118 else if (_lightType == HdPrimTypeTokens->diskLight) {
119 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->radius);
120 if (!value.IsEmpty()) {
121 const float size = value.Get<
float>() * 2.0f;
122 _light->set_sizeu(
size);
123 _light->set_sizev(
size);
127 const float radius = _light->get_sizeu() * 0.5f;
128 strength *=
M_PI * radius * radius;
131 else if (_lightType == HdPrimTypeTokens->rectLight) {
132 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->width);
133 if (!value.IsEmpty()) {
134 _light->set_sizeu(value.Get<
float>());
137 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->height);
138 if (!value.IsEmpty()) {
139 _light->set_sizev(value.Get<
float>());
143 strength *= _light->get_sizeu() * _light->get_sizeu();
146 else if (_lightType == HdPrimTypeTokens->sphereLight) {
147 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->radius);
148 if (!value.IsEmpty()) {
149 _light->set_size(value.Get<
float>());
152 bool shaping =
false;
154 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->shapingConeAngle);
155 if (!value.IsEmpty()) {
156 _light->set_spot_angle(GfDegreesToRadians(value.Get<
float>()) * 2.0f);
160 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->shapingConeSoftness);
161 if (!value.IsEmpty()) {
162 _light->set_spot_smooth(value.Get<
float>());
169 const float radius = _light->get_size();
170 strength *=
M_PI * radius * radius * 4.0f;
174 const bool visible = sceneDelegate->GetVisible(
id);
181 _light->set_strength(strength);
182 _light->set_is_enabled(visible);
184 PopulateShaderGraph(sceneDelegate);
187 else if (_light->tfm_is_modified() && (_lightType == HdPrimTypeTokens->domeLight ||
188 _light->get_shader()->has_surface_spatial_varying)) {
189 PopulateShaderGraph(sceneDelegate);
192 if (_light->is_modified()) {
193 _light->tag_update(
lock.scene);
196 *dirtyBits = DirtyBits::Clean;
199 void HdCyclesLight::PopulateShaderGraph(HdSceneDelegate *sceneDelegate)
204 if (_lightType == HdPrimTypeTokens->domeLight) {
207 bgNode->set_color(_light->get_strength());
210 graph->connect(bgNode->
output(
"Background"),
graph->output()->input(
"Surface"));
217 emissionNode->set_strength(1.0f);
218 graph->add(emissionNode);
220 graph->connect(emissionNode->
output(
"Emission"),
graph->output()->input(
"Surface"));
222 outputNode = emissionNode;
226 const SdfPath &
id = GetId();
227 bool hasSpatialVarying =
false;
228 bool hasColorTemperature =
false;
230 if (sceneDelegate !=
nullptr) {
231 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->enableColorTemperature);
232 const bool enableColorTemperature = value.IsHolding<
bool>() && value.UncheckedGet<
bool>();
234 if (enableColorTemperature) {
235 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->colorTemperature);
236 if (value.IsHolding<
float>()) {
238 blackbodyNode->set_temperature(value.UncheckedGet<
float>());
239 graph->add(blackbodyNode);
241 if (_lightType == HdPrimTypeTokens->domeLight) {
244 mathNode->set_vector2(_light->get_strength());
245 graph->add(mathNode);
254 hasColorTemperature =
true;
258 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->shapingIesFile);
259 if (value.IsHolding<SdfAssetPath>()) {
260 std::string filename = value.UncheckedGet<SdfAssetPath>().GetResolvedPath();
261 if (filename.empty()) {
262 filename = value.UncheckedGet<SdfAssetPath>().GetAssetPath();
266 coordNode->set_ob_tfm(_light->get_tfm());
267 coordNode->set_use_transform(
true);
268 graph->add(coordNode);
271 iesNode->set_filename(ustring(filename));
276 hasSpatialVarying =
true;
279 value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->textureFile);
280 if (value.IsHolding<SdfAssetPath>()) {
281 std::string filename = value.UncheckedGet<SdfAssetPath>().GetResolvedPath();
282 if (filename.empty()) {
283 filename = value.UncheckedGet<SdfAssetPath>().GetAssetPath();
287 if (_lightType == HdPrimTypeTokens->domeLight) {
292 coordNode->set_ob_tfm(tfm);
293 coordNode->set_use_transform(
true);
294 graph->add(coordNode);
298 graph->add(textureNode);
302 hasSpatialVarying =
true;
306 graph->add(coordNode);
309 static_cast<ImageTextureNode *
>(textureNode)->set_filename(ustring(filename));
310 graph->add(textureNode);
312 graph->connect(coordNode->
output(
"Parametric"), textureNode->
input(
"Vector"));
315 if (hasColorTemperature) {
318 graph->add(mathNode);
322 graph->connect(outputNodeInput->
link, mathNode->
input(
"Vector2"));
323 graph->disconnect(outputNodeInput);
324 graph->connect(mathNode->
output(
"Vector"), outputNodeInput);
326 else if (_lightType == HdPrimTypeTokens->domeLight) {
329 mathNode->set_vector2(_light->get_strength());
330 graph->add(mathNode);
341 Shader *
const shader = _light->get_shader();
355 const bool keep_nodes =
static_cast<const HdCyclesSession *
>(renderParam)->keep_nodes;
358 lock.scene->delete_node(_light);
364 void HdCyclesLight::Initialize(HdRenderParam *renderParam)
372 _light =
lock.scene->create_node<
Light>();
373 _light->name = GetId().GetString();
377 if (_lightType == HdPrimTypeTokens->domeLight) {
380 else if (_lightType == HdPrimTypeTokens->distantLight) {
383 else if (_lightType == HdPrimTypeTokens->diskLight) {
385 _light->set_round(
true);
386 _light->set_size(1.0f);
388 else if (_lightType == HdPrimTypeTokens->rectLight) {
390 _light->set_round(
false);
391 _light->set_size(1.0f);
393 else if (_lightType == HdPrimTypeTokens->sphereLight) {
395 _light->set_size(1.0f);
398 _light->set_use_mis(
true);
399 _light->set_use_camera(
false);
402 _light->set_shader(shader);
405 PopulateShaderGraph(
nullptr);
@ NODE_VECTOR_MATH_MULTIPLY
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
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
void Finalize(PXR_NS::HdRenderParam *renderParam) override
void Sync(PXR_NS::HdSceneDelegate *sceneDelegate, PXR_NS::HdRenderParam *renderParam, PXR_NS::HdDirtyBits *dirtyBits) override
~HdCyclesLight() override
HdCyclesLight(const PXR_NS::SdfPath &sprimId, const PXR_NS::TfToken &lightType)
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
ShaderInput * input(const char *name)
ShaderOutput * output(const char *name)
bool has_surface_spatial_varying
void set_graph(ShaderGraph *graph)
void tag_update(Scene *scene)
static uint hash_string(const char *str)
ccl_device_inline uint hash_uint2(uint kx, uint ky)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
TF_DEFINE_PRIVATE_TOKENS(_tokens,(visibleInPrimaryRay))
HDCYCLES_NAMESPACE_OPEN_SCOPE Transform convert_transform(const GfMatrix4d &matrix)
ccl_device_inline float3 one_float3()
ccl_device_inline float3 zero_float3()
vec_base< T, Size > normalize(const vec_base< T, Size > &v)