- Home /
Problems taking screenshots using anti-aliasing
Hi folks.
Been Googling for hours and can't find a solution to a problem I've been having with anti-aliasing and taking screenshots.
My script runs through all the cameras in the level and takes a screenshot of them. Everything works nicely and quickly, exporting out 60+ 2048x1280 pics in no time at all. Sounds good.
Here's where it falls down. After enabling anti-aliasing about 75% of the renders will work (and look great and anti-aliased!) but about 25% will fail and save another camera's view as the texture! The cameras that fail fail consistently, but I can't see anything different about those cameras than the ones that work.
Here's my script:
using UnityEngine;
using System.Collections;
public class TakeScreenshotMaster : MonoBehaviour {
public int resWidth = 2048;
public int resHeight = 1280;
public string cameraName = "";
private bool takeHiResShot = false;
public static string ScreenShotName(int width, int height, string cameraName) {
Debug.Log(string.Format("ScreenShotName: Name is now: {0}", cameraName));
return string.Format("d:/screenshots/{4}_{1}x{2}_{3}.png",
Application.dataPath,
width, height,
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"),
cameraName);
}
// Last to run...
void LateUpdate() {
takeHiResShot |= Input.GetKeyDown("k");
if (takeHiResShot)
{
Debug.Log(string.Format("k press detected!"));
takeHiResShot = false;
StartCoroutine(TakeScreenshotsOfEverything());
}
}
IEnumerator TakeScreenshotsOfEverything()
{
Debug.Log(string.Format("Printing all screens!"));
// Create new game object to hold all possible cameras...
GameObject[] cameras = GameObject.FindGameObjectsWithTag("Camera");
// Loop through each tagged camera...
foreach (GameObject cams in cameras){
// Get next camera...
Camera theCam = cams.GetComponent<Camera>() as Camera;
theCam.enabled = true;
yield return new WaitForEndOfFrame();
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
rt.antiAliasing = 1;
rt.filterMode = FilterMode.Trilinear;
theCam.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
RenderTexture.active = rt;
theCam.Render();
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
// Cleanup...
theCam.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
theCam.enabled = false;
// Write to file...
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight, theCam.name);
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log("Capture!");
}
}
// Use this for initialization
void Start () {
Debug.Log("Master camera reporting for duty!");
// Create new game object to hold all possible cameras...
GameObject[] cameras = GameObject.FindGameObjectsWithTag("Camera");
// Loop through each tagged camera...
// Disable each for now...
foreach (GameObject cams in cameras){
Camera theCam = cams.GetComponent<Camera>() as Camera;
theCam.enabled = false;
}
// Enable the first camera...
//Camera firstCamera = cameras [0].GetComponent<Camera> () as Camera;
//firstCamera.enabled = true;
}
// Update is called once per frame
void Update () {
//Debug.Log(string.Format("Update: Camera {0} is running!", cameraName));
//camera.
}
}
Pulling my hair out with this one! Does anyone have any ideas? Like I said, it works 100% if I don't use anti-aliasing, and 75% will work if I enable anti-aliasing, with 25% fail by rendering one of the other working cameras instead.
Cheers, Chris
Your answer
![](https://koobas.hobune.stream/wayback/20220613163107im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why won't anti-aliasing work in the Editor for me? 2 Answers
Making fonts antialiased at all sizes 0 Answers
Anti-aliasing on Android 1 Answer
Anti-Aliasing Methods 1 Answer
Antialias LineRenderer Lines 1 Answer