public void GetImageBytes(glTF gltf, IStorage storage) { if (IsAsset) { return; } var image = gltf.GetImageFromTextureIndex(m_textureIndex); if (string.IsNullOrEmpty(image.uri)) { // // use buffer view (GLB) // var byteSegment = gltf.GetViewBytes(image.bufferView); m_imageBytes = ToArray(byteSegment); m_textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#GLB", m_textureIndex); } else { m_imageBytes = ToArray(storage.Get(image.uri)); if (image.uri.StartsWith("data:")) { m_textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#Base64Embeded", m_textureIndex); } else { m_textureName = !string.IsNullOrEmpty(image.name) ? image.name : Path.GetFileNameWithoutExtension(image.uri); } } }
public static ArraySegment <Byte> GetImageBytes(this glTF self, IStorage storage, int imageIndex, out string textureName) { var image = self.images[imageIndex]; if (string.IsNullOrEmpty(image.uri)) { // // use buffer view (GLB) // //m_imageBytes = ToArray(byteSegment); textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#GLB", imageIndex); return(self.GetViewBytes(image.bufferView)); } else { if (image.uri.FastStartsWith("data:")) { textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#Base64Embedded", imageIndex); } else { textureName = !string.IsNullOrEmpty(image.name) ? image.name : Path.GetFileNameWithoutExtension(image.uri); } return(storage.Get(image.uri)); } }
public void SaveTexturesAsPng(UnityPath prefabPath) { TextureBaseDir = prefabPath.Parent; TextureBaseDir.ImportAsset(); // // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html // for (int i = 0; i < GLTF.textures.Count; ++i) { var x = GLTF.textures[i]; var image = GLTF.images[x.source]; if (string.IsNullOrEmpty(image.uri)) { // glb buffer var folder = prefabPath.GetAssetFolder(".Textures"); folder.EnsureFolder(); // name & bytes var textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#GLB", i); var byteSegment = GLTF.GetViewBytes(image.bufferView); // path var png = folder.Child(textureName + ".png"); File.WriteAllBytes(png.FullPath, byteSegment.ToArray()); png.ImportAsset(); image.uri = png.Value.Substring(TextureBaseDir.Value.Length + 1); //Debug.LogFormat("image.uri: {0}", image.uri); } } UnityEditor.AssetDatabase.Refresh(); }
public static ArraySegment <Byte> GetImageBytes(this glTF self, IStorage storage, int imageIndex) { var image = self.images[imageIndex]; if (string.IsNullOrEmpty(image.uri)) { return(self.GetViewBytes(image.bufferView)); } else { return(storage.Get(image.uri)); } }
static TextureItem _ImportTexture(glTF gltf, int index) { var image = gltf.images[index]; if (string.IsNullOrEmpty(image.uri)) { // use buffer view var texture = new Texture2D(2, 2); texture.name = string.IsNullOrEmpty(image.extra.name) ? string.Format("buffer#{0:00}", index) : image.extra.name; var byteSegment = gltf.GetViewBytes(image.bufferView); var bytes = byteSegment.ToArray(); texture.LoadImage(bytes, true); return(new TextureItem(texture, index, false)); } else if (image.uri.StartsWith("data:")) { // embeded var bytes = UriByteBuffer.ReadEmbeded(image.uri); var texture = new Texture2D(2, 2); texture.name = string.IsNullOrEmpty(image.extra.name) ? "embeded" : image.extra.name; texture.LoadImage(bytes); return(new TextureItem(texture, index, false)); } #if UNITY_EDITOR else if (gltf.baseDir.StartsWith("Assets/")) { // local folder var path = Path.Combine(gltf.baseDir, image.uri); UnityEditor.AssetDatabase.ImportAsset(path); var texture = UnityEditor.AssetDatabase.LoadAssetAtPath <Texture2D>(path); texture.name = string.IsNullOrEmpty(image.extra.name) ? Path.GetFileNameWithoutExtension(path) : image.extra.name; return(new TextureItem(texture, index, true)); } #endif else { // external var path = Path.Combine(gltf.baseDir, image.uri); var bytes = File.ReadAllBytes(path); var texture = new Texture2D(2, 2); texture.name = string.IsNullOrEmpty(image.extra.name) ? Path.GetFileNameWithoutExtension(path) : image.extra.name; texture.LoadImage(bytes); return(new TextureItem(texture, index, false)); } }
private static Texture2D AssignTextureToMaterialPropertyAndExportAndExtract(Texture2D srcTex, string srcImageName, string propertyName) { // Prepare var root = GameObject.CreatePrimitive(PrimitiveType.Cube); var mat = new Material(Shader.Find("Standard")); mat.SetTexture(propertyName, srcTex); root.GetComponent <MeshRenderer>().sharedMaterial = mat; // Export glTF var gltf = new glTF(); using (var exporter = new gltfExporter(gltf, new GltfExportSettings { InverseAxis = Axes.X })) { exporter.Prepare(root); var settings = new GltfExportSettings { ExportOnlyBlendShapePosition = false, UseSparseAccessorForMorphTarget = false, DivideVertexBuffer = false, }; exporter.Export(settings, new EditorTextureSerializer()); } Assert.AreEqual(1, gltf.images.Count); var exportedImage = gltf.images[0]; Assert.AreEqual("image/png", exportedImage.mimeType); Assert.AreEqual(srcImageName, exportedImage.name); UnityEngine.Object.DestroyImmediate(mat); UnityEngine.Object.DestroyImmediate(root); // Extract Image to Texture2D var exportedBytes = gltf.GetViewBytes(exportedImage.bufferView).ToArray(); var exportedTexture = new Texture2D(2, 2, TextureFormat.ARGB32, mipChain: false, linear: false); Assert.IsTrue(exportedTexture.LoadImage(exportedBytes)); // Always true ? Assert.AreEqual(srcTex.width, exportedTexture.width); Assert.AreEqual(srcTex.height, exportedTexture.height); return(exportedTexture); }
public static TextureItem ImportTexture(glTF gltf, int index) { var image = gltf.images[index]; if (string.IsNullOrEmpty(image.uri)) { // use buffer view var texture = new Texture2D(2, 2); texture.name = string.Format("buffer#{0:00}", index); var byteSegment = gltf.GetViewBytes(image.bufferView); var bytes = byteSegment.Array.Skip(byteSegment.Offset).Take(byteSegment.Count).ToArray(); texture.LoadImage(bytes, true); return(new TextureItem(texture, index, false)); } #if UNITY_EDITOR else if (gltf.baseDir.StartsWith("Assets")) { // local folder var path = Path.Combine(gltf.baseDir, image.uri); path = path.Replace("%20", " "); var texture = UnityEditor.AssetDatabase.LoadAssetAtPath <Texture2D>(path); texture.name = Path.GetFileNameWithoutExtension(path); return(new TextureItem(texture, index, true)); } #endif else { // external var path = Path.Combine(gltf.baseDir, image.uri); var bytes = File.ReadAllBytes(path); var texture = new Texture2D(2, 2); texture.name = Path.GetFileNameWithoutExtension(path); texture.LoadImage(bytes); return(new TextureItem(texture, index, true)); } }