Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HYDRA-1175 : Use LightsManagement scene index instead of removing the… #249

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/flowViewport/API/fvpFilteringSceneIndexClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace FVP_NS_DEF
* @brief Callback function to append a scene index.
*
* This callback function gets called for you to append a scene index to a Hydra viewport scene index, like a filtering scene index.
* A typical case is when a new Hydra viewport is created, after some internal managment of this scene index, we call this function so you can append one scene index
* A typical case is when a new Hydra viewport is created, after some internal management of this scene index, we call this function so you can append one scene index
* or a chain of scene indices and return the last element of the chain.
* The returned value of this function is the last custom scene index of a a chain that you want to append to this scene index,
* or just return the input scene index passed if you don't want to append any scene index.
Expand All @@ -71,7 +71,7 @@ namespace FVP_NS_DEF
* @brief Callback function to append a scene index.
*
* This callback function gets called for you to append a scene index to a Hydra viewport scene index, like a filtering scene index.
* A typical case is when a new Hydra viewport is created, after some internal managment of this scene index, we call this function so you can append one scene index
* A typical case is when a new Hydra viewport is created, after some internal management of this scene index, we call this function so you can append one scene index
* or a chain of scene indices and return the last element of the chain.
* The returned value of this function is the last custom scene index of a a chain that you want to append to this scene index,
* or just return the input scene index passed if you don't want to append any scene index.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class FVP_API FilteringSceneIndexClientExample : public FilteringSceneIndexClien
* @brief Callback function to append a scene index.
*
* This callback function gets called for you to append a scene index to a Hydra viewport scene index, like a filtering scene index.
* A typical case is when a new Hydra viewport is created, after some internal managment of this scene index, we call this function so you can append one scene index
* A typical case is when a new Hydra viewport is created, after some internal management of this scene index, we call this function so you can append one scene index
* or a chain of scene indices and return the last element of the chain.
* The returned value of this function is the last custom scene index of a a chain that you want to append to this scene index,
* or just return the input scene index passed if you don't want to append any scene index.
Expand Down
171 changes: 115 additions & 56 deletions lib/flowViewport/sceneIndex/fvpLightsManagementSceneIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,89 @@
#include <ufe/globalSelection.h>
#include <ufe/observableSelection.h>

//std
#include <array>


PXR_NAMESPACE_USING_DIRECTIVE

namespace {

// Helper function to extract a value from a light data source.
template <typename T>
T _GetLightData(const HdContainerDataSourceHandle& primDataSource, const TfToken& name)
{
if (auto lightSchema = HdLightSchema::GetFromParent(primDataSource)) {
if (auto dataSource
= HdTypedSampledDataSource<T>::Cast(lightSchema.GetContainer()->Get(name))) {
return dataSource->GetTypedValue(0.0f);
}
}

return {};
}

void _DisableLight(HdSceneIndexPrim& prim)
{
HdContainerDataSourceEditor editor(prim.dataSource);
//We don't set the intensity to 0 as for domelights this makes the geometry disappear
for (const auto& t : { HdLightTokens->ambient, HdLightTokens->diffuse, HdLightTokens->specular }) {

const bool isSimpleLight = prim.primType == HdPrimTypeTokens->simpleLight;

if (isSimpleLight) {
// A simple light contains in its params a GlfSimpleLight which needs to be disabled
// by setting its diffuse, specular and ambient to 0
GlfSimpleLight simpleLight
= _GetLightData<GlfSimpleLight>(prim.dataSource, HdTokens->params);
simpleLight.SetDiffuse(GfVec4f(0.0f));
simpleLight.SetSpecular(GfVec4f(0.0f));
simpleLight.SetAmbient(GfVec4f(0.0f));
editor.Set(
HdLightSchema::GetDefaultLocator().Append(t),
HdRetainedTypedSampledDataSource<float>::New(0.0f));
HdLightSchema::GetDefaultLocator().Append(HdTokens->params),
HdRetainedTypedSampledDataSource<GlfSimpleLight>::New(simpleLight));
} else {
// We don't set the intensity to 0 as for domelights this makes the geometry disappear
static const std::array<TfToken, 3> lightTokens
= { HdLightTokens->ambient, HdLightTokens->diffuse, HdLightTokens->specular };
for (const auto& token : lightTokens) {
editor.Set(
HdLightSchema::GetDefaultLocator().Append(token),
HdRetainedTypedSampledDataSource<float>::New(0.0f));
}
}

prim.dataSource = editor.Finish();
}

bool _IsPrimOrAncestorSelected(const SdfPath& primPath)
{
const Ufe::Selection& ufeSelection = *Ufe::GlobalSelection::get();
if (ufeSelection.empty()) {
return false;
}

// Convert UFE selection to SdfPath
SdfPathVector selectedSdfPath;
for (const auto& snItem : ufeSelection) {
auto primSelections = Fvp::ufePathToPrimSelections(snItem->path());
for (const auto& primSelection : primSelections) {
selectedSdfPath.push_back(primSelection.primPath);
}
}

if (std::find(selectedSdfPath.cbegin(), selectedSdfPath.cend(), primPath)
!= selectedSdfPath.cend()) {
return true;
}

for (const auto& selectedPath : selectedSdfPath) {
if (primPath.HasPrefix(selectedPath)) {
return true;
}
}

return false;
}

} // end of anonymous namespace

/// This is a filtering scene index that manages lights primitives
Expand Down Expand Up @@ -91,60 +156,54 @@ bool LightsManagementSceneIndex::_IsDefaultLight(const SdfPath& primPath)const
return primPath == _defaultLightPath;
}

HdSceneIndexPrim LightsManagementSceneIndex::GetPrim(const SdfPath& primPath) const
HdSceneIndexPrim LightsManagementSceneIndex::GetPrim(const SdfPath& primPath) const
{
auto prim = GetInputSceneIndex()->GetPrim(primPath);
auto primType = prim.primType;
if (! HdPrimTypeIsLight(primType)) {
return prim;//return any non light primitive
}

//This is a light
switch (_lightingMode) {
case LightingMode::kNoLighting: {
_DisableLight(prim);
return prim;
} break;
default:
case LightingMode::kSceneLighting: {
return prim;
} break;
case LightingMode::kDefaultLighting: {
if (! _IsDefaultLight(primPath)){
_DisableLight(prim);
}
return prim;
} break;
case LightingMode::kSelectedLightsOnly: {
const Ufe::Selection& ufeSelection = *Ufe::GlobalSelection::get();
if (ufeSelection.empty()) {
// Nothing is selected
_DisableLight(prim);
return prim;
}

//Convert ufe selection to SdfPath
SdfPathVector selectedLightsSdfPath;
for (const auto& snItem : ufeSelection) {
auto primSelections = ufePathToPrimSelections(snItem->path());
for (const auto& primSelection : primSelections) {
selectedLightsSdfPath.push_back(primSelection.primPath);
}
}
const bool isSelected = selectedLightsSdfPath.end()
!= std::find(selectedLightsSdfPath.begin(),
selectedLightsSdfPath.end(),
primPath);

if (! isSelected) {
_DisableLight(prim);
}

return prim;
} break;
}

return prim;

if (!HdPrimTypeIsLight(primType)) {
return prim; // Return any non-light primitive
}

// This is a light
switch (_lightingMode) {
case LightingMode::kNoLighting:
_DisableLight(prim);
break;

case LightingMode::kSceneLighting:
//Disable non active lights from maya native data
if (_disabledLightsPrims.find(primPath) != _disabledLightsPrims.end()) {
_DisableLight(prim);
}
break;

case LightingMode::kDefaultLighting:
if (!_IsDefaultLight(primPath)) {
_DisableLight(prim);
}
break;

case LightingMode::kSelectedLightsOnly: {
const bool shouldBeUsedForLigthing = _IsPrimOrAncestorSelected(primPath);
if (! shouldBeUsedForLigthing) {
_DisableLight(prim);
}
break;
}
}

return prim;
}

void LightsManagementSceneIndex::SetDisabledLightsPrims(
const std::set<PXR_NS::SdfPath>& activeLightsPrims)
{
if (_disabledLightsPrims != activeLightsPrims) {
_disabledLightsPrims = activeLightsPrims;
_DirtyAllLightsPrims();
}
}


}//end of namespace FVP_NS_DEF
12 changes: 9 additions & 3 deletions lib/flowViewport/sceneIndex/fvpLightsManagementSceneIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ class LightsManagementSceneIndex : public PXR_NS::HdSingleInputFilteringSceneInd
FVP_API
LightingMode GetLightingMode()const {return _lightingMode;}

protected:
FVP_API
void SetDisabledLightsPrims(const std::set<PXR_NS::SdfPath>& activeLightsPrims);

LightsManagementSceneIndex(const PXR_NS::HdSceneIndexBaseRefPtr& inputSceneIndex, const PXR_NS::SdfPath& defaultLightPath);
private:
LightsManagementSceneIndex(
const PXR_NS::HdSceneIndexBaseRefPtr& inputSceneIndex,
const PXR_NS::SdfPath& defaultLightPath);

//From HdSingleInputFilteringSceneIndexBase
void _PrimsAdded(const PXR_NS::HdSceneIndexBase& sender, const PXR_NS::HdSceneIndexObserver::AddedPrimEntries& entries) override{
Expand All @@ -89,11 +93,13 @@ class LightsManagementSceneIndex : public PXR_NS::HdSingleInputFilteringSceneInd
_SendPrimsDirtied(entries);
}

void _DirtyAllLightsPrims();

bool _IsDefaultLight(const PXR_NS::SdfPath& primPath)const;
void _DirtyAllLightsPrims();

LightingMode _lightingMode = LightingMode::kSceneLighting;
PXR_NS::SdfPath _defaultLightPath;
std::set<PXR_NS::SdfPath> _disabledLightsPrims;
};

}//end of namespace FVP_NS_DEF
Expand Down
10 changes: 0 additions & 10 deletions lib/mayaHydra/hydraExtensions/adapters/lightAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,4 @@ bool MayaHydraLightAdapter::_GetVisibility() const
return false;
}

void MayaHydraLightAdapter::SetLightingOn(bool isLightingOn)
{
if (_isLightingOn != isLightingOn) {
_isLightingOn = isLightingOn;

RemovePrim();
Populate();
}
}

PXR_NAMESPACE_CLOSE_SCOPE
4 changes: 1 addition & 3 deletions lib/mayaHydra/hydraExtensions/adapters/lightAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ class MayaHydraLightAdapter : public MayaHydraDagAdapter
virtual void CreateCallbacks() override;
MAYAHYDRALIB_API
void SetShadowProjectionMatrix(const GfMatrix4d& matrix);
MAYAHYDRALIB_API
void SetLightingOn(bool isLightingOn);


protected:
MAYAHYDRALIB_API
virtual void _CalculateLightParams(GlfSimpleLight& light) { }
Expand Down
22 changes: 18 additions & 4 deletions lib/mayaHydra/hydraExtensions/mayaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,28 @@ MStatus GetObjectsFromNodeNames(const MStringArray& nodeNames, MObjectArray & ou
return MS::kSuccess;
}

bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath)
{
bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath)
{
static const MString _aiSkyDomeLight("aiSkyDomeLight");

if (! dagPath.isValid()) return false;

if (!dagPath.isValid()) {
return false;
}
auto shapeDagPath = dagPath;
shapeDagPath.extendToShape();
return _aiSkyDomeLight == MFnDependencyNode(shapeDagPath.node()).typeName();
}

bool IsDagPathALight(const MDagPath& dagPath)
{
static const MString _lightString("Light");

if (!dagPath.isValid())
return false;
auto shapeDagPath = dagPath;
shapeDagPath.extendToShape();
const MString typeName = MFnDependencyNode(shapeDagPath.node()).typeName();
return (typeName.indexW(_lightString) != -1);//Does the typename contains "Light"
}

} // namespace MAYAHYDRA_NS_DEF
11 changes: 10 additions & 1 deletion lib/mayaHydra/hydraExtensions/mayaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,19 @@ bool SetNodeAttribute(MObject node, std::string attrName, AttrType newValue)
*
* @param[in] dagPath is a MDagPath
*
* @return true if the object is a sky dome light, false otherwise
* @return true if the object is an Arnold sky dome light, false otherwise
*/
bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath);

/**
* @brief Get if this MDagPath is a light.
*
* @param[in] dagPath is a MDagPath
*
* @return true if the object is a light, false otherwise
*/
bool IsDagPathALight(const MDagPath& dagPath);

} // namespace MAYAHYDRA_NS_DEF

#endif // MAYAHYDRALIB_MAYA_UTILS_H
Loading
Loading