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

Added vertex colour utility node and translation for USD preview surface #2016

Merged
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
1 change: 1 addition & 0 deletions lib/usd/pxrUsdPreviewSurface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_library(${TARGET_NAME} SHARED)
# -----------------------------------------------------------------------------
target_sources(${TARGET_NAME}
PRIVATE
cpvColor.cpp
usdPreviewSurface.cpp
usdPreviewSurfacePlugin.cpp
usdPreviewSurfaceReader.cpp
Expand Down
111 changes: 111 additions & 0 deletions lib/usd/pxrUsdPreviewSurface/cpvColor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// Copyright 2021 Animal Logic
//
// 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 "cpvColor.h"

#include <maya/MFnNumericAttribute.h>

PXR_NAMESPACE_OPEN_SCOPE

static const MString cpvInputFragmentName("mayaCPVInput");

const MString CPVColor::name("cpvColor");
const MString CPVColor::userClassification("utility/color:");
const MString CPVColor::drawClassification("drawdb/shader/utility/color/");

const MTypeId CPVColor::id(0x58000098);
MObject CPVColor::aOutColor;
MObject CPVColor::aOutAlpha;
MObject CPVColor::aOutOpacity;

void* CPVColor::creator() { return new CPVColor(); }

CPVColor::CPVColor() { }

CPVColor::~CPVColor() { }

MStatus CPVColor::initialize()
{
// Define implicit shading network attributes
MFnNumericAttribute nAttr;
aOutColor = nAttr.createColor("outColor", "oc");
CHECK_MSTATUS(nAttr.setKeyable(false));
CHECK_MSTATUS(nAttr.setStorable(false));
CHECK_MSTATUS(nAttr.setReadable(true));
CHECK_MSTATUS(nAttr.setWritable(false));
aOutAlpha = nAttr.create("outAlpha", "oa", MFnNumericData::kFloat);
CHECK_MSTATUS(nAttr.setKeyable(false));
CHECK_MSTATUS(nAttr.setStorable(false));
CHECK_MSTATUS(nAttr.setReadable(true));
CHECK_MSTATUS(nAttr.setWritable(false));
CHECK_MSTATUS(nAttr.setHidden(true));
aOutOpacity = nAttr.create("outOpacity", "oo", MFnNumericData::kFloat);
CHECK_MSTATUS(nAttr.setKeyable(false));
CHECK_MSTATUS(nAttr.setStorable(false));
CHECK_MSTATUS(nAttr.setReadable(true));
CHECK_MSTATUS(nAttr.setWritable(false));

// Add attributes
CHECK_MSTATUS(addAttribute(aOutColor));
CHECK_MSTATUS(addAttribute(aOutAlpha));
CHECK_MSTATUS(addAttribute(aOutOpacity));

return MS::kSuccess;
}

MPxNode::SchedulingType CPVColor::schedulingType() const { return SchedulingType::kParallel; }

MStatus CPVColor::compute(const MPlug& plug, MDataBlock& block)
{
if ((plug != aOutColor) && (plug.parent() != aOutColor) && (plug != aOutAlpha)
&& (plug != aOutOpacity))
return MS::kUnknownParameter;

// Complement alpha for opacity attribute
MDataHandle outAlphaHandle = block.outputValue(aOutAlpha);
MDataHandle outOpacityHandle = block.outputValue(aOutOpacity);
float& outOpacity = outOpacityHandle.asFloat();
outOpacity = 1 - outAlphaHandle.asFloat();
outOpacityHandle.setClean();
outAlphaHandle.setClean();

return MS::kSuccess;
}

MHWRender::MPxShadingNodeOverride* CPVColorShadingNodeOverride::creator(const MObject& obj)
{
return new CPVColorShadingNodeOverride(obj);
}

CPVColorShadingNodeOverride::CPVColorShadingNodeOverride(const MObject& obj)
: MPxShadingNodeOverride(obj)
{
}

CPVColorShadingNodeOverride::~CPVColorShadingNodeOverride() { }

MHWRender::DrawAPI CPVColorShadingNodeOverride::supportedDrawAPIs() const
{
return MHWRender::kOpenGL | MHWRender::kDirectX11 | MHWRender::kOpenGLCoreProfile;
}

MString CPVColorShadingNodeOverride::fragmentName() const
{
// Delegate the maya standard CPV input fragment to drive output
return cpvInputFragmentName;
}

PXR_NAMESPACE_CLOSE_SCOPE
91 changes: 91 additions & 0 deletions lib/usd/pxrUsdPreviewSurface/cpvColor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Copyright 2021 Animal Logic
//
// 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 PXRUSDPREVIEWSURFACE_CPVCOLOR_H
#define PXRUSDPREVIEWSURFACE_CPVCOLOR_H

#include "api.h"

#include <pxr/pxr.h>

#include <maya/MPxNode.h>
#include <maya/MPxShadingNodeOverride.h>

PXR_NAMESPACE_OPEN_SCOPE

class CPVColor : public MPxNode
{
public:
PXRUSDPREVIEWSURFACE_API
static const MString name;

PXRUSDPREVIEWSURFACE_API
static const MString userClassification;

PXRUSDPREVIEWSURFACE_API
static const MString drawClassification;

PXRUSDPREVIEWSURFACE_API
static void* creator();

PXRUSDPREVIEWSURFACE_API
CPVColor();

PXRUSDPREVIEWSURFACE_API
~CPVColor() override;

PXRUSDPREVIEWSURFACE_API
static MStatus initialize();

PXRUSDPREVIEWSURFACE_API
SchedulingType schedulingType() const override;

PXRUSDPREVIEWSURFACE_API
MStatus compute(const MPlug& plug, MDataBlock& block) override;

PXRUSDPREVIEWSURFACE_API
static const MTypeId id;

private:
static MObject aOutColor;

static MObject aOutAlpha;

static MObject aOutOpacity;
};

class CPVColorShadingNodeOverride : public MHWRender::MPxShadingNodeOverride
{
public:
PXRUSDPREVIEWSURFACE_API
static MHWRender::MPxShadingNodeOverride* creator(const MObject& obj);

PXRUSDPREVIEWSURFACE_API
~CPVColorShadingNodeOverride() override;

PXRUSDPREVIEWSURFACE_API
MHWRender::DrawAPI supportedDrawAPIs() const override;

PXRUSDPREVIEWSURFACE_API
MString fragmentName() const override;

private:
CPVColorShadingNodeOverride(const MObject& obj);
};

PXR_NAMESPACE_CLOSE_SCOPE

#endif
29 changes: 27 additions & 2 deletions lib/usd/pxrUsdPreviewSurface/usdPreviewSurfacePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//
#include "usdPreviewSurfacePlugin.h"

#include "cpvColor.h"
#include "usdPreviewSurface.h"
#include "usdPreviewSurfaceReader.h"
#include "usdPreviewSurfaceShadingNodeOverride.h"
Expand All @@ -37,7 +38,7 @@ PXR_NAMESPACE_OPEN_SCOPE

namespace {
TfToken::Set _registeredTypeNames;
}
} // namespace
/* static */
MStatus PxrMayaUsdPreviewSurfacePlugin::initialize(
MFnPlugin& plugin,
Expand Down Expand Up @@ -71,6 +72,24 @@ MStatus PxrMayaUsdPreviewSurfacePlugin::initialize(
drawDbClassification, registrantId, PxrMayaUsdPreviewSurfaceShadingNodeOverride::creator);
CHECK_MSTATUS(status);

// Register CPV shader node
const MString cpvDrawClassify(CPVColor::drawClassification + CPVColor::name);
MString cpvUserClassify(CPVColor::userClassification);
cpvUserClassify += cpvDrawClassify;

status = plugin.registerNode(
CPVColor::name,
CPVColor::id,
CPVColor::creator,
CPVColor::initialize,
MPxNode::kDependNode,
&cpvUserClassify);
CHECK_MSTATUS(status);

status = MHWRender::MDrawRegistry::registerShadingNodeOverrideCreator(
cpvDrawClassify, registrantId, CPVColorShadingNodeOverride::creator);
CHECK_MSTATUS(status);

return status;
}

Expand All @@ -93,7 +112,13 @@ MStatus PxrMayaUsdPreviewSurfacePlugin::finalize(

deregisterFragments();

MStatus status = MHWRender::MDrawRegistry::deregisterSurfaceShadingNodeOverrideCreator(
MStatus status = plugin.deregisterNode(CPVColor::id);
CHECK_MSTATUS(status);
status = MHWRender::MDrawRegistry::deregisterShadingNodeOverrideCreator(
CPVColor::drawClassification + CPVColor::name, registrantId);
CHECK_MSTATUS(status);

status = MHWRender::MDrawRegistry::deregisterSurfaceShadingNodeOverrideCreator(
drawDbClassification, registrantId);
CHECK_MSTATUS(status);

Expand Down
1 change: 1 addition & 0 deletions lib/usd/translators/shading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_sources(${TARGET_NAME}
usdPhongEWriter.cpp
usdReflectWriter.cpp
usdUVTextureReader.cpp
usdPrimvarReaderFloat3Reader.cpp
)

if (BUILD_RFM_TRANSLATORS)
Expand Down
2 changes: 1 addition & 1 deletion lib/usd/translators/shading/shadingTokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TF_DEFINE_PUBLIC_TOKENS(TrUsdTokens, TR_USD_COMMON TR_USD_TEXTURE TR_USD_PRIMVAR

TF_DEFINE_PUBLIC_TOKENS(
TrMayaTokens,
TR_MAYA_MATERIALS TR_MAYA_STANDARD_SURFACE TR_MAYA_FILE TR_MAYA_UV);
TR_MAYA_MATERIALS TR_MAYA_STANDARD_SURFACE TR_MAYA_FILE TR_MAYA_UV TR_MAYA_PRIMVAR);

#ifdef WANT_MATERIALX_TRANSLATORS
TF_DEFINE_PUBLIC_TOKENS(
Expand Down
6 changes: 5 additions & 1 deletion lib/usd/translators/shading/shadingTokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ PXR_NAMESPACE_OPEN_SCOPE

#define TR_USD_PRIMVAR \
(UsdPrimvarReader_float2) \
(displayColor) \
(result)

#define TR_USD_XFORM \
Expand Down Expand Up @@ -173,12 +174,15 @@ TF_DECLARE_PUBLIC_TOKENS(TrUsdTokens, , TR_USD_COMMON TR_USD_TEXTURE TR_USD_PRIM
(outUV) \
(uvCoord)

#define TR_MAYA_PRIMVAR \
(cpvColor)

// clang-format on

TF_DECLARE_PUBLIC_TOKENS(
TrMayaTokens,
,
TR_MAYA_MATERIALS TR_MAYA_STANDARD_SURFACE TR_MAYA_FILE TR_MAYA_UV);
TR_MAYA_MATERIALS TR_MAYA_STANDARD_SURFACE TR_MAYA_FILE TR_MAYA_UV TR_MAYA_PRIMVAR);

#ifdef WANT_MATERIALX_TRANSLATORS

Expand Down
Loading