I see the “camera preview” looks perfect and the colors and brightness appear exactly as I would hope.
But when I use camera.render()
to save the rendertexture to a file, the saved image is always super dark and looks completely different than the “camera preview”.
Here is the code I use to create the image texture:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(TakeScreenshot))]
class TakeScreenshotEditor : Editor {
public override void OnInspectorGUI() {
TakeScreenshot baseScript = (TakeScreenshot)target;
if(GUILayout.Button("Take Screenshot")) {
var folder = Path.Combine( Application.dataPath, "Screenshots");
if (!Directory.Exists (folder)) {
Directory.CreateDirectory (folder);
}
var path = Path.Combine (folder, "screenshot.png");
Debug.Log (path);
baseScript.CamCapture(path);
}
if(GUILayout.Button("Set Frustum To Scene Object")) {
baseScript.SetFrustumToSceneObject();
}
if(GUILayout.Button("Evaluate Curve")) {
if (baseScript.t > 1) {
baseScript.t = 0;
}
baseScript.EvaluateCurve(baseScript.m_sceneObject, baseScript.t);
baseScript.t += 0.025f;
}
DrawDefaultInspector ();
}
}
/*Add this script to any camera which also has a randertexture*/
[ExecuteInEditMode]
public class TakeScreenshot : MonoBehaviour {
public Camera m_camera { get {return GetComponent<Camera>();} }
public Texture2D CamCapture()
{
m_camera.Render();
Texture2D image = new Texture2D(m_camera.targetTexture.width, m_camera.targetTexture.height);
image.ReadPixels(new Rect(0, 0, m_camera.targetTexture.width, m_camera.targetTexture.height), 0, 0);
image.Apply();
return image;
}
public void CamCapture(string destPath)
{
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = m_camera.targetTexture;
Texture2D image = CamCapture ();
RenderTexture.active = currentRT;
var Bytes = image.EncodeToPNG();
DestroyImmediate(image);
var destDir = Path.GetDirectoryName (destPath);
if (!Directory.Exists (destDir)) {
Directory.CreateDirectory (destDir);
}
File.WriteAllBytes(destPath, Bytes);
}
}
What’s even stranger is how Unity “almost” got it when I changed the colorformat of the Rendertextur, I see the colors and brightness are correct, but yet again there is a bug and Unity generates artifacts in the saved image:
I can’t get Unity to work correctly, no matter how I change lighting settings, or camera settings, or rendertexture settings, or meshrenderer settings, the final image is always darker than the preview.
What should I do? All I want to do is save an image exactly as it appears in the render preview, this should be easy, what am I doing wrong?