public void CreateTextureItems(UnityPath imageBaseDir = default(UnityPath)) { if (m_textures.Any()) { return; } for (int i = 0; i < GLTF.textures.Count; ++i) { TextureItem item = null; #if UNITY_EDITOR var image = GLTF.GetImageFromTextureIndex(i); if (imageBaseDir.IsUnderAssetsFolder && !string.IsNullOrEmpty(image.uri) && !image.uri.StartsWith("data:") ) { /// /// required SaveTexturesAsPng or SetTextureBaseDir /// var assetPath = imageBaseDir.Child(image.uri); var textureName = !string.IsNullOrEmpty(image.name) ? image.name : Path.GetFileNameWithoutExtension(image.uri); item = new TextureItem(i, assetPath, textureName); } else #endif { item = new TextureItem(i, CreateTextureLoader(i)); } AddTexture(item); } }
public static IEnumerable <(string, UnityEngine.Object)> EnumerateTexturesFromUri(Dictionary <AssetImporter.SourceAssetIdentifier, UnityEngine.Object> exclude, GltfParser parser, UnityPath dir) { foreach (var texParam in GltfTextureEnumerator.Enumerate(parser.GLTF)) { switch (texParam.TextureType) { case GetTextureParam.TextureTypes.StandardMap: break; default: { var gltfTexture = parser.GLTF.textures.First(y => y.name == texParam.GltflName); var gltfImage = parser.GLTF.images[gltfTexture.source]; if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:")) { var child = dir.Child(gltfImage.uri); var asset = AssetDatabase.LoadAssetAtPath <Texture2D>(child.Value); if (asset == null) { throw new System.IO.FileNotFoundException($"{child}"); } // Debug.Log($"exists: {child}: {asset}"); if (exclude == null || !exclude.Any(kv => kv.Value.name == asset.name)) { yield return(asset.name, asset); } } } break; } } }
public void Extract(GetTextureParam param, bool hasUri) { if (Textures.Values.Contains(param)) { return; } var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName); UnityPath targetPath = default; if (hasUri && !param.ExtractConverted) { var gltfTexture = GLTF.textures[param.Index0.Value]; var gltfImage = GLTF.images[gltfTexture.source]; var ext = GetExt(gltfImage.mimeType, gltfImage.uri); targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}"); } else { switch (param.TextureType) { case GetTextureParam.TextureTypes.StandardMap: { // write converted texture targetPath = m_textureDirectory.Child($"{param.ConvertedName}.png"); File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray()); targetPath.ImportAsset(); break; } default: { // write original bytes var gltfTexture = GLTF.textures[param.Index0.Value]; var gltfImage = GLTF.images[gltfTexture.source]; var ext = GetExt(gltfImage.mimeType, gltfImage.uri); targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}"); File.WriteAllBytes(targetPath.FullPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray()); targetPath.ImportAsset(); break; } } } Textures.Add(targetPath, param); }
public void Extract(TextureImportParam param) { if (Textures.Values.Contains(param)) { return; } UnityPath targetPath = default; if (!string.IsNullOrEmpty(param.Uri) && !param.ExtractConverted) { targetPath = m_textureDirectory.Child(param.GltfFileName); } else { switch (param.TextureType) { case TextureImportTypes.StandardMap: { // write converted texture var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName); targetPath = m_textureDirectory.Child(param.ConvertedFileName); File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray()); targetPath.ImportAsset(); break; } default: { // write original bytes targetPath = m_textureDirectory.Child(param.GltfFileName); File.WriteAllBytes(targetPath.FullPath, param.Index0().Result.ToArray()); targetPath.ImportAsset(); break; } } } Textures.Add(targetPath, param); }
public TextureItem(glTF gltf, int index, UnityPath textureBase = default(UnityPath)) { m_textureIndex = index; var image = gltf.GetImageFromTextureIndex(m_textureIndex); #if UNITY_EDITOR if (!string.IsNullOrEmpty(image.uri) && !image.uri.StartsWith("data:") && textureBase.IsUnderAssetsFolder) { m_assetPath = textureBase.Child(image.uri); m_textureName = !string.IsNullOrEmpty(image.name) ? image.name : Path.GetFileNameWithoutExtension(image.uri); } #endif }
public void Extract(SubAssetKey key, TextureDescriptor texDesc) { if (Textures.ContainsKey(key)) { return; } // write converted texture if (m_subAssets.TryGetValue(key, out var texture) && texture is Texture2D tex2D) { var targetPath = m_textureDirectory.Child($"{key.Name}.png"); File.WriteAllBytes(targetPath.FullPath, tex2D.EncodeToPNG().ToArray()); targetPath.ImportAsset(); Textures.Add(key, targetPath); } else { throw new Exception($"{key} is not converted."); } }