private void GenerateBackground(Random rng, int width, int height) { const int noiseSize = 256; var starTexture = GeneratePointStarTexture(rng, width, height); var noiseTexture = GenerateNoise(rng, noiseSize); if (Nebulae != null && Nebulae.Length > 0) { _material = new Material(Shader, ColorRgba.White, starTexture); using (var renderTarget = new RenderTarget(AAQuality.Off, starTexture)) using (var device = new DrawDevice()) { device.Perspective = PerspectiveMode.Flat; device.VisibilityMask = VisibilityFlag.AllGroups | VisibilityFlag.ScreenOverlay; device.RenderMode = RenderMatrix.OrthoScreen; device.Target = renderTarget; device.ViewportRect = new Rect(renderTarget.Width, renderTarget.Height); device.PrepareForDrawcalls(); var canvas = new Canvas(device); canvas.State.SetMaterial(_material); foreach (var n in Nebulae.Select(n => n.Res).Where(n => n != null)) { _material.SetTexture("source", starTexture); _material.SetTexture("noise", starTexture); _material.SetUniform("color", 255f / n.Color.R, 255f / n.Color.G, 255f / n.Color.B); _material.SetUniform("offset", n.Offset.X, n.Offset.Y); _material.SetUniform("scale", n.Scale); _material.SetUniform("density", n.Density); _material.SetUniform("falloff", n.Falloff); _material.SetUniform("tNoiseSize", noiseSize); canvas.DrawRect(0, 0, width, height); device.Render(ClearFlag.None, ColorRgba.TransparentBlack, 1.0f); } } } }