- Home /
Capture image use Camera.Render but get different result between preview in editor and saved image.
Sorry for the complex title. But I really being crazy about this issue for days. I manually call Camera.Render to a RenderTexture, then use texture2D to get the texture and then save it as PNG. But what I get in the saved png file is different in the game view. Specificly, I find that gameobject with certain material looks seem in both editor view and saved png file.
// Public Properties
public int maxFrames; // maximum number of frames you want to record in one video
public int frameRate = 30; // number of frames to capture per second
// The Encoder Thread
private Thread encoderThread;
// Texture Readback Objects
private RenderTexture tempRenderTexture;
private Texture2D tempTexture2D;
// Timing Data
private float captureFrameTime;
private float lastFrameTime;
private int frameNumber;
private int savingFrameNumber;
// Encoder Thread Shared Resources
private Queue<byte[]> frameQueue;
private string persistentDataPath;
private int screenWidth;
private int screenHeight;
private bool threadIsProcessing; //
private bool terminateThreadWhenDone;
private string ffmpegBatchPath;
public Camera testCamera;
public Camera mainCamera;
private void LateUpdate()
{
TakeScreenShot();
}
void TakeScreenShot()
{
// Encode texture into PNG
//byte[] bytes = imageOverview.EncodeToPNG();
if (frameNumber <= maxFrames)
{
// Check if render target size has changed, if so, terminate
if (testCamera.pixelWidth!= screenWidth || testCamera.pixelHeight != screenHeight)
{
threadIsProcessing = false;
this.enabled = false;
throw new UnityException("ScreenRecorder render target size has changed!");
}
// Calculate number of video frames to produce from this game frame
// Generate 'padding' frames if desired framerate is higher than actual framerate
float thisFrameTime = Time.time;
int framesToCapture = ((int)(thisFrameTime / captureFrameTime)) - ((int)(lastFrameTime / captureFrameTime));
// Capture the frame
if (framesToCapture > 0)
{
tempRenderTexture.Create();
testCamera.targetTexture = tempRenderTexture;
testCamera.Render();
RenderTexture.active = testCamera.targetTexture;
tempTexture2D.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
tempTexture2D.Apply();
RenderTexture.active = null;
tempRenderTexture.Release();
//RenderTexture.active = null;
}
// Add the required number of copies to the queue
for (int i = 0; i < framesToCapture && frameNumber <= maxFrames; ++i)
{
frameQueue.Enqueue(tempTexture2D.EncodeToPNG());
frameNumber++;
}
lastFrameTime = thisFrameTime;
}
else //keep making screenshots until it reaches the max frame amount
{
// Inform thread to terminate when finished processing frames
terminateThreadWhenDone = true;
// Disable script
this.enabled = false;
}
}
This is what the camera render in editor
And this is the image I get as png
And there is another strange thing, the character with certain material and the background rendered the same in both editor preview and saved png, but other part of the character which have different materials rendered differently.
Your answer
Follow this Question
Related Questions
Texture2D to RenderTexture 1 Answer
CustomRenderTexture (RFloat) wont initialize in the same frame it is created. 0 Answers
Texture.GetNativeTexturePtr sometime returns 0. 2 Answers
EncodeToJPG() creates gray image after using CopyTexture(). 1 Answer
How to get a camera to render to render texture at a custom aspect ratio. 1 Answer