I am adding to LOD to my object. I have three different shaders: one for inside the object, one close distance and one far.
The following I use for my LODding. The second if statement doesn’t seem to be reached at the right moments. The idea is I get the closest distance to the renderer.bounds, not just the center position; so if an object is really big it will get the right shader even though I am pretty far from the center.
Edit: I accidentally made a typo. The second statement should be shaders[1];
What is still an issue though is the flickering when changing materials. How can I fix this?
Note: shaders is a material array.
if (renderer.bounds.Intersects(GameObject.Find("Camera").collider.bounds)) {
if (activeShader != 0) {
renderer.sharedMaterial = shaders[0];
activeShader = 0;
print ("Replaced shader with 0");
}
} else if (renderer.bounds.SqrDistance(GameObject.Find("Camera").transform.position) < preciseDepthDistance) {
if (activeShader != 1) {
renderer.sharedMaterial = shaders[2]; // Fixed: shaders[1]
activeShader = 1;
print ("Replaced shader with 1");
}
} else {
if (activeShader != 2) {
renderer.sharedMaterial = shaders[2];
activeShader = 2;
print ("Replaced shader with 2");
}
}