Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
pllong's avatar
pllong
Honored Guest
11 years ago

Updating Texture2D with Rift

Hi, I am trying to take advantage of C#'s built in graphics and drawing to create dynamic textures for a HUD. This works just fine with the normal camera. However, when I try to use it with the Oculus, the textures don't update. This is the class file that I am using:


public class UpdateRender : MonoBehaviour {

[SerializeField] private Texture2D m_LevelPi;
[SerializeField] private Texture2D m_DepartmentPi;
private Image m_LevelPiImg;
private Image m_DepartmentPiImg;

public bool m_Update;

// Use this for initialization
void Start () {
m_LevelPiImg = new Bitmap (256, 256);
m_DepartmentPiImg = new Bitmap (256, 256);
m_Update = true;

using (var g = System.Drawing.Graphics.FromImage (m_LevelPiImg)) {
g.FillRectangle (new SolidBrush (System.Drawing.Color.Black), new System.Drawing.Rectangle (96, 96, 64, 64));
}
}

// Update is called once per frame
void Update () {
if (m_Update) {
MemoryStream ms;
// Load Levels
ms = new MemoryStream();
m_LevelPiImg.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
m_LevelPi.LoadImage(ms.ToArray());
ms.Close();
// Load Departments
ms = new MemoryStream();
m_DepartmentPiImg.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
m_DepartmentPi.LoadImage(ms.ToArray());
ms.Close();
ms = null;
m_Update = false;
}
}

public Image GetDepartmentPi() {
m_Update = true;
return m_DepartmentPiImg;
}

public Image GetLevelPi() {
m_Update = true;
return m_LevelPiImg;
}
}

3 Replies

Replies have been turned off for this discussion
  • Seem that should work, but there are a few things that may help. Converting to a PNG and then loading it seems pretty inefficient. You should probably convert the System.Drawing.Bitmap to a byte array and then set the texture's pixels.
  • Have you figured out the solution. I have the same issue! The texture just cannot updated
  • Try calling Texture2D.Update each time you modify the data. As Cybereality points out, this approach is going to be very slow because you are working on the raster in system memory.