- Home /
Using RenderTextures in editor script
Hi,
I'm currently implementing a node-based editor to create procedural heightmaps and materials and the editor contains a "preview" window that displays the output material.
I have a function that renders a material to a full-screen quad and then saves that quad as a RenderTexture. I then use this RenderTexture as a source to other materials' properties.
Here is my function to save a material as a texture. This function is called on each MouseUp event.
public Texture GetHeightMapRT(Material mat)
{
heightmapRT = new RenderTexture(rtWidth, rtHeight, 24, RenderTextureFormat.ARGB32);
GameObject renderCamera = new GameObject();
renderCamera.AddComponent<Camera>();
renderCamera.GetComponent<Camera>().backgroundColor = Color.black;
renderCamera.GetComponent<Camera>().orthographic = true;
renderCamera.GetComponent<Camera>().orthographicSize = 5;
renderCamera.GetComponent<Camera>().transform.localPosition = new Vector3(9999, 10009, 9999);
renderCamera.GetComponent<Camera>().transform.localRotation = Quaternion.Euler(new Vector3(90.0f, 0.0f, 0.0f));
GameObject renderPlane = GameObject.CreatePrimitive(PrimitiveType.Plane);
renderPlane.transform.localPosition = new Vector3(9999, 9999, 9999);
renderPlane.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));
renderPlane.GetComponent<MeshRenderer>().material = mat;
RenderTexture.active = heightmapRT;
renderCamera.GetComponent<Camera>().targetTexture = heightmapRT;
renderCamera.GetComponent<Camera>().Render();
renderCamera.GetComponent<Camera>().targetTexture = null;
RenderTexture.active = null;
DestroyImmediate(renderPlane);
DestroyImmediate(renderCamera);
return heightmapRT;
}
This function works, but I get the following error when it is called:
m_Memory.renderTextureBytes<0 UnityEngine.RenderTexture:set_active(RenderTexture)
The error is not present when calling the function in-game from script.
Any help would be greatly appreciated.
Thanks!
Your answer
Follow this Question
Related Questions
Render to Texture artifacts 0 Answers
Converting a RenderTexture to a Texture2D for use in a shader 2 Answers
Capture RenderTexture pixels multiple times in one frame? 1 Answer
RenderTexture not working when in WebPlayer, unless in fullscreen 1 Answer
How to update texture during runtime to display damage? 0 Answers