From 59cb83a5fccf97140ca485ba77a0bb63ca66d4ce Mon Sep 17 00:00:00 2001 From: unhyperbolic Date: Fri, 5 Aug 2022 11:28:49 -0700 Subject: [PATCH] HdPrman_CameraContext: sanitizing focalDistance and fStop values when setting the camera shader params and clean-up of that code. This in anticipation to switching to PxrProjection which is more sensitive to invalid values. (Internal change: 2244556) --- .../plugin/hdPrman/cameraContext.cpp | 79 +++++++++++-------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/third_party/renderman-24/plugin/hdPrman/cameraContext.cpp b/third_party/renderman-24/plugin/hdPrman/cameraContext.cpp index 1618c6afef..5aff006611 100644 --- a/third_party/renderman-24/plugin/hdPrman/cameraContext.cpp +++ b/third_party/renderman-24/plugin/hdPrman/cameraContext.cpp @@ -261,55 +261,72 @@ _ComputeProjectionShader(const HdCamera::Projection projection) return us_PxrPerspective; } -// Compute parameters for the camera riley::ShadingNode. -static +// Compute parameters for the camera riley::ShadingNode for perspective camera RtParamList -_ComputeNodeParams(const HdCamera * const camera) +_ComputePerspectiveNodeParams(const HdCamera * const camera) { RtParamList result; - // Following parameters can be set on the projection shader: - // fov (currently unhandled) - // fovEnd (currently unhandled) - // fStop - // focalLength - // focalDistance - // RenderMan defines disabled DOF as fStop=inf not zero - - float fStop = RI_INFINITY; - const float cameraFStop = camera->GetFStop(); - if (cameraFStop > 0.0) { - fStop = cameraFStop; - } - result.SetFloat(RixStr.k_fStop, fStop); - - // Do not use the initial value 0 which we get if the scene delegate - // did not provide a focal length. + // FOV settings. const float focalLength = camera->GetFocalLength(); if (focalLength > 0) { result.SetFloat(RixStr.k_focalLength, focalLength); + const float r = camera->GetHorizontalAperture() / focalLength; + const float fov = 2.0f * GfRadiansToDegrees(std::atan(0.5f * r)); + result.SetFloat(RixStr.k_fov, fov); + } else { + // If focal length is bogus, don't set it. + // Fallback to sane FOV. + result.SetFloat(RixStr.k_fov, 90.0f); } - // Similar for focus distance. + // Depth of field settings. const float focusDistance = camera->GetFocusDistance(); - if (focusDistance > 0) { + if (focusDistance > 0.0f) { result.SetFloat(RixStr.k_focalDistance, focusDistance); + } else { + // If value is bogus, set to sane value. + result.SetFloat(RixStr.k_focalDistance, 1000.0f); } - if (camera->GetProjection() == HdCamera::Perspective) { - float fov = 90.0f; - - if (focalLength > 0) { - const float h = camera->GetHorizontalAperture(); - fov = 2.0f * GfRadiansToDegrees(std::atan(0.5f * h / focalLength)); - } - - result.SetFloat(RixStr.k_fov, fov); + const float fStop = camera->GetFStop(); + if (fStop > 0.0f && focusDistance > 0.0f) { + result.SetFloat(RixStr.k_fStop, fStop); + } else { + // If values are bogus, disable depth of field by setting + // ininie f-Stop and a sane value for focalDistance. + result.SetFloat(RixStr.k_fStop, RI_INFINITY); } + // Not setting fov frame begin/end - thus we do not support motion blur + // due to changing FOV. + return result; } +// Compute parameters for the camera riley::ShadingNode for orthographic camera +RtParamList +_ComputeOrthographicNodeParams(const HdCamera * const camera) +{ + return {}; +} + +// Compute parameters for the camera riley::ShadingNode +static +RtParamList +_ComputeNodeParams(const HdCamera * const camera) +{ + switch(camera->GetProjection()) { + case HdCamera::Perspective: + return _ComputePerspectiveNodeParams(camera); + case HdCamera::Orthographic: + return _ComputeOrthographicNodeParams(camera); + } + + // Make compiler happy + return _ComputePerspectiveNodeParams(camera); +} + // Compute params given to Riley::ModifyCamera RtParamList HdPrman_CameraContext::_ComputeCameraParams(