At first it looked like it worked correctly, but upon inspecting I noticed that there were no alphas in the image.
I went ahead and changed the encoding of the image to include the alpha and got this
I don’t know why the opaque objects are being rendered transparent. I introduced a default sphere and deleted lights in the scene but I keep getting the same treatment. I’ve tried different texture format combinations and still no luck. I just don’t understand well enough what’s going on under the hood. I’ve included the script I’m using below. The script is being run by ab editor script with a button to execute.
using UnityEngine;
using System.Collections;
public class renderPNG : MonoBehaviour {
public int imgX = 512;
public int imgY = 512;
public Camera virtuCamera;
public string imageName = "myRenderTexture";
public void renderTex()
{
// capture the virtuCam and save it as a square PNG.
//virtuCamera.camera.aspect = 1.0f;
// recall that the height is now the "actual" size from now on
RenderTexture tempRT = new RenderTexture(imgX,imgY,24, RenderTextureFormat.ARGB32 );
// the 24 can be 0,16,24, formats like
// RenderTextureFormat.Default, ARGB32 etc.
virtuCamera.camera.targetTexture = tempRT;
virtuCamera.camera.Render();
RenderTexture.active = tempRT;
Texture2D virtualPhoto =
new Texture2D(imgX,imgY, TextureFormat.ARGB32, false);
// false, meaning no need for mipmaps
virtualPhoto.ReadPixels( new Rect(0, 0, imgX,imgY), 0, 0);
RenderTexture.active = null; //can help avoid errors
virtuCamera.camera.targetTexture = null;
// consider ... Destroy(tempRT);
byte[] bytes;
bytes = virtualPhoto.EncodeToPNG();
System.IO.File.WriteAllBytes(
OurTempSquareImageLocation(), bytes );
// virtualCam.SetActive(false); ... no great need for this.
Debug.Log ("Saved image in this location " + OurTempSquareImageLocation());
// now use the image,
//UseFileImageAt( OurTempSquareImageLocation() );
}
private string OurTempSquareImageLocation()
{
string r = Application.dataPath +"/"+imageName+".png";
return r;
}
}
If anyone can kindly direct me to the right information to help me get a transparent background with opaque objects I would be forever grateful!
I found this fantastic script which makes taking screenshots very easily and with transparencies. For some reason I get the same issue. It looks ok in the preview but if you open the image in photoshop it shows that the cube is transparent.
I’ve tried on both mac and pc and get the same result.
There is certainly something that I am overlooking or there is a defect somewhere. Anyone have any ideas?
Hi invadererik, thank you for your feedback. If you could kindly clarify points 1 and 2 for me, that would help me out greatly.
Your second camera needs to use a clear colour with the alpha value you want in empty spaces.
When you say second camera, do you mean the camera creating the render texture? I only ask since I’m doing this in the editor for marketing images, so it’s not run time.
The objects rendered by your second camera need to use shaders that write appropriate alpha values
I dioremember seeing this mentioned somewhere but I don’t know how to confirm that the shaders are writing to the correct channels. In this case I would assume that they should only be writing to RGB channels since I want them to be opaque. Hw would I make sure they are? Would default shader on a cube meet these requirements?