예제 #1
0
        /// <summary>
        /// Map all UsdPrims and build Unity GameObjects, reconstructing the parent relationship.
        /// </summary>
        /// <remarks>
        /// When forceRebuild is true, game objects will be destroyed and recreated. If buildHierarchy
        /// is false, the primMap will be populated, but missing game objects will not be created.
        /// </remarks>
        static public PrimMap BuildGameObjects(Scene scene,
                                               GameObject unityRoot,
                                               SdfPath usdRoot,
                                               IEnumerable <SdfPath> paths,
                                               PrimMap map,
                                               SceneImportOptions options)
        {
            map[usdRoot] = unityRoot;

            // Like all GameObjects imported from USD, ensure the root has a UsdPrimSource.
            if (unityRoot.GetComponent <UsdPrimSource>() == null)
            {
                var ua = unityRoot.AddComponent <UsdPrimSource>();
                ua.m_usdPrimPath = usdRoot.ToString();
            }

            Profiler.BeginSample("Build Object Lists");
            var hierInfo = BuildObjectLists(scene, unityRoot, usdRoot, map, options);

            Profiler.EndSample();

            // TODO: Should recurse to discover deeply nested instancing.
            // TODO: Generates garbage for every prim, but we expect few masters.
            if (options.importPointInstances || options.importSceneInstances)
            {
                Profiler.BeginSample("Build Masters");
                foreach (var masterRootPrim in scene.Stage.GetMasters())
                {
                    var goMaster = FindOrCreateGameObject(unityRoot.transform,
                                                          masterRootPrim.GetPath(),
                                                          unityRoot.transform,
                                                          map,
                                                          options);

                    goMaster.hideFlags = HideFlags.HideInHierarchy;
                    goMaster.SetActive(false);
                    map.AddMasterRoot(masterRootPrim.GetPath(), goMaster);
                    try {
                        var info = new HierInfo();
                        info.prim = masterRootPrim;
                        ReadModelInfo(ref info);
                        AddModelRoot(goMaster, info);
                        AddVariantSet(goMaster, masterRootPrim);
                    } catch (Exception ex) {
                        Debug.LogException(new Exception("Error processing " + masterRootPrim.GetPath(), ex));
                    }

                    foreach (var usdPrim in masterRootPrim.GetDescendants())
                    {
                        var       parentPath = usdPrim.GetPath().GetParentPath();
                        Transform parentXf   = null;
                        if (parentPath == masterRootPrim.GetPath())
                        {
                            parentXf = goMaster.transform;
                        }
                        else
                        {
                            parentXf = map[parentPath].transform;
                        }

                        var goPrim = FindOrCreateGameObject(parentXf,
                                                            usdPrim.GetPath(),
                                                            unityRoot.transform,
                                                            map,
                                                            options);
                        ApplySelfVisibility(goPrim, usdPrim);

                        if (usdPrim.IsInstance())
                        {
                            map.AddInstanceRoot(usdPrim.GetPath(), goPrim, usdPrim.GetMaster().GetPath());
                        }

                        try {
                            var info = new HierInfo();
                            info.prim = usdPrim;
                            ReadModelInfo(ref info);
                            AddModelRoot(goPrim, info);
                            AddVariantSet(goPrim, usdPrim);
                        } catch (Exception ex) {
                            Debug.LogException(new Exception("Error processing " + usdPrim.GetPath(), ex));
                            continue;
                        }
                    }
                }
                Profiler.EndSample();
            }

            if (options.importSkinning)
            {
                Profiler.BeginSample("Expand Skeletons");
                foreach (var info in hierInfo)
                {
                    if (info.skelJoints == null || info.skelJoints.Length == 0)
                    {
                        continue;
                    }

                    try {
                        ExpandSkeleton(info, unityRoot, usdRoot, info.prim, map, options);
                    } catch (Exception ex) {
                        Debug.LogException(new Exception("Error expanding skeleton at " + info.prim.GetPath(), ex));
                    }
                }
                Profiler.EndSample();
            }

            return(map);
        }