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

Lanierd/hydra 311 #149

Merged
merged 13 commits into from
Jul 18, 2024
Merged
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
2 changes: 2 additions & 0 deletions lib/flowViewport/sceneIndex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_sources(${TARGET_NAME}
fvpBBoxSceneIndex.cpp
fvpReprSelectorSceneIndex.cpp
fvpBlockPrimRemovalPropagationSceneIndex.cpp
fvpDefaultMaterialSceneIndex.cpp
)

set(HEADERS
Expand All @@ -31,6 +32,7 @@ set(HEADERS
fvpBBoxSceneIndex.h
fvpReprSelectorSceneIndex.h
fvpBlockPrimRemovalPropagationSceneIndex.h
fvpDefaultMaterialSceneIndex.h
)

# -----------------------------------------------------------------------------
Expand Down
135 changes: 135 additions & 0 deletions lib/flowViewport/sceneIndex/fvpDefaultMaterialSceneIndex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2024 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "flowViewport/sceneIndex/fvpDefaultMaterialSceneIndex.h"

#include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hd/sceneIndexPrimView.h>
#include <pxr/imaging/hd/materialSchema.h>
#include <pxr/imaging/hd/materialBindingSchema.h>
#include <pxr/imaging/hd/materialBindingsSchema.h>
#include <pxr/imaging/hd/containerDataSourceEditor.h>
#include <pxr/imaging/hd/retainedDataSource.h>

namespace FVP_NS_DEF {

PXR_NAMESPACE_USING_DIRECTIVE

namespace{
static const TfToken purposes[] = { HdMaterialBindingsSchemaTokens->allPurpose };
}

// static
DefaultMaterialSceneIndexRefPtr
DefaultMaterialSceneIndex::New(
const HdSceneIndexBaseRefPtr &inputSceneIndex,
const PXR_NS::SdfPath& defaultMaterialPath,
const PXR_NS::SdfPathVector& defaultMaterialExclusionList)
{
return TfCreateRefPtr(
new DefaultMaterialSceneIndex(inputSceneIndex, defaultMaterialPath, defaultMaterialExclusionList));
}

DefaultMaterialSceneIndex::DefaultMaterialSceneIndex(
HdSceneIndexBaseRefPtr const &inputSceneIndex,
const PXR_NS::SdfPath& defaultMaterialPath,
const PXR_NS::SdfPathVector& defaultMaterialExclusionList)
: HdSingleInputFilteringSceneIndexBase(inputSceneIndex),
InputSceneIndexUtils(inputSceneIndex),
_defaultMaterialPath(defaultMaterialPath),
_defaultMaterialExclusionList(defaultMaterialExclusionList)
{
}

HdSceneIndexPrim DefaultMaterialSceneIndex::GetPrim(const SdfPath& primPath) const
{
HdSceneIndexPrim prim = GetInputSceneIndex()->GetPrim(primPath);
if (! _isEnabled){
return prim;
}

_SetDefaultMaterial(prim);

return prim;
}

bool DefaultMaterialSceneIndex::_ShouldWeApplyTheDefaultMaterial(const HdSceneIndexPrim& prim)const
{
// Only for meshes so far
if (HdPrimTypeTokens->mesh != prim.primType) {
return false;
}

// Check if it has a material which is not in the exclusion list
HdMaterialBindingsSchema bindings = HdMaterialBindingsSchema::GetFromParent(prim.dataSource);
HdMaterialBindingSchema binding = bindings.GetMaterialBinding();
HdPathDataSourceHandle bindingPathDS = binding.GetPath();
if (bindingPathDS) { // If a mesh prim has no material( bindingPathDS is empty), apply anyway the default material
const SdfPath materialPath = bindingPathDS->GetTypedValue(0.0f);
auto foundExcludedMaterialPath = std::find(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What size is this _defaultMaterialExclusionList expected to be? We are looping over this for each prim. If the list size is small, it's fine, but otherwise we can consider using an std::set.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only one at this time, do you think I should switch to a std::set now ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just add a comment mentioning this as a possibility if the defaultMaterialExclusionList becomes large.

_defaultMaterialExclusionList.cbegin(),
_defaultMaterialExclusionList.cend(),
materialPath);
if (foundExcludedMaterialPath != _defaultMaterialExclusionList.cend()) {
return false; // materialPath is in the exclusion list !
}
}

return true;
}

void DefaultMaterialSceneIndex::_SetDefaultMaterial(HdSceneIndexPrim& inoutPrim)const
{
static HdDataSourceBaseHandle const materialBindingSources[]
= { HdMaterialBindingSchema::Builder()
.SetPath(HdRetainedTypedSampledDataSource<SdfPath>::New(_defaultMaterialPath))
.Build() };

if (_ShouldWeApplyTheDefaultMaterial(inoutPrim)) {
inoutPrim.dataSource = HdContainerDataSourceEditor(inoutPrim.dataSource)
.Set(
HdMaterialBindingsSchema::GetDefaultLocator(),
HdMaterialBindingsSchema::BuildRetained(TfArraySize(purposes), purposes, materialBindingSources)
)
.Finish();
}
}

void DefaultMaterialSceneIndex::Enable(bool enable)
{
if (_isEnabled == enable){
return;
}

_isEnabled = enable;
_MarkMaterialsDirty();
}

void DefaultMaterialSceneIndex::_MarkMaterialsDirty()
{
static const auto locator = HdMaterialBindingsSchema::GetDefaultLocator();

HdSceneIndexObserver::DirtiedPrimEntries entries;
for (const SdfPath& primPath : HdSceneIndexPrimView(GetInputSceneIndex())) {
HdSceneIndexPrim prim = GetInputSceneIndex()->GetPrim(primPath);
// Dirty only prims where we should apply the default material
if (_ShouldWeApplyTheDefaultMaterial(prim)) {
entries.push_back({ primPath, locator });
}
}
_SendPrimsDirtied(entries);
}

} //end of namespace FVP_NS_DEF
98 changes: 98 additions & 0 deletions lib/flowViewport/sceneIndex/fvpDefaultMaterialSceneIndex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2024 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef FVP_DEFAULT_MATERIAL_SCENE_INDEX_H
#define FVP_DEFAULT_MATERIAL_SCENE_INDEX_H

#include "flowViewport/api.h"
#include "flowViewport/sceneIndex/fvpSceneIndexUtils.h"

#include <pxr/imaging/hd/filteringSceneIndex.h>

namespace FVP_NS_DEF {

class DefaultMaterialSceneIndex;
typedef PXR_NS::TfRefPtr<DefaultMaterialSceneIndex> DefaultMaterialSceneIndexRefPtr;
typedef PXR_NS::TfRefPtr<const DefaultMaterialSceneIndex> DefaultMaterialSceneIndexConstRefPtr;

class DefaultMaterialSceneIndex :
public PXR_NS::HdSingleInputFilteringSceneIndexBase
, public Fvp::InputSceneIndexUtils<DefaultMaterialSceneIndex>
{
public:
using ParentClass = PXR_NS::HdSingleInputFilteringSceneIndexBase;
using ParentClass::_GetInputSceneIndex;

FVP_API
static DefaultMaterialSceneIndexRefPtr New(
const PXR_NS::HdSceneIndexBaseRefPtr &inputScene,
const PXR_NS::SdfPath& defaultMaterialPath,
const PXR_NS::SdfPathVector& defaultMaterialExclusionList
);

FVP_API
void Enable(bool enable);


protected:
void _MarkMaterialsDirty();

// From HdSceneIndexBase
PXR_NS::HdSceneIndexPrim GetPrim(const PXR_NS::SdfPath& primPath) const override;

PXR_NS::SdfPathVector GetChildPrimPaths(const PXR_NS::SdfPath& primPath) const override {
return GetInputSceneIndex()->GetChildPrimPaths(primPath);
}

// From HdSingleInputFilteringSceneIndexBase
void _PrimsAdded(
const PXR_NS::HdSceneIndexBase& sender,
const PXR_NS::HdSceneIndexObserver::AddedPrimEntries& entries) override{
if (!_IsObserved())
return;
_SendPrimsAdded(entries);
}

void _PrimsRemoved(
const PXR_NS::HdSceneIndexBase& sender,
const PXR_NS::HdSceneIndexObserver::RemovedPrimEntries& entries) override
{
if (!_IsObserved())
return;
_SendPrimsRemoved(entries);
}
void _PrimsDirtied(
const PXR_NS::HdSceneIndexBase& sender,
const PXR_NS::HdSceneIndexObserver::DirtiedPrimEntries& entries) override
{
if (!_IsObserved())
return;
_SendPrimsDirtied(entries);
}

void _SetDefaultMaterial(PXR_NS::HdSceneIndexPrim& inoutPrim) const;
bool _ShouldWeApplyTheDefaultMaterial(const PXR_NS::HdSceneIndexPrim& prim)const;

private:
bool _isEnabled = false;
const PXR_NS::SdfPath _defaultMaterialPath;
PXR_NS::SdfPathVector _defaultMaterialExclusionList;//These are the materials that should not be affected by the default material, they should be skipped

DefaultMaterialSceneIndex(
PXR_NS::HdSceneIndexBaseRefPtr const &inputSceneIndex, const PXR_NS::SdfPath& defaultMaterialPath, const PXR_NS::SdfPathVector& defaultMaterialExclusionList);
};

} //end of namespace FVP_NS_DEF

#endif //FVP_DEFAULT_MATERIAL_SCENE_INDEX_H
26 changes: 26 additions & 0 deletions lib/mayaHydra/hydraExtensions/mayaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <maya/MGlobal.h>
#include <maya/MMatrix.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MSelectionList.h>
#include <maya/MObjectArray.h>
#include <maya/MFnAttribute.h>
Expand Down Expand Up @@ -158,4 +159,29 @@ bool IsAMayaVisibilityAttribute(const MPlug& plug, bool& outVal)
return isVisibility;
}

MObject GetShadingGroupFromShader(const MObject& shader)
{
MObject shadingGroup;
MFnDependencyNode fn(shader);

// Get the "outColor" plug of the shader
MPlug outColorPlug = fn.findPlug("outColor", true);

// Get the connected plugs
MPlugArray connectedPlugs;
outColorPlug.connectedTo(connectedPlugs, false, true);

// Loop over the connected plugs
for (unsigned int i = 0; i < connectedPlugs.length(); ++i) {
MObject node = connectedPlugs[i].node();
if (node.apiType() == MFn::kShadingEngine) // Check if the node is a shading group
{
shadingGroup = node;
break;
}
}

return shadingGroup;
}

} // namespace MAYAHYDRA_NS_DEF
9 changes: 9 additions & 0 deletions lib/mayaHydra/hydraExtensions/mayaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ bool SetNodeAttribute(MObject node, std::string attrName, AttrType newValue)
return plug.setValue(newValue);
}

/**
* @brief Get the shading group MObject from a shader MObject.
*
* @param[in] shader is the MObject of the shader
*
* @return the MObject of the shading group
*/
MObject GetShadingGroupFromShader(const MObject& shader);

} // namespace MAYAHYDRA_NS_DEF

#endif // MAYAHYDRALIB_MAYA_UTILS_H
Loading