public void RenderForLight(MatrixD lightViewProjection, Matrix lightViewProjectionAtZero, ref Vector3D lightPosition, Texture shadowRenderTarget, Texture shadowDepth, int spotIndex, List <uint> ignoredObjects) { m_renderElementsForShadows.Clear(); m_castingRenderObjectsUnique.Clear(); m_spotFrustum.Matrix = lightViewProjection; //MyRender.GetEntitiesFromPrunningStructure(ref lightBoundingBox, m_castingRenderObjects); //MyRender.GetEntitiesFromShadowStructure(ref lightBoundingBox, m_castingRenderObjects); int occludedItemsStats = 0; //always 0 for shadows MyRender.PrepareEntitiesForDraw(ref m_spotFrustum, lightPosition, (MyOcclusionQueryID)(1), m_castingRenderObjects, null, m_castingCullObjects, m_castingManualCullObjects, null, ref occludedItemsStats); foreach (MyElement element in m_castingRenderObjects) { MyRenderObject renderObject = (MyRenderObject)element; if (ignoredObjects.Contains(renderObject.ID)) { continue; } renderObject.GetRenderElementsForShadowmap(MyLodTypeEnum.LOD0, m_renderElementsForShadows, null); } // Set our targets MyRender.SetRenderTarget(shadowRenderTarget, shadowDepth); MyRender.GraphicsDevice.Clear(ClearFlags.All, new ColorBGRA(1.0f), 1.0f, 0); DepthStencilState.Default.Apply(); RasterizerState.CullNone.Apply(); BlendState.Opaque.Apply(); RenderShadowMap(lightViewProjectionAtZero); MyRender.TakeScreenshot("ShadowMapSpot", shadowRenderTarget, MyEffectScreenshot.ScreenshotTechniqueEnum.Color); }
void PrepareCascadesForDraw() { if (MyRender.Sun.Direction == Vector3.Zero) { return; } MyRender.GetRenderProfiler().StartProfilingBlock("UpdateFrustums"); UpdateFrustums(); MyRender.GetRenderProfiler().EndProfilingBlock(); // Set casting shadows geometry MyRender.GetRenderProfiler().StartProfilingBlock("update entities"); int frustumIndex = 0; foreach (MyOrthographicCamera lightCamera in m_lightCameras) { if (m_skip[frustumIndex]) { frustumIndex++; continue; } m_renderElementsForShadows.Clear(); m_transparentRenderElementsForShadows.Clear(); m_castingRenderObjectsUnique.Clear(); MyRender.GetRenderProfiler().StartProfilingBlock("OverlapAllBoundingBox"); var castersBox = lightCamera.BoundingBox; //Cannot use unscaled - incorrect result because of different cascade viewport size var castersFrustum = lightCamera.BoundingFrustum; //Cannot use unscaled - incorrect result because of different cascade viewport size int occludedItemsStats = 0; //always 0 for shadows //MyRender.PrepareEntitiesForDraw(ref castersBox, (MyOcclusionQueryID)(frustumIndex + 1), m_castingRenderObjects, m_occlusionQueriesLists[frustumIndex], ref MyPerformanceCounter.PerCameraDrawWrite.ShadowEntitiesOccluded[frustumIndex]); MyRender.PrepareEntitiesForDraw(ref castersFrustum, lightCamera.Position, (MyOcclusionQueryID)(frustumIndex + 1), m_castingRenderObjects, null, m_castingCullObjects, m_castingManualCullObjects, null, ref occludedItemsStats); MyRender.GetRenderProfiler().EndProfilingBlock(); MyRender.GetRenderProfiler().StartProfilingBlock("m_castingRenderObjects"); int c = 0; int skipped = 0; while (c < m_castingRenderObjects.Count) { MyRenderObject renderObject = (MyRenderObject)m_castingRenderObjects[c]; if (RespectCastShadowsFlags) { // System.Diagnostics.Debug.Assert(!(entity is MyDummyPoint) && !(entity is AppCode.Game.Entities.WayPoints.MyWayPoint)); if ((renderObject.ShadowCastUpdateInterval > 0) && ((MyRender.RenderCounter % renderObject.ShadowCastUpdateInterval) == 0)) { renderObject.NeedsResolveCastShadow = true; //We have to leave last value, because true when not casting shadow make radiation to ship // renderObject.CastShadow = true; } if (renderObject.NeedsResolveCastShadow) { //Resolve raycast to sun if (renderObject.CastShadowJob == null) { renderObject.CastShadowJob = new MyCastShadowJob(renderObject); renderObject.CastShadowTask = ParallelTasks.Parallel.Start(renderObject.CastShadowJob); } else if (renderObject.CastShadowTask.IsComplete) { renderObject.CastShadows = renderObject.CastShadowJob.VisibleFromSun; renderObject.CastShadowTask = new ParallelTasks.Task(); renderObject.CastShadowJob = null; renderObject.NeedsResolveCastShadow = false; if (renderObject.CastShadows == false) { HiddenResolvedObjects++; } } } if (!renderObject.NeedsResolveCastShadow && !renderObject.CastShadows) { if (renderObject is MyRenderVoxelCell) { } m_castingRenderObjects.RemoveAtFast(c); skipped++; continue; } } else { renderObject.NeedsResolveCastShadow = true; } if (!m_castingRenderObjectsUnique.Contains(renderObject)) { m_castingRenderObjectsUnique.Add(renderObject); if (frustumIndex < MyRenderConstants.RenderQualityProfile.ShadowCascadeLODTreshold) { renderObject.GetRenderElementsForShadowmap(MyLodTypeEnum.LOD0, m_renderElementsForShadows, m_transparentRenderElementsForShadows); } else { renderObject.GetRenderElementsForShadowmap(MyLodTypeEnum.LOD1, m_renderElementsForShadows, m_transparentRenderElementsForShadows); } } c++; } MyRender.GetRenderProfiler().EndProfilingBlock(); //Sorting VBs to minimize VB switches m_renderElementsForShadows.Sort(m_shadowElementsComparer); lightCamera.CastingRenderElements = m_renderElementsForShadows; MyPerformanceCounter.PerCameraDrawWrite.RenderElementsInShadows += m_renderElementsForShadows.Count; frustumIndex++; } MyRender.GetRenderProfiler().EndProfilingBlock(); }