Beispiel #1
0
        private Model LoadModel(DataModels.JSONModels.AssetInfo modelInfo)
        {
            var assetBundlesURI = modelInfo.vrAssets;

            if (modelInfo.vrAssets == null || modelInfo.vrAssets.Length == 0)
            {
                if (modelInfo.assets != null && modelInfo.assets.Length > 0)
                {
                    Debug.LogWarning("Win 64 bundles are not generated for this model. Using WebGL bundles...");
                    assetBundlesURI = modelInfo.assets;
                }
                else
                {
                    throw new RepoModelLoadingException("This model does not have any unity bundles.");
                }
            }

            Vector3 relativeOffset = Vector3.zero;

            if (worldOffset == null)
            {
                worldOffset = modelInfo.offset;
            }
            else
            {
                relativeOffset = new Vector3((float)(modelInfo.offset[0] - worldOffset[0]),
                                             (float)(modelInfo.offset[1] - worldOffset[1]),
                                             (float)-(modelInfo.offset[2] - worldOffset[2]));   //unity goes towards the other direction than WebGL
            }

            GameObject[]    gameObjects = new GameObject[assetBundlesURI.Length];
            SuperMeshInfo[] smInfo      = new SuperMeshInfo[modelInfo.jsonFiles.Length];

            //TODO: this can be done asynchronously
            for (int i = 0; i < modelInfo.jsonFiles.Length; ++i)
            {
                smInfo[i] = ProcessSuperMeshInfo(repoHttpClient.LoadBundleJSON(modelInfo.jsonFiles[i]));
            }

            //TODO: this can be done asynchronously
            for (int i = 0; i < assetBundlesURI.Length; ++i)
            {
                AssetBundle bundle = repoHttpClient.LoadBundle(assetBundlesURI[i]);

                var names     = bundle.GetAllAssetNames();
                var bundleObj = bundle.LoadAsset(names[0]) as GameObject;
                gameObjects[i] = UnityEngine.Object.Instantiate(bundleObj);
                gameObjects[i].transform.position += relativeOffset;
                gameObjects[i].name = bundleObj.name;
                AttachShader(gameObjects[i], smInfo[i]);
                bundle.Unload(false);
            }

            return(new Model(modelInfo.database + "." + modelInfo.model, gameObjects, new Vector3((float)modelInfo.offset[0], (float)modelInfo.offset[1], (float)modelInfo.offset[2])));
        }
Beispiel #2
0
        /**
         * Load model given the model Info. This is typically called by the other LoadModel function.
         * @param modelInfo an object containing URIs to model information, this is obtained by  RepoWebInterface::GetUnityAssetInfo()
         * @return returns a model object containing model information
         */
        private Model LoadModel(DataModels.JSONModels.AssetInfo modelInfo, bool addPhyCollider)
        {
            var assetBundlesURI = modelInfo.vrAssets;

            if (modelInfo.vrAssets == null || modelInfo.vrAssets.Length == 0)
            {
                if (modelInfo.assets != null && modelInfo.assets.Length > 0)
                {
                    Debug.LogWarning("Win 64 bundles are not generated for this model. Using WebGL bundles...");
                    assetBundlesURI = modelInfo.assets;
                }
                else
                {
                    throw new RepoModelLoadingException("This model does not have any unity bundles.");
                }
            }

            Vector3 relativeOffset = Vector3.zero;

            if (worldOffset == null)
            {
                worldOffset = modelInfo.offset;
            }
            else
            {
                relativeOffset = new Vector3((float)(modelInfo.offset[0] - worldOffset[0]),
                                             (float)(modelInfo.offset[1] - worldOffset[1]),
                                             (float)-(modelInfo.offset[2] - worldOffset[2]));   //unity goes towards the other direction than WebGL
            }

            Dictionary <string, SuperMeshInfo>        supermeshes     = new Dictionary <string, SuperMeshInfo>();
            Dictionary <string, List <MeshLocation> > meshToLocations = new Dictionary <string, List <MeshLocation> >();
            Dictionary <string, Bounds> meshBboxEntries = new Dictionary <string, Bounds>();

            var revisionId = ExtractRevisionIdFromURI(assetBundlesURI[0]);

            var settings = repoHttpClient.LoadModelSettings(modelInfo.database, modelInfo.model);

            //TODO: this can be done asynchronously
            for (int i = 0; i < modelInfo.jsonFiles.Length; ++i)
            {
                var info = ProcessSuperMeshInfo(repoHttpClient.LoadBundleJSON(modelInfo.jsonFiles[i]), meshToLocations, meshBboxEntries);
                supermeshes[info.name] = info;
            }

            //TODO: this can be done asynchronously
            for (int i = 0; i < assetBundlesURI.Length; ++i)
            {
                AssetBundle bundle = repoHttpClient.LoadBundle(assetBundlesURI[i]);

                var names         = bundle.GetAllAssetNames();
                var bundleObj     = bundle.LoadAsset(names[0]) as GameObject;
                var superMeshName = bundleObj.name;
                var gameObject    = UnityEngine.Object.Instantiate(bundleObj);
                gameObject.transform.position += relativeOffset;
                gameObject.name = superMeshName;

                if (addPhyCollider)
                {
                    foreach (var meshFilter in gameObject.GetComponentsInChildren <MeshFilter>())
                    {
                        var collider = meshFilter.gameObject.AddComponent <MeshCollider>();
                        collider.sharedMesh = meshFilter.mesh;
                    }
                }
                supermeshes[superMeshName].gameObj = gameObject;
                AttachShader(supermeshes[superMeshName]);
                bundle.Unload(false);
            }

            return(new Model(modelInfo.database, modelInfo.model, revisionId, settings,
                             supermeshes, meshToLocations, meshBboxEntries, new Vector3((float)modelInfo.offset[0], (float)modelInfo.offset[1], (float)modelInfo.offset[2]),
                             repoHttpClient));
        }