private void ExportTextures(string texturePath) { foreach (TextureInfo texture in loader.textures) { if (texture == null) { continue; } string textureName = texture.name != null ? texture.name : texture.offset.offset.ToString("X8"); string path = Path.Combine(texturePath, texture.name + ".png"); if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } if (File.Exists(path)) { File.Delete(path); } using (FileStream fileStream = File.Create(path)) { byte[] pngData = ImageConversion.EncodeToPNG(texture.Texture); fileStream.Write(pngData, 0, pngData.Length); fileStream.Flush(); fileStream.Close(); } } }
public void SaveTextureToPng(Texture2D tex, string path, string fileName) { var data = ImageConversion.EncodeToPNG(tex); var fullName = string.Concat(path, "/", fileName, ".png"); File.WriteAllBytes(fullName, data); }
public byte[] ConvertFrom(Cubemap cubemap) { int width = cubemap.width; int height = cubemap.height; Texture2D texture = new Texture2D( width, height * 6, TextureFormat.RGB24, false); // SetPixelsが画像の左下から右上に描画していくため、 // 下から上の順番でコピー var faces = new CubemapFace[] { CubemapFace.NegativeZ, CubemapFace.PositiveZ, CubemapFace.NegativeY, CubemapFace.PositiveY, CubemapFace.NegativeX, CubemapFace.PositiveX, }; int y = 0; foreach (var face in faces) { texture.SetPixels(0, y, width, height, cubemap, face); y += height; } texture.Apply(); return(ImageConversion.EncodeToPNG(texture)); }
public SerializedMap(Map map) { thumbnail = new SerializedTexture(ImageConversion.EncodeToPNG(map.thumbnail.texture), map.thumbnail.texture.width, map.thumbnail.texture.height); guid = map.guid.ToString(); name = map.name; teamString = map.teamString; }
public override void OnInspectorGUI() { DrawDefaultInspector(); ColourGrading mp = (ColourGrading)target; if (GUILayout.Button("Generate Default LUT")) { Texture2D lut = new Texture2D(256, 16, TextureFormat.RGB24, false, true); Color[] pixels = lut.GetPixels(); for (int r = 0; r < 16; ++r) { for (int g = 0; g < 16; ++g) { for (int b = 0; b < 16; ++b) { pixels[b * 16 + g * 256 + r] = new Color(r / 15.0f, g / 15.0f, b / 15.0f); } } } lut.SetPixels(pixels); lut.Apply(false); string path = Path.Combine(Application.dataPath, "NullLUT.png"); File.WriteAllBytes(path, ImageConversion.EncodeToPNG(lut)); AssetDatabase.ImportAsset(path); } }
private static void ExportCache(int mip) { Debug.LogFormat("Exporting Mip{0}...", mip); var cache = GetCache(mip); int pageSizewithPadding = s_PageSize + s_PaddingSize * 2; int pageCount = cache.Size / s_PageSize; for (int row = 0; row < pageCount; row++) { for (int col = 0; col < pageCount; col++) { var pixels = cache.GetPixels( col * s_PageSize - s_PaddingSize, row * s_PageSize - s_PaddingSize, pageSizewithPadding, pageSizewithPadding); var texture = new Texture2D(pageSizewithPadding, pageSizewithPadding, TextureFormat.RGB24, false); texture.LoadRawTextureData(pixels); texture.Apply(false); var bytes = ImageConversion.EncodeToPNG(texture); var file = Path.Combine(Application.streamingAssetsPath, string.Format("Slide_MIP{2}_Y{1}_X{0}.png", col, row, mip)); File.WriteAllBytes(file, bytes); } } Debug.LogFormat("Export Mip{0} Done.", mip); }
void Start() { var graph = MixtureDatabase.GetGraphFromTexture(graphTexture); graph.SetParameterValue("Source", source); graph.SetParameterValue("Target", target); // make sure compression is not enabled for the readback graph.outputNode.mainOutput.enableCompression = false; // Create the destination texture var settings = graph.outputNode.settings; Texture2D destination = new Texture2D( settings.GetResolvedWidth(graph), settings.GetResolvedHeight(graph), settings.GetGraphicsFormat(graph), TextureCreationFlags.None ); // Process the graph MixtureGraphProcessor processor = new MixtureGraphProcessor(graph); processor.Run(); // Readback the result graph.ReadbackMainTexture(destination); // TODO: debug // image.texture = debugRT; // Write the file at the target destination var bytes = ImageConversion.EncodeToPNG(destination); File.WriteAllBytes(outputPath, bytes); }
public static void GenerateThumbnailImage() { foreach (var obj in Selection.objects) { var objFilePath = AssetDatabase.GetAssetPath(obj); var imageFilePath = Path.Combine(Path.GetDirectoryName(objFilePath), Path.GetFileNameWithoutExtension(objFilePath) + ".png"); var thumbnailTexture = AssetPreview.GetAssetPreview(obj); if (thumbnailTexture != null) { if (File.Exists(imageFilePath)) { var asset = Provider.GetAssetByPath(imageFilePath); if (Provider.CheckoutIsValid(asset)) { Provider.Checkout(asset, CheckoutMode.Asset).Wait(); } } File.WriteAllBytes(imageFilePath, ImageConversion.EncodeToPNG(thumbnailTexture)); } else { Debug.LogError($"Unable to create thumbnail {imageFilePath}"); } } }
public static void CreatePngScreen(ref byte[] pngData, int createW, int createH) { Vector2 screenSize = ScreenInfo.GetScreenSize(); int x = (int)screenSize.x; int y = (int)screenSize.y; Texture2D tex = new Texture2D(x, y, (TextureFormat)3, false); RenderTexture renderTexture = QualitySettings.get_antiAliasing() != 0 ? RenderTexture.GetTemporary(x, y, 24, (RenderTextureFormat)7, (RenderTextureReadWrite)0, QualitySettings.get_antiAliasing()) : RenderTexture.GetTemporary(x, y, 24); if (Object.op_Inequality((Object)null, (Object)Camera.get_main())) { Camera main = Camera.get_main(); RenderTexture targetTexture = main.get_targetTexture(); Rect rect = main.get_rect(); main.set_targetTexture(renderTexture); main.Render(); main.set_targetTexture(targetTexture); main.set_rect(rect); } RenderTexture.set_active(renderTexture); tex.ReadPixels(new Rect(0.0f, 0.0f, (float)x, (float)y), 0, 0); tex.Apply(); RenderTexture.set_active((RenderTexture)null); RenderTexture.ReleaseTemporary(renderTexture); TextureScale.Bilinear(tex, createW, createH); pngData = ImageConversion.EncodeToPNG(tex); Object.Destroy((Object)tex); }
private static Texture2D ExportNormal(Vector3[] normalTriangle) { Color[] colorArray = new Color[m_terrain.get_terrainData().get_heightmapWidth() * m_terrain.get_terrainData().get_heightmapHeight()]; int index = 0; for (int i = m_terrain.get_terrainData().get_heightmapHeight() - 1; i >= 0; i--) { for (int j = 0; j < m_terrain.get_terrainData().get_heightmapWidth(); j++) { Vector3 vector = calcVertextNorml1(j, i, normalTriangle, m_terrain.get_terrainData().get_heightmapWidth() - 1, m_terrain.get_terrainData().get_heightmapHeight() - 1); vector.x = (vector.x + 1f) * 0.5f; vector.y = (vector.y + 1f) * 0.5f; vector.z = (vector.z + 1f) * 0.5f; colorArray[index] = new Color(vector.x, vector.y, vector.z, 1f); index++; } } Texture2D textured = new Texture2D(m_terrain.get_terrainData().get_heightmapWidth(), m_terrain.get_terrainData().get_heightmapHeight(), 4, false); textured.SetPixels(colorArray); textured.Apply(); textured.set_name(m_terrain.get_name().ToLower() + "_normalMap"); File.WriteAllBytes(m_saveLocation + "/" + textured.get_name() + ".png", ImageConversion.EncodeToPNG(textured)); return(textured); }
public string TakePicture(string name = "image", string savePath = "") { //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Expected O, but got Unknown //IL_0060: Unknown result type (might be due to invalid IL or missing references) //IL_0066: Expected O, but got Unknown //IL_0090: Unknown result type (might be due to invalid IL or missing references) RenderTexture targetTexture = shotCamera.get_targetTexture(); float aspect = shotCamera.get_aspect(); RenderTexture val = new RenderTexture(resWidth, resHeight, 24); shotCamera.set_targetTexture(val); shotCamera.set_aspect((float)resWidth / (float)resHeight); Texture2D val2 = new Texture2D(resWidth, resHeight, 3, false); shotCamera.Render(); RenderTexture.set_active(val); val2.ReadPixels(new Rect(0f, 0f, (float)resWidth, (float)resHeight), 0, 0); shotCamera.set_targetTexture(targetTexture); RenderTexture.set_active(null); shotCamera.set_aspect(aspect); Object.Destroy(val); byte[] bytes = ImageConversion.EncodeToPNG(val2); if (string.IsNullOrEmpty(savePath)) { savePath = Application.get_temporaryCachePath(); } string text = ImageName(resWidth, resHeight, name, savePath); File.WriteAllBytes(text, bytes); Debug.Log((object)$"Took screenshot to: {text}"); return(text); }
private ArtifactSaveData ArtifactToSaveData(Artifact artifact) { if (artifact == null) { return(null); } var artifactInfo = new ArtifactSaveData(); artifactInfo.type = artifact.GetType().ToString(); Texture2D texture = artifact.ArtifactSprite.texture; artifactInfo.spriteBytes = ImageConversion.EncodeToPNG(texture); artifactInfo.spriteX = artifact.ArtifactSprite.texture.width; artifactInfo.spriteY = artifact.ArtifactSprite.texture.height; if (artifact.unityShell != null) { artifactInfo.gameName = artifact.unityShell.gameObject.name; } artifactInfo.name = artifact.Name; artifactInfo.description = artifact.Description; artifactInfo.hasPower = artifact.HasPower; artifactInfo.power = (int)artifact.Power; if (artifact.GetType().IsSubclassOf(typeof(Bottle))) { artifactInfo.power = (int)(artifact as Bottle).Volume; } artifactInfo.isFightOnly = artifact.IsFightOnly; artifactInfo.used = artifact.Used; return(artifactInfo); }
private void ExportTexture(string texName, int graphIndex, int texIndex) { string path = EditorUtility.SaveFilePanel("Export Textures", Application.dataPath, graphNames[graphIndex] + " - " + texName, "png"); if (!string.IsNullOrEmpty(path)) { if (textures[graphIndex][texIndex] == null) { return; } byte[] bytes = new byte[0]; if (((Texture2D)textures[graphIndex][texIndex]).IsCompressed()) { Texture2D newTex = new Texture2D(textures[graphIndex][texIndex].width, textures[graphIndex][texIndex].height, TextureFormat.ARGB32, false); Color32[] colors = ((Texture2D)textures[graphIndex][texIndex]).GetPixels32(); newTex.SetPixels32(colors); newTex.Apply(); bytes = ImageConversion.EncodeToPNG(newTex); } else { bytes = ImageConversion.EncodeToPNG((Texture2D)textures[graphIndex][texIndex]); } File.WriteAllBytes(path, bytes); CustomEditorUtility.SelectOrReveal(path, false, true); } }
public void CreateItem() { if (itemIndex == -1) { newItem.itemName = itemName.text; newItem.itemID = itemID.text; if (catagories.text != string.Empty) { string[] cat = catagories.text.Split(','); for (int i = 0; i < cat.Length; ++i) { cat[i] = cat[i].Trim(); } newItem.categories = cat.ToList(); } else if (catagories.text == string.Empty) { newItem.categories.Clear(); } newItem.stats.Clear(); foreach (GameObject statObject in GameObject.FindGameObjectsWithTag("StatType")) { StatType stat = statObject.GetComponent <StatType>(); if (stat.statName == null) { newItem.stats.Add(new Stat("header", stat.statValue.text)); } else { newItem.stats.Add(new Stat(stat.statName.text, stat.statValue.text)); if (!searchManager.searchOptions.Contains(stat.statName.text)) { searchManager.searchOptions.Add(stat.statName.text); searchManager.UpdateDropdown(); } } } if (changedImage == false) { byte[] defaultIconData = ImageConversion.EncodeToPNG(defaultIcon); newItem.imageData = defaultIconData; } newItem.description = description.text; dataManager.CreateItem(newItem); LoadingManager.UpdateItems(); RefreshFields(); changedImage = false; gameObject.SetActive(false); } else if (itemIndex != -1) { SubmitEdit(); } }
void CreateTextureOnPath(string spritePath) { var data = ImageConversion.EncodeToPNG(new Texture2D(32, 32)); File.WriteAllBytes(spritePath, data); AssetDatabase.ImportAsset(spritePath, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate); }
private void getFramePixels() { Camera cam = renderCamera.GetComponent(typeof(Camera)) as Camera; if (cam == null) { return; } cam.targetTexture = frameRT; cam.Render(); newPos = cam.transform.position - startPos; if (readFrame) { RenderTexture.active = frameRT; frameTex.ReadPixels(rectReadPicture, 0, 0); readFrame = false; } else { framebuffer = ImageConversion.EncodeToPNG(frameTex); curPos = newPos; readFrame = true; canGetFrame = true; } }
public byte[] ConvertFrom(Cubemap cubemap) { int width = cubemap.width; int height = cubemap.height; Texture2D texture = new Texture2D( width * 6, height, TextureFormat.RGB24, false); var faces = new CubemapFace[] { CubemapFace.PositiveX, CubemapFace.NegativeX, CubemapFace.PositiveY, CubemapFace.NegativeY, CubemapFace.PositiveZ, CubemapFace.NegativeZ, }; int x = 0; foreach (var face in faces) { texture.SetPixels(x, 0, width, height, cubemap, face); x += width; } texture.Apply(); return(ImageConversion.EncodeToPNG(texture)); }
private void SaveMazeTexture() { Texture2D mazeTexture = new Texture2D(15, 15, TextureFormat.RGBA32, false); for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (matrix[i][j].GetComponent <MeshRenderer>().material.color == Color.black) { mazeTexture.SetPixel(i, j, Color.black); } else if (matrix[i][j].GetComponent <MeshRenderer>().material.color == Color.white) { mazeTexture.SetPixel(i, j, Color.white); } else { mazeTexture.SetPixel(i, j, Color.green); } } } mazeTexture.Apply(); File.WriteAllBytes("Assets/Mazetexture.png", ImageConversion.EncodeToPNG(mazeTexture)); }
public static void DumpTexture2DToFile(Texture2D target, bool useRandomFilenames = false) { if (target == null) { return; } string text = "DUMPsprites/" + "DUMP" + target.name; string text2 = text + "/" + "DUMP" + target.name; if (useRandomFilenames) { text += ("_" + Guid.NewGuid().ToString()); text2 += ("_" + Guid.NewGuid().ToString()); } string path = Path.Combine(ETGMod.ResourcesDirectory, text.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + ".png"); bool fileExists = File.Exists(path); if (!fileExists) { path = Path.Combine(ETGMod.ResourcesDirectory, text2.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + ".png"); bool folderPath = !File.Exists(path); if (folderPath) { Directory.GetParent(path).Create(); File.WriteAllBytes(path, ImageConversion.EncodeToPNG(target)); } } }
public static void SimpleCaptureTransparentScreenshot(Camera cam, int width, int height, string screengrabfile_path) { // Depending on your render pipeline, this may not work. var bak_cam_targetTexture = cam.targetTexture; var bak_cam_clearFlags = cam.clearFlags; var bak_RenderTexture_active = RenderTexture.active; var tex_transparent = new Texture2D(width, height, TextureFormat.ARGB32, false); // Must use 24-bit depth buffer to be able to fill background. var render_texture = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32); var grab_area = new Rect(0, 0, width, height); RenderTexture.active = render_texture; cam.targetTexture = render_texture; cam.clearFlags = CameraClearFlags.SolidColor; // Simple: use a clear background cam.backgroundColor = Color.clear; cam.Render(); tex_transparent.ReadPixels(grab_area, 0, 0); tex_transparent.Apply(); // Encode the resulting output texture to a byte array then write to the file byte[] pngShot = ImageConversion.EncodeToPNG(tex_transparent); File.WriteAllBytes(screengrabfile_path, pngShot); cam.clearFlags = bak_cam_clearFlags; cam.targetTexture = bak_cam_targetTexture; RenderTexture.active = bak_RenderTexture_active; RenderTexture.ReleaseTemporary(render_texture); Texture2D.Destroy(tex_transparent); }
void Start() { var graph = MixtureDatabase.GetGraphFromTexture(graphTexture); var variant = ScriptableObject.CreateInstance <MixtureVariant>(); variant.SetParent(graph); variant.SetParameterValue("Source", source); variant.SetParameterValue("Target", target); variant.ProcessGraphWithOverrides(); // Create the destination texture var settings = graph.outputNode.settings; Texture2D destination = new Texture2D( settings.GetResolvedWidth(graph), settings.GetResolvedHeight(graph), settings.GetGraphicsFormat(graph), TextureCreationFlags.None ); // Readback the result graph.ReadbackMainTexture(destination); // Write the file at the target destination var bytes = ImageConversion.EncodeToPNG(destination); File.WriteAllBytes(outputPath, bytes); }
void CaptureFrame() { RenderTexture.active = MyRenderTexture; colorBuffer.ReadPixels(new Rect(0, 0, colorBuffer.width, colorBuffer.height), 0, 0, false); colorBuffer.Apply(); pngs.Add(ImageConversion.EncodeToPNG(colorBuffer)); }
public static IEnumerator GetTexture(string cardName, Transform art) { string path = Application.dataPath + $"/Resources/Arts/{cardName}.png"; string url = $"https://art.hearthstonejson.com/v1/512x/{cardName}.jpg"; Debug.Log(url); UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Got the f*****g pick!"); Texture2D artTexture = ((DownloadHandlerTexture)www.downloadHandler).texture; art.GetComponent <Image>().sprite = Sprite.Create(artTexture, new Rect(0, 0, artTexture.width, artTexture.height), new Vector2(0, 0)); byte[] bytes = ImageConversion.EncodeToPNG(artTexture as Texture2D); // For testing purposes, also write to a file in the project folder Debug.Log($"saving picture to: {path}"); File.WriteAllBytes(path, bytes); } }
public SerializableTexture2D(string id, Texture2D texture) { m_id = id; m_width = texture.width; m_height = texture.height; m_texture = texture; m_byte = IsValid ? ImageConversion.EncodeToPNG(texture.ToDecompressedTexture()) : Array.Empty <byte>(); }
public static string Texture2DToBase64(Texture2D texture) { SD.Stopwatch stopwatch = SD.Stopwatch.StartNew(); byte[] imageData = ImageConversion.EncodeToPNG(texture); string base64 = Convert.ToBase64String(imageData).Insert(0, "data:image/png;base64,"); return(base64); }
static void ExportToPNG(string path, Texture2D tex) { #if UNITY_2018_1_OR_NEWER System.IO.File.WriteAllBytes(path, ImageConversion.EncodeToPNG(tex)); #else System.IO.File.WriteAllBytes(path, tex.EncodeToPNG()); #endif }
/// <summary> /// Outputs a Texture2D as a .png file for debug purposes /// </summary> /// <param name="path">The path to write to, including filename ending in .png</param> /// <param name="sourceTexture">The texture to export</param> public static void WriteTexture2D(string path, Texture2D sourceTexture) { byte[] exrBytes = ImageConversion.EncodeToPNG(sourceTexture); if (exrBytes != null) { PWCommon4.Utils.WriteAllBytes(path, exrBytes); } }
void Start() { #if UNITY_2017 byteTexture_ = ImageConversion.EncodeToPNG(texture); #else byteTexture_ = texture.EncodeToPNG(); #endif }
public static Png TexToPng(Texture2D tex) { Png img = new Png(); img.pixScale = 1; img.t = ImageConversion.EncodeToPNG(tex); return(img); }
string CreateTexture(string path) { var data = ImageConversion.EncodeToPNG(new Texture2D(32, 32)); File.WriteAllBytes(path, data); AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate); return(AssetDatabase.AssetPathToGUID(path)); }