public override void Draw(DeviceContext context, int index) { if (index == _smDrawID) { //Draw the DephtBuffer texture _shadowMap.DrawDepthBuffer(context, ref depthBufferDrawSize); } else { CreateLightViewProjectionMatrix(out LightViewProjection); var m = Matrix.Translation(_camManager.ActiveCamera.WorldPosition.ValueInterp.AsVector3()); m.Invert(); _shadowMap.Begin(); // draw players DynamicEntityManager.VoxelDraw(context, m * LightViewProjection); // draw chunks Matrix worldFocus = Matrix.Identity; foreach (var chunk in WorldChunks.Chunks.Where(x => x.Graphics.IsExistingMesh4Drawing)) { _worldFocusManager.CenterTranslationMatrixOnFocus(ref chunk.World, ref worldFocus); _landScapeShadowMapEffect.Begin(context); _landScapeShadowMapEffect.CBPerDraw.Values.LightWVP = Matrix.Transpose(worldFocus * LightViewProjection); _landScapeShadowMapEffect.CBPerDraw.IsDirty = true; _landScapeShadowMapEffect.Apply(context); chunk.Graphics.DrawSolidFaces(context); } //WorldChunks.PrepareVoxelDraw(context, m * LightViewProjection); _entitiesShadowMapEffect.Begin(context); _entitiesShadowMapEffect.CBPerDraw.Values.LightWVP = Matrix.Transpose(m * LightViewProjection); _entitiesShadowMapEffect.CBPerDraw.IsDirty = true; _entitiesShadowMapEffect.Apply(context); foreach (var chunk in WorldChunks.Chunks.Where(x => x.Graphics.IsExistingMesh4Drawing && x.DistanceFromPlayer <= WorldChunks.StaticEntityViewRange)) { WorldChunks.DrawStaticEntitiesShadow(context, chunk); } _shadowMap.End(); _d3dEngine.SetRenderTargetsAndViewPort(context); #if DEBUG if (!_debugSMTextureNeedToBeSaved) { //Texture2D.ToFile(context, _shadowMap.DepthMap.Resource, ImageFileFormat.Dds, @"E:\1.dds"); _debugSMTextureNeedToBeSaved = true; } #endif } }
public Texture2D CreateVoxelIcon(VisualVoxelModel visualVoxelModel, Size2 iconSize, VoxelModelState state = null, DeviceContext context = null, Matrix transform = default(Matrix)) { if (context == null) { context = _d3DEngine.ImmediateContext; } //Create the render texture var texture = ToDispose(new RenderedTexture2D(_d3DEngine, iconSize.Width, iconSize.Height, Format.R8G8B8A8_UNorm) { BackGroundColor = new Color4(0, 0, 0, 0) }); float aspectRatio = IconSize / IconSize; Matrix projection; var fov = (float)Math.PI / 3.6f; Matrix.PerspectiveFovLH(fov, aspectRatio, 0.5f, 100f, out projection); Matrix view = Matrix.LookAtLH(new Vector3(0, 0, -1.9f), Vector3.Zero, Vector3.UnitY); texture.Begin(context); RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Enabled, DXStates.DepthStencils.DepthReadWriteEnabled); _voxelEffect.Begin(context); _voxelEffect.CBPerFrame.Values.LightDirection = Vector3.Zero; _voxelEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(view * projection); _voxelEffect.CBPerFrame.IsDirty = true; var instance = visualVoxelModel.VoxelModel.CreateInstance(); if (state == null) { var iconState = visualVoxelModel.VoxelModel.States.FirstOrDefault(s => string.Equals(s.Name, "Icon", StringComparison.CurrentCultureIgnoreCase)); state = iconState ?? visualVoxelModel.VoxelModel.GetMainState(); } instance.SetState(state); var sphere = BoundingSphere.FromBox(state.BoundingBox); var rMax = 2f * Math.Sin(fov / 2); var size = state.BoundingBox.GetSize(); var offset = -size / 2 - state.BoundingBox.Minimum; var scale = (float)rMax / sphere.Radius; // Math.Min(scaleFactor / size.X, Math.Min(scaleFactor / size.Y, scaleFactor / size.Z)); if (transform == default(Matrix)) { instance.World = Matrix.Translation(offset) * Matrix.Scaling(scale) * Matrix.RotationY(MathHelper.Pi + MathHelper.PiOver4) * Matrix.RotationX(-MathHelper.Pi / 5); } else { instance.World = transform; } visualVoxelModel.Draw(context, _voxelEffect, instance); texture.End(context, false); var tex2D = texture.CloneTexture(context, ResourceUsage.Default); tex2D = DrawOuterShadow(context, texture, tex2D, iconSize.Width); _d3DEngine.SetRenderTargetsAndViewPort(context); return(tex2D); }