Im using Edge detection post process effect (https://docs.unity3d.com/540/Documentation/Manual/script-EdgeDetectEffectNormals.html)
Script is attached to my camera and wondering how to capture screen with this effect.
What I expect:
![alt text][1]
What i have:
![alt text][2]
[1]: /storage/temp/146320-q1.png
[2]: /storage/temp/146321-q2.png
My outlines are renderer via shader and everything looks great at runtime. The problem is how to apply post effect to my rendertexture.
private Rect rect;
private RenderTexture renderTexture;
private Texture2D screenShot;
ImageFormat format = ImageFormat.PPM;
private void CreateScreenShoot()
{
camera.targetTexture = renderTexture;
camera.Render();
// read pixels will read from the currently active render texture so make our offscreen
// render texture active and then read the pixels
RenderTexture.active = renderTexture;
screenShot.ReadPixels(rect, 0, 0);
// reset active camera texture and render texture
camera.targetTexture = null;
RenderTexture.active = null;
DataStorageBase.CreateDirectory(DataStorageBase.MiniaturesDataPath);
string filename = Path.Combine(DataStorageBase.MiniaturesDataPath, DataStorageBase.ChosenImage + DataStorageBase.MiniaturesSavingExtension);
// string filename = Path.Combine(DataStorageBase.MiniaturesDataPath, DataStorageBase.ChosenImage + ".png");
SaveTexture(filename,screenShot, format);
}
private static void SaveTexture(string filename, Texture2D screenShot, ImageFormat format)
{
// pull in our file header/data bytes for the specified image format (has to be done from main thread)
byte[] fileHeader = null;
byte[] fileData = null;
if (format == ImageFormat.RAW)
{
fileData = screenShot.GetRawTextureData();
}
else if (format == ImageFormat.PNG)
{
fileData = screenShot.EncodeToPNG();
}
else if (format == ImageFormat.JPG)
{
fileData = screenShot.EncodeToJPG();
}
else // ppm
{
// create a file header for ppm formatted file
string headerStr = string.Format("P6\n{0} {1}\n255\n", screenShot.width, screenShot.height);
fileHeader = System.Text.Encoding.ASCII.GetBytes(headerStr);
fileData = screenShot.GetRawTextureData();
}
Debug.Log(filename);
// create new thread to save the image to file (only operation that can be done in background)
new System.Threading.Thread(() =>
{
// create file and write optional header with image bytes
var f = System.IO.File.Create(filename);
if (fileHeader != null)
f.Write(fileHeader, 0, fileHeader.Length);
f.Write(fileData, 0, fileData.Length);
f.Close();
}).Start();
}
↧