Пример #1
0
        /// <summary>
        /// Asynchronously imports the given .obj file from the specified url
        /// </summary>
        /// <param name="url">The url to the .obj file</param>
        /// <returns>The GameObject that was created for the imported .obj</returns>
        public async Task <GameObject> ImportAsync(string url)
        {
            i5Debug.Log("Starting import", this);
            Uri uri = new Uri(url);
            // fetch the model
            WebResponse <string> resp = await FetchModelAsync(uri);

            // if there was an error, we cannot create anything
            if (!resp.Successful)
            {
                i5Debug.LogError("Error fetching obj. No object imported.\n" + resp.ErrorMessage, this);
                return(null);
            }

            // create the parent object
            // it is a standard GameObject; its only purpose is to bundle the child objects
            GameObject parentObject = ObjectPool <GameObject> .RequestResource(() => { return(new GameObject()); });

            parentObject.name = System.IO.Path.GetFileNameWithoutExtension(uri.LocalPath);

            // parse the .obj file
            List <ObjParseResult> parseResults = await ParseModelAsync(resp.Content);

            // for each sub-object in the .obj file, an own parse result was created
            foreach (ObjParseResult parseResult in parseResults)
            {
                // check that the referenced mtl library is already loaded; if not: load it
                if (!MtlLibrary.LibraryLoaded(parseResult.LibraryPath))
                {
                    string mtlUri      = UriUtils.RewriteFileUriPath(uri, parseResult.LibraryPath);
                    string libraryName = System.IO.Path.GetFileNameWithoutExtension(uri.LocalPath);
                    bool   successful  = await MtlLibrary.LoadLibraryAsyc(new Uri(mtlUri), libraryName);

                    if (!successful)
                    {
                        i5Debug.LogError("Could not load .mtl file " + parseResult.LibraryPath, this);
                    }
                }

                // get the material constructor of the sub-object
                MaterialConstructor mat = MtlLibrary.GetMaterialConstructor(
                    System.IO.Path.GetFileNameWithoutExtension(uri.LocalPath),
                    parseResult.MaterialName);

                if (mat != null)
                {
                    // first get dependencies; this will e.g. fetch referenced textures
                    await mat.FetchDependencies();

                    parseResult.ObjectConstructor.MaterialConstructor = mat;
                }

                // construct the object and make it a child of the parentObject
                parseResult.ObjectConstructor.ConstructObject(parentObject.transform);
            }

            return(parentObject);
        }
Пример #2
0
        /// <summary>
        /// Called by the service manager to initialize the service if it is started
        /// </summary>
        /// <param name="owner">The service manager that owns this service</param>
        public void Initialize(IServiceManager owner)
        {
            MtlLibrary = new MtlLibrary();

            // initialize the content loader
            if (ContentLoader == null)
            {
                ContentLoader = new UnityWebRequestLoader();
            }

            // reserve a mesh object pool id
            meshObjectPoolId = ObjectPool <GameObject> .CreateNewPool();
        }