Beispiel #1
0
 void UpdateScene()
 {
     foreach (var path in m_scene.AllPaths)
     {
         if (!m_primMap.ContainsKey(path))
         {
             continue;
         }
         var xf = new USD.NET.Unity.XformSample();
         m_scene.Read(new pxr.SdfPath(path), xf);
         var go = m_primMap[path];
         AssignTransform(xf, go);
     }
 }
Beispiel #2
0
        public static void Xform2Test()
        {
            var sample  = new USD.NET.Unity.XformSample();
            var sample2 = new USD.NET.Unity.XformSample();

            var mat = new Matrix4x4();

            for (int i = 0; i < 16; i++)
            {
                mat[i] = i;
            }
            sample.transform = mat;

            WriteAndRead(ref sample, ref sample2, true);

            AssertEqual(sample2.transform, sample.transform);
            AssertEqual(sample2.xformOpOrder, sample.xformOpOrder);
        }
Beispiel #3
0
        public static void TestXform()
        {
            var sample  = new USD.NET.Unity.XformSample();
            var sample2 = new USD.NET.Unity.XformSample();

            sample.transform = UnityEngine.Matrix4x4.identity;

            WriteAndRead(ref sample, ref sample2, true);

            if (sample2.transform != sample.transform)
            {
                throw new Exception("Values do not match");
            }
            if (sample2.xformOpOrder[0] != sample.xformOpOrder[0])
            {
                throw new Exception("XformOpOrder do not match");
            }
        }
Beispiel #4
0
        public static void XformTest()
        {
            var sample  = new USD.NET.Unity.XformSample();
            var sample2 = new USD.NET.Unity.XformSample();

            sample.transform = Matrix4x4.identity;
            WriteAndRead(ref sample, ref sample2, true);

            AssertEqual(sample.transform, sample2.transform);
            AssertEqual(sample.xformOpOrder, sample2.xformOpOrder);

            // Matrix4x4.TRS cannot be used outside of of the actual Unity runtime.
            // Using GfMatrix here instead.
            var m = new pxr.GfMatrix4d();

            m.SetScale(new pxr.GfVec3d(8, 9, 10));
            m.SetTranslateOnly(new pxr.GfVec3d(1, 2, 3));
            m.SetRotateOnly(new pxr.GfQuatd(4, 5, 6, 7));
            sample.transform = USD.NET.Unity.UnityTypeConverter.FromMatrix(m);

            sample2 = new USD.NET.Unity.XformSample();
            WriteAndRead(ref sample, ref sample2, true);
            AssertEqual(sample.transform, sample2.transform);
            AssertEqual(sample.xformOpOrder, sample2.xformOpOrder);

            sample.ConvertTransform();
            sample2 = new USD.NET.Unity.XformSample();
            WriteAndRead(ref sample, ref sample2, true);
            AssertEqual(sample.transform, sample2.transform);
            AssertEqual(sample.xformOpOrder, sample2.xformOpOrder);

            sample.ConvertTransform();
            sample2.ConvertTransform();
            AssertEqual(sample.transform, sample2.transform);
            AssertEqual(sample.xformOpOrder, sample2.xformOpOrder);
        }
Beispiel #5
0
        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();
        }
        void Update()
        {
            if (string.IsNullOrEmpty(m_usdFile))
            {
                if (m_scene == null)
                {
                    return;
                }
                m_scene.Close();
                m_scene = null;
                UnloadGameObjects();
                return;
            }

            // 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();

            // Import the new scene.
            m_scene = Scene.Open(m_usdFile);
            if (m_scene == null)
            {
                throw new Exception("Failed to import");
            }

            // Set the time at which to read samples from USD.
            m_scene.Time = m_usdTime;

            // Handle configurable up-axis (Y or Z).
            Vector3 up     = GetUpVector(m_scene);
            var     rootXf = new GameObject("root");

            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 = -1 * up + -1 * (Vector3.one - up);

            // Assign this transform as the root.
            m_primMap.Add("/", rootXf);

            // Import 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);
            }

            // Import 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);
            }

            // Ensure the file and the identifier match.
            m_usdFile = m_scene.Stage.GetRootLayer().GetIdentifier();
        }
 // Convert Matrix4x4 into TSR components.
 void AssignTransform(USD.NET.Unity.XformSample xf, GameObject go)
 {
     go.transform.localPosition = ExtractPosition(xf.transform);
     go.transform.localScale    = ExtractScale(xf.transform);
     go.transform.localRotation = ExtractRotation(xf.transform);
 }