Skip to main content
New Participant
June 4, 2015

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;
}
}
    This topic has been closed for replies.

    3 replies

    Inspiring
    June 4, 2015
    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.
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
    New Participant
    September 14, 2016
    Have you figured out the solution. I have the same issue! The texture just cannot updated
    Inspiring
    September 14, 2016
    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.