- Home /
Wierd Screen Shot Problem, texture of screen shot looks different in editor and in system.
Hello, I have run into a very strange problem. I am taking screenshot of certain area of the screen using the following code.
IEnumerator Save() { camera.enabled = true; //camera.targetTexture = new RenderTexture(512,512,24,RenderTextureFormat.ARGB32,RenderTextureReadWrite.sRGB);
originalSide = Manager._instance.side;
Manager._instance.side = 1;
_shirtHandler.LoadPattern();
//camera.Render();
//yield return new WaitForEndOfFrame();
RenderTexture.active = camera.targetTexture;
Texture2D texFront = new Texture2D(camera.targetTexture.width,camera.targetTexture.height,TextureFormat.ARGB32,false );
texFront.ReadPixels(new Rect(0,0,512,512), 0, 0);
texFront.Apply();
yield return null;
Manager._instance.side = 1;
_shirtHandler.LoadPattern();
//camera.Render();
//yield return new WaitForEndOfFrame();
RenderTexture.active = camera.targetTexture;
Texture2D texBack = new Texture2D(camera.targetTexture.width,camera.targetTexture.height,TextureFormat.ARGB32,false );
texBack.ReadPixels(new Rect(0,0,512,512), 0, 0);
texBack.Apply();
Manager._instance.isSaving = false;
//temp.material.mainTexture = texFront;
camera.enabled=false;
yield return null;
//upload it to server..
byte[] bytesFront= texFront.EncodeToPNG();
File.WriteAllBytes("C:/Users/Vijay/Desktop/Front.png", bytesFront );
File.WriteAllBytes("C:/Users/Vijay/Desktop/Back.png", texBack.EncodeToPNG());
Manager._instance.side = originalSide;
_shirtHandler.LoadPattern();
}
There are two cameras in game. one is the main camera and another one is only for the purpose of saving the screen shot of certain area of screen. I am enabling the camera at the time of capturing alone. I am getting the screenshot as per my requirement but only until i save it to local... when I apply the captured screenshot to a material in game it is working fine but whn i save the screen shot, I could hardly see the colors in there. and when i import the saved screen shot into unity, it is looking normal. But every where else except unity im seeing an image without much colors...I am attaching both the versions in this mail...
The saved screenshot
The above might look like empty image but has colors in it, whn zoomed a bit.
The same file after importing to unity,
Could someone tell me why this is happening??
Answer by robertbu · May 26, 2013 at 05:37 PM
Your alpha values in the texture are very small. Most of the shaders in unity ignore the alpha channel of the texture, so in the preview or when using this texture on a material that does not use the alpha channel, the image will look fine. To see your image with the alpha channel, create a material using your texture and the Unlit/Transparent shader. Place this material on a game object. You will see your image as a ghost.
hi robert, You are right. the alpha in the texture is very low... but how do i solve this... I checked the materials of the objects that are getting screen shoted, they all have 255 alpha.. what else the problem could be. FYI: the camera that takes the screenshot is a secondary camera.
I don't know what is causing this issue. You code looks fine (though I've only had a limited amount of exposure to RenderTextures). Did you turn off the main camera while you take the shot? A hackish solution would be to GetPixels32() and set all the alphas to 255 before calling EncodeToPng().
ya I thought of doing that but i have to save like 8 images in a row.. which is causing a struck up in program for like 15-20 secs (even in my high end machine), when get pixes and apply is used..
And I am not turning off the main cam. will it affect in anyway!!... the problem is I cant coz of other scripts
thanks robert, I have solved it...
While creating the texture, I have created RGB24 ins$$anonymous$$d of ARGB32.. which solves the issue.
Texture2D texBack = new Texture2D(camera.targetTexture.width,camera.targetTexture.height,TextureFormat.RGB24,false );