/// <summary> /// Sets the data of one of the <see cref="Texture2DArray"/>'s images from an <see cref="Image{Rgba32}"/>. /// </summary> /// <param name="texture">The <see cref="Texture2DArray"/> to set data for.</param> /// <param name="depthLevel">The array layer to set the data for.</param> /// <param name="image">The image with the pixel data to set.</param> public static void SetData(this Texture2DArray texture, int depthLevel, Image <Rgba32> image) { if (texture == null) { throw new ArgumentNullException(nameof(texture)); } if (image == null) { throw new ArgumentNullException(nameof(image)); } if (texture.ImageFormat != TextureImageFormat.Color4b) { throw new InvalidOperationException(nameof(TextureCubemap.ImageFormat) + " must be " + nameof(TextureImageFormat.Color4b) + " in order to do this"); } if (image.Width != texture.Width || image.Height != texture.Height) { throw new InvalidOperationException("The size of the image must match the size of the " + nameof(Texture2DArray)); } if (!image.TryGetSinglePixelSpan(out Span <Rgba32> pixels)) { throw new InvalidDataException(ImageUtils.ImageNotContiguousError); } texture.SetData <Rgba32>(pixels, depthLevel, PixelFormat.Rgba); }
public SceneControl(ScreenComponent manager, string style = "") : base(manager, style) { player = manager.Player; camera = manager.Camera; assets = manager.Game.Assets; Manager = manager; simpleShader = manager.Game.Content.Load<Effect>("simple"); sunTexture = assets.LoadTexture(typeof(ScreenComponent), "sun"); //List<Bitmap> bitmaps = new List<Bitmap>(); var definitions = DefinitionManager.Instance.GetBlockDefinitions(); int textureCount = 0; foreach (var definition in definitions) { textureCount += definition.Textures.Length; } int bitmapSize = 128; blockTextures = new Texture2DArray(manager.GraphicsDevice, 1, bitmapSize, bitmapSize, textureCount); int layer = 0; foreach (var definition in definitions) { foreach (var bitmap in definition.Textures) { System.Drawing.Bitmap texture = manager.Game.Assets.LoadBitmap(definition.GetType(), bitmap); var scaled = texture;//new Bitmap(bitmap, new System.Drawing.Size(bitmapSize, bitmapSize)); int[] data = new int[scaled.Width * scaled.Height]; var bitmapData = scaled.LockBits(new System.Drawing.Rectangle(0, 0, scaled.Width, scaled.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, data, 0, data.Length); blockTextures.SetData(data, layer); scaled.UnlockBits(bitmapData); layer++; } } /*int size = (int)Math.Ceiling(Math.Sqrt(bitmaps.Count)); Bitmap blocks = new Bitmap(size * TEXTURESIZE, size * TEXTURESIZE); using (Graphics g = Graphics.FromImage(blocks)) { int counter = 0; foreach (var bitmap in bitmaps) { int x = counter % size; int y = (int)(counter / size); g.DrawImage(bitmap, new System.Drawing.Rectangle(TEXTURESIZE * x, TEXTURESIZE * y, TEXTURESIZE, TEXTURESIZE)); counter++; } } using (MemoryStream stream = new MemoryStream()) { blocks.Save(stream, ImageFormat.Png); stream.Seek(0, SeekOrigin.Begin); blockTextures = Texture2D.FromStream(manager.GraphicsDevice, stream); }*/ planet = ResourceManager.Instance.GetPlanet(0); // TODO: evtl. Cache-Size (Dimensions) VIEWRANGE + 1 int range = ((int)Math.Pow(2, VIEWRANGE) - 2) / 2; localChunkCache = new LocalChunkCache(ResourceManager.Instance.GlobalChunkCache, VIEWRANGE, range); chunkRenderer = new ChunkRenderer[ (int)Math.Pow(2, VIEWRANGE) * (int)Math.Pow(2, VIEWRANGE), planet.Size.Z]; orderedChunkRenderer = new List<ChunkRenderer>( (int)Math.Pow(2, VIEWRANGE) * (int)Math.Pow(2, VIEWRANGE) * planet.Size.Z); for (int i = 0; i < chunkRenderer.GetLength(0); i++) { for (int j = 0; j < chunkRenderer.GetLength(1); j++) { ChunkRenderer renderer = new ChunkRenderer(simpleShader, manager.GraphicsDevice, camera.Projection, blockTextures); chunkRenderer[i, j] = renderer; orderedChunkRenderer.Add(renderer); } } backgroundThread = new Thread(BackgroundLoop); backgroundThread.Priority = ThreadPriority.Lowest; backgroundThread.IsBackground = true; backgroundThread.Start(); var selectionVertices = new[] { new VertexPositionColor(new Vector3(-0.001f, +1.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, +1.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, -0.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, -0.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, +1.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, +1.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, -0.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, -0.001f, -0.001f), Color.Black * 0.5f), }; var billboardVertices = new[] { new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 1)), new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 1)), }; var selectionIndices = new short[] { 0, 1, 0, 2, 1, 3, 2, 3, 4, 5, 4, 6, 5, 7, 6, 7, 0, 4, 1, 5, 2, 6, 3, 7 }; selectionLines = new VertexBuffer(manager.GraphicsDevice, VertexPositionColor.VertexDeclaration, selectionVertices.Length); selectionLines.SetData(selectionVertices); selectionIndexBuffer = new IndexBuffer(manager.GraphicsDevice, DrawElementsType.UnsignedShort, selectionIndices.Length); selectionIndexBuffer.SetData(selectionIndices); billboardVertexbuffer = new VertexBuffer(manager.GraphicsDevice, VertexPositionTexture.VertexDeclaration, billboardVertices.Length); billboardVertexbuffer.SetData(billboardVertices); sunEffect = new BasicEffect(manager.GraphicsDevice); sunEffect.TextureEnabled = true; selectionEffect = new BasicEffect(manager.GraphicsDevice); selectionEffect.VertexColorEnabled = true; MiniMapTexture = new RenderTarget2D(manager.GraphicsDevice, 128, 128, PixelInternalFormat.Rgb8); // , false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.PreserveContents); miniMapProjectionMatrix = Matrix.CreateOrthographic(128, 128, 1, 10000); }
public SceneControl(ScreenComponent manager, string style = "") : base(manager, style) { Mask = (int)Math.Pow(2, VIEWRANGE) - 1; Span = (int)Math.Pow(2, VIEWRANGE); SpanOver2 = Span >> 1; player = manager.Player; camera = manager.Camera; assets = manager.Game.Assets; entities = manager.Game.Entity; Manager = manager; simpleShader = manager.Game.Content.Load <Effect>("simple"); sunTexture = assets.LoadTexture(typeof(ScreenComponent), "sun"); //List<Bitmap> bitmaps = new List<Bitmap>(); var definitions = Manager.Game.DefinitionManager.GetBlockDefinitions(); int textureCount = 0; foreach (var definition in definitions) { textureCount += definition.Textures.Length; } int bitmapSize = 128; blockTextures = new Texture2DArray(manager.GraphicsDevice, 1, bitmapSize, bitmapSize, textureCount); int layer = 0; foreach (var definition in definitions) { foreach (var bitmap in definition.Textures) { System.Drawing.Bitmap texture = manager.Game.Assets.LoadBitmap(definition.GetType(), bitmap); var scaled = texture;//new Bitmap(bitmap, new System.Drawing.Size(bitmapSize, bitmapSize)); int[] data = new int[scaled.Width * scaled.Height]; var bitmapData = scaled.LockBits(new System.Drawing.Rectangle(0, 0, scaled.Width, scaled.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, data, 0, data.Length); blockTextures.SetData(data, layer); scaled.UnlockBits(bitmapData); layer++; } } planet = Manager.Game.ResourceManager.GetPlanet(player.Position.Position.Planet); // TODO: evtl. Cache-Size (Dimensions) VIEWRANGE + 1 int range = ((int)Math.Pow(2, VIEWRANGE) - 2) / 2; localChunkCache = new LocalChunkCache(planet.GlobalChunkCache, VIEWRANGE, range); chunkRenderer = new ChunkRenderer[ (int)Math.Pow(2, VIEWRANGE) * (int)Math.Pow(2, VIEWRANGE), planet.Size.Z]; orderedChunkRenderer = new List <ChunkRenderer>( (int)Math.Pow(2, VIEWRANGE) * (int)Math.Pow(2, VIEWRANGE) * planet.Size.Z); for (int i = 0; i < chunkRenderer.GetLength(0); i++) { for (int j = 0; j < chunkRenderer.GetLength(1); j++) { ChunkRenderer renderer = new ChunkRenderer(this, Manager.Game.DefinitionManager, simpleShader, manager.GraphicsDevice, camera.Projection, blockTextures); chunkRenderer[i, j] = renderer; orderedChunkRenderer.Add(renderer); } } backgroundThread = new Thread(BackgroundLoop) { Priority = ThreadPriority.Lowest, IsBackground = true }; backgroundThread.Start(); backgroundThread2 = new Thread(ForceUpdateBackgroundLoop) { Priority = ThreadPriority.Lowest, IsBackground = true }; backgroundThread2.Start(); var additional = Environment.ProcessorCount / 3; additional = additional == 0 ? 1 : additional; _fillIncrement = additional + 1; additionalFillResetEvents = new AutoResetEvent[additional]; _additionalRegenerationThreads = new Thread[additional]; for (int i = 0; i < additional; i++) { var t = new Thread(AdditionalFillerBackgroundLoop) { Priority = ThreadPriority.Lowest, IsBackground = true }; var are = new AutoResetEvent(false); t.Start(new object[] { are, i }); additionalFillResetEvents[i] = are; _additionalRegenerationThreads[i] = t; } var selectionVertices = new[] { new VertexPositionColor(new Vector3(-0.001f, +1.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, +1.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, -0.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, -0.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, +1.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, +1.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, -0.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, -0.001f, -0.001f), Color.Black * 0.5f), }; var billboardVertices = new[] { new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 1)), new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 1)), }; var selectionIndices = new short[] { 0, 1, 0, 2, 1, 3, 2, 3, 4, 5, 4, 6, 5, 7, 6, 7, 0, 4, 1, 5, 2, 6, 3, 7 }; selectionLines = new VertexBuffer(manager.GraphicsDevice, VertexPositionColor.VertexDeclaration, selectionVertices.Length); selectionLines.SetData(selectionVertices); selectionIndexBuffer = new IndexBuffer(manager.GraphicsDevice, DrawElementsType.UnsignedShort, selectionIndices.Length); selectionIndexBuffer.SetData(selectionIndices); billboardVertexbuffer = new VertexBuffer(manager.GraphicsDevice, VertexPositionTexture.VertexDeclaration, billboardVertices.Length); billboardVertexbuffer.SetData(billboardVertices); sunEffect = new BasicEffect(manager.GraphicsDevice) { TextureEnabled = true }; selectionEffect = new BasicEffect(manager.GraphicsDevice) { VertexColorEnabled = true }; MiniMapTexture = new RenderTarget2D(manager.GraphicsDevice, 128, 128, PixelInternalFormat.Rgb8); // , false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.PreserveContents); miniMapProjectionMatrix = Matrix.CreateOrthographic(128, 128, 1, 10000); }
public SceneControl(ScreenComponent manager, string style = "") : base(manager, style) { player = manager.Player; camera = manager.Camera; assets = manager.Game.Assets; Manager = manager; simpleShader = manager.Game.Content.Load <Effect>("simple"); sunTexture = assets.LoadTexture(typeof(ScreenComponent), "sun"); //List<Bitmap> bitmaps = new List<Bitmap>(); var definitions = DefinitionManager.Instance.GetBlockDefinitions(); int textureCount = 0; foreach (var definition in definitions) { textureCount += definition.Textures.Length; } int bitmapSize = 128; blockTextures = new Texture2DArray(manager.GraphicsDevice, 1, bitmapSize, bitmapSize, textureCount); int layer = 0; foreach (var definition in definitions) { foreach (var bitmap in definition.Textures) { System.Drawing.Bitmap texture = manager.Game.Assets.LoadBitmap(definition.GetType(), bitmap); var scaled = texture;//new Bitmap(bitmap, new System.Drawing.Size(bitmapSize, bitmapSize)); int[] data = new int[scaled.Width * scaled.Height]; var bitmapData = scaled.LockBits(new System.Drawing.Rectangle(0, 0, scaled.Width, scaled.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, data, 0, data.Length); blockTextures.SetData(data, layer); scaled.UnlockBits(bitmapData); layer++; } } /*int size = (int)Math.Ceiling(Math.Sqrt(bitmaps.Count)); * Bitmap blocks = new Bitmap(size * TEXTURESIZE, size * TEXTURESIZE); * using (Graphics g = Graphics.FromImage(blocks)) * { * int counter = 0; * foreach (var bitmap in bitmaps) * { * int x = counter % size; * int y = (int)(counter / size); * g.DrawImage(bitmap, new System.Drawing.Rectangle(TEXTURESIZE * x, TEXTURESIZE * y, TEXTURESIZE, TEXTURESIZE)); * counter++; * } * } * * using (MemoryStream stream = new MemoryStream()) * { * blocks.Save(stream, ImageFormat.Png); * stream.Seek(0, SeekOrigin.Begin); * blockTextures = Texture2D.FromStream(manager.GraphicsDevice, stream); * }*/ planet = ResourceManager.Instance.GetPlanet(0); // TODO: evtl. Cache-Size (Dimensions) VIEWRANGE + 1 int range = ((int)Math.Pow(2, VIEWRANGE) - 2) / 2; localChunkCache = new LocalChunkCache(ResourceManager.Instance.GlobalChunkCache, VIEWRANGE, range); chunkRenderer = new ChunkRenderer[ (int)Math.Pow(2, VIEWRANGE) * (int)Math.Pow(2, VIEWRANGE), planet.Size.Z]; orderedChunkRenderer = new List <ChunkRenderer>( (int)Math.Pow(2, VIEWRANGE) * (int)Math.Pow(2, VIEWRANGE) * planet.Size.Z); for (int i = 0; i < chunkRenderer.GetLength(0); i++) { for (int j = 0; j < chunkRenderer.GetLength(1); j++) { ChunkRenderer renderer = new ChunkRenderer(simpleShader, manager.GraphicsDevice, camera.Projection, blockTextures); chunkRenderer[i, j] = renderer; orderedChunkRenderer.Add(renderer); } } backgroundThread = new Thread(BackgroundLoop); backgroundThread.Priority = ThreadPriority.Lowest; backgroundThread.IsBackground = true; backgroundThread.Start(); var selectionVertices = new[] { new VertexPositionColor(new Vector3(-0.001f, +1.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, +1.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, -0.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, -0.001f, +1.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, +1.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, +1.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(-0.001f, -0.001f, -0.001f), Color.Black * 0.5f), new VertexPositionColor(new Vector3(+1.001f, -0.001f, -0.001f), Color.Black * 0.5f), }; var billboardVertices = new[] { new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 1)), new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 1)), }; var selectionIndices = new short[] { 0, 1, 0, 2, 1, 3, 2, 3, 4, 5, 4, 6, 5, 7, 6, 7, 0, 4, 1, 5, 2, 6, 3, 7 }; selectionLines = new VertexBuffer(manager.GraphicsDevice, VertexPositionColor.VertexDeclaration, selectionVertices.Length); selectionLines.SetData(selectionVertices); selectionIndexBuffer = new IndexBuffer(manager.GraphicsDevice, DrawElementsType.UnsignedShort, selectionIndices.Length); selectionIndexBuffer.SetData(selectionIndices); billboardVertexbuffer = new VertexBuffer(manager.GraphicsDevice, VertexPositionTexture.VertexDeclaration, billboardVertices.Length); billboardVertexbuffer.SetData(billboardVertices); sunEffect = new BasicEffect(manager.GraphicsDevice); sunEffect.TextureEnabled = true; selectionEffect = new BasicEffect(manager.GraphicsDevice); selectionEffect.VertexColorEnabled = true; MiniMapTexture = new RenderTarget2D(manager.GraphicsDevice, 128, 128, PixelInternalFormat.Rgb8); // , false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.PreserveContents); miniMapProjectionMatrix = Matrix.CreateOrthographic(128, 128, 1, 10000); }