Problem with render texture

hi guys,
I try to implement a mini-map system in my game. I have map 2048x2048 and I use render texture to show mini-map on my UI. To show all mini-map I need to set size 1200 on my camera and then I have a huge drop on fps.
I don’t know how I can improve the system. any ideas?

code to create texture
_miniMapRenderTexture = new RenderTexture((int) _screenWidth/2, (int) _screenHeight/2, 24)
{
filterMode = FilterMode.Point,
wrapMode = TextureWrapMode.Repeat,
useDynamicScale = true,
memorylessMode = RenderTextureMemoryless.Color
};

I attached camera setting

everything works well if the camera size is smaller but I think it’s obvious

What is the size of the render texture? If the render texture is bigger then you are drawing it on the screen then make it smaller.
Edit: Oops, nevermind, I see it.

Another thing to try is to use layers to filter out anything that does not need to be rendered on the minimap.

Oh, and the most obvious thing is that unless your map is constantly changing, keep in mind that you don’t need to keep rendering it over and over every frame.

You’re rendering the whole game again every frame just to get a minimap. You’re also doing it with a huge camera which is probably forcing it to render everything rather than just what your main camera can see. Both of those will have a performance hit.

A few things to consider:

  • Render the landscape to a rendertexture on Start(), and afterwards only render stuff that moves to another texture with transparency, then draw both in your UI.
  • Save an image of your landscape to a texture in the Editor, so it doesn’t have to be rendered at all in-game. This also lets you easily modify it in something like Photoshop to stylise it however you’d like.
  • Depending on what items you have which move, you might not have to render those either. Calculate their positions in “map space” and then draw icons in their places.
  • If you’ve got enough moving stuff in your game that it’s worth re-rendering the minimap regularly, use LayerMasks to ensure that the map camera is only rendering what it needs to. Also look into LOD groups to ensure that you’re not drawing high-poly models intended for close-up viewing when you don’t need to. Your map view can probably get away with highly simplified shapes.

For future reference, General Discussion is specifically not a support area.

1 Like

Hi, thanks for your responses. I use only one layer in camera to render the mini- map. and I render only simple Sprite Renderer (white texture with color)

attached screen

Have you used the profiler to see if you can get any details on where exactly your bad performance is coming from?

@kdgall yeah, I saw a huge effort on rendering process

Where you able to break it down any further?

I tried to reduce texture size and change a few settings on render texture but still the same result, I tested also few options on camera and the same

I tried also set pixels on texture via code but the performance is also not good

It looks like you are doing a tile per pixel? Do you actually have a separate object for each pixel on your minimap?

yes, one object per pixel and I have also few moving pixels on mini-map. rest of is static

That’ll be a problem but, like angrypenguin already suggested, you can render it only once at the beginning of the level and move it to a regular Texture2D so you can just use the rendered-out texture each frame.
https://answers.unity.com/questions/9969/convert-a-rendertexture-to-a-texture2d.html

Once you rendered the minimap once, de-Activate that whole mess of minimap tiles or something.

@kdgall I can’t do this because the player discovers the mini-map only on the end see all tiles. so the map can change every frame

So you have two textures. One is your pre-rendered minimap, the other is a “fog of war” which you draw on top. As the player moves around you change the alpha on the fog of war so the player can see through it to the map image.

This way you only have to change a few pixels at a time, and only when you know that more “fog” has been pushed back.

1 Like
  1. Use Texture2D.SetPixel instead of RenderTexture to manually generate your minimap texture.
  2. Build the texture once on start from all tile objects, then reuse the same texture every frame.
  3. Have all your dynamic tile objects broadcast an event whenever they move to a different tile. The event should contain both the previous and the new positions.
  4. Have your minimap class listen for these events and use Texture2D.SetPixel to update the pixels of the previous and new positions in the minimap.
  5. Delay calling Texture2D.Apply until the end of the frame, or even only call it every couple of frames, so that if multiple objects should move in the minimap during the same frame, the texture is still only rebuilt once. Texture2D.Apply is very slow, so the less you need to call it, the better.
  6. And most importantly: To avoid lag spikes when Texture2D.Apply is called, you will most likely need to split your minimap into multiple texture chunks. This way when a tile object moves to a different tile, you don’t need to rebuild the whole 2048x2048 map, but only a smaller portion of it.
1 Like