public override void Initialize() { //Create the Block mesh IMeshFactory meshfactory = new MilkShape3DMeshFactory(); meshfactory.LoadMesh(@"\Meshes\block.txt", out _meshBluePrint, 0); _staticBlockVB = ToDispose(new VertexBuffer <VertexMesh>(_engine.Device, _meshBluePrint.Vertices.Length, SharpDX.Direct3D.PrimitiveTopology.TriangleList, "rotatingBlockVB")); _staticBlockIB = ToDispose(new IndexBuffer <ushort>(_engine.Device, _meshBluePrint.Indices.Length, "rotatingBlockIB")); _cubeShader = ToDispose(new HLSLLoadingCube(_engine.Device, ClientSettings.EffectPack + @"Entities/LoadingCube.hlsl", VertexMesh.VertexDeclaration)); RotationCube cube; //Create Cubes 1 cube = new RotationCube { ID = 0, Rotation = new Vector3(-MathHelper.Pi * 6 / 5, MathHelper.PiOver4, 0), SpinningRotation = new Vector3(0.01f, 0.02f, 0.0001f) }; _rotatingCubes.Add(cube); cube = new RotationCube { ID = 1, Rotation = new Vector3(-MathHelper.PiOver2 * 6 * 1.2f, MathHelper.PiOver2 * 0.125f, MathHelper.Pi * 1.23f), SpinningRotation = new Vector3(0.01f, 0.01f, 0.001f) }; _rotatingCubes.Add(cube); cube = new RotationCube { ID = 2, Rotation = new Vector3(-MathHelper.PiOver2 * 1.9f, MathHelper.PiOver2 * 0.6f, MathHelper.Pi * 1.3f), SpinningRotation = new Vector3(0.001f, 0.05f, 0.002f) }; _rotatingCubes.Add(cube); cube = new RotationCube { ID = 3, Rotation = new Vector3(-MathHelper.PiOver2 * 1.6f, MathHelper.PiOver2 * 0.9f, MathHelper.Pi * 1.9f), SpinningRotation = new Vector3(0.001f, 0.0004f, 0.03f) }; _rotatingCubes.Add(cube); Resize(_engine.ViewPort); }
public override void Initialize() { IMeshFactory meshfactory = new MilkShape3DMeshFactory(); meshfactory.LoadMesh(@"\Meshes\block.txt", out _meshBluePrint, 0); _staticBlockVB = ToDispose(new VertexBuffer <VertexMesh>(_engine.Device, _meshBluePrint.Vertices.Length, SharpDX.Direct3D.PrimitiveTopology.TriangleList, "rotatingBlockVB")); _staticBlockIB = ToDispose(new IndexBuffer <ushort>(_engine.Device, _meshBluePrint.Indices.Length, "rotatingBlockIB")); _cubeShader = ToDispose(new HLSLLoadingCube(_engine.Device, ClientSettings.EffectPack + @"Entities/LoadingCube.hlsl", VertexMesh.VertexDeclaration)); base.Initialize(); }
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); }