private async void Start() { var path = AbsolutePath; if (!File.Exists(path)) { Debug.LogError($"Unable to find the glTF object at {path}"); DebugText.SetActive(true); return; } DebugText.SetActive(false); GltfObject gltfObject = null; try { gltfObject = await GltfUtility.ImportGltfObjectFromPathAsync(path); // Put object in front of user gltfObject.GameObjectReference.transform.position = new Vector3(0.0f, 0.0f, 1.0f); gltfObject.GameObjectReference.transform.localScale *= this.ScaleFactor; } catch (Exception e) { Debug.LogError($"TestGltfLoading start failed - {e.Message}\n{e.StackTrace}"); } if (gltfObject != null) { Debug.Log("Import successful"); } }
private async void Start() { var path = $"{Application.streamingAssetsPath}{uri}"; path = path.Replace("/", "\\"); if (!File.Exists(path)) { Debug.LogError($"Unable to find the glTF object at {path}"); } GltfObject gltfObject = null; try { gltfObject = await GltfUtility.ImportGltfObjectFromPathAsync(path); } catch (Exception e) { Debug.LogError($"{e.Message}\n{e.StackTrace}"); } if (gltfObject != null) { Debug.Log("Import successful"); } }
private async void Start() { await new WaitForSeconds(5f); var gltfObject = await GltfUtility.ImportGltfObjectFromPathAsync($"{Application.dataPath}{uri}"); if (gltfObject != null) { Debug.Log("Import successful"); } }
public IEnumerator TestGltfCustomAttributes() { // Load glTF string path = AssetDatabase.GUIDToAssetPath(AvocadoCustomAttrGuid); var task = GltfUtility.ImportGltfObjectFromPathAsync(path); yield return(WaitForTask(task)); GltfObject gltfObject = task.Result; yield return(null); // Check for custom attribute int temperatureIdx; gltfObject.meshes[0].primitives[0].Attributes.TryGetValue("_TEMPERATURE", out temperatureIdx); int temperature = gltfObject.accessors[temperatureIdx].count; Assert.AreEqual(100, temperature); }
public IEnumerator TestGltfLoads() { // Load glTF string path = AssetDatabase.GUIDToAssetPath(AvocadoCustomAttrGuid); var task = GltfUtility.ImportGltfObjectFromPathAsync(path); yield return(WaitForTask(task)); GltfObject gltfObject = task.Result; yield return(null); Assert.IsNotNull(gltfObject); Assert.AreEqual(1, gltfObject.meshes.Length); Assert.AreEqual(1, gltfObject.nodes.Length); // Check if mesh variables have been set by attributes Assert.AreEqual(406, gltfObject.meshes[0].Mesh.uv.Length); Assert.AreEqual(406, gltfObject.meshes[0].Mesh.normals.Length); Assert.AreEqual(406, gltfObject.meshes[0].Mesh.tangents.Length); Assert.AreEqual(406, gltfObject.meshes[0].Mesh.vertexCount); }
public IEnumerator TestGltfCustomAttributesData() { // Load glTF string path = AssetDatabase.GUIDToAssetPath(CubeCustomAttrGuid); var task = GltfUtility.ImportGltfObjectFromPathAsync(path); yield return(WaitForTask(task)); GltfObject gltfObject = task.Result; yield return(null); // Check for custom vertex data is a list of 10s gltfObject.meshes[0].primitives[0].Attributes.TryGetValue("_CUSTOM_ATTR", out var customAttrIdx); GltfAccessor accessor = gltfObject.GetAccessor(customAttrIdx); var intArray = accessor.GetIntArray(false); foreach (var item in intArray) { Assert.AreEqual(10, item); } }
private async void Start() { var path = $"{Application.streamingAssetsPath}{uri}"; path = path.Replace("/", "\\"); if (!File.Exists(path)) { Debug.LogError($"Unable to find the glTF object at {path}"); this.DebugText.SetActive(true); return; } this.DebugText.SetActive(false); GltfObject gltfObject = null; try { gltfObject = await GltfUtility.ImportGltfObjectFromPathAsync(path); // Put object in front of user gltfObject.GameObjectReference.transform.position = new Vector3(0.0f, 0.0f, 1.0f); gltfObject.GameObjectReference.transform.localScale *= this.ScaleFactor; } catch (Exception e) { Debug.LogError($"{e.Message}\n{e.StackTrace}"); } if (gltfObject != null) { Debug.Log("Import successful"); } }
public static async void OnImportGltfAsset(AssetImportContext context) { var importedObject = await GltfUtility.ImportGltfObjectFromPathAsync(context.assetPath); if (importedObject == null || importedObject.GameObjectReference == null) { Debug.LogError("Failed to import glTF object"); return; } var gltfAsset = (GltfAsset)ScriptableObject.CreateInstance(typeof(GltfAsset)); gltfAsset.GltfObject = importedObject; gltfAsset.name = $"{gltfAsset.GltfObject.Name}{Path.GetExtension(context.assetPath)}"; gltfAsset.Model = importedObject.GameObjectReference; context.AddObjectToAsset("main", gltfAsset.Model); context.SetMainObject(importedObject.GameObjectReference); context.AddObjectToAsset("glTF data", gltfAsset); bool reImport = false; for (var i = 0; i < gltfAsset.GltfObject.textures?.Length; i++) { GltfTexture gltfTexture = gltfAsset.GltfObject.textures[i]; if (gltfTexture == null) { continue; } var path = AssetDatabase.GetAssetPath(gltfTexture.Texture); if (string.IsNullOrWhiteSpace(path)) { var textureName = gltfTexture.name; if (string.IsNullOrWhiteSpace(textureName)) { textureName = $"Texture_{i}"; gltfTexture.Texture.name = textureName; } context.AddObjectToAsset(textureName, gltfTexture.Texture); } else { if (!gltfTexture.Texture.isReadable) { var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; if (textureImporter != null) { textureImporter.isReadable = true; textureImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings { format = TextureImporterFormat.RGBA32 }); textureImporter.SaveAndReimport(); reImport = true; } } } } if (reImport) { var importer = AssetImporter.GetAtPath(context.assetPath); importer.SaveAndReimport(); return; } for (var i = 0; i < gltfAsset.GltfObject.meshes?.Length; i++) { GltfMesh gltfMesh = gltfAsset.GltfObject.meshes[i]; string meshName = string.IsNullOrWhiteSpace(gltfMesh.name) ? $"Mesh_{i}" : gltfMesh.name; gltfMesh.Mesh.name = meshName; context.AddObjectToAsset($"{meshName}", gltfMesh.Mesh); } if (gltfAsset.GltfObject.materials != null) { foreach (GltfMaterial gltfMaterial in gltfAsset.GltfObject.materials) { context.AddObjectToAsset(gltfMaterial.name, gltfMaterial.Material); } } }
public static async void OnImportGltfAsset(AssetImportContext context) { var gltfAsset = (GltfAsset)ScriptableObject.CreateInstance(typeof(GltfAsset)); var importedObject = await GltfUtility.ImportGltfObjectFromPathAsync(context.assetPath); gltfAsset.GltfObject = importedObject; gltfAsset.name = $"{gltfAsset.GltfObject.Name}{Path.GetExtension(context.assetPath)}"; gltfAsset.Model = importedObject.GameObjectReference; context.AddObjectToAsset("main", gltfAsset.Model); context.SetMainObject(importedObject.GameObjectReference); context.AddObjectToAsset("glTF data", gltfAsset); bool reImport = false; for (var i = 0; i < gltfAsset.GltfObject.textures.Length; i++) { GltfTexture gltfTexture = gltfAsset.GltfObject.textures[i]; var path = AssetDatabase.GetAssetPath(gltfTexture.Texture); if (string.IsNullOrWhiteSpace(path)) { var textureName = gltfTexture.name; if (string.IsNullOrWhiteSpace(textureName)) { textureName = $"Texture_{i}"; gltfTexture.Texture.name = textureName; } context.AddObjectToAsset(textureName, gltfTexture.Texture); } else { if (!gltfTexture.Texture.isReadable) { var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; textureImporter.isReadable = true; textureImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings { format = TextureImporterFormat.RGBA32 }); textureImporter.SaveAndReimport(); reImport = true; } } } if (reImport) { var importer = AssetImporter.GetAtPath(context.assetPath); importer.SaveAndReimport(); return; } for (var i = 0; i < gltfAsset.GltfObject.meshes.Length; i++) { GltfMesh gltfMesh = gltfAsset.GltfObject.meshes[i]; string meshName = string.IsNullOrWhiteSpace(gltfMesh.name) ? $"Mesh_{i}" : gltfMesh.name; gltfMesh.Mesh.name = meshName; context.AddObjectToAsset($"{meshName}", gltfMesh.Mesh); } foreach (GltfMaterial gltfMaterial in gltfAsset.GltfObject.materials) { if (context.assetPath.EndsWith(".glb")) { context.AddObjectToAsset(gltfMaterial.name, gltfMaterial.Material); } else { var path = Path.GetFullPath(Path.GetDirectoryName(context.assetPath)); path = path.Replace("\\", "/").Replace(Application.dataPath, "Assets"); path = $"{path}/{gltfMaterial.name}.mat"; AssetDatabase.CreateAsset(gltfMaterial.Material, path); gltfMaterial.Material = AssetDatabase.LoadAssetAtPath <Material>(path); } } }