- Home /
How can I lower Decal draw calls at runtime?
I think this is a quite common issue with decals, so I'd appreciate some optimization tips.
Decals are great for things like bullet holes or for adding that sense of variety into textures and models, but the problem is of course if not handled correctly each new decal can mean an extra draw call.
In my case I am using decals for bullet holes. I'm using a texture atlas which has all of the different bullet holes packed together into a single texture. When I fire and the bullet holes appear at runtime, if I only use 1 material all of the bullet holes get batched. This is perfect, but of course it makes all the bullet holes look exactly the same. If I create one material for each bullet hole (still using the one texture atlas) and simply adjust the tiling and offset; then all the bullet holes now look nice and different, but it creates a lot of draw calls (one for each material obviously). So I'm wondering how can I get the best of both worlds, have a variety of different looking bullet holes but be able to batch them all as well.
I've looked at Bootcamp and the free Decal System and both seem to be creating meshes at runtime, which is a bit beyond my programming expertise since I'm simply using a plane which is positioned and rotated to where the bullet hit. I also can't combine the decal planes since they are obviously dynamic objects. Any suggestions?
Interesting, I've never used mesh.uv before so I'll try this. I will need to change the uvs each time a bullet hits a surface though, since there's no way to know what bullet hole in the atlas should render until the surface hit is actually detected. Would I be able to change it this way at runtime many times?
And btw, yes the planes I'm using are two triangle meshes.
Thanks a lot, I've done some testing and it seems this will work for what I need. If you'd like to convert your comment to an answer I'll mark it as accepted ;)
Answer by robertbu · Jun 29, 2013 at 06:47 AM
I can suggest a direction for you. You'll have to get your hands a bit dirty with meshes, but not to the extent of creating/expanding meshes. What you need to do is to set the UV coordinates for your atlas directly in the mesh, not use the tiling and offset. If you set the UV directly in the mesh of each plane, all the bullets will batch. Note this will be easier if you use two triangle planes. The CreatePlane script can be used to create two triange planes if you don't have access a 3D modeling package.
http://docs.unity3d.com/Documentation/ScriptReference/Mesh-uv.html
Note creating meshes at runtime is the best way to solve this problem. @alucardj did a great answer on how to render footsteps that you might examine to get the basic concepts:
http://answers.unity3d.com/questions/475795/rendering-permanent-trails-such-as-footsteps.html