Esempio n. 1
0
        // Creates ancestors, but note that this method does not apply visibility, since it was
        // designed to create bones, which cannot have visibility opinions in USD.
        static void CreateAncestors(SdfPath path,
                                    PrimMap map,
                                    GameObject unityRoot,
                                    SdfPath usdRoot,
                                    SceneImportOptions options,
                                    out GameObject parentGo)
        {
            var parentPath = path.GetParentPath();

            if (path == parentPath)
            {
                Debug.LogException(new Exception("Parent path was identical to current path: " + path.ToString()));
                parentGo = null;
                return;
            }

            if (map.TryGetValue(parentPath, out parentGo) && parentGo)
            {
                return;
            }

            // Base case.
            if (parentPath == usdRoot)
            {
                map[parentPath] = unityRoot;
                return;
            }

            if (parentPath == kAbsoluteRootPath)
            {
                // Something went wrong.
                Debug.LogException(new Exception(
                                       "Error: unexpected path </> creating ancestors for <" + usdRoot.ToString() + ">"));
            }

            // Recursive case.
            // First, get the grandparent (parent's parent).
            GameObject grandparentGo;

            CreateAncestors(parentPath, map, unityRoot, usdRoot, options, out grandparentGo);
            if (!grandparentGo)
            {
                Debug.LogError("Failed to find ancestor for " + parentPath);
                return;
            }

            // Then find/create the current parent.
            parentGo = FindOrCreateGameObject(grandparentGo.transform,
                                              parentPath,
                                              unityRoot.transform,
                                              map,
                                              options);
        }
        public static void WriteSparseOverrides(Scene scene,
                                                PrimMap primMap,
                                                BasisTransformation changeHandedness,
                                                float tolerance = 0.0001f)
        {
            var oldMode = scene.WriteMode;

            scene.WriteMode = Scene.WriteModes.Over;

            try
            {
                foreach (var path in scene.Find <XformableSample>())
                {
                    GameObject go;
                    if (!primMap.TryGetValue(path, out go))
                    {
                        continue;
                    }

                    var tx    = go.transform;
                    var xfNew = XformSample.FromTransform(tx);
                    var xfOld = new XformSample();

                    scene.Read(path, xfOld);

                    bool areClose = true;
                    for (int i = 0; i < 16; i++)
                    {
                        if (Mathf.Abs(xfNew.transform[i] - xfOld.transform[i]) > tolerance)
                        {
                            areClose = false;
                            break;
                        }
                    }

                    if (areClose)
                    {
                        continue;
                    }

                    if (changeHandedness == BasisTransformation.SlowAndSafe)
                    {
                        xfNew.ConvertTransform();
                    }

                    scene.Write(path, xfNew);
                }
            }
            finally
            {
                scene.WriteMode = oldMode;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Given an array of bone names (HierInfo.skelJoints), creates GameObjects under unityRoot.
        /// </summary>
        static void ExpandSkeleton(HierInfo info,
                                   GameObject unityRoot,
                                   SdfPath usdRoot,
                                   UsdPrim prim,
                                   PrimMap map,
                                   SceneImportOptions options)
        {
            foreach (var joint in info.skelJoints)
            {
                var        path     = joint;
                GameObject parentGo = null;
                if (!map.TryGetValue(path.GetParentPath(), out parentGo))
                {
                    // This will happen when the joints are discontinuous, for example:
                    //
                    //   Foo/Bar
                    //   Foo/Bar/Baz/Qux
                    //
                    // Baz is implicitly defined, which is allowed by UsdSkel.
                    CreateAncestors(path, map, unityRoot, usdRoot, options, out parentGo);
                    if (!parentGo)
                    {
                        Debug.LogException(new Exception("Failed to create ancestors for " + path + " for prim: " +
                                                         prim.GetPath()));
                        continue;
                    }
                }

                Transform child = parentGo.transform.Find(path.GetName());
                if (!child)
                {
                    child = new GameObject(path.GetName()).transform;
                    child.SetParent(parentGo.transform, worldPositionStays: false);
                }

                map[path] = child.gameObject;
            }
        }
Esempio n. 4
0
        static void ProcessPaths(HierInfo[] infos,
                                 Scene scene,
                                 GameObject unityRoot,
                                 SdfPath usdRoot,
                                 PrimMap map,
                                 SceneImportOptions options)
        {
            Profiler.BeginSample("Process all paths");
            foreach (var info in infos)
            {
                var prim = info.prim;
                var path = info.prim.GetPath();

                if (info.skelBindings != null)
                {
                    // Collect all discovered skelBindings back into the PrimMap.
                    map.SkelBindings.Add(info.prim.GetPath(), info.skelBindings);
                }

                GameObject go;
                if (path == usdRoot)
                {
                    go = unityRoot;
                }
                else
                {
                    GameObject parentGo = null;
                    CreateAncestors(path, map, unityRoot, usdRoot, options, out parentGo);

                    if (!parentGo)
                    {
                        Debug.LogWarning("Parent path not found for child: " + path.ToString());
                        continue;
                    }

                    var parent = parentGo ? parentGo.transform : null;
                    if (!map.TryGetValue(path, out go))
                    {
                        go = FindOrCreateGameObject(parent,
                                                    path,
                                                    unityRoot.transform,
                                                    map,
                                                    options);
                    }
                }

                if (options.importSceneInstances)
                {
                    Profiler.BeginSample("Add Scene Instance Root");
                    if (prim.IsInstance())
                    {
                        map.AddInstanceRoot(prim.GetPath(), go, prim.GetMaster().GetPath());
                    }
                    Profiler.EndSample();
                }

                if (!options.importHierarchy)
                {
                    continue;
                }

                ApplySelfVisibility(go, prim);

                try {
                    Profiler.BeginSample("Add Model Root");
                    AddModelRoot(go, info);
                    Profiler.EndSample();

                    Profiler.BeginSample("Add Variant Set");
                    AddVariantSet(go, prim);
                    Profiler.EndSample();

                    Profiler.BeginSample("Add Payload");
                    AddPayload(go, info, options);
                    Profiler.EndSample();
                } catch (Exception ex) {
                    Debug.LogException(new Exception("Error processing " + prim.GetPath(), ex));
                }
            }
            Profiler.EndSample();
        }
Esempio n. 5
0
        public static void BuildPointInstances(Scene scene,
                                               PrimMap primMap,
                                               string pointInstancerPath,
                                               PointInstancerSample sample,
                                               GameObject root,
                                               SceneImportOptions options)
        {
            Matrix4x4[] transforms = sample.ComputeInstanceMatrices(scene, pointInstancerPath);
            int         i          = 0;

            foreach (var protoRoot in sample.prototypes.targetPaths)
            {
                GameObject go;
                if (!primMap.TryGetValue(new pxr.SdfPath(protoRoot), out go))
                {
                    Debug.LogWarning("Proto not found in PrimMap: " + protoRoot);
                    continue;
                }

                go.SetActive(false);
                if (options.enableGpuInstancing)
                {
                    EnableGpuInstancing(go);
                }
            }

            var inactiveIds = new System.Collections.Generic.HashSet <long>();

            /*
             * Disabled until this bug is resolved:
             * https://github.com/PixarAnimationStudios/USD/issues/639
             *
             * if (sample.inactiveIds != null) {
             * foreach (long id in sample.inactiveIds.GetExplicitItems()) {
             *  inactiveIds.Add(id);
             * }
             * }
             */

            foreach (var index in sample.protoIndices)
            {
                if (inactiveIds.Contains(index))
                {
                    continue;
                }

                if (index >= sample.prototypes.targetPaths.Length)
                {
                    Debug.LogWarning("ProtoIndex out of bounds: [" + index + "] " +
                                     "for instancer: " + pointInstancerPath);
                    continue;
                }

                var targetPath = sample.prototypes.targetPaths[index];

                GameObject goMaster;
                if (!primMap.TryGetValue(new pxr.SdfPath(targetPath), out goMaster))
                {
                    Debug.LogWarning("Proto not found in PrimMap: " + targetPath);
                    continue;
                }

                if (i >= transforms.Length)
                {
                    Debug.LogWarning("No transform for instance index [" + i + "] " +
                                     "for instancer: " + pointInstancerPath);
                    break;
                }

                var xf         = transforms[i];
                var goInstance = GameObject.Instantiate(goMaster, root.transform);
                goInstance.SetActive(true);
                goInstance.name = goMaster.name + "_" + i;
                XformImporter.BuildXform(xf, goInstance, options);

                i++;
            }
        }