http://forum.unity3d.com/viewtopic.php?t=48185 is a good thread to start. What I learnt is this. You have two cameras. One does ReadPixels of the “normal” scene. The 2nd camera does ReadPixels using the “XRay” shader in the thread mentioned.
Then, what I do is take the color (RGB) from the first camera and then take the alpha of the 2nd camera, combining it so that it is ARGB32.
You have to use RendererTexture so that the cameras can render separately.
I’m sorry I can’t provide more information I have been up for 12 hours and achieved a lot but have to wind down now.
Here’s the code dump. A lot of parts have been commented in or out because I was doing other stuff with it but hope it gives you an insight into how it can be done.
function renderToScreenTempFunction(theFrame : int)
{
//Wait for framebuffer to be rendered
//NEEDED?? yield WaitForEndOfFrame();
// Create a texture the size of the screen, ARGB32 format
//FREEZE THE SCENE!
Time.timeScale = 0;
//Create a texture of main camera
// var mainCamTexture = new Texture2D(1280,720,TextureFormat.RGB24,false);
var mainCam : Camera = Global_ApplicationData.getTheEnvironment().getDefaultCamera().camera;
var theAlphaCam : Camera = GameObject.Find("PPCamera").camera;
//Initialize and render
var mainCamRenderTexture : RenderTexture = new RenderTexture(2048,1152,24);
var alphaCamRenderTexture : RenderTexture = new RenderTexture(2048,1152,24);
mainCam.targetTexture = mainCamRenderTexture;
mainCam.Render();
RenderTexture.active = mainCamRenderTexture;
//var width = Screen.width;
//var height = Screen.height;
var tex1 = new Texture2D (2048, 1152, TextureFormat.RGB24, false); //OR ARGB32 AS WORKS PREVIOUSLY
// Read screen contents into the texture
tex1.ReadPixels (Rect(0, 0, 2048, 1152), 0, 0);
tex1.Apply ();
// Encode texture into PNG
//var bytes = tex.EncodeToPNG();
//Now do it for Alpha Camera
theAlphaCam.targetTexture = alphaCamRenderTexture;
theAlphaCam.Render();
RenderTexture.active = alphaCamRenderTexture;
var tex2 = new Texture2D (2048, 1152, TextureFormat.ARGB32, false);
tex2.ReadPixels (Rect(0, 0, 2048, 1152), 0, 0);
tex2.Apply ();
for ( var x: int = 0; x < tex2.width; x++)
{
for ( var y: int = 0; y < tex2.height; y++)
{
var color:Color = tex2.GetPixel(x, y);
//let's get tex1 color
var color2:Color = tex1.GetPixel(x,y);
var alpha:float = color.a;
if (alpha != 0)
{
color /= alpha;
color.a = alpha;
//color2.a = alpha;
color2.a = alpha+0.05; //attempt transparency reduction
tex2.SetPixel(x, y, color2);
}
}
}
//attempt merge
var cols1 = tex1.GetPixels();
var cols2 = tex2.GetPixels();
for(var i = 0; i < cols1.Length; ++i)
{
cols1 _+= cols2*;*_
_ Debug.Log(cols1*);
}
tex1.SetPixels(cols1);
tex1.Apply();*_
// Encode texture into PNG
var bytes2 = tex2.EncodeToPNG();
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(“D:/SavedAlpha_”+theFrame+“.png”, bytes2);
* Destroy (tex1);*
* Destroy (tex2);*
// For testing purposes, also write to a file in the project folder
//File.WriteAllBytes(“D:/SavedScreen_”+theFrame+“.png”, bytes);
* //Clean Up*
* theAlphaCam.targetTexture = null;*
* RenderTexture.active = null;*
* DestroyImmediate(alphaCamRenderTexture);*
//Clean Up
* mainCam.targetTexture = null;*
* RenderTexture.active = null;*
* DestroyImmediate(mainCamRenderTexture);*
* //UNFREEZE THE SCENE!*
Time.timeScale = 0.05;
* //Time.timeScale = 1.0;*
_Time.fixedDeltaTime = 0.02 * Time.timeScale; _
* }*