public static void Load(string path, Action <GameObject> onLoad) { //GLTF Component Settings int MaximumLod = 300; bool Multithreaded = true; ILoader loader = null; Shader shaderOverride = null; GLTFSceneImporter sceneImporter = null; string directoryPath = URIHelper.GetDirectoryName(path); loader = new WebRequestLoader(directoryPath); sceneImporter = new GLTFSceneImporter( Path.GetFileName(path), loader ); sceneImporter.Collider = GLTFSceneImporter.ColliderType.None; sceneImporter.MaximumLod = MaximumLod; sceneImporter.CustomShaderName = shaderOverride ? shaderOverride.name : null; Action <GameObject> newOnLoad = (go) => { onLoad(go); }; // Call coroutine to load AR object ThreadHelper.Instance.StartCoroutine(LoadScene(sceneImporter, Multithreaded, newOnLoad)); }
public static async Task Load(GameObject gameObjectLoader, string url, Action callback, Action <GameObject> onInitializeGltfObject) { ImporterFactory Factory = null; bool UseStream = true; string GLTFUri = url; bool Multithreaded = true; int MaximumLod = 300; int Timeout = 8; var importOptions = new ImportOptions { AsyncCoroutineHelper = gameObjectLoader.GetComponent <AsyncCoroutineHelper>() ?? gameObjectLoader.AddComponent <AsyncCoroutineHelper>() }; GLTFSceneImporter sceneImporter = null; var cancelToken = new CancellationTokenSource(); try { Factory = Factory ?? ScriptableObject.CreateInstance <DefaultImporterFactory>(); if (UseStream) { var fullPath = GLTFUri; string directoryPath = URIHelper.GetDirectoryName(fullPath); importOptions.DataLoader = new FileLoader(directoryPath); sceneImporter = Factory.CreateSceneImporter(Path.GetFileName(GLTFUri), importOptions); } else { string directoryPath = URIHelper.GetDirectoryName(GLTFUri); importOptions.DataLoader = new WebRequestLoader(directoryPath); sceneImporter = Factory.CreateSceneImporter(URIHelper.GetFileFromUri(new Uri(GLTFUri)), importOptions); } sceneImporter.SceneParent = gameObjectLoader.transform; sceneImporter.MaximumLod = MaximumLod; sceneImporter.Timeout = Timeout; sceneImporter.IsMultithreaded = Multithreaded; sceneImporter.OnInitializeGltfObject = onInitializeGltfObject; mTasks.Add(cancelToken); await sceneImporter.LoadSceneAsync(-1, true, null, cancelToken.Token); } finally { if (importOptions.DataLoader != null) { if (sceneImporter != null) { mCreatedScenes.Add(sceneImporter.CreatedObject); if (cancelToken.IsCancellationRequested) { sceneImporter.CreatedObject.DestroySelf(); } sceneImporter.Dispose(); sceneImporter = null; } importOptions.DataLoader = null; } callback.InvokeGracefully(); } }
IEnumerator Start() { GLTFSceneImporter sceneImporter = null; ILoader loader = null; if (UseStream) { // Path.Combine treats paths that start with the separator character // as absolute paths, ignoring the first path passed in. This removes // that character to properly handle a filename written with it. GLTFUri = GLTFUri.TrimStart(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); string fullPath = Path.Combine(Application.streamingAssetsPath, GLTFUri); string directoryPath = URIHelper.GetDirectoryName(fullPath); loader = new FileLoader(directoryPath); sceneImporter = new GLTFSceneImporter( Path.GetFileName(GLTFUri), loader ); } else { string directoryPath = URIHelper.GetDirectoryName(GLTFUri); loader = new WebRequestLoader(directoryPath); sceneImporter = new GLTFSceneImporter( URIHelper.GetFileFromUri(new Uri(GLTFUri)), loader ); } sceneImporter.SceneParent = gameObject.transform; sceneImporter.Collider = Colliders; sceneImporter.MaximumLod = MaximumLod; yield return(sceneImporter.LoadScene(-1, Multithreaded, HandleAction)); }
public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null) { this.provider = provider; this.url = url.Substring(url.LastIndexOf('/') + 1); this.id = hash ?? url; // We separate the directory path of the GLB and its file name, to be able to use the directory path when // fetching relative assets like textures in the ParseGLTFWebRequestedFile() event call assetDirectoryPath = URIHelper.GetDirectoryName(url); }
IEnumerator Start() { ILoader loader = new WebRequestLoader(URIHelper.GetDirectoryName(Url)); var sceneImporter = new GLTFSceneImporter( URIHelper.GetFileFromUri(new Uri(Url)), loader ); sceneImporter.SceneParent = gameObject.transform; yield return(sceneImporter.LoadScene(-1, Multithreaded)); IntegrationTest.Pass(); }
public IEnumerator Download(Promise <bool> loadComplete) { string directoryPath = URIHelper.GetDirectoryName(Url); string relativePath = Url.Replace(directoryPath, ""); var wrl = new B3DMLoader(new WebRequestLoader(directoryPath)); GLTFSceneImporter sceneImporter = new GLTFSceneImporter( relativePath, wrl ); sceneImporter.SceneParent = gameObject.transform; sceneImporter.CustomShaderName = ShaderOverride ? ShaderOverride.name : null; sceneImporter.MaximumLod = MaximumLod; if (addColliders) { sceneImporter.Collider = GLTFSceneImporter.ColliderType.Mesh; } yield return(sceneImporter.LoadScene(-1, Multithreaded, sceneObject => { if (sceneObject != null) { loadComplete.Resolve(true); } else { loadComplete.Resolve(false); } Destroy(this); })); // Override the shaders on all materials if a shader is provided if (ShaderOverride != null) { Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>(); foreach (Renderer renderer in renderers) { renderer.sharedMaterial.shader = ShaderOverride; } } }
private async Task <IList <Asset> > LoadAssetsFromGLTF(AssetSource source, Guid containerId, ColliderType colliderType) { WebRequestLoader loader = null; Stream stream = null; source.ParsedUri = new Uri(_app.ServerAssetUri, source.ParsedUri); var rootUri = URIHelper.GetDirectoryName(source.ParsedUri.AbsoluteUri); var cachedVersion = MREAPI.AppsAPI.AssetCache.SupportsSync ? MREAPI.AppsAPI.AssetCache.GetVersionSync(source.ParsedUri) : await MREAPI.AppsAPI.AssetCache.GetVersion(source.ParsedUri); // Wait asynchronously until the load throttler lets us through. using (var scope = await AssetLoadThrottling.AcquireLoadScope()) { // set up loader loader = new WebRequestLoader(rootUri); if (!string.IsNullOrEmpty(cachedVersion)) { loader.BeforeRequestCallback += (msg) => { if (msg.RequestUri == source.ParsedUri) { msg.Headers.Add("If-None-Match", cachedVersion); } }; } // download root gltf file, check for cache hit try { stream = await loader.LoadStreamAsync(URIHelper.GetFileFromUri(source.ParsedUri)); source.Version = loader.LastResponse.Headers.ETag?.Tag ?? ""; } catch (HttpRequestException) { if (loader.LastResponse.StatusCode == System.Net.HttpStatusCode.NotModified) { source.Version = cachedVersion; } else { throw; } } } IList <Asset> assetDefs = new List <Asset>(30); DeterministicGuids guidGenerator = new DeterministicGuids(UtilMethods.StringToGuid( $"{containerId}:{source.ParsedUri.AbsoluteUri}")); IList <UnityEngine.Object> assets; // fetch assets from glTF stream or cache if (source.Version != cachedVersion) { assets = await LoadGltfFromStream(loader, stream, colliderType); MREAPI.AppsAPI.AssetCache.StoreAssets(source.ParsedUri, assets, source.Version); } else { var assetsEnum = MREAPI.AppsAPI.AssetCache.SupportsSync ? MREAPI.AppsAPI.AssetCache.LeaseAssetsSync(source.ParsedUri) : await MREAPI.AppsAPI.AssetCache.LeaseAssets(source.ParsedUri); assets = assetsEnum.ToList(); } // catalog assets int textureIndex = 0, meshIndex = 0, materialIndex = 0, prefabIndex = 0; foreach (var asset in assets) { var assetDef = GenerateAssetPatch(asset, guidGenerator.Next()); assetDef.Name = asset.name; string internalId = null; if (asset is UnityEngine.Texture) { internalId = $"texture:{textureIndex++}"; } else if (asset is UnityEngine.Mesh) { internalId = $"mesh:{meshIndex++}"; } else if (asset is UnityEngine.Material) { internalId = $"material:{materialIndex++}"; } else if (asset is GameObject) { internalId = $"scene:{prefabIndex++}"; } assetDef.Source = new AssetSource(source.ContainerType, source.ParsedUri.AbsoluteUri, internalId, source.Version); ColliderGeometry colliderGeo = null; if (asset is UnityEngine.Mesh mesh) { colliderGeo = colliderType == ColliderType.Mesh ? (ColliderGeometry) new MeshColliderGeometry() { MeshId = assetDef.Id } : (ColliderGeometry) new BoxColliderGeometry() { Size = (mesh.bounds.size * 0.8f).CreateMWVector3(), Center = mesh.bounds.center.CreateMWVector3() }; } _app.AssetManager.Set(assetDef.Id, containerId, asset, colliderGeo, assetDef.Source); assetDefs.Add(assetDef); } return(assetDefs); }
private async Task <IList <Asset> > LoadAssetsFromGLTF(AssetSource source, Guid containerId, ColliderType colliderType) { IList <Asset> assets = new List <Asset>(); DeterministicGuids guidGenerator = new DeterministicGuids(UtilMethods.StringToGuid( $"{containerId}:{source.ParsedUri.AbsoluteUri}")); // download file var rootUrl = URIHelper.GetDirectoryName(source.ParsedUri.AbsoluteUri); var loader = new WebRequestLoader(rootUrl); var stream = await loader.LoadStreamAsync(URIHelper.GetFileFromUri(source.ParsedUri)); // pre-parse glTF document so we can get a scene count // TODO: run this in thread GLTF.GLTFParser.ParseJson(stream, out GLTF.Schema.GLTFRoot gltfRoot); stream.Position = 0; GLTFSceneImporter importer = MREAPI.AppsAPI.GLTFImporterFactory.CreateImporter(gltfRoot, loader, _asyncHelper, stream); importer.SceneParent = MREAPI.AppsAPI.AssetCache.CacheRootGO().transform; importer.Collider = colliderType.ToGLTFColliderType(); // load textures if (gltfRoot.Textures != null) { for (var i = 0; i < gltfRoot.Textures.Count; i++) { await importer.LoadTextureAsync(gltfRoot.Textures[i], i, true); var texture = importer.GetTexture(i); texture.name = gltfRoot.Textures[i].Name ?? $"texture:{i}"; var asset = GenerateAssetPatch(texture, guidGenerator.Next()); asset.Name = texture.name; asset.Source = new AssetSource(source.ContainerType, source.Uri, $"texture:{i}"); MREAPI.AppsAPI.AssetCache.CacheAsset(texture, asset.Id, containerId, source); assets.Add(asset); } } // load meshes if (gltfRoot.Meshes != null) { var cancellationSource = new System.Threading.CancellationTokenSource(); for (var i = 0; i < gltfRoot.Meshes.Count; i++) { var mesh = await importer.LoadMeshAsync(i, cancellationSource.Token); mesh.name = gltfRoot.Meshes[i].Name ?? $"mesh:{i}"; var asset = GenerateAssetPatch(mesh, guidGenerator.Next()); asset.Name = mesh.name; asset.Source = new AssetSource(source.ContainerType, source.Uri, $"mesh:{i}"); var colliderGeo = colliderType == ColliderType.Mesh ? (ColliderGeometry) new MeshColliderGeometry() { MeshId = asset.Id } : (ColliderGeometry) new BoxColliderGeometry() { Size = (mesh.bounds.size * 0.8f).CreateMWVector3() }; MREAPI.AppsAPI.AssetCache.CacheAsset(mesh, asset.Id, containerId, source, colliderGeo); assets.Add(asset); } } // load materials if (gltfRoot.Materials != null) { for (var i = 0; i < gltfRoot.Materials.Count; i++) { var matdef = gltfRoot.Materials[i]; var material = await importer.LoadMaterialAsync(i); material.name = matdef.Name ?? $"material:{i}"; var asset = GenerateAssetPatch(material, guidGenerator.Next()); asset.Name = material.name; asset.Source = new AssetSource(source.ContainerType, source.Uri, $"material:{i}"); MREAPI.AppsAPI.AssetCache.CacheAsset(material, asset.Id, containerId, source); assets.Add(asset); } } // load prefabs if (gltfRoot.Scenes != null) { for (var i = 0; i < gltfRoot.Scenes.Count; i++) { await importer.LoadSceneAsync(i).ConfigureAwait(true); GameObject rootObject = importer.LastLoadedScene; rootObject.name = gltfRoot.Scenes[i].Name ?? $"scene:{i}"; var animation = rootObject.GetComponent <UnityEngine.Animation>(); if (animation != null) { animation.playAutomatically = false; // initialize mapping so we know which gameobjects are targeted by which animation clips var mapping = rootObject.AddComponent <PrefabAnimationTargets>(); mapping.Initialize(gltfRoot, i); } MWGOTreeWalker.VisitTree(rootObject, (go) => { go.layer = MREAPI.AppsAPI.LayerApplicator.DefaultLayer; }); var def = GenerateAssetPatch(rootObject, guidGenerator.Next()); def.Name = rootObject.name; def.Source = new AssetSource(source.ContainerType, source.Uri, $"scene:{i}"); MREAPI.AppsAPI.AssetCache.CacheAsset(rootObject, def.Id, containerId, source); assets.Add(def); } } importer.Dispose(); return(assets); }
public async Task LoadGltfAsync(GameObject newObject, string fullPath, ModelImportOptions importOptions) { asyncCoroutineHelper = newObject.GetComponent <AsyncCoroutineHelper>() ?? gameObject.AddComponent <AsyncCoroutineHelper>(); //GLTFSceneImporter sceneImporter = null; ILoader loader = null; try { // Path.Combine treats paths that start with the separator character // as absolute paths, ignoring the first path passed in. This removes // that character to properly handle a filename written with it. fullPath = fullPath.TrimStart(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); string directoryPath = URIHelper.GetDirectoryName(fullPath); loader = new FileLoader(directoryPath); sceneImporter = new GLTFSceneImporter( Path.GetFileName(fullPath), loader, asyncCoroutineHelper ); sceneImporter.SceneParent = newObject.transform; sceneImporter.Collider = Collider; if (importOptions != null) { if (importOptions.buildColliders) { sceneImporter.Collider = GLTFSceneImporter.ColliderType.MeshConvex; //if (importOptions.colliderConvex) //{ // sceneImporter.Collider = GLTFSceneImporter.ColliderType.MeshConvex; //} //else //{ // sceneImporter.Collider = GLTFSceneImporter.ColliderType.Mesh; //} } else { sceneImporter.Collider = GLTFSceneImporter.ColliderType.None; } } sceneImporter.MaximumLod = MaximumLod; sceneImporter.Timeout = Timeout; sceneImporter.isMultithreaded = Multithreaded; sceneImporter.CustomShaderName = shaderOverride ? shaderOverride.name : null; await sceneImporter.LoadSceneAsync(); // Override the shaders on all materials if a shader is provided if (shaderOverride != null) { Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>(); foreach (Renderer renderer in renderers) { renderer.sharedMaterial.shader = shaderOverride; } } if (PlayAnimationOnLoad) { Animation[] animations = sceneImporter.LastLoadedScene.GetComponents <Animation>(); foreach (Animation anim in animations) { anim.Play(); } } } finally { if (loader != null) { sceneImporter?.Dispose(); sceneImporter = null; loader = null; } } }