diff --git a/lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.cpp b/lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.cpp index 3de451b770..a23cb088da 100644 --- a/lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.cpp +++ b/lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -137,9 +138,36 @@ PxrMayaHdSceneDelegate::PxrMayaHdSceneDelegate( renderIndex->InsertSprim(HdPrimTypeTokens->camera, this, _cameraId); _ValueCache& cache = _valueCacheMap[_cameraId]; + cache[HdTokens->transform] = VtValue(GfMatrix4d(1.0)); + cache[HdCameraTokens->windowPolicy] = VtValue(CameraUtilFit); + cache[HdCameraTokens->clipPlanes] = VtValue(std::vector()); +#if PXR_VERSION >= 2102 + cache[HdCameraTokens->projection] = VtValue(HdCamera::Orthographic); + cache[HdCameraTokens->horizontalAperture] = VtValue(2.0f * float(GfCamera::APERTURE_UNIT)); + cache[HdCameraTokens->verticalAperture] = VtValue(2.0f * float(GfCamera::APERTURE_UNIT)); + cache[HdCameraTokens->horizontalApertureOffset] = VtValue(0.0f); + cache[HdCameraTokens->verticalApertureOffset] = VtValue(0.0f); + cache[HdCameraTokens->focalLength] = VtValue(5.0f * float(GfCamera::FOCAL_LENGTH_UNIT)); + cache[HdCameraTokens->clippingRange] = VtValue(GfRange1f(-1.0f, 1.0f)); +#else cache[HdCameraTokens->worldToViewMatrix] = VtValue(GfMatrix4d(1.0)); cache[HdCameraTokens->projectionMatrix] = VtValue(GfMatrix4d(1.0)); - cache[HdCameraTokens->windowPolicy] = VtValue(CameraUtilFit); + + cache[HdCameraTokens->horizontalAperture] = VtValue(); + cache[HdCameraTokens->verticalAperture] = VtValue(); + cache[HdCameraTokens->horizontalApertureOffset] = VtValue(); + cache[HdCameraTokens->verticalApertureOffset] = VtValue(); + cache[HdCameraTokens->focalLength] = VtValue(); + cache[HdCameraTokens->clippingRange] = VtValue(); +#endif + + cache[HdCameraTokens->fStop] = VtValue(); + cache[HdCameraTokens->focusDistance] = VtValue(); + cache[HdCameraTokens->shutterOpen] = VtValue(); + cache[HdCameraTokens->shutterClose] = VtValue(); +#if PXR_VERSION >= 2011 + cache[HdCameraTokens->exposure] = VtValue(); +#endif } // Simple lighting task. @@ -220,6 +248,13 @@ PxrMayaHdSceneDelegate::GetCameraParamValue(SdfPath const& cameraId, TfToken con return Get(cameraId, paramName); } +/*virtual*/ +GfMatrix4d PxrMayaHdSceneDelegate::GetTransform(SdfPath const& id) +{ + static const GfMatrix4d fallback(1.0); + return Get(id, HdTokens->transform).GetWithDefault(fallback); +} + TfTokenVector PxrMayaHdSceneDelegate::GetTaskRenderTags(SdfPath const& taskId) { VtValue value = Get(taskId, HdTokens->renderTags); @@ -227,6 +262,18 @@ TfTokenVector PxrMayaHdSceneDelegate::GetTaskRenderTags(SdfPath const& taskId) return value.Get(); } +#if PXR_VERSION >= 2102 +static HdCamera::Projection _ToHd(const GfCamera::Projection projection) +{ + switch (projection) { + case GfCamera::Perspective: return HdCamera::Perspective; + case GfCamera::Orthographic: return HdCamera::Orthographic; + } + TF_CODING_ERROR("Bad GfCamera::Projection value"); + return HdCamera::Perspective; +} +#endif + void PxrMayaHdSceneDelegate::SetCameraState( const GfMatrix4d& worldToViewMatrix, const GfMatrix4d& projectionMatrix, @@ -234,22 +281,38 @@ void PxrMayaHdSceneDelegate::SetCameraState( { // cache the camera matrices _ValueCache& cache = _valueCacheMap[_cameraId]; - cache[HdCameraTokens->worldToViewMatrix] = VtValue(worldToViewMatrix); - cache[HdCameraTokens->projectionMatrix] = VtValue(projectionMatrix); cache[HdCameraTokens->windowPolicy] = VtValue(CameraUtilFit); cache[HdCameraTokens->clipPlanes] = VtValue(std::vector()); - // Provide other keys so that pulling on those in HdCamera::Sync doesn't - // trigger coding errors. #if PXR_VERSION >= 2102 - cache[HdCameraTokens->projection] = VtValue(); -#endif + GfCamera cam; + cam.SetFromViewAndProjectionMatrix(worldToViewMatrix, projectionMatrix); + cache[HdTokens->transform] = VtValue(cam.GetTransform()); + + cache[HdCameraTokens->projection] = VtValue(_ToHd(cam.GetProjection())); + cache[HdCameraTokens->horizontalAperture] + = VtValue(cam.GetHorizontalAperture() * float(GfCamera::APERTURE_UNIT)); + cache[HdCameraTokens->verticalAperture] + = VtValue(cam.GetVerticalAperture() * float(GfCamera::APERTURE_UNIT)); + cache[HdCameraTokens->horizontalApertureOffset] + = VtValue(cam.GetHorizontalApertureOffset() * float(GfCamera::APERTURE_UNIT)); + cache[HdCameraTokens->verticalApertureOffset] + = VtValue(cam.GetVerticalApertureOffset() * float(GfCamera::APERTURE_UNIT)); + cache[HdCameraTokens->focalLength] + = VtValue(cam.GetFocalLength() * float(GfCamera::FOCAL_LENGTH_UNIT)); + cache[HdCameraTokens->clippingRange] = VtValue(cam.GetClippingRange()); +#else + cache[HdTokens->transform] = VtValue(worldToViewMatrix.GetInverse()); + cache[HdCameraTokens->worldToViewMatrix] = VtValue(worldToViewMatrix); + cache[HdCameraTokens->projectionMatrix] = VtValue(projectionMatrix); cache[HdCameraTokens->horizontalAperture] = VtValue(); cache[HdCameraTokens->verticalAperture] = VtValue(); cache[HdCameraTokens->horizontalApertureOffset] = VtValue(); cache[HdCameraTokens->verticalApertureOffset] = VtValue(); cache[HdCameraTokens->focalLength] = VtValue(); cache[HdCameraTokens->clippingRange] = VtValue(); +#endif + cache[HdCameraTokens->clipPlanes] = VtValue(); cache[HdCameraTokens->fStop] = VtValue(); cache[HdCameraTokens->focusDistance] = VtValue(); diff --git a/lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.h b/lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.h index 9f3188125c..3cc2819fb1 100644 --- a/lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.h +++ b/lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.h @@ -74,6 +74,9 @@ class PxrMayaHdSceneDelegate : public HdSceneDelegate MAYAUSD_CORE_PUBLIC VtValue GetCameraParamValue(SdfPath const& cameraId, TfToken const& paramName) override; + MAYAUSD_CORE_PUBLIC + GfMatrix4d GetTransform(SdfPath const& id) override; + MAYAUSD_CORE_PUBLIC TfTokenVector GetTaskRenderTags(SdfPath const& taskId) override; diff --git a/lib/usd/hdMaya/adapters/cameraAdapter.cpp b/lib/usd/hdMaya/adapters/cameraAdapter.cpp index 69c6ce7d83..272179200e 100644 --- a/lib/usd/hdMaya/adapters/cameraAdapter.cpp +++ b/lib/usd/hdMaya/adapters/cameraAdapter.cpp @@ -72,9 +72,11 @@ void HdMayaCameraAdapter::Populate() void HdMayaCameraAdapter::MarkDirty(HdDirtyBits dirtyBits) { if (_isPopulated && dirtyBits != 0) { +#if PXR_VERSION < 2102 if (dirtyBits & HdChangeTracker::DirtyTransform) { dirtyBits |= HdCamera::DirtyViewMatrix; } +#endif dirtyBits = dirtyBits & HdCamera::AllDirty; GetDelegate()->GetChangeTracker().MarkSprimDirty(GetID(), dirtyBits); } @@ -104,7 +106,11 @@ void HdMayaCameraAdapter::CreateCallbacks() dag, +[](MObject& transformNode, MDagMessage::MatrixModifiedFlags& modified, void* clientData) { auto* adapter = reinterpret_cast(clientData); +#if PXR_VERSION < 2102 adapter->MarkDirty(HdCamera::DirtyViewMatrix); +#else + adapter->MarkDirty(HdCamera::DirtyTransform); +#endif adapter->InvalidateTransform(); }, reinterpret_cast(this), @@ -176,6 +182,8 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName) aspectRatio, apertureX, apertureY, offsetX, offsetY, true, false, true); }; +#if PXR_VERSION < 2102 + auto projectionMatrix = [&](const MFnCamera& camera, bool isOrtho, const GfVec4d* viewport) -> GfMatrix4d { double left, right, bottom, top, cameraNear = camera.nearClippingPlane(), @@ -266,6 +274,8 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName) 0); }; +#endif + auto hadError = [&](MStatus& status) -> bool { if (ARCH_LIKELY(status)) return false; @@ -285,6 +295,7 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName) return {}; } +#if PXR_VERSION < 2102 if (paramName == HdCameraTokens->projectionMatrix) { const auto projMatrix = projectionMatrix(camera, isOrtho, _viewport.get()); if (hadError(status)) @@ -294,6 +305,8 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName) if (paramName == HdCameraTokens->worldToViewMatrix) { return VtValue(GetTransform().GetInverse()); } +#endif + if (paramName == HdCameraTokens->shutterOpen) { // No motion samples, instantaneous shutter if (!GetDelegate()->GetParams().motionSamplesEnabled()) @@ -324,10 +337,19 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName) return VtValue(float(focusDistance * mayaInchToHydraCentimeter)); } if (paramName == HdCameraTokens->focalLength) { - GfMatrix4d glProjMatrix = projectionMatrix(camera, isOrtho, _viewport.get()); - const int index - = convertFit(camera) == CameraUtilConformWindowPolicy::CameraUtilMatchVertically; - const auto focalLen = glProjMatrix[index][index]; + const double aspectRatio = _viewport + ? (((*_viewport)[2] - (*_viewport)[0]) / ((*_viewport)[3] - (*_viewport)[1])) + : camera.aspectRatio(); + + double left, right, bottom, top; + status = camera.getViewingFrustum(aspectRatio, left, right, bottom, top, true, false, true); + + const double cameraNear = camera.nearClippingPlane(); + + const double focalLen + = (convertFit(camera) == CameraUtilConformWindowPolicy::CameraUtilMatchVertically) + ? (2.0 * cameraNear) / (top - bottom) + : (2.0 * cameraNear) / (right - left); return VtValue(float(focalLen * mayaFocaLenToHydra)); } if (paramName == HdCameraTokens->fStop) { @@ -373,6 +395,16 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName) return {}; return VtValue(windowPolicy); } +#if PXR_VERSION >= 2102 + if (paramName == HdCameraTokens->projection) { + if (isOrtho) { + return VtValue(HdCamera::Orthographic); + } else { + return VtValue(HdCamera::Perspective); + } + } + +#endif return {}; }