private static void ConstructTexture(this GltfObject gltfObject, GltfTexture gltfTexture) { var parentDirectory = Directory.GetParent(gltfObject.Uri).FullName; if (gltfTexture.source >= 0) { GltfImage gltfImage = gltfObject.images[gltfTexture.source]; var imagePath = $"{parentDirectory}\\{gltfImage.uri}"; // TODO Check if texture is in unity project, and use the asset reference instead. gltfImage.Texture = new Texture2D(0, 0); #if UNITY_WSA var imageData = UnityEngine.Windows.File.ReadAllBytes(imagePath); #else var imageData = File.ReadAllBytes(imagePath); #endif gltfImage.Texture.LoadImage(imageData, false); } }
private static async Task ConstructTextureAsync(this GltfObject gltfObject, GltfTexture gltfTexture) { if (gltfTexture.source >= 0) { GltfImage gltfImage = gltfObject.images[gltfTexture.source]; // TODO Check if texture is in unity project, and use the asset reference instead. await BackgroundThread; byte[] imageData; if (!string.IsNullOrEmpty(gltfObject.Uri) && !string.IsNullOrEmpty(gltfImage.uri)) { var parentDirectory = Directory.GetParent(gltfObject.Uri).FullName; using (FileStream stream = File.Open($"{parentDirectory}\\{gltfImage.uri}", FileMode.Open)) { imageData = new byte[stream.Length]; await stream.ReadAsync(imageData, 0, (int)stream.Length); } } else { var imageBufferView = gltfObject.bufferViews[gltfImage.bufferView]; imageData = new byte[imageBufferView.byteLength]; Array.Copy(imageBufferView.Buffer.BufferData, imageBufferView.byteOffset, imageData, 0, imageData.Length); } await Update; gltfImage.Texture = new Texture2D(2, 2); // TODO Load texture async gltfImage.Texture.LoadImage(imageData); await BackgroundThread; } }
private static async Task ConstructTextureAsync(this GltfObject gltfObject, GltfTexture gltfTexture) { if (Application.isPlaying) { await BackgroundThread; } if (gltfTexture.source >= 0) { GltfImage gltfImage = gltfObject.images[gltfTexture.source]; byte[] imageData = null; Texture2D texture = null; if (!string.IsNullOrEmpty(gltfObject.Uri) && !string.IsNullOrEmpty(gltfImage.uri)) { // TODO update to download and use http paths. var parentDirectory = Directory.GetParent(gltfObject.Uri).FullName; var path = $"{parentDirectory}\\{gltfImage.uri}"; #if UNITY_EDITOR if (Application.isPlaying) { await Update; } var projectPath = path.Replace("\\", "/"); projectPath = projectPath.Replace(Application.dataPath, "Assets"); texture = UnityEditor.AssetDatabase.LoadAssetAtPath <Texture2D>(projectPath); if (Application.isPlaying) { await BackgroundThread; } #endif if (texture == null) { Debug.LogWarning($"Attempting to load asset at {path}"); using (FileStream stream = File.Open(path, FileMode.Open)) { imageData = new byte[stream.Length]; if (Application.isPlaying) { await stream.ReadAsync(imageData, 0, (int)stream.Length); } else { stream.Read(imageData, 0, (int)stream.Length); } } } } else { var imageBufferView = gltfObject.bufferViews[gltfImage.bufferView]; imageData = new byte[imageBufferView.byteLength]; Array.Copy(imageBufferView.Buffer.BufferData, imageBufferView.byteOffset, imageData, 0, imageData.Length); } if (texture == null) { if (Application.isPlaying) { await Update; } // TODO Load texture async texture = new Texture2D(2, 2); gltfImage.Texture = texture; gltfImage.Texture.LoadImage(imageData); } else { gltfImage.Texture = texture; } gltfTexture.Texture = texture; if (Application.isPlaying) { await BackgroundThread; } } }
private static async Task ConstructTextureAsync(this GltfObject gltfObject, GltfTexture gltfTexture) { if (gltfObject.UseBackgroundThread) { await BackgroundThread; } if (gltfTexture.source >= 0) { GltfImage gltfImage = gltfObject.images[gltfTexture.source]; byte[] imageData = null; Texture2D texture = null; if (!string.IsNullOrEmpty(gltfObject.Uri) && !string.IsNullOrEmpty(gltfImage.uri)) { var parentDirectory = Directory.GetParent(gltfObject.Uri).FullName; var path = Path.Combine(parentDirectory, gltfImage.uri); #if UNITY_EDITOR if (gltfObject.UseBackgroundThread) { await Update; } var projectPath = Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "Assets"); texture = UnityEditor.AssetDatabase.LoadAssetAtPath <Texture2D>(projectPath); if (gltfObject.UseBackgroundThread) { await BackgroundThread; } #endif if (texture == null) { #if WINDOWS_UWP if (gltfObject.UseBackgroundThread) { try { var storageFile = await StorageFile.GetFileFromPathAsync(path); if (storageFile != null) { var buffer = await FileIO.ReadBufferAsync(storageFile); using (DataReader dataReader = DataReader.FromBuffer(buffer)) { imageData = new byte[buffer.Length]; dataReader.ReadBytes(imageData); } } } catch (Exception e) { Debug.LogError(e.Message); } } else { imageData = UnityEngine.Windows.File.ReadAllBytes(path); } #else using (FileStream stream = File.Open(path, FileMode.Open)) { imageData = new byte[stream.Length]; if (gltfObject.UseBackgroundThread) { await stream.ReadAsync(imageData, 0, (int)stream.Length); } else { stream.Read(imageData, 0, (int)stream.Length); } } #endif } } else { var imageBufferView = gltfObject.bufferViews[gltfImage.bufferView]; imageData = new byte[imageBufferView.byteLength]; Array.Copy(imageBufferView.Buffer.BufferData, imageBufferView.byteOffset, imageData, 0, imageData.Length); } if (texture == null) { if (gltfObject.UseBackgroundThread) { await Update; } // TODO Load texture async texture = new Texture2D(2, 2); gltfImage.Texture = texture; gltfImage.Texture.LoadImage(imageData); } else { gltfImage.Texture = texture; } gltfTexture.Texture = texture; if (gltfObject.UseBackgroundThread) { await BackgroundThread; } } }
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); } } }