private List <Texture2D> Create3DBlockIcons(DeviceContext context, ShaderResourceView cubesTexture, int iconSize) { List <Texture2D> createdIconsTexture = new List <Texture2D>(); SpriteRenderer spriteRenderer = new SpriteRenderer(_d3DEngine, Path.Combine(ClientSettings.PathRoot, @"Effects\Sprites\Sprites2.hlsl")); //Get the "Block" mesh that will be used to draw the various blocks. IMeshFactory meshfactory = new MilkShape3DMeshFactory(); Mesh meshBluePrint; int textureSize = iconSize; meshfactory.LoadMesh(ClientSettings.PathRoot + @"\Meshes\block.txt", out meshBluePrint, 0); //Create Vertex/Index Buffer to store the loaded mesh. VertexBuffer <VertexMesh> vb = new VertexBuffer <VertexMesh>(_d3DEngine.Device, meshBluePrint.Vertices.Length, SharpDX.Direct3D.PrimitiveTopology.TriangleList, "Block VB"); IndexBuffer <ushort> ib = new IndexBuffer <ushort>(_d3DEngine.Device, meshBluePrint.Indices.Length, "Block IB"); //Create the render texture RenderedTexture2D texture = ToDispose(new RenderedTexture2D(_d3DEngine, textureSize, textureSize, SharpDX.DXGI.Format.R8G8B8A8_UNorm) { BackGroundColor = new Color4(0, 0, 0, 0) }); Texture2DDescription SpriteTextureDesc = new Texture2DDescription() { Width = 1, Height = 1, MipLevels = 1, ArraySize = 1, Format = Format.R32G32B32A32_Float, SampleDescription = new SampleDescription() { Count = 1 }, Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write }; Texture2D sTexture = new Texture2D(_d3DEngine.Device, SpriteTextureDesc); DataStream dataStream; DataBox data = context.MapSubresource(sTexture, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out dataStream); dataStream.Position = 0; dataStream.Write <Vector4>(new Vector4(1.0f, 1.0f, 1.0f, 1.0f)); //Ecrire dans la texture dataStream.Position = 0; context.UnmapSubresource(sTexture, 0); dataStream.Dispose(); SpriteTexture spriteTexture = new SpriteTexture(sTexture); spriteTexture.ScreenPosition = new Rectangle(spriteTexture.ScreenPosition.X, spriteTexture.ScreenPosition.Y, textureSize, textureSize); sTexture.Dispose(); //Create the Shadder used to render on the texture. HLSLIcons shader = new HLSLIcons(_d3DEngine.Device, ClientSettings.EffectPack + @"Entities/Icons.hlsl", VertexMesh.VertexDeclaration); //Compute projection + View matrix float aspectRatio = textureSize / textureSize; Matrix projection; Matrix.PerspectiveFovLH((float)Math.PI / 3.6f, aspectRatio, 0.5f, 100f, out projection); Matrix view = Matrix.LookAtLH(new Vector3(0, 0, -1.9f), Vector3.Zero, Vector3.UnitY); Matrix WorldScale; Dictionary <int, int> MaterialChangeMapping = new Dictionary <int, int>(); MaterialChangeMapping.Add(0, 0); //Change the Back Texture Id MaterialChangeMapping.Add(1, 0); //Change the Front Texture Id MaterialChangeMapping.Add(2, 0); //Change the Bottom Texture Id MaterialChangeMapping.Add(3, 0); //Change the Top Texture Id MaterialChangeMapping.Add(4, 0); //Change the Left Texture Id MaterialChangeMapping.Add(5, 0); //Change the Right Texture Id _cubeIconIndexes = new Dictionary <byte, byte>(); //Create a texture for each cubes existing ! foreach (BlockProfile profile in _visualWorldParameters.WorldParameters.Configuration.GetAllCubesProfiles()) { //Don't create "Air" cube if (profile.Id == WorldConfiguration.CubeId.Air) { continue; } //Create the new Material MeshMapping //Here the key parameter is the ID name given to the texture inside the file model. //In our case the model loaded has these Materials/texture Ids : // 0 = Back // 1 = Front // 2 = Bottom // 3 = Top // 4 = Left // 5 = Right //The value attached to it is simply the TextureID from the texture array to use. MaterialChangeMapping[0] = profile.Textures == null ? 0 : profile.Tex_Back.TextureArrayId; //Change the Back Texture Id MaterialChangeMapping[1] = profile.Textures == null ? 0 : profile.Tex_Front.TextureArrayId; //Change the Front Texture Id MaterialChangeMapping[2] = profile.Textures == null ? 0 : profile.Tex_Bottom.TextureArrayId; //Change the Bottom Texture Id MaterialChangeMapping[3] = profile.Textures == null ? 0 : profile.Tex_Top.TextureArrayId; //Change the Top Texture Id MaterialChangeMapping[4] = profile.Textures == null ? 0 : profile.Tex_Left.TextureArrayId; //Change the Left Texture Id MaterialChangeMapping[5] = profile.Textures == null ? 0 : profile.Tex_Right.TextureArrayId; //Change the Right Texture Id Mesh mesh = meshBluePrint.Clone(MaterialChangeMapping); //Stored the mesh data inside the buffers vb.SetData(context, mesh.Vertices); ib.SetData(context, mesh.Indices); //Begin Drawing texture.Begin(context); RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Enabled, DXStates.DepthStencils.DepthDisabled); //Set sampler shader.SamplerDiffuse.Value = RenderStatesRepo.GetSamplerState(DXStates.Samplers.UVWrap_MinMagMipLinear); shader.Begin(context); shader.CBPerFrame.Values.DiffuseLightDirection = new Vector3(-0.8f, -0.9f, 1.5f) * -1; shader.CBPerFrame.Values.View = Matrix.Transpose(view); shader.CBPerFrame.Values.Projection = Matrix.Transpose(projection); shader.CBPerFrame.IsDirty = true; if (profile.YBlockOffset > 0) { WorldScale = Matrix.Scaling(1, (float)(1.0f - profile.YBlockOffset), 1); } else { WorldScale = Matrix.Identity; } shader.CBPerDraw.Values.World = Matrix.Transpose(WorldScale * Matrix.RotationY(MathHelper.PiOver4) * Matrix.RotationX(-MathHelper.Pi / 5)); shader.CBPerDraw.IsDirty = true; shader.DiffuseTexture.Value = cubesTexture; shader.Apply(context); //Set the buffer to the device vb.SetToDevice(context, 0); ib.SetToDevice(context, 0); //Draw things here. context.DrawIndexed(ib.IndicesCount, 0, 0); //Draw a sprite for lighting block if (profile.IsEmissiveColorLightSource) { spriteRenderer.Begin(true, context); ByteColor color = new ByteColor(profile.EmissiveColor.R, profile.EmissiveColor.G, profile.EmissiveColor.B, (byte)127); spriteRenderer.Draw(spriteTexture, ref spriteTexture.ScreenPosition, ref color); spriteRenderer.EndWithCustomProjection(context, ref texture.Projection2D); } //End Drawing texture.End(context, false); var tex2d = texture.CloneTexture(context, ResourceUsage.Default); //Create Shadow around Icon object displayed if (!profile.IsEmissiveColorLightSource) { tex2d = DrawOuterShadow(context, texture, tex2d); } createdIconsTexture.Add(tex2d); _cubeIconIndexes.Add(profile.Id, (byte)(createdIconsTexture.Count - 1)); } //Reset device Default render target _d3DEngine.SetRenderTargetsAndViewPort(context); //Dispose temp resource. spriteTexture.Dispose(); shader.Dispose(); vb.Dispose(); ib.Dispose(); spriteRenderer.Dispose(); return(createdIconsTexture); }
private Texture2D DrawOuterShadow(DeviceContext context, RenderedTexture2D texture, Texture2D tex2D, int size = 0) { if (size == 0) { size = IconSize; } var clampSampler = ToDispose(new SamplerState(context.Device, new SamplerStateDescription { AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, Filter = Filter.MinMagMipLinear, MaximumLod = float.MaxValue, MinimumLod = 0 })); SpriteRenderer spriteRenderer = new SpriteRenderer(_d3DEngine, Path.Combine(ClientSettings.PathRoot, @"Effects\Sprites\Sprites2.hlsl")); texture.Begin(context); var voxelIconSpriteTexture = new SpriteTexture(tex2D); // draw black overlay RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Enabled, DXStates.DepthStencils.DepthDisabled); _overlayEffect.Begin(context); _overlayEffect.SpriteTexture.Value = voxelIconSpriteTexture.Texture; _overlayEffect.SpriteSampler.Value = RenderStatesRepo.GetSamplerState(DXStates.Samplers.UVWrap_MinMagMipLinear); _overlayEffect.CBPerDraw.Values.Color = new Color4(0.0f, 0.0f, 0.0f, 1.0f); _overlayEffect.CBPerDraw.IsDirty = true; _overlayEffect.Apply(context); _blurVertexBuffer.SetToDevice(context, 0); _iBuffer.SetToDevice(context, 0); context.DrawIndexed(6, 0, 0); texture.End(context, false); var tex2DOverlayed = texture.CloneTexture(context, ResourceUsage.Default); // blur horisontal texture.Begin(context); _blurHorisontalEffect.Begin(context); _blurHorisontalEffect.SpriteTexture.Value = new SpriteTexture(tex2DOverlayed).Texture; _blurHorisontalEffect.SpriteSampler.Value = clampSampler; _blurHorisontalEffect.CBPerDraw.Values.Size = size; _blurHorisontalEffect.CBPerDraw.IsDirty = true; _blurHorisontalEffect.Apply(context); context.DrawIndexed(6, 0, 0); texture.End(context, false); var tex2DHorisontalBlurred = texture.CloneTexture(context, ResourceUsage.Default); // blur vertical texture.Begin(context); _blurVerticalEffect.Begin(context); _blurVerticalEffect.SpriteTexture.Value = new SpriteTexture(tex2DHorisontalBlurred).Texture; _blurVerticalEffect.SpriteSampler.Value = clampSampler; _blurVerticalEffect.CBPerDraw.Values.Size = size; _blurVerticalEffect.CBPerDraw.IsDirty = true; _blurVerticalEffect.Apply(context); context.DrawIndexed(6, 0, 0); texture.End(context, false); var tex2DBlurred = texture.CloneTexture(context, ResourceUsage.Default); //Resource.ToFile(context, tex2DBlurred, ImageFileFormat.Png, visualVoxelModel.VoxelModel.Name + "-blur.png"); texture.Begin(context); spriteRenderer.Begin(false, context); ByteColor color = new ByteColor(255, 255, 255, 255); var spriteTexture = new SpriteTexture(tex2DBlurred); spriteRenderer.Draw(spriteTexture, ref spriteTexture.ScreenPosition, ref color); spriteRenderer.Draw(voxelIconSpriteTexture, ref voxelIconSpriteTexture.ScreenPosition, ref color); spriteRenderer.EndWithCustomProjection(context, ref texture.Projection2D); texture.End(context, false); return(texture.CloneTexture(context, ResourceUsage.Default)); }