Exemplo n.º 1
0
        static bool IsVisible(UsdPrim usdPrim)
        {
            if (!usdPrim)
            {
                return(false);
            }

            var img = new UsdGeomImageable(usdPrim);

            if (!img)
            {
                return(true);
            }

            // Using time=0.0 will enable this to pickup a single animated value by virtue of
            // interpolation, but correct handling of animated visibility would query it over time.

            // Also note this code is intentionally not using ComputeVisibility(), since Unity will apply
            // inherited visibility. This is technically incorrect, since USD supports "super vis",
            // enabling visible children with invisible parents. The goal here is to avoid spamming all
            // descendent children with active=false. A better implementation would check for visible
            // children with invisible parents and somehow translate that to the Unity scenegraph, but
            // is left as a future improvement.

            VtValue visValue = new VtValue();

            if (!img.GetVisibilityAttr().Get(visValue, 0.0))
            {
                return(true);
            }

            return(UsdCs.VtValueToTfToken(visValue) != UsdGeomTokens.invisible);
        }
Exemplo n.º 2
0
        static void ReadModelInfo(ref HierInfo info)
        {
            if (!info.prim)
            {
                return;
            }

            var modelApi = new UsdModelAPI(info.prim);

            if (!modelApi)
            {
                return;
            }

            var kindTok = new TfToken();

            if (!modelApi.GetKind(kindTok))
            {
                return;
            }

            if (KindRegistry.IsA(kindTok, KindTokens.assembly))
            {
                info.isAssembly = true;
            }
            else if (!modelApi.IsModel() || modelApi.IsGroup())
            {
                return;
            }

            var modelInfo = new VtDictionary();

            if (!modelApi.GetAssetInfo(modelInfo))
            {
                return;
            }

            info.isModel = true;

            var valName       = modelInfo.GetValueAtPath("name");
            var valVersion    = modelInfo.GetValueAtPath("version");
            var valIdentifier = modelInfo.GetValueAtPath("identifier");

            if (valIdentifier != null && !valIdentifier.IsEmpty())
            {
                info.modelAssetPath = UsdCs.VtValueToSdfAssetPath(valIdentifier).GetAssetPath().ToString();
            }

            if (valName != null && !valName.IsEmpty())
            {
                info.modelName = UsdCs.VtValueTostring(valName);
            }

            if (valVersion != null && !valVersion.IsEmpty())
            {
                info.modelVersion = UsdCs.VtValueTostring(valVersion);
            }
        }
Exemplo n.º 3
0
        public bool MoveNext()
        {
            m_i++;
            bool valid = m_i < m_size;

            if (valid)
            {
                UsdCs.GetPathFromVector(m_paths, m_i, m_current);
            }
            return(valid);
        }
Exemplo n.º 4
0
        public void ExportUsdz(string filePath, bool deleteUsdDirectory)
        {
            InitUsd.Initialize();

            var currentDir   = Directory.GetCurrentDirectory();
            var usdFileName  = Path.GetFileName(filePath);
            var usdzFileName = Path.GetFileNameWithoutExtension(usdFileName) + ".usdz";

            string usdzDirectroy;

            if (string.IsNullOrEmpty(this.exportDirectory))
            {
                usdzDirectroy = currentDir;
            }
            else
            {
                usdzDirectroy = Path.GetFullPath(this.exportDirectory);
            }
            var usdzFilePath = Path.Combine(usdzDirectroy, usdzFileName);

            try
            {
                // 画像の検索パスを合わせるためにカレントディレクトリを変更する
                // 後で戻し忘れるとUnityが壊れるので注意
                Directory.SetCurrentDirectory(Path.GetDirectoryName(filePath));

                var assetPath = new SdfAssetPath(usdFileName);
                var success   = UsdCs.UsdUtilsCreateNewARKitUsdzPackage(assetPath, usdzFilePath);
                if (!success)
                {
                    Debug.LogError("usdzファイルの出力に失敗しました");
                    return;
                }

                Debug.Log($"出力完了: '{usdzFilePath}'");
            }
            finally
            {
                // 変更していたディレクトリを戻す
                Directory.SetCurrentDirectory(currentDir);

                if (deleteUsdDirectory)
                {
                    var d = Path.GetDirectoryName(filePath);
                    // 違うディレクトリを消してしまうとやばいのでチェック
                    if (Path.GetFileName(d).StartsWith("temp-"))
                    {
                        var di = new DirectoryInfo(d);
                        di.Delete(true);
                    }
                }
            }
        }
        static public UnityEngine.Bounds BoundsFromVtArray(VtValue vtBounds)
        {
            var bbox = UsdIo.ArrayAllocator.Malloc <UnityEngine.Vector3>(2);
            var v3   = (VtVec3fArray)UsdIo.ArrayAllocator.MallocHandle(typeof(VtVec3fArray));

            UsdCs.VtValueToVtVec3fArray(vtBounds, v3);
            FromVtArray(v3, ref bbox);
            UsdIo.ArrayAllocator.FreeHandle(v3.GetType(), v3);
            UnityEngine.Bounds bnds = new UnityEngine.Bounds();
            bnds.min = bbox[0];
            bnds.max = bbox[1];
            UsdIo.ArrayAllocator.Free(bbox.GetType(), (uint)bbox.Length, bbox);
            return(bnds);
        }
Exemplo n.º 6
0
        protected override void EndRecording(RecordingSession session)
        {
            context.scene.EndTime = session.recorderTime * session.settings.FrameRate;
            context.scene.Save();
            context.scene.Close();

            if (Settings.ExportFormat == UsdRecorderSettings.Format.USDZ)
            {
                try
                {
                    Directory.SetCurrentDirectory(usdzTemporaryDir.FullName);
                    var assetPath = new SdfAssetPath(usdcFileName);
                    var success   = UsdCs.UsdUtilsCreateNewARKitUsdzPackage(assetPath, usdzFileName);

                    if (!success)
                    {
                        Debug.LogError("Couldn't export to the usdz file: " + usdzFilePath);
                        return;
                    }

                    // needed if we export into temp folder first
                    File.Copy(usdzFileName, usdzFilePath, overwrite: true);
                }
                finally
                {
                    // Clean up temp files.
                    Directory.SetCurrentDirectory(currentDir);
                    if (usdzTemporaryDir != null && usdzTemporaryDir.Exists)
                    {
                        usdzTemporaryDir.Delete(recursive: true);
                    }
                }
            }

            context       = null;
            Input.Context = null;

            base.EndRecording(session);
        }
 static public string MakeValidIdentifier(string unityIdentifier)
 {
     return(UsdCs.TfMakeValidIdentifier(unityIdentifier));
 }
Exemplo n.º 8
0
        /// <summary>
        /// If HierInfo represents a UsdSkelRoot, reads the associated skelton joints into the
        /// skelJoints member.
        /// </summary>
        static void ReadSkeletonJoints(ref HierInfo skelRootInfo)
        {
            if (skelRootInfo.prim == null)
            {
                return;
            }

            var skelRoot = new UsdSkelRoot(skelRootInfo.prim);

            if (!skelRoot)
            {
                return;
            }

            var processed = new HashSet <SdfPath>();

            foreach (UsdSkelBinding binding in skelRootInfo.skelBindings)
            {
                var skel = binding.GetSkeleton();

                if (!skel)
                {
                    continue;
                }

                // If the same skeleton is referenced multiple times, only process it once.
                if (processed.Contains(skel.GetPath()))
                {
                    continue;
                }
                processed.Add(skel.GetPath());

                var jointsAttr = skel.GetJointsAttr();
                if (!jointsAttr)
                {
                    continue;
                }

                var vtJoints = jointsAttr.Get();
                if (vtJoints.IsEmpty())
                {
                    continue;
                }
                var vtStrings = UsdCs.VtValueToVtTokenArray(vtJoints);
                var joints    = UnityTypeConverter.FromVtArray(vtStrings);

                var skelPath = skel.GetPath();
                skelRootInfo.skelJoints = new SdfPath[joints.Length];

                for (int i = 0; i < joints.Length; i++)
                {
                    var jointPath = new SdfPath(joints[i]);
                    if (joints[i] == "/")
                    {
                        skelRootInfo.skelJoints[i] = skelPath;
                        continue;
                    }
                    else if (jointPath.IsAbsolutePath())
                    {
                        Debug.LogException(new Exception("Unexpected absolute joint path: " + jointPath));
                        jointPath = new SdfPath(joints[i].TrimStart('/'));
                    }
                    skelRootInfo.skelJoints[i] = skelPath.AppendPath(jointPath);
                }
            }
        }
Exemplo n.º 9
0
        static void LoadPrimvars(Scene scene,
                                 Mesh unityMesh,
                                 string usdMeshPath,
                                 List <string> primvars,
                                 int[] faceVertexCounts,
                                 int[] faceVertexIndices)
        {
            if (primvars == null || primvars.Count == 0)
            {
                return;
            }
            var prim = scene.GetPrimAtPath(usdMeshPath);

            for (int i = 0; i < primvars.Count; i++)
            {
                var attr = prim.GetAttribute(new TfToken("primvars:" + primvars[i]));
                if (!attr)
                {
                    continue;
                }

                // Read the raw values.
                VtValue val = attr.Get(0.0);
                if (val.IsEmpty())
                {
                    continue;
                }
                VtVec2fArray vec2fArray = UsdCs.VtValueToVtVec2fArray(val);
                Vector2[]    values     = UnityTypeConverter.FromVtArray(vec2fArray);

                // Unroll indexed primvars.
                var        pv        = new UsdGeomPrimvar(attr);
                VtIntArray vtIndices = new VtIntArray();
                if (pv.GetIndices(vtIndices, 0.0))
                {
                    int[] indices = UnityTypeConverter.FromVtArray(vtIndices);
                    values = indices.Select(idx => values[idx]).ToArray();
                }

                // Handle primvar interpolation modes.
                TfToken interp = pv.GetInterpolation();
                if (interp == UsdGeomTokens.constant)
                {
                    Debug.Assert(values.Length == 1);
                    var newValues = new Vector2[unityMesh.vertexCount];
                    for (int idx = 0; idx < values.Length; idx++)
                    {
                        newValues[idx] = values[0];
                    }
                    values = newValues;
                }
                else if (interp == UsdGeomTokens.uniform)
                {
                    Debug.Assert(values.Length == faceVertexCounts.Length);
                    for (int faceIndex = 0; faceIndex < values.Length; faceIndex++)
                    {
                        var faceColor = values[faceIndex];
                        int idx       = 0;
                        var newValues = new Vector2[unityMesh.vertexCount];
                        for (int f = 0; f < faceVertexCounts[faceIndex]; f++)
                        {
                            int vertexInFaceIdx = faceVertexIndices[idx++];
                            newValues[vertexInFaceIdx] = faceColor;
                        }
                        values = newValues;
                    }
                }
                else if (interp == UsdGeomTokens.faceVarying)
                {
                    values = UnrollFaceVarying(unityMesh.vertexCount, values, faceVertexCounts, faceVertexIndices);
                }

                // Send them to Unity.
                unityMesh.SetUVs(i, values.ToList());
            }
        }
Exemplo n.º 10
0
        static void ReadSkeleton(ref HierInfo info)
        {
            if (info.prim == null)
            {
                return;
            }

            var skelRoot = new UsdSkelRoot(info.prim);

            if (!skelRoot)
            {
                return;
            }

            var skelRel = info.prim.GetRelationship(UsdSkelTokens.skelSkeleton);

            if (!skelRel)
            {
                return;
            }

            SdfPathVector targets = skelRel.GetForwardedTargets();

            if (targets == null || targets.Count == 0)
            {
                return;
            }

            var skelPrim = info.prim.GetStage().GetPrimAtPath(targets[0]);

            if (!skelPrim)
            {
                return;
            }

            var skel = new UsdSkelSkeleton(skelPrim);

            if (!skel)
            {
                return;
            }

            var jointsAttr = skel.GetJointsAttr();

            if (!jointsAttr)
            {
                return;
            }

            var vtJoints = jointsAttr.Get();

            if (vtJoints.IsEmpty())
            {
                return;
            }
            var vtStrings = UsdCs.VtValueToVtTokenArray(vtJoints);
            var joints    = UnityTypeConverter.FromVtArray(vtStrings);

            var skelPath = skelPrim.GetPath();

            info.skelJoints = new SdfPath[joints.Length];

            for (int i = 0; i < joints.Length; i++)
            {
                var jointPath = new SdfPath(joints[i]);
                if (joints[i] == "/")
                {
                    info.skelJoints[i] = skelPath;
                    continue;
                }
                else if (jointPath.IsAbsolutePath())
                {
                    Debug.LogException(new Exception("Unexpected absolute joint path: " + jointPath));
                    jointPath = new SdfPath(joints[i].TrimStart('/'));
                }
                info.skelJoints[i] = skelPath.AppendPath(jointPath);
            }
        }