I have a system where I'm using a custom shader to blit some information into a RenderTexture, which I then display using World Space canvases and RawImages. It works fine in terms of generating the images, but if I enter play mode, or if I say, move one of the canvases and use "undo", the RenderTexture is replaced with a blank one, and the RawImage has lost its reference.
I've checked my code and I'm never altering the RenderTexture here. The RenderTexture is set to be serialized and I even stuck in an "EditorUtility.SetDirty(this)" after its generated.
[Here's an imgur album.][1]
So in my main class I have this function:
private void GenerateTexture()
{
m_renderTexture = m_textureCreator.GenerateTextureFromField(m_gravityPoints, m_resolution);
m_rawImage.texture = m_renderTexture;
#if UNITY_EDITOR
EditorUtility.SetDirty(this);
#endif
}
This method generates the texture:
public RenderTexture GenerateTextureFromField(Vector2[] field, int fieldSize)
{
using (m_computeBuffer = new ComputeBuffer(field.Length, 8))
{
m_computeBuffer.SetData(field);
m_material.SetBuffer(POINT_ARRAY_PROPERTY, m_computeBuffer);
m_material.SetInt(POINT_COUNT_PROPERTY, field.Length);
m_material.SetInt(FIELD_SIZE_PROPERTY, fieldSize);
// commented this part out (which is meant to clear the current render texture)
// to see if it was the reason. it wasn't.
//RenderTexture rt = RenderTexture.active;
//RenderTexture.active = m_renderTexture;
//GL.Clear(true, true, Color.clear);
//RenderTexture.active = rt;
RenderTexture temp = RenderTexture.GetTemporary(m_renderTexture.width, m_renderTexture.height);
Graphics.Blit(temp, m_renderTexture, m_material);
RenderTexture.ReleaseTemporary(temp);
}
return m_renderTexture;
}
Any ideas what could be causing this? Thank you!
[1]: https://imgur.com/a/uK5LAAt
↧