How to render a RenderTexture into a specific mipLevel

Hey everyone,

I am wondering how can I render a specific downsampled RenterTexture into a mipLevel using Graphics.SetRenderTarget ?

Thanks a lot !

What do you mean? It’s right there in the docs you linked:
public static void SetRenderTarget(RenderBuffer colorBuffer, RenderBuffer depthBuffer, int mipLevel);

possibly followed by Graphics.Blit(Texture source, Material mat, int pass = -1);

@Dolkar of course, but I can’t get it work, can you provide some working example ? It would be awesome.

I don’t think I have one on hand, but I know this works and does not differ too much from rendering to regular targets… The only issue you might be facing is if you’re trying to read from another mip level of the same texture you’re writing to, which is not going to work.

Another bit that could be causing problems is even the target-less Graphics.Blit overload doing something weird behind the scenes. You could try replacing it by setting the desired material pass and drawing a full screen quad manually.

@Dolkar , Thank you. This might be offtopic but do you have an example of a RenderTarget for a specific material pass ? I also have some dofficulties to see how it works if you want to render a specific pass of a shader, applied to multiple objects, in a new buffer.

One way to do it is like this: (pseudocode)

Graphics.SetRenderTarget(renderTexture);
mat.SetPass(passNumber);

foreach (GameObject go in objects) {
    Graphics.DrawMeshNow(go.GetComponent<MeshFilter>().sharedMesh, go.transform.localToWorldMatrix);
}

Or alternatively using command buffers, which allow you to draw skinned meshes and other renderers this way:

var commands = new CommandBuffer();
commands.name = "Drawing Objects";
commands.SetRenderTarget(renderTexture);

foreach (GameObject go in objects) {
    commands.DrawRenderer(go.GetComponent<Renderer>(), mat, 0, passNumber);
}

You can then either execute it immediately with Graphics.ExecuteCommandBuffer or even better, bind it to any point in the rendering pipeline using Camera.AddCommandBuffer. Note that you only need to create the command buffer once. You can read more about it here: Unity - Scripting API: CommandBuffer

@Dolkar Thanks again for sharing your knowledge. Do you know if using SetRenderTarget with an existing pass like the deferred one or forwardadd allows you to output it in a rendertexture ?

I mean, if I understand your example, you need to trigger the rendering but what if the pass rendering is already triggered by Unity. Should the deferred or forwardadd be rendered twice (one regular and one for the rendertarget) ?

I would simply like to output the result of one of those passes (deferred and/or forward add) and use it in an other one.

Thank you again for the time accorded to my questions @Dolkar .

Yeah, once you need to incorporate lighting and make your mesh fit the scene, you’re better off using higher level methods and let Unity do the job. One way would be to set up a second camera rendering only a specific layer into a separate render texture, which you can then bind later to any shader as you please.

Otherwise, to achieve correct lighting, you need to render the material as a whole instead of a specific pass, like with Graphics.DrawMesh. If neither of those fit your needs, I think it would be a good idea to elaborate on what you’re trying to do and what your goals are. Perhaps we can find another solution altogether.