using System; using UnityEngine; using Framework.Services; namespace Framework.Support { /// Adds a margin to the bounds of a mesh public class CullingMarginExtender : ScriptComponent { /// Additional margin that will be added to the bounding boxes public Vector3 AdditionalMargin = new Vector3(4.0f, 4.0f, 4.0f); /// Called once before the game object is updated for the first time public void Start() { ExtendCullingMargin(); } /// Extends the culling margin of all meshes in the game object public void ExtendCullingMargin() { Renderer[] meshRenderers = transform.GetComponentsInChildren( includeInactive: true ); if((meshRenderers == null) || (meshRenderers.Length == 0)) { Debug.LogWarning( "Culling margin extender on '" + name + "' has no mesh renderers for " + "which it could extend the culling margin. Doing nothing." ); return; } enlargeBoundingBoxes(meshRenderers); } /// Enlarged the bounding boxes of all renderers in the list /// Renderers whose bounding boxes will be enlarged private void enlargeBoundingBoxes(Renderer[] meshRenderers) { for(int index = 0; index < meshRenderers.Length; ++index) { var skinnedMeshRenderer = meshRenderers[index] as SkinnedMeshRenderer; if(skinnedMeshRenderer != null) { Bounds bounds = skinnedMeshRenderer.localBounds; Vector3 minimumBounds = bounds.min - this.AdditionalMargin; Vector3 maximumBounds = bounds.max + this.AdditionalMargin; bounds.SetMinMax(minimumBounds, maximumBounds); skinnedMeshRenderer.localBounds = bounds; continue; } Debug.LogWarning( "Culling margin extender on '" + name + "' could not find a skinned " + "mesh renderer for '" + meshRenderers[index].name + "'. " + "Culling will be unchanged for this particular mesh." ); } } } } // namespace Framework.Support