- Home /
SetPropertyBlock for LOD?
I use SetPropertyBlock to paint color for my model. My model has 5 level of details (LOD0 to LOD4).
The problem is LODs in Unity doesn't share the same materials array (sharedMaterials), thus I paint color for LOD0 doesn't affect LOD1,.., LOD4. So I tried painting for other LOD too:
Transform[] arrLods;//The array of LODs
//Assign arrLods
//...
MaterialPropertyBlock propBlock = new MaterialPropertyBlock();
Renderer renderer = arrLods[0].GetComponent<Renderer>();
//Paint for LOD0
//...
//Paint for other LODs
Material[] matLODs;
Renderer rendererLODs;
for (int i = 1; i < arrLods.Length; ++i)
{
rendererLODs = arrLods[i].GetComponent<Renderer>();
matLODs = rendererLODs.sharedMaterials;
for (int m = 0; m < mat.Length; ++m)
{
for(int mLODs = 0; mLODs < matLODs.Length; ++mLODs)
{
if (mat[m].name == matLODs[mLODs].name)
{
rendererLODs.GetPropertyBlock(propBlock, mLODs);
if(propBlock.isEmpty)
{
renderer.GetPropertyBlock(propBlock, m);
rendererLODs.SetPropertyBlock(propBlock, mLODs);
break;
}
}
}
}
}
Because the order of other LODs doesn't match the order of LOD0, the code "Paint for other LODs" is a little complex, but I still have bug in case the materials have same name (Brick1, Brick2,... use same material Brick, but difference color)
so I wonder if there is a better way to properly SetPropertyBlock for LOD?
Answer by NathanJSmith · Oct 24, 2018 at 01:07 PM
I think there are 2 solutions:
Solution 1 - Do something in your modeling software so that the order of material the same for each LOD, then you can manually paint each LOD.
Solution 2 - Paint only for LOD0, other LODs (LOD1, LOD2,...) will have 1 material only (thus we don't need to paint it). I see that in GTA IV's ped, they do this, when the ped is far away, it turns gray.
I'll take solution 2, because each material in the model requires one draw-call, so I want to save draw-call for far objects.