- Home /
Saving Texture at Run time and Reusing it
I am trying to create an aggregate heat map!
for that I have a plane with a map texture on it, and an orthographic camera looking at it. Every time I want to save it, I take a screenshot by creating a render texture and saving it as "snap.png" in resources. Once it is saved, my delegate updates the plane texture with "snap.png".
problem is due to repeatedly writing, reading and drawing the same texture (like Inception: snap of a snap of a snap... ) my map diagrams becomes whitish at each iteration until there is no drawing left.. please suggest me something! there should be something wrong with readpixels or camera settings!
also I am using VRTK for HTC VIVE but that should not have any impact on this. Since this is a seperate conventional Camera
public class LineCreator : MonoBehaviour {
public delegate void UpdateMapEvent();
public static event UpdateMapEvent updateGazeMap;
//...
void saveImage(){
int width = 1028;
int height = 768;
RenderTexture RT = new RenderTexture (width, height, 24);
Camera snapCam = gameObject.GetComponent<Camera> ();
snapCam.targetTexture = RT;
snapCam.Render ();
RenderTexture.active = RT;
Texture2D snap = new Texture2D (width, height, TextureFormat.ARGB32, false);
snap.ReadPixels ( new Rect(0, 0, width, height),0 ,0);
RenderTexture.active = null;
snapCam.targetTexture = null;
saveToPNG (snap);
}
void saveToPNG(Texture2D snap){
string iter = System.DateTime.UtcNow.ToLongTimeString ().Replace(":", "_");
byte[] bytes;
bytes = snap.EncodeToPNG();
System.IO.File.WriteAllBytes(Application.dataPath+ "\\Resources\\snap.png" , bytes);
updateGazeMap ();
}
// Class LoadGazeMap
public class LoadGazeMap : MonoBehaviour {
void Awake() {
LineCreator.updateGazeMap += updateMap;
updateMap ();
}
void updateMap (){
var filePath = Application.dataPath + "/Resources/snap.png";
if (System.IO.File.Exists (filePath)) {
/* this is one more problem that I don't get I have to find the GameObject because gameObject.GetComponent<Renderer>.. is giving me missing reference after first iteration, when this script is attached to the plane itself. maybe its something related to delegate keeping address for previous plane I am not sure.. but Find does the trick.. */!
GameObject obj = GameObject.Find ("GazePlane");
Texture tex = Resources.Load ("snap") as Texture;
obj.GetComponent<Renderer> ().material.SetTexture ("_MainTex", tex);
}
}
}
Your answer
Follow this Question
Related Questions
Uniform Texture, Rendering Problem 0 Answers
transparent shader objects disappear on certain camera angles 6 Answers
Render texture and shader issues 0 Answers
Texture showing camera view 3 Answers