19#include <pxr/imaging/hd/lightSchema.h>
20#include <pxr/imaging/hd/sceneDelegate.h>
21#include <pxr/imaging/hd/visibilitySchema.h>
22#include <pxr/imaging/hd/xformSchema.h>
23#include <pxr/usd/sdf/assetPath.h>
38 : HdLight(sprimId), _lightType(lightType)
46 return DirtyBits::DirtyTransform | DirtyBits::DirtyParams;
50 HdRenderParam *renderParam,
51 HdDirtyBits *dirtyBits)
53 if (*dirtyBits == DirtyBits::Clean) {
57 Initialize(renderParam);
61 const SdfPath &
id = GetId();
62 const HdSceneIndexPrim prim =
GetPrim(sceneDelegate,
id);
63 const HdContainerDataSourceHandle &primDs = prim.dataSource;
64 const HdContainerDataSourceHandle lightDs = HdLightSchema::GetFromParent(primDs).GetContainer();
66 if (*dirtyBits & DirtyBits::DirtyTransform) {
67 const float metersPerUnit =
70 GfMatrix4d xform(1.0);
71 if (
auto matrixDs = HdXformSchema::GetFromParent(primDs).GetMatrix()) {
72 xform = matrixDs->GetTypedValue(0.0f);
76 _object->set_tfm(tfm);
79 if (*dirtyBits & DirtyBits::DirtyParams) {
84 lightDs, HdLightTokens->color, GfVec3f(1.0f, 1.0f, 1.0f));
85 strength =
make_float3(color[0], color[1], color[2]);
91 if (_lightType == HdPrimTypeTokens->distantLight) {
103 if (ds->GetTypedValue(0.0f)) {
112 _light->set_cast_shadow(ds->GetTypedValue(0.0f));
115 if (_lightType == HdPrimTypeTokens->distantLight) {
117 static_cast<SunLight *
>(_light)->set_angle(GfDegreesToRadians(ds->GetTypedValue(0.0f)));
120 else if (_lightType == HdPrimTypeTokens->diskLight) {
123 const float size = ds->GetTypedValue(0.0f) * 2.0f;
124 area_light->set_sizeu(size);
125 area_light->set_sizev(size);
128 else if (_lightType == HdPrimTypeTokens->rectLight) {
131 area_light->set_sizeu(ds->GetTypedValue(0.0f));
134 area_light->set_sizev(ds->GetTypedValue(0.0f));
137 else if (_lightType == HdPrimTypeTokens->sphereLight) {
141 spot_light->set_radius(0.0f);
144 spot_light->set_radius(ds->GetTypedValue(0.0f));
147 bool shaping =
false;
150 spot_light->set_angle(GfDegreesToRadians(ds->GetTypedValue(0.0f)) * 2.0f);
155 spot_light->set_smooth(ds->GetTypedValue(0.0f));
163 if (
auto ds = HdVisibilitySchema::GetFromParent(primDs).GetVisibility()) {
164 visible = ds->GetTypedValue(0.0f);
172 _light->set_strength(strength);
173 _light->set_is_enabled(visible);
175 PopulateShaderGraph(lightDs);
178 else if (_object->tfm_is_modified() && (_lightType == HdPrimTypeTokens->domeLight ||
179 _light->get_shader()->has_surface_spatial_varying))
181 PopulateShaderGraph(lightDs);
184 if (_light->is_modified()) {
185 _light->tag_update(
lock.scene);
188 *dirtyBits = DirtyBits::Clean;
191void HdCyclesLight::PopulateShaderGraph(
const HdContainerDataSourceHandle &lightContainer)
196 if (_lightType == HdPrimTypeTokens->domeLight) {
199 bgNode->set_color(_light->get_strength());
201 graph->connect(bgNode->
output(
"Background"), graph->output()->input(
"Surface"));
205 else if (lightContainer) {
206 if (
auto ds = HdStringDataSource::Cast(lightContainer->Get(_tokens->falloff))) {
207 const std::string strVal = ds->GetTypedValue(0.0f);
208 if (strVal ==
"Constant" || strVal ==
"Linear" || strVal ==
"Quadratic") {
209 LightFalloffNode *lfoNode = graph->create_node<LightFalloffNode>();
210 lfoNode->set_strength(1.f);
211 graph->connect(lfoNode->
output(strVal.c_str()), graph->output()->input(
"Surface"));
212 outputNode = lfoNode;
217 if (outputNode ==
nullptr) {
218 EmissionNode *emissionNode = graph->create_node<EmissionNode>();
220 emissionNode->set_strength(1.0f);
222 graph->connect(emissionNode->
output(
"Emission"), graph->output()->input(
"Surface"));
224 outputNode = emissionNode;
227 bool hasSpatialVarying =
false;
228 bool hasColorTemperature =
false;
230 if (lightContainer) {
232 lightContainer, HdLightTokens->enableColorTemperature,
false);
234 if (enableColorTemperature) {
235 if (
auto ds = HdFloatDataSource::Cast(lightContainer->Get(HdLightTokens->colorTemperature)))
237 BlackbodyNode *blackbodyNode = graph->create_node<BlackbodyNode>();
238 blackbodyNode->set_temperature(ds->GetTypedValue(0.0f));
240 if (_lightType == HdPrimTypeTokens->domeLight) {
241 VectorMathNode *mathNode = graph->create_node<VectorMathNode>();
243 mathNode->set_vector2(_light->get_strength());
245 graph->connect(blackbodyNode->
output(
"Color"), mathNode->
input(
"Vector1"));
246 graph->connect(mathNode->
output(
"Vector"), outputNode->
input(
"Color"));
249 graph->connect(blackbodyNode->
output(
"Color"), outputNode->
input(
"Color"));
252 hasColorTemperature =
true;
256 if (
auto ds = HdAssetPathDataSource::Cast(lightContainer->Get(HdLightTokens->shapingIesFile)))
258 const SdfAssetPath assetPath = ds->GetTypedValue(0.0f);
259 std::string
filename = assetPath.GetResolvedPath();
261 filename = assetPath.GetAssetPath();
265 TextureCoordinateNode *coordNode = graph->create_node<TextureCoordinateNode>();
266 coordNode->set_ob_tfm(_object->get_tfm());
267 coordNode->set_use_transform(
true);
269 IESLightNode *iesNode = graph->create_node<IESLightNode>();
270 iesNode->set_filename(ustring(filename));
272 graph->connect(coordNode->
output(
"Normal"), iesNode->
input(
"Vector"));
273 graph->connect(iesNode->
output(
"Fac"), outputNode->
input(
"Strength"));
275 hasSpatialVarying =
true;
279 if (
auto ds = HdAssetPathDataSource::Cast(lightContainer->Get(HdLightTokens->textureFile))) {
280 const SdfAssetPath assetPath = ds->GetTypedValue(0.0f);
281 std::string
filename = assetPath.GetResolvedPath();
283 filename = assetPath.GetAssetPath();
287 ImageSlotTextureNode *textureNode =
nullptr;
288 if (_lightType == HdPrimTypeTokens->domeLight) {
292 TextureCoordinateNode *coordNode = graph->create_node<TextureCoordinateNode>();
293 coordNode->set_ob_tfm(tfm);
294 coordNode->set_use_transform(
true);
296 textureNode = graph->create_node<EnvironmentTextureNode>();
297 static_cast<EnvironmentTextureNode *
>(textureNode)->set_filename(ustring(filename));
299 graph->connect(coordNode->
output(
"Object"), textureNode->
input(
"Vector"));
301 hasSpatialVarying =
true;
304 GeometryNode *coordNode = graph->create_node<GeometryNode>();
306 textureNode = graph->create_node<ImageTextureNode>();
307 static_cast<ImageTextureNode *
>(textureNode)->set_filename(ustring(filename));
309 graph->connect(coordNode->
output(
"Parametric"), textureNode->
input(
"Vector"));
312 if (hasColorTemperature) {
313 VectorMathNode *mathNode = graph->create_node<VectorMathNode>();
316 graph->connect(textureNode->
output(
"Color"), mathNode->
input(
"Vector1"));
317 ShaderInput *
const outputNodeInput = outputNode->
input(
"Color");
318 graph->connect(outputNodeInput->
link, mathNode->
input(
"Vector2"));
319 graph->disconnect(outputNodeInput);
320 graph->connect(mathNode->
output(
"Vector"), outputNodeInput);
322 else if (_lightType == HdPrimTypeTokens->domeLight) {
323 VectorMathNode *mathNode = graph->create_node<VectorMathNode>();
325 mathNode->set_vector2(_light->get_strength());
327 graph->connect(textureNode->
output(
"Color"), mathNode->
input(
"Vector1"));
328 graph->connect(mathNode->
output(
"Vector"), outputNode->
input(
"Color"));
331 graph->connect(textureNode->
output(
"Color"), outputNode->
input(
"Color"));
337 Shader *
const shader = _light->get_shader();
339 shader->
tag_update((Scene *)_light->get_owner());
351 const bool keep_nodes =
static_cast<const HdCyclesSession *
>(renderParam)->keep_nodes;
355 lock.scene->delete_node(_object);
356 lock.scene->delete_node(_light);
363void HdCyclesLight::Initialize(HdRenderParam *renderParam)
369 const SceneLock
lock(renderParam);
372 _object->name = GetId().GetString();
374 if (_lightType == HdPrimTypeTokens->domeLight) {
375 _light =
lock.scene->create_node<BackgroundLight>();
377 else if (_lightType == HdPrimTypeTokens->distantLight) {
378 _light =
lock.scene->create_node<SunLight>();
380 else if (_lightType == HdPrimTypeTokens->diskLight) {
381 _light =
lock.scene->create_node<AreaLight>();
382 static_cast<AreaLight *
>(_light)->set_ellipse(
true);
384 else if (_lightType == HdPrimTypeTokens->rectLight) {
385 _light =
lock.scene->create_node<AreaLight>();
386 static_cast<AreaLight *
>(_light)->set_ellipse(
false);
388 else if (_lightType == HdPrimTypeTokens->sphereLight) {
391 _light =
lock.scene->create_node<SpotLight>();
394 _light->set_use_mis(
true);
395 _light->name = GetId().GetString();
397 _object->set_geometry(_light);
402 array<Node *> used_shaders;
403 used_shaders.push_back_slow(shader);
404 _light->set_used_shaders(used_shaders);
407 PopulateShaderGraph(HdContainerDataSourceHandle());
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(unique_ptr< ShaderGraph > &&graph)
void tag_update(Scene *scene)
T GetTypedValue(const PXR_NS::HdContainerDataSourceHandle &container, const PXR_NS::TfToken &name, const T &fallback=T())
PXR_NS::HdTypedSampledDataSource< T >::Handle GetTypedDataSource(const PXR_NS::HdContainerDataSourceHandle &container, const PXR_NS::TfToken &name)
uint hash_uint2(uint kx, uint ky)
HDCYCLES_NAMESPACE_OPEN_SCOPE Transform convert_transform(const GfMatrix4d &matrix)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
TF_DEFINE_PRIVATE_TOKENS(_tokens,(visibleInPrimaryRay)(treatAsPoint)(falloff))
HDCYCLES_NAMESPACE_OPEN_SCOPE HdSceneIndexPrim GetPrim(HdSceneDelegate *delegate, const SdfPath &id)
@ NODE_VECTOR_MATH_MULTIPLY
@ PATH_RAY_VISIBILITY_CAMERA
@ PATH_RAY_VISIBILITY_ALL
ccl_device_inline float3 one_float3()
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
std::string filename(const std::string_view &str, size_t pos)
constexpr uint64_t hash_string(StringRef str)