public static Image FromGltf(this GltfImage x, GltfSerialization.GltfStorage storage) { var view = storage.Gltf.bufferViews[x.bufferView]; var buffer = storage.Gltf.buffers[view.buffer]; var memory = storage.GetBufferBytes(buffer); // テクスチャの用途を調べる var usage = default(ImageUsage); foreach (var material in storage.Gltf.materials) { var colorImage = GetColorImage(storage, material); if (colorImage == x) { usage |= ImageUsage.Color; } var normalImage = GetNormalImage(storage, material); if (normalImage == x) { usage |= ImageUsage.Normal; } } return(new Image(x.name, x.mimeType, usage, memory.Slice(view.byteOffset, view.byteLength))); }
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; } } }