- Home /
Use GPU Instancing and Render To Texture at the same time
I want to use gpu instancing to render the screen to a frame buffer in unity but the problem is the only two interface concerning gpu instancing that unity exposed involves cameras. This means if I want to use gpu instancing, it will either render to all cameras or the one that I specified. On the other hand, I want to use SetRenderTarget to make unity render to the render texture that I specified. According to my test, SetRenderTarget before DrawMeshInstanced doesn't change anything. The scene was still rendered to the cameras but not to the render texture. I think I can blit the camera's result to my render texture, but it will be slow. I tried set the camera's target texture to my render texture and call DrawMeshInstanced but it didn't work. It rendered the scene without the instanced mesh that I specified. I wonder is there any solution to this? @bgolus
Answer by fox3fire · Jul 14, 2018 at 09:13 AM
My method is correct. What I did wrong was I only rendered to the render texture when I press down a button and after that didn't change the camera's target texture to null. This will cause the camera overwrite the result in the texture.
However, I encountered another problem. Is there any way to set camera's target texture to a RenderTexture which has the form of a Texture2DArray. In short how can I specify a camera's target texture with a depthSlice parameter?
FINAL ANSWER: USE COMMAND BUFFER FOR EVERYTHING YOU WANT TO DO THAT STARTS WITH "GRAPHICS.". FINALLY, I CAN CALL SET RENDER TARGET AND DRAW MESH INSTANCED AT THE SAME TIME.
Because in command buffer, draw mesh instanced does not require a camera or any camera!!!