- Home /
Shader / Material Refreshing, AssetBundles, and dynamic batching issues
Hey all, I've run into an optimization issue that I am struggling to find a lot of information for:
Basically I am creating a 3D WebGL game that downloads AssetBundles on every startup to get the model content from our servers. We have a business requirement to grab these models each time the game loads, so they cannot be packed into the webgl artifact.
If you have ever used AssetBundles in webgl in this way using the standard shader, you know that for every material that is being unpacked on the fly, you need to refresh its shader or it will not know how to render it properly. Here is my sample "RefreshShader" code:
public static void RefreshShader(GameObject clone)
{
var renderers = clone.GetComponentsInChildren<Renderer>();
for (int i = 0; i < renderers.Length; i++)
{
var materials = renderers[i].materials; //I have also tried sharedMaterials here, doesn't help
foreach (var material in materials)
{
var rq = material.renderQueue;
material.shader = Shader.Find(material.shader.name);
foreach (var s in material.shaderKeywords)
material.EnableKeyword(s);
material.renderQueue = rq;
}
}
}
If I don't execute this code when I instantiate a model GameObject, the mesh does not render properly, since the shader does not know what properties to use. It doesn't seem like I have a choice in this matter (I welcome any suggestions otherwise!)
As a result of having to refresh each material shader, I am basically breaking Unity's ability to dynamically batch my objects that share materials, so I have WAY too many drawcalls when a scene gets busy. CPU draw calls seem to be the biggest performance bottleneck and I am trying to cut these down, but can't due to the material refresh issue.
Does anyone have a suggestion to 1. Avoid having to refresh materials/shaders in this manner that breaks batching, or 2. refresh these materials/shaders in such a way that it does not break batching (i tried to use sharedMaterials instead, the materials do work correctly when I use this, but it still does break batching for some reason). or 3. reduce my draw calls in an alternative way that is not dynamic batching or culling
Please also note that these objects are movable so I cannot use static batching to handle this.
Your answer
Follow this Question
Related Questions
Dynamic batching 6 Answers
Batching draw calls? 2 Answers
Dynamic batching breaks with the material Mobile/Alpha Blended. 0 Answers
Optimizing bump map or luminous textures on spheres for mobile 1 Answer
Share material to reduce draw calls 3 Answers