/// <summary> /// Returns the mesh managers suitable for a shadow. /// </summary> /// <param name="shadow"> /// The shadow to get mesh manager for. /// </param> /// <returns> /// The <see cref="ShadowMeshManager"/>. /// </returns> private ShadowMeshManager GetMeshManager(SwiftShadow shadow) { ShadowMeshManager meshManager; if (_meshManagers.TryGetValue(shadow.GetMeshManagerHashCode(), out meshManager)) { return(meshManager); } if (shadow.Material == null) { throw new ArgumentNullException("shadow.material"); } if (_meshManagersNew.TryGetValue(shadow.GetMeshManagerHashCode(), out meshManager)) { return(meshManager); } meshManager = new ShadowMeshManager(shadow.Material, shadow.Layer, shadow.IsStatic); if (_isRecalculatingMesh) { _meshManagersNew.Add(meshManager.GetInstanceHashCode(), meshManager); } else { _meshManagers.Add(meshManager.GetInstanceHashCode(), meshManager); } return(meshManager); }
private void UpdateAndDrawMeshManager(ShadowMeshManager meshManager, Camera camera, int cameraCullingMask) { // Skip calculating and rendering shadows that are on // layers culled by the camera if ((cameraCullingMask & meshManager.LayerMask) == 0) { return; } meshManager.RecalculateGeometry(camera != null ? _cameraFrustumPlanes : null); meshManager.DrawMesh(camera); }
/// <summary> /// Forces the recalculation of static shadows. /// </summary> public void UpdateStaticShadows() { ShadowMeshManagerMapEnumerator meshManagersEnumerator = new ShadowMeshManagerMapEnumerator(_meshManagers); while (meshManagersEnumerator.MoveNext()) { ShadowMeshManager meshManager = meshManagersEnumerator.CurrentValue; if (meshManager.IsStatic) { meshManager.ForceStaticRecalculate(); } } }
/// <summary> /// Renders the shadows to a camera /// </summary> /// <param name="camera"> /// The camera to render shadows on. /// </param> private void Render(Camera camera) { // Calculate the camera frustum planes int cameraCullingMask; if (camera != null) { #if SUPPORTS_REFLECTION GeometryUtilityInternal.CalculateFrustumPlanes(_cameraFrustumPlanes, camera); #else _cameraFrustumPlanes = GeometryUtility.CalculateFrustumPlanes(camera); #endif cameraCullingMask = camera.cullingMask; } else { cameraCullingMask = unchecked (~0); } _isRecalculatingMesh = true; // Clearing list of managers added on previous recalculations _meshManagersNew.Clear(); // Loop known mesh managers, we don't care if new items will be added inside the loop ShadowMeshManagerMapEnumerator meshManagersEnumerator = new ShadowMeshManagerMapEnumerator(_meshManagers); while (meshManagersEnumerator.MoveNext()) { ShadowMeshManager meshManager = meshManagersEnumerator.CurrentValue; UpdateAndDrawMeshManager(meshManager, camera, cameraCullingMask); } _isRecalculatingMesh = false; // Calculate & draw the newly added managers (if AutoStatic is used) // and add them to the actual list ShadowMeshManagerMapEnumerator meshManagersNewEnumerator = new ShadowMeshManagerMapEnumerator(_meshManagersNew); while (meshManagersNewEnumerator.MoveNext()) { ShadowMeshManager meshManager = meshManagersNewEnumerator.CurrentValue; UpdateAndDrawMeshManager(meshManager, camera, cameraCullingMask); _meshManagers.Add(meshManager.GetInstanceHashCode(), meshManager); } }
private void OnLevelWasLoaded(int level) { UpdateCameraEvents(); // Remove empty mesh managers ExposedList <ShadowMeshManager> emptyShadowMeshManagers = new ExposedList <ShadowMeshManager>(); ShadowMeshManagerMapEnumerator meshManagersEnumerator = new ShadowMeshManagerMapEnumerator(_meshManagers); while (meshManagersEnumerator.MoveNext()) { ShadowMeshManager meshManager = meshManagersEnumerator.CurrentValue; if (meshManager.ShadowsCount == 0) { emptyShadowMeshManagers.Add(meshManager); } } for (int i = 0; i < emptyShadowMeshManagers.Count; i++) { _meshManagers.Remove(emptyShadowMeshManagers.Items[i].GetInstanceHashCode()); } }
public int GetMeshManagerHashCode() { int layer = _useForceLayer ? _forceLayer : gameObject.layer; return(ShadowMeshManager.CalculateMeshManagerHashCode(_isStatic, _material, layer)); }