Skip to content

Commit

Permalink
USD Viewer: initial implementation of the TRS widget
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Dec 2, 2023
1 parent af6b5f8 commit abd9fcd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
22 changes: 22 additions & 0 deletions Samples/USDViewer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ cmake_minimum_required (VERSION 3.13)

project(USDViewer CXX)


include(FetchContent)

# Fetch ImGuizmo
message("Fetching ImGuizmo repository...")
FetchContent_Declare(
ImGuizmo
GIT_REPOSITORY https://github.com/CedricGuillemet/ImGuizmo
GIT_TAG ba662b1
)
FetchContent_MakeAvailable(ImGuizmo)


set(SOURCE
src/USDViewer.cpp
)
Expand Down Expand Up @@ -30,6 +43,15 @@ if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
)
endif()

set(IMGUIZMO_SOURCE
${imguizmo_SOURCE_DIR}/ImGuizmo.h
${imguizmo_SOURCE_DIR}/ImGuizmo.cpp
)
target_sources(USDViewer PRIVATE ${IMGUIZMO_SOURCE})
target_include_directories(USDViewer PRIVATE ${imguizmo_SOURCE_DIR})
source_group("ImGuizmo" FILES ${IMGUIZMO_SOURCE})


target_link_libraries(USDViewer
PRIVATE
USD-Libraries
Expand Down
87 changes: 76 additions & 11 deletions Samples/USDViewer/src/USDViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#include "imgui.h"
#include "ImGuiUtils.hpp"
#include "ImGuizmo.h"

#include "pxr/usd/usd/prim.h"
#include "pxr/usd/usd/property.h"
Expand Down Expand Up @@ -278,16 +279,8 @@ void USDViewer::Render()
if (!m_Stage)
return;

auto CameraDist = m_Camera.GetDist();
auto CameraView = m_Camera.GetRotation().ToMatrix() * float4x4::Translation(0.f, 0.0f, CameraDist);
// Apply pretransform matrix that rotates the scene according the surface orientation
CameraView *= GetSurfacePretransformMatrix(float3{0, 0, 1});

// Get projection matrix adjusted to the current screen orientation
const auto CameraProj = GetAdjustedProjectionMatrix(PI_F / 4.0f, CameraDist / 100.f, CameraDist * 3.f);

m_Stage.Camera->SetViewMatrix(CameraView);
m_Stage.Camera->SetProjectionMatrix(CameraProj);
m_Stage.Camera->SetViewMatrix(m_CameraView);
m_Stage.Camera->SetProjectionMatrix(m_CameraProj);

m_Stage.Light->SetDirection(m_LightDirection);
m_Stage.Light->SetIntensity(m_LightColor * m_LightIntensity);
Expand Down Expand Up @@ -344,12 +337,71 @@ void USDViewer::PopulateSceneTree(const pxr::UsdPrim& Prim)
}
}

static pxr::GfMatrix4d GetPrimGlobalTransform(pxr::UsdPrim Prim)
{
pxr::GfMatrix4d GlobalXForm{1};
while (Prim)
{
if (pxr::UsdGeomXformable XFormable{Prim})
{
pxr::GfMatrix4d LocalXform{1.0};
bool ResetsXformStack = false;
if (XFormable.GetLocalTransformation(&LocalXform, &ResetsXformStack))
{
GlobalXForm = LocalXform * GlobalXForm;
}
}

Prim = Prim.GetParent();
}

return GlobalXForm;
}

void USDViewer::EditSelectePrimTransform()
{
pxr::UsdPrim Prim = m_Stage.Stage->GetPrimAtPath(m_Stage.SelectedPrimId);
if (!Prim)
return;

pxr::UsdGeomXformable XFormable{Prim};
if (!XFormable)
return;

pxr::GfMatrix4d ParentGlobalXForm = GetPrimGlobalTransform(Prim.GetParent());
float4x4 ParentGlobalMatrix = float4x4::MakeMatrix(ParentGlobalXForm.data());

ParentGlobalMatrix = m_RenderParams.Transform * ParentGlobalMatrix;

pxr::GfMatrix4d LocalXform = pxr::GfMatrix4d{1.0};
bool ResetsXformStack = false;
XFormable.GetLocalTransformation(&LocalXform, &ResetsXformStack);

float4x4 NewGlobalMatrix = float4x4::MakeMatrix(LocalXform.data()) * ParentGlobalMatrix;

ImGuiIO& io = ImGui::GetIO();
ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
ImGuizmo::OPERATION mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
ImGuizmo::MODE mCurrentGizmoMode = ImGuizmo::WORLD;
if (ImGuizmo::Manipulate(m_CameraView.Data(), m_CameraProj.Data(), mCurrentGizmoOperation, mCurrentGizmoMode, NewGlobalMatrix.Data(), NULL, NULL))
{
Diligent::float4x4 NewLocalMatrix = NewGlobalMatrix * ParentGlobalMatrix.Inverse();
XFormable.MakeMatrixXform().Set(pxr::GfMatrix4d{
NewLocalMatrix.m00, NewLocalMatrix.m01, NewLocalMatrix.m02, NewLocalMatrix.m03,
NewLocalMatrix.m10, NewLocalMatrix.m11, NewLocalMatrix.m12, NewLocalMatrix.m13,
NewLocalMatrix.m20, NewLocalMatrix.m21, NewLocalMatrix.m22, NewLocalMatrix.m23,
NewLocalMatrix.m30, NewLocalMatrix.m31, NewLocalMatrix.m32, NewLocalMatrix.m33});
}
}

void USDViewer::UpdateUI()
{
bool UpdateRenderParams = false;
bool UpdateFrameParams = false;
bool UpdatePostProcessParams = false;

ImGuizmo::BeginFrame();

ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(300, 550), ImGuiCond_FirstUseEver);
if (ImGui::Begin("Settings", nullptr))
Expand Down Expand Up @@ -600,6 +652,11 @@ void USDViewer::UpdateUI()
}
ImGui::End();

if (!m_Stage.SelectedPrimId.IsEmpty())
{
EditSelectePrimTransform();
}

if (UpdateRenderParams)
m_Stage.TaskManager->SetRenderRprimParams(m_RenderParams);
if (UpdatePostProcessParams)
Expand All @@ -611,9 +668,17 @@ void USDViewer::UpdateUI()
void USDViewer::Update(double CurrTime, double ElapsedTime)
{
SampleBase::Update(CurrTime, ElapsedTime);
UpdateUI();
m_Camera.SetZoomSpeed(m_Camera.GetDist() * 0.1f);
m_Camera.Update(m_InputController);
auto CameraDist = m_Camera.GetDist();
m_CameraView = m_Camera.GetRotation().ToMatrix() * float4x4::Translation(0.f, 0.0f, CameraDist);
// Apply pretransform matrix that rotates the scene according the surface orientation
m_CameraView *= GetSurfacePretransformMatrix(float3{0, 0, 1});

// Get projection matrix adjusted to the current screen orientation
m_CameraProj = GetAdjustedProjectionMatrix(PI_F / 4.0f, CameraDist / 100.f, CameraDist * 3.f);

UpdateUI();

if (!m_Stage)
return;
Expand Down
3 changes: 3 additions & 0 deletions Samples/USDViewer/src/USDViewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class USDViewer final : public SampleBase
void LoadStage();
void PopulateSceneTree(const pxr::UsdPrim& Prim);
void SetSelectedPrim(const pxr::SdfPath& SelectedPrimId);
void EditSelectePrimTransform();

float4x4 ComputeStageTransform();
BoundBox ComputeSceneBounds(const pxr::UsdPrim& Prim) const;
Expand Down Expand Up @@ -118,6 +119,8 @@ class USDViewer final : public SampleBase
RefCntAutoPtr<ITextureView> m_EnvironmentMapSRV;

TrackballCamera<float> m_Camera;
float4x4 m_CameraView;
float4x4 m_CameraProj;

float3 m_LightDirection = normalize(float3{0.5f, 0.6f, -0.2f});
float4 m_LightColor = {1, 1, 1, 1};
Expand Down

0 comments on commit abd9fcd

Please sign in to comment.