private byte[] CaptureOpaque(int ResolutionX, int ResolutionY, int DownscalingRate) { var main = Camera.main; var tt = main.targetTexture; ResolutionX *= DownscalingRate; ResolutionY *= DownscalingRate; var rta = RenderTexture.active; var rt = RenderTexture.GetTemporary(ResolutionX, ResolutionY, 24); main.targetTexture = rt; var ss = new Texture2D(ResolutionX, ResolutionY, TextureFormat.RGB24, false); main.Render(); RenderTexture.active = rt; ss.ReadPixels(new Rect(0, 0, ResolutionX, ResolutionY), 0, 0); main.targetTexture = tt; RenderTexture.active = rta; RenderTexture.ReleaseTemporary(rt); var texture2D4 = new Texture2D(ResolutionX, ResolutionY, TextureFormat.ARGB32, false); var pixels = ScaleUnityTexture.ScaleLanczos(ss.GetPixels32(), ss.width, ResolutionX, ResolutionY); texture2D4.SetPixels32(pixels); GameObject.Destroy(ss); byte[] ret = texture2D4.EncodeToPNG(); GameObject.Destroy(texture2D4); return(ret); }
private static void DownscaleEncoded(ref byte[] encoded) { if (RenderRate <= 1) { return; } //Texture buffer for fullres. var t2d = new Texture2D(2, 2); t2d.LoadImage(encoded); //New width/height after downsampling. var nw = t2d.width / RenderRate; var nh = t2d.height / RenderRate; //Downsample texture var pixels = ScaleUnityTexture.ScaleLanczos(t2d.GetPixels32(), t2d.width, nw, nh); GameObject.Destroy(t2d); //Load pixel data into a new texture, encode to PNG and overwrite original result. var np = new Texture2D(nw, nh); np.SetPixels32(pixels); encoded = np.EncodeToPNG(); GameObject.Destroy(np); }
public byte[] Capture(int ResolutionX, int ResolutionY, int DownscalingRate, bool Transparent) { Texture2D fullSizeCapture = null; int newWidth = ResolutionX * DownscalingRate; int newHeight = ResolutionY * DownscalingRate; var currentScene = SceneManager.GetActiveScene().name; if (Transparent && (currentScene == "CustomScene" || currentScene == "Studio")) { fullSizeCapture = CaptureAlpha(newWidth, newHeight); } else { fullSizeCapture = CaptureOpaque(newWidth, newHeight); } byte[] ret = null; if (DownscalingRate > 1) { var pixels = ScaleUnityTexture.ScaleLanczos(fullSizeCapture.GetPixels32(), fullSizeCapture.width, ResolutionX, ResolutionY); GameObject.Destroy(fullSizeCapture); var texture2D4 = new Texture2D(ResolutionX, ResolutionY, TextureFormat.ARGB32, false); texture2D4.SetPixels32(pixels); ret = texture2D4.EncodeToPNG(); GameObject.Destroy(texture2D4); } else { ret = fullSizeCapture.EncodeToPNG(); GameObject.Destroy(fullSizeCapture); } return(ret); }
private byte[] CaptureAlpha(int ResolutionX, int ResolutionY, int DownscalingRate) { var main = Camera.main; var baf = main.GetComponent <BloomAndFlares>(); var baf_e = baf?.enabled; if (baf) { baf.enabled = false; } var vig = main.GetComponent <VignetteAndChromaticAberration>(); var vig_e = vig?.enabled; if (vig) { vig.enabled = false; } var texture2D = PerformCapture(ResolutionX, ResolutionY, DownscalingRate, true); var texture2D2 = PerformCapture(ResolutionX, ResolutionY, DownscalingRate, false); var rt = RenderTexture.GetTemporary(texture2D.width, texture2D.height, 0, RenderTextureFormat.ARGB32); matMask.SetTexture("_Mask", texture2D); Graphics.Blit(texture2D2, rt, matMask); GameObject.Destroy(texture2D); GameObject.Destroy(texture2D2); var texture2D3 = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false); var prev = RenderTexture.active; RenderTexture.active = rt; texture2D3.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0, false); RenderTexture.active = prev; RenderTexture.ReleaseTemporary(rt); var texture2D4 = new Texture2D(ResolutionX, ResolutionY, TextureFormat.ARGB32, false); var pixels = ScaleUnityTexture.ScaleLanczos(texture2D3.GetPixels32(), texture2D3.width, ResolutionX, ResolutionY); texture2D4.SetPixels32(pixels); GameObject.Destroy(texture2D3); byte[] ret = texture2D4.EncodeToPNG(); GameObject.Destroy(texture2D4); if (baf) { baf.enabled = baf_e.Value; } if (vig) { vig.enabled = vig_e.Value; } StartCoroutine(RestoreSkinTexture()); return(ret); }
public byte[] Capture(int ResolutionX, int ResolutionY, int DownscalingRate) { var main = Camera.main; var baf = main.GetComponent <BloomAndFlares>(); var baf_e = baf.enabled; baf.enabled = false; var texture2D = PerformCapture(ResolutionX, ResolutionY, DownscalingRate, true); var texture2D2 = PerformCapture(ResolutionX, ResolutionY, DownscalingRate, false); var texture2D3 = new Texture2D(texture2D.width, texture2D.height, TextureFormat.ARGB32, false); var pixels = texture2D.GetPixels(); var pixels2 = texture2D2.GetPixels(); for (int i = 0; i < pixels.Length; i++) { var a = 1 - pixels[i].r; pixels2[i].a = a; } texture2D3.SetPixels(pixels2); texture2D3.Apply(); byte[] ret; //Downsample texture if (DownscalingRate > 1) { var pixelsResized = ScaleUnityTexture.ScaleLanczos(texture2D3.GetPixels32(), texture2D3.width, ResolutionX, ResolutionY); GameObject.Destroy(texture2D3); //Load pixel data into a new texture, encode to PNG and overwrite original result. var np = new Texture2D(ResolutionX, ResolutionY, TextureFormat.ARGB32, false); np.SetPixels32(pixelsResized); ret = np.EncodeToPNG(); GameObject.Destroy(np); } else { ret = texture2D3.EncodeToPNG(); } GameObject.Destroy(texture2D); GameObject.Destroy(texture2D2); GameObject.Destroy(texture2D3); baf.enabled = baf_e; StartCoroutine(RestoreSkinTexture()); return(ret); }
public static void post_CreatePng(ref byte[] pngData) { //Texture buffer for fullres. var t2d = new Texture2D(2, 2); t2d.LoadImage(pngData); //New width/height after downsampling. var nw = t2d.width / RenderRate; var nh = t2d.height / RenderRate; //Downsample texture var pixels = ScaleUnityTexture.ScaleLanczos(t2d.GetPixels32(), t2d.width, nw, nh); GameObject.Destroy(t2d); //Load pixel data into a new texture, encode to PNG and overwrite original result. var np = new Texture2D(nw, nh); np.SetPixels32(pixels); pngData = np.EncodeToPNG(); GameObject.Destroy(np); }
public static void Scale(ScaleType _type, string _path, string _target) { string path = _path; string newPath = _target; // // load the texture from the disc by // setting the importer to use RGBA32 and ensure the texture is readable // TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter; TextureImporterSettings orgSettings = new TextureImporterSettings(); ti.ReadTextureSettings(orgSettings); ti.textureFormat = TextureImporterFormat.RGBA32; ti.isReadable = true; AssetDatabase.ImportAsset(path); // get the pixels Texture2D originalTexture = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D; Color32[] c1 = originalTexture.GetPixels32(); int orgWidth = originalTexture.width; int orgHeight = originalTexture.height; originalTexture = null; // // restore original import settings // ti = AssetImporter.GetAtPath(path) as TextureImporter; ti.SetTextureSettings(orgSettings); AssetDatabase.ImportAsset(path); // arbitrary target size int width = 433; //preserve aspect ratio int height = Mathf.RoundToInt((width / (float)orgWidth) * orgHeight); //actually scale the data switch (_type) { case ScaleType.Lanczos: c1 = ScaleUnityTexture.ScaleLanczos(c1, orgWidth, width, height); break; case ScaleType.Linear: c1 = ScaleUnityTexture.ScaleLinear(c1, orgWidth, width, height); break; case ScaleType.Point: c1 = ScaleUnityTexture.ScalePoint(c1, orgWidth, width, height); break; } // create target texture Texture2D outT = new Texture2D(width, height, TextureFormat.RGBA32, false); // set the pixels outT.SetPixels32(c1); // encode the texture byte[] outBytes = outT.EncodeToPNG(); // save texture System.IO.File.WriteAllBytes(newPath, outBytes); AssetDatabase.ImportAsset(newPath); // Apply the same import settings for this texture ti = AssetImporter.GetAtPath(newPath) as TextureImporter; ti.SetTextureSettings(orgSettings); AssetDatabase.ImportAsset(newPath); }