} // ModelRendererFrustumCulling /// <summary> /// This is task that's is performed over the threads. /// </summary> private static void ModelRendererFrustumCullingTask(FrustumCullingParameters parameters) { BoundingFrustum boundingFrustum = parameters.boundingFrustum; List <ModelRenderer> modelsToRender = parameters.modelsToRender; int startPosition = parameters.startPosition; int count = parameters.count; FastBoundingFrustum fastBoundingFrustum = new FastBoundingFrustum(boundingFrustum); // I improved memory locality with FrustumCullingDataPool. // However, in order to do that I had to make the code a little more complicated and error prone. // There is a performance gain, but it is small. // I recommend to perform this kind of optimizations is the game/application has a CPU bottleneck. // Besides, the old code was half data oriented. for (int i = startPosition; i < (count + startPosition); i++) { ModelRenderer.FrustumCullingData frustumCullingData = ModelRenderer.FrustumCullingDataPool.Elements[i]; if (frustumCullingData.model != null && // Not need to waste cycles in this, how many ModelRenderer components will not have a model? // Is Visible? Layer.IsVisible(frustumCullingData.layerMask) && frustumCullingData.ownerActive && frustumCullingData.enabled) { if (fastBoundingFrustum.Intersects(ref frustumCullingData.boundingSphere)) { modelsToRender.Add(frustumCullingData.component); } } } } // ModelRendererFrustumCullingTask
} // PointLightFrustumCulling /// <summary> /// Frustum Culling. /// </summary> /// <param name="boundingFrustum">Bounding Frustum.</param> /// <param name="spotLightsToRender">The result.</param> public static void SpotLightFrustumCulling(BoundingFrustum boundingFrustum, List <SpotLight> spotLightsToRender) { FastBoundingFrustum fastBoundingFrustum = new FastBoundingFrustum(boundingFrustum); for (int i = 0; i < SpotLight.ComponentPool.Count; i++) { SpotLight component = SpotLight.ComponentPool.Elements[i]; if (component.Intensity > 0 && component.IsVisible) { BoundingSphere boundingSphere = new BoundingSphere(component.cachedPosition, component.Range); if (fastBoundingFrustum.Intersects(ref boundingSphere)) { //if (boundingFrustum.Intersects(frustumCullingData.boundingSphere)) // It is not thread safe and is slow as hell. spotLightsToRender.Add(component); } } } } // FrustumCulling