// ----------------------------------------------------------------------------------------- //
        // Paths
        // ----------------------------------------------------------------------------------------- //
        /// <summary>
        /// Returns a valid UsdPath for the given Unity GameObject.
        /// </summary>
        /// <remarks>
        /// Note that illegal characters are converted into legal characters, so invalid names may
        /// collide in the USD namespace.
        /// </remarks>
        static public string GetPath(UnityEngine.Transform unityObj)
        {
            // Base case.
            if (unityObj == null)
            {
                return("");
            }

            // Build the path from root to leaf.
            return(GetPath(unityObj.transform.parent)
                   + "/" + UnityTypeConverter.MakeValidIdentifier(unityObj.name));
        }
示例#2
0
        public Matrix4x4[] ComputeInstanceMatrices(Scene scene, string primPath)
        {
            var prim   = scene.GetPrimAtPath(primPath);
            var pi     = new pxr.UsdGeomPointInstancer(prim);
            var xforms = new pxr.VtMatrix4dArray();

            pi.ComputeInstanceTransformsAtTime(xforms, scene.Time == null ? pxr.UsdTimeCode.Default() : scene.Time, 0);

            // Slow, but works.
            var matrices = new Matrix4x4[xforms.size()];

            for (int i = 0; i < xforms.size(); i++)
            {
                matrices[i] = UnityTypeConverter.FromMatrix(xforms[i]);
            }
            return(matrices);
        }
        /// <summary>
        /// Returns a valid UsdPath for the given Unity GameObject, relative to the given root.
        /// For example: obj = /Foo/Bar/Baz, root = /Foo the result will be /Bar/Baz
        /// </summary>
        /// <remarks>
        /// Note that illegal characters are converted into legal characters, so invalid names may
        /// collide in the USD namespace.
        /// </remarks>
        static public string GetPath(UnityEngine.Transform unityObj,
                                     UnityEngine.Transform unityObjRoot)
        {
            // Base case.
            if (unityObjRoot != null && unityObj == null)
            {
                throw new Exception("Expected to find root " + unityObjRoot.name + " but did not.");
            }

            if (unityObj == unityObjRoot)
            {
                return("");
            }

            // Build the path from root to leaf.
            return(GetPath(unityObj.transform.parent, unityObjRoot)
                   + "/" + UnityTypeConverter.MakeValidIdentifier(unityObj.name));
        }
 /// <summary>
 /// Converts the transform from Unity to USD or vice versa. This is required after reading
 /// values from USD or before writing values to USD.
 /// </summary>
 public void ConvertTransform()
 {
     m_xf = UnityTypeConverter.ChangeBasis(m_xf);
 }
        /// <summary>
        /// Registers Unity-specifc data type conversions in the global type binder (UsdIo.Bindings).
        /// </summary>
        static public void RegisterTypes()
        {
            if (isInitialized)
            {
                return;
            }
            isInitialized = true;

            TypeBinder binder = USD.NET.UsdIo.Bindings;

            //
            // Quaternion
            //
            binder.BindType(typeof(Quaternion), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.QuaternionToQuatf((Quaternion)obj),
                                (pxr.VtValue value) => UnityTypeConverter.QuatfToQuaternion(value),
                                SdfValueTypeNames.Quatf));
            binder.BindArrayType <UnityTypeConverter>(typeof(Quaternion[]), typeof(pxr.VtQuatfArray), SdfValueTypeNames.QuatfArray);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Quaternion>), typeof(pxr.VtQuatfArray), SdfValueTypeNames.QuatfArray, "List");

            //
            // Scalar Vector{2,3,4}
            //
            binder.BindType(typeof(Vector2), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.Vector2ToVec2f((Vector2)obj),
                                (pxr.VtValue value) => UnityTypeConverter.Vec2fToVector2(value),
                                SdfValueTypeNames.Float2));
            binder.BindType(typeof(Vector3), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.Vector3ToVec3f((Vector3)obj),
                                (pxr.VtValue value) => UnityTypeConverter.Vec3fToVector3(value),
                                SdfValueTypeNames.Float3));
            binder.BindType(typeof(Vector4), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.Vector4ToVec4f((Vector4)obj),
                                (pxr.VtValue value) => UnityTypeConverter.Vec4fToVector4(value),
                                SdfValueTypeNames.Float4));

            //
            // Scaler Rect <-> GfVec4f
            //
            binder.BindType(typeof(Rect), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.RectToVtVec4((Rect)obj),
                                (pxr.VtValue value) => UnityTypeConverter.Vec4fToRect(value),
                                SdfValueTypeNames.Float4));

            //
            // Vector {2,3,4} {[],List<>}
            //
            binder.BindArrayType <UnityTypeConverter>(typeof(Vector2[]), typeof(pxr.VtVec2fArray), SdfValueTypeNames.Float2Array);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Vector2>), typeof(pxr.VtVec2fArray), SdfValueTypeNames.Float2Array, "List");

            binder.BindArrayType <UnityTypeConverter>(typeof(Vector3[]), typeof(pxr.VtVec3fArray), SdfValueTypeNames.Float3Array);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Vector3>), typeof(pxr.VtVec3fArray), SdfValueTypeNames.Float3Array, "List");

            binder.BindArrayType <UnityTypeConverter>(typeof(Vector4[]), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Float4Array);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Vector4>), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Float4Array, "List");

            //
            // Color / Color32
            //
            binder.BindType(typeof(Color[]), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.ToVtArray(((Color[])obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.ColorFromVtArray(vtVal),
                                SdfValueTypeNames.Color4fArray));

            binder.BindType(typeof(Color), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.ColorToVec4f(((Color)obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.Vec4fToColor(vtVal),
                                SdfValueTypeNames.Color4f));

            binder.BindType(typeof(Color32), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.Color32ToVec4f(((Color32)obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.Vec4fToColor32(vtVal),
                                SdfValueTypeNames.Color4f));

            binder.BindType(typeof(Bounds), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.BoundsToVtArray(((Bounds)obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.BoundsFromVtArray(vtVal),
                                SdfValueTypeNames.Float3Array));

            binder.BindArrayType <UnityTypeConverter>(typeof(List <Color>), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Color4fArray);
            binder.BindArrayType <UnityTypeConverter>(typeof(Color32[]), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Color4fArray);

            //
            // Matrix4x4
            //

            // Note that UsdGeom exclusively uses double-precision matrices for storage.
            // In the future, it may be nice to support single-precision for clients who aren't targeting
            // UsdGeom.
            binder.BindType(typeof(Matrix4x4), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.ToGfMatrix(((Matrix4x4)obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.FromMatrix((pxr.GfMatrix4d)vtVal),
                                SdfValueTypeNames.Matrix4d));

            binder.BindArrayType <UnityTypeConverter>(typeof(Matrix4x4[]), typeof(pxr.VtMatrix4dArray), SdfValueTypeNames.Matrix4dArray);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Matrix4x4>), typeof(pxr.VtMatrix4dArray), SdfValueTypeNames.Matrix4dArray, "List");
        }