Optimize 3D scene performance with texture atlas [Texture Atlas]

Insert image description here

Recommended: Use NSDT editor to quickly build programmable 3D scenes

When developing mobile apps in Unity, it's always important to make sure everything is optimized. Maximizing frame rate allows us to focus on optimizing scripts, baking lights, modifying objects, etc.

This becomes even more important when we bring mobile apps into virtual reality. While there are many areas we can focus on to take steps to optimize, we will only focus on one, the Texture Atlas.

1. Why use texture atlas?

Texture atlases are a way for us to logically group all of our game images or textures into a single file (also called a sprite sheet).
Insert image description here

When creating objects in a modeling program such as Blender, the coordinates of each image can be mapped to the object we want to use. This makes rendering in Unity more efficient and we can have many objects sharing the same material. When we subsequently make objects static, Unity can use static batching to combine (non-moving) game objects into large meshes and render them faster.

2. Make our stickers

In order to create and use texture atlases we need to be able to create our own objects for Unity and put the images into a file. To put images into a graphic, we can use Gimp, a free image editing program. To map our atlas onto an object, we can use Blender, a 3D modeling program.

2.1 Add images

For the sake of this demonstration, we'll just look at a few objects below. Thinking of some materials we want to use, we can grab them online and save them to our computer.

Insert image description here

After saving the images, we can create 1028x1028 images in Gimp and drag and drop each image into our texture atlas. The size of the image can be any size as long as it is not very large.

When collecting images for a texture atlas, it makes sense to group the images logically. If we have a building, we can put all these textures (bricks, walls, floors, etc.) into one texture set. We can do the same with our characters. This keeps things organized, but we're not picky in this case. Let's throw them all in here.
Insert image description here

Use Gimp to drag all images into our 1028x1028

Here is the final complete image:
Insert image description here

Complete Texture Atlas

One thing to note is that these are simple images. There are programs that can generate sophisticated textures for 3D models. However, in our case, even without it, we can still get nice textures on the shapes.

2.2 Creating and mapping our textures

Once the texture atlas is complete, the next step is to open Blender. We can add the primitives we want to use by holding Shift + A and selecting Mesh > Cube.
Insert image description here

Shift + A shortcut key

Insert image description here

Look...cube

The next step is to split the window so that our object and soon our UV image editor can be seen. We can click on the upper right corner of the window and drag to the left to create two views.

Insert image description here

Then select the editor type icon at the bottom to switch to the UV/Image Editor view.

Insert image description here

Now that the view is set up, we can do things. When Unity assigns materials to game objects, it uses UV coordinates as a reference so that each texture is mapped correctly. UV is much like an X/Y coordinate system. If undefined, our entire texture atlas will be mapped to this object. We will soon discover that by defining the model's UV coordinates to the atlas texture, Unity will know which part of the object matches the texture atlas.

Insert image description here

On the left is the 3D viewer... on the right is the UV/image editor

With the left window selected, we can click on the tab and switch to edit mode. This allows us to select all or individual parts of an object. With the shortcut key a we can select everything.
Insert image description here

In edit mode we can position one side of the cube by selecting just the faces.

2.3 Expand our object

After selecting all objects we have to open it. This will take our 3D shape and unfold it onto a 2D plane so that we can define which part of the texture it is mapped to.

Back in the 3D view of the cube, we can click u to expand it and then select expand. This will unfold all faces in our UV/Image Editor. If we choose Smart UV Projection, we can map all faces next to each other. This is useful when unfolding complex objects.

Insert image description here

Left: Expanded example—Right: Smart UV projection

Next, open our texture atlas and load it.

Insert image description here

This will load the image and let us see where the expanded image falls on the texture. Obviously, we don't need the entire image, so we need to click a to select everything. We can then press s to scale the selection to the texture of our choice.
Insert image description here

The expanded image (not currently selected) covers the entire texture atlas

Once the unfold is scaled down to our liking, we can press g to grab it and move it to whatever texture we target. In this case, light wood grain.
Insert image description here

Once done we can save the file and now we have an object with mapped UV coordinates referencing this texture atlas.

2.3 Mapping different faces

Before jumping into Unity, it should be noted that we can select each face individually. In the case of our cube, each face can be mapped to a different texture in the texture atlas, and it works great. In this case, the cube is already mapped and we can select a face of the cube in the 3D viewer. We need to make sure we are in edit mode and have faces selected.

The corresponding expansion is as follows. Go to the UV/Image Editor and hit a to select all points, then we can hit g to move the unfolded cube face to whatever texture we want to assign it.
Insert image description here

We can do this for every face or just a few faces to achieve a finer texture. This can be useful as our models become more complex.
Insert image description here

Draw three independent faces of a cube
Insert image description here

Select the stool base to be painted in the UV/Image Editor

Before jumping into Unity, we create several objects (spheres, cylinders, etc.) and map them to our texture atlas. Then we're ready.

3. Import the 3D model into Unity

Once we have created objects in Blender that have been mapped to a texture atlas, we can drag these objects into Unity and into our project window. If we have a prefabs folder we can put them in it.
Insert image description here

Typically, our objects and their materials will be imported into Unity's materials folder. But sometimes, you might find that you wasted an hour just opening the object. If this happens, there are solutions. In Unity, we should import the texture atlas. We can put it together with the rest of the textures.

Insert image description here

Then right-click in the project window and select Create > Material. With the material selected, look at the inspector and make sure we have Move/No Light selected (lightmaps supported). Since we are focusing on mobile devices, we will bake all the lights into textures. Assigning a mobile/unlit shader is a good way to achieve this.

Once we specify "move/no lighting" we can select the texture associated with that material. This is where we click Select and find our texture atlas.
Insert image description here

Having Mobile/Unlit ensures the best performance on mobile devices
Insert image description here

Once our texture is assigned to the material, the object will update

Now that we have materials that use texture atlases, any object we map in Blender syncs nicely with the texture we specify.

3.1 Staticize and bake the scene

After setting up all objects with the same material, the next step is to take advantage of Unity's static batching. This can be done quickly by marking all objects as static. Assuming these are not animated in our application, this is what must be done.

Use some spotlights for good effect and we make sure to mark them as zones (baked only). This way, our scene is obviously getting some light, and our objects can now also have lighting baked onto their textures, since they are now static.

To make these changes visible we can go to Window > Lighting > Settings and generate lighting for the scene.

Insert image description here

Baking scene

3.2 Where are the static batches?

We mentioned at the beginning that static batching is a way to optimize scene rendering. Unity takes a static object with the same material and renders it into a large mesh to speed things up. Fewer draw calls in Unity means more frames per second, and when we have over 1000 objects we'll see why this is so important.

Since we create texture atlases for our objects, Unity can draw all game objects at once. One way to see what's actually going on is to use the frame debugger.

3.3 Unity’s frame debugger

The Frame Debugger allows us to pause the game during play mode and break down how each frame is rendered. By stepping through each item in the hierarchy on the right, we can see the order in which each render occurs. Notice that the Static Batch line is highlighted below the game view and we see that Unity renders all of these objects at once with a single draw call. Great, right?

Insert image description here

If we assign a different material to each object, we add draw calls and force Unity to render each object individually, as shown below. Not very efficient.
Insert image description here

Without shared materials, there is no static batching

4. Atlas, unpacking and batching...

Generating a simple texture atlas allows us to create shared materials across multiple game objects in Unity. If we can create or modify models in a 3D modeling tool like Blender, we can have more control over the mapping of these textures and make our applications run more efficiently.

Even as mobile devices become more powerful, we continue to push the limits of hardware. Of the many ways we need to optimize our apps to provide the best user experience, this is just one important step toward achieving that goal. Remember the texture atlas.


Original link: Optimizing 3D scenes with texture atlases—BimAnt

Guess you like

Origin blog.csdn.net/shebao3333/article/details/133302306