void BuildCamera(pxr.UsdGeomCamera usdCamera, GameObject go) { var cam = go.AddComponent <Camera>(); var cameraSample = usdCamera.GetCamera(m_scene.Time); cam.fieldOfView = cameraSample.GetFieldOfView(pxr.GfCamera.FOVDirection.FOVHorizontal); cam.nearClipPlane = cameraSample.GetClippingRange().GetMin(); cam.farClipPlane = cameraSample.GetClippingRange().GetMax(); }
public void SetupScene() { InitUsd.Initialize(); // Is the stage already loaded? if (m_scene != null && m_scene.Stage.GetRootLayer().GetIdentifier() == m_usdFile) { return; } // Does the path exist? if (!System.IO.File.Exists(m_usdFile)) { return; } // Clear out the old scene. UnloadGameObjects(); // Load the new scene. m_scene = Scene.Open(m_usdFile); if (m_scene == null) { throw new Exception("Failed to load"); } // Set the time at which to read samples from USD. m_scene.Time = 0; // Handle configurable up-axis (Y or Z). Vector3 up = GetUpVector(m_scene); var rootXf = new GameObject("root"); rootXf.transform.SetParent(transform, worldPositionStays: false); if (up != Vector3.up) { rootXf.transform.localRotation = Quaternion.FromToRotation(Vector3.up, up); } // Convert from right-handed (USD) to left-handed (Unity). // The math below works out to either (1, -1, 1) or (1, 1, -1), depending on up. rootXf.transform.localScale = up + -1 * (Vector3.one - up - Vector3.right) + Vector3.right; // Assign this transform as the root. m_primMap.Add("/", rootXf); // Load transforms. foreach (var path in m_scene.AllXforms) { var xf = new USD.NET.Unity.XformSample(); m_scene.Read(path, xf); var go = new GameObject(path.GetName()); AssignTransform(xf, go); AssignParent(path, go); } // Load meshes. foreach (var path in m_scene.AllMeshes) { var mesh = new USD.NET.Unity.MeshSample(); m_scene.Read(path, mesh); var go = new GameObject(path.GetName()); AssignTransform(mesh, go); AssignParent(path, go); BuildMesh(mesh, go); } // Load cameras. foreach (var prim in m_scene.Stage.GetAllPrimsByType("Camera")) { pxr.UsdGeomCamera camera = new pxr.UsdGeomCamera(prim); var go = new GameObject(prim.GetName()); var xf = new USD.NET.Unity.XformSample(); m_scene.Read(prim.GetPath(), xf); AssignTransform(xf, go); BuildCamera(camera, go); AssignParent(prim.GetPath(), go); } // Ensure the file and the identifier match. m_usdFile = m_scene.Stage.GetRootLayer().GetIdentifier(); }