Hey everyone!
I’m quite lost on this one, not really sure where the issue is.
There are two issues:
- Not being able to accumulate Graphics.Blit stamps
- Not being able to get the accumulated Graphics.Blit on the CPU memory
I solved the first issue at some point, but it resurfaced, and I don’t know why.
But even when I was able to accumulate Graphics.Blit stamps, those only showed up
- in the Game view, not
- in the Scene view or
- when Saving it to a .png.
Are there some known issues regarding this?
If someone wants to have a look, I attached the project.
Only the stamps from the last Blit show up.
Here is the code I am using:
using System.Collections; using System.Collections.Generic;
using System.IO;
using UnityEngine; using UnityEngine.UI;
public class DrawToCPU : MonoBehaviour {
string exportFolder = "";
void Awake () {
exportFolder = Application.persistentDataPath + "/PngExport/";
if (!Directory.Exists(exportFolder)) {Directory.CreateDirectory(exportFolder);}
draw = Draw();
StartCoroutine(draw);
}
Vector2 blitScale = new Vector2(5.0f,5.0f);
Vector2[] blitOffsets = new Vector2[4]{
new Vector2(-1.25f,-1.25f),
new Vector2(-1.25f,-3.75f),
new Vector2(-3.75f,-1.25f),
new Vector2(-3.75f,-3.75f)
};
Color WhiteA0 = new Color(1.0f,1.0f,1.0f,0.0f);
public Texture2D stamp, result;
public RenderTexture rend, temp;
IEnumerator draw;
IEnumerator Draw () {
GL.Clear(false,false,WhiteA0);
for (int i = 0; i < 4; i++) {
temp = RenderTexture.GetTemporary(2048,2048);
Graphics.Blit(stamp, temp, blitScale, blitOffsets[i]);
Graphics.Blit(temp, rend);
//RenderTexture.ReleaseTemporary(temp);
Debug.Log("Drawn to " + blitOffsets[i]);
yield return new WaitForSeconds(0.5f);
}
result = GPUtoCPU(rend);
Save(result,exportFolder);
}
Texture2D GPUtoCPU (RenderTexture rend) {
RenderTexture curr = RenderTexture.active;
RenderTexture.active = rend;
Texture2D img = new Texture2D(rend.width,rend.height);
img.ReadPixels(new Rect(0,0,img.width,img.height),0,0);
img.Apply();
RenderTexture.active = curr;
return img;
}
void Save (Texture2D img, string exportFolder) {
string filePath = string.Empty;
filePath = exportFolder + "Image.png";
FileStream fs = new FileStream(filePath, FileMode.Create);
byte[] imgBytes = img.EncodeToPNG();
fs.Write(imgBytes,0,imgBytes.Length);
fs.Close();
Debug.Log("File saved to " + exportFolder);
}
}
Best wishes,
Shu
8670879–1168068–BlitStamps.zip (49.3 KB)