Search code examples
unity-game-enginetexturesdirectx-11

Render a RenderTexture to a mesh


In Unity, I'm updating a Render Texture procedurally (via writing data to it) via a DirectX plugin. I do something like the following to initially create my RenderTexture:

RenderTexture myTexture = new RenderTexture (100, 100, 0);
myTexture.Create ();
transform.GetComponent<Renderer> ().material.mainTexture = myTexture;
transform.GetComponent<Renderer> ().enabled = true;

Then later on I modify the texture as needed. Yet this object's material (what it looks like in the real world) doesn't change. If I click on that object, then click on it's material, and click on this RenderTexture attached to it, I can see it updating, just for some reason it doesn't update on the actual mesh. Why is this? I've tried using different built-in shaders, but that hasn't seemed to help. Is there a way to write a shader to render a RenderTexture to a mesh, as one idea?


Solution

  • I found the best option is simply to use a RawImage instead of a Material, and apply the render texture to that RawImage's texture (not mainTexture, just texture). A material can then even be applied to that raw image if you want to use a shader.