Beispiel #1
0
        public ImperativeRenderer(
            IBatchContainer container,
            DefaultMaterialSet materials,
            int layer = 0, 
            RasterizerState rasterizerState = null,
            DepthStencilState depthStencilState = null,
            BlendState blendState = null,
            SamplerState samplerState = null,
            bool worldSpace = true,
            bool useZBuffer = false,
            bool autoIncrementSortKey = false,
            bool autoIncrementLayer = false
        )
        {
            if (container == null)
                throw new ArgumentNullException("container");
            if (materials == null)
                throw new ArgumentNullException("materials");

            Container = container;
            Materials = materials;
            Layer = layer;
            RasterizerState = rasterizerState;
            DepthStencilState = depthStencilState;
            BlendState = blendState;
            SamplerState = samplerState;
            UseZBuffer = useZBuffer;
            WorldSpace = worldSpace;
            AutoIncrementSortKey = autoIncrementSortKey;
            AutoIncrementLayer = autoIncrementLayer;
            NextSortKey = 0;
            PreviousBatch = null;
        }
Beispiel #2
0
        /// <summary>
        /// Draw everything in the level from background to foreground.
        /// </summary>
        public void Draw(GameTime gameTime, Frame frame, DefaultMaterialSet materials)
        {
            BitmapBatch bb;

            for (int i = 0; i <= EntityLayer; ++i)
            {
                using (bb = BitmapBatch.New(frame, i, materials.ScreenSpaceBitmap))
                    bb.Add(new BitmapDrawCall(layers[i], Vector2.Zero));
            }

            using (bb = BitmapBatch.New(frame, EntityLayer + 1, materials.ScreenSpaceBitmap))
                DrawTiles(bb);

            using (var entityBatch = BitmapBatch.New(frame, EntityLayer + 1, materials.ScreenSpaceBitmap)) {
                foreach (Gem gem in gems)
                {
                    gem.Draw(gameTime, entityBatch);
                }

                Player.Draw(gameTime, entityBatch);

                foreach (Enemy enemy in enemies)
                {
                    enemy.Draw(gameTime, entityBatch);
                }
            }

            for (int i = EntityLayer + 1; i < layers.Length; ++i)
            {
                using (bb = BitmapBatch.New(frame, i + 1, materials.ScreenSpaceBitmap))
                    bb.Add(new BitmapDrawCall(layers[i], Vector2.Zero));
            }
        }
Beispiel #3
0
        protected override void LoadContent()
        {
            base.LoadContent();

            Rt = new RenderTarget2D(Graphics.GraphicsDevice, 512, 512);

            var provider = new EmbeddedFreeTypeFontProvider(RenderCoordinator);

            Font             = provider.Load("FiraSans-Medium");
            Font.SizePoints  = 20;
            Font.GlyphMargin = 4;

            Texture = Texture2D.FromStream(Graphics.GraphicsDevice, Assembly.GetExecutingAssembly().GetManifestResourceStream("bunny"));

            try {
                Materials = new DefaultMaterialSet(RenderCoordinator)
                {
                    ViewTransform = ViewTransform.CreateOrthographic(Graphics.PreferredBackBufferWidth, Graphics.PreferredBackBufferHeight)
                };
            } catch (Exception exc) {
                Console.WriteLine("Shader load failed.");
                Console.WriteLine(exc);
                Thread.Sleep(5000);
                Environment.Exit(1);
                return;
            }

            EffectProvider = new EmbeddedEffectProvider(RenderCoordinator);
            Vpos           = new Material(EffectProvider.Load("vpos"), "vposShader");
            Materials.Add(Vpos);
        }
Beispiel #4
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Load fonts
            hudFont = Content.Load <SpriteFont>("Fonts/Hud");

            // Load overlay textures
            winOverlay  = Content.Load <Texture2D>("Overlays/you_win");
            loseOverlay = Content.Load <Texture2D>("Overlays/you_lose");
            diedOverlay = Content.Load <Texture2D>("Overlays/you_died");

            // Load materials used by threaded renderer
            materials = new DefaultMaterialSet(Services)
            {
                ProjectionMatrix = Matrix.CreateOrthographicOffCenter(
                    0.0f, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1
                    )
            };

            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(Content.Load <Song>("Sounds/Music"));

            LoadNextLevel();
        }
Beispiel #5
0
        public override void LoadContent()
        {
            LightmapMaterials = new DefaultMaterialSet(Game.Services);

            // Since the spiral is very detailed
            LightingEnvironment.DefaultSubdivision = 128f;

            Environment = new LightingEnvironment();

            Renderer = new LightingRenderer(Game.Content, Game.RenderCoordinator, LightmapMaterials, Environment);

            var light = new LightSource {
                Position  = new Vector2(64, 64),
                Color     = new Vector4(1f, 1f, 1f, 1),
                RampStart = 50,
                RampEnd   = 275,
            };

            Lights.Add(light);
            Environment.LightSources.Add(light);

            var rng = new Random(1234);

            for (var i = 0; i < 25; i++)
            {
                light = new LightSource {
                    Position  = new Vector2(64, 64),
                    Color     = new Vector4((float)rng.NextDouble(0.1f, 1.0f), (float)rng.NextDouble(0.1f, 1.0f), (float)rng.NextDouble(0.1f, 1.0f), 1.0f),
                    RampStart = rng.NextFloat(24, 40),
                    RampEnd   = rng.NextFloat(140, 160),
                    RampMode  = LightSourceRampMode.Exponential
                };

                Lights.Add(light);
                Environment.LightSources.Add(light);
            }

            const int spiralCount = 1800;
            float     spiralRadius = 0, spiralRadiusStep = 330f / spiralCount;
            float     spiralAngle = 0, spiralAngleStep = (float)(Math.PI / (spiralCount / 36f));
            Vector2   previous = default(Vector2);

            for (int i = 0; i < spiralCount; i++, spiralAngle += spiralAngleStep, spiralRadius += spiralRadiusStep)
            {
                var current = new Vector2(
                    (float)(Math.Cos(spiralAngle) * spiralRadius) + (Width / 2f),
                    (float)(Math.Sin(spiralAngle) * spiralRadius) + (Height / 2f)
                    );

                if (i > 0)
                {
                    Environment.Obstructions.Add(new LightObstructionLine(
                                                     previous, current
                                                     ));
                }

                previous = current;
            }
        }
        internal IlluminantMaterials(DefaultMaterialSet materialSet)
        {
            MaterialSet = materialSet;

            EffectsToSetGammaCompressionParametersOn = new Effect[2];
            EffectsToSetToneMappingParametersOn      = new Effect[2];
            EffectsToSetRampTextureOn = new Effect[2];
        }
        public override void LoadContent () {
            LightmapMaterials = new DefaultMaterialSet(Game.Services);

            // Since the spiral is very detailed
            LightingEnvironment.DefaultSubdivision = 128f;

            Environment = new LightingEnvironment();

            Renderer = new LightingRenderer(Game.Content, Game.RenderCoordinator, LightmapMaterials, Environment);

            var light = new LightSource {
                Position = new Vector2(64, 64),
                Color = new Vector4(1f, 1f, 1f, 1),
                RampStart = 50,
                RampEnd = 275,
            };

            Lights.Add(light);
            Environment.LightSources.Add(light);

            var rng = new Random(1234);
            for (var i = 0; i < 25; i++) {
                light = new LightSource {
                    Position = new Vector2(64, 64),
                    Color = new Vector4((float)rng.NextDouble(0.1f, 1.0f), (float)rng.NextDouble(0.1f, 1.0f), (float)rng.NextDouble(0.1f, 1.0f), 1.0f),
                    RampStart = rng.NextFloat(24, 40),
                    RampEnd = rng.NextFloat(140, 160),
                    RampMode = LightSourceRampMode.Exponential
                };

                Lights.Add(light);
                Environment.LightSources.Add(light);
            }

            const int spiralCount = 1800;
            float spiralRadius = 0, spiralRadiusStep = 330f / spiralCount;
            float spiralAngle = 0, spiralAngleStep = (float)(Math.PI / (spiralCount / 36f));
            Vector2 previous = default(Vector2);

            for (int i = 0; i < spiralCount; i++, spiralAngle += spiralAngleStep, spiralRadius += spiralRadiusStep) {
                var current = new Vector2(
                    (float)(Math.Cos(spiralAngle) * spiralRadius) + (Width / 2f),
                    (float)(Math.Sin(spiralAngle) * spiralRadius) + (Height / 2f)
                );

                if (i > 0) {
                    Environment.Obstructions.Add(new LightObstructionLine(
                        previous, current
                    ));
                }

                previous = current;
            }
        }
Beispiel #8
0
        protected override void Initialize()
        {
            base.Initialize();

            Materials = new DefaultMaterialSet(RenderCoordinator);

            Alignment.Pressed += (s, e) => {
                Text.Alignment = (HorizontalAlignment)(((int)Text.Alignment + 1) % 7);
            };
            CharacterWrap.Pressed += (s, e) => {
                Text.CharacterWrap = !Text.CharacterWrap;
            };
            WordWrap.Pressed += (s, e) => {
                Text.WordWrap = !Text.WordWrap;
            };
            Hinting.Pressed += (s, e) => {
                var ftf = (FreeTypeFont)LatinFont;
                ftf.Hinting = !ftf.Hinting;
                ftf.Invalidate();
                ftf         = (FreeTypeFont)UniFont;
                ftf.Hinting = !ftf.Hinting;
                ftf.Invalidate();
                Text.Invalidate();
            };
            Margin.Pressed += (s, e) => {
                var ftf = (FreeTypeFont)LatinFont;
                ftf.GlyphMargin = (ftf.GlyphMargin + 1) % 6;
                ftf.Invalidate();
                Text.Invalidate();
            };
            Monochrome.Pressed += (s, e) => {
                var ftf = (FreeTypeFont)LatinFont;
                ftf.Monochrome = !ftf.Monochrome;
                ftf.Invalidate();
                ftf            = (FreeTypeFont)UniFont;
                ftf.Monochrome = !ftf.Monochrome;
                ftf.Invalidate();
                Text.Invalidate();
            };
            FreeType.Pressed += (s, e) => {
                if (ActiveFont == FallbackFont)
                {
                    ActiveFont = new SpriteFontGlyphSource(DutchAndHarley);
                    TextScale  = 1f;
                }
                else
                {
                    ActiveFont = FallbackFont;
                    TextScale  = 2f;
                }
                Text.GlyphSource = ActiveFont;
                Text.Scale       = TextScale;
            };
        }
Beispiel #9
0
        protected override void LoadContent()
        {
            // Load the default materials provided with the rendering library
            //  and provide a projection matrix
            Materials = new DefaultMaterialSet(Services)
            {
                ProjectionMatrix = Matrix.CreateOrthographicOffCenter(
                    0.0f, Width, Height, 0.0f, -1.0f, 1.0f
                    )
            };

            // Attach a blend state setter to the geometry material so that we get alpha blending
            Materials.WorldSpaceGeometry = Materials.WorldSpaceGeometry.SetStates(blendState: BlendState.AlphaBlend);
        }
Beispiel #10
0
        protected override void LoadContent () {
            base.LoadContent();

            ScreenMaterials = new DefaultMaterialSet(Services) {
                ViewportScale = new Vector2(1, 1),
                ViewportPosition = new Vector2(0, 0),
                ProjectionMatrix = Matrix.CreateOrthographicOffCenter(
                    0, GraphicsDevice.Viewport.Width,
                    GraphicsDevice.Viewport.Height, 0,
                    0, 1
                )
            };

            foreach (var scene in Scenes)
                scene.LoadContent();
        }
        public override void LoadContent()
        {
            LightmapMaterials = new DefaultMaterialSet(Game.Services);

            Environment = new LightingEnvironment();

            TestImage = Game.Content.Load <Texture2D>("ramp_test_image");

            RampTexture = Game.Content.Load <Texture2D>("LightGradients");

            Environment.LightSources.AddRange(new[] {
                new LightSource {
                    Position  = new Vector2(128, 128),
                    Color     = Vector4.One,
                    RampStart = 32,
                    RampEnd   = 128,
                    RampMode  = LightSourceRampMode.Linear
                },
                new LightSource {
                    Position  = new Vector2(400, 128),
                    Color     = Vector4.One,
                    RampStart = 32,
                    RampEnd   = 128,
                    RampMode  = LightSourceRampMode.Exponential
                },
                new LightSource {
                    Position    = new Vector2(128, 400),
                    Color       = Vector4.One,
                    RampStart   = 32,
                    RampEnd     = 128,
                    RampMode    = LightSourceRampMode.Linear,
                    RampTexture = RampTexture
                },
                new LightSource {
                    Position    = new Vector2(400, 400),
                    Color       = Vector4.One,
                    RampStart   = 32,
                    RampEnd     = 128,
                    RampMode    = LightSourceRampMode.Exponential,
                    RampTexture = RampTexture
                }
            });

            Renderer = new LightingRenderer(Game.Content, Game.RenderCoordinator, LightmapMaterials, Environment);
        }
Beispiel #12
0
        public override void LoadContent () {
            LightmapMaterials = new DefaultMaterialSet(Game.Services);

            Environment = new LightingEnvironment();

            TestImage = Game.Content.Load<Texture2D>("ramp_test_image");

            RampTexture = Game.Content.Load<Texture2D>("LightGradients");

            Environment.LightSources.AddRange(new[] {
                new LightSource {
                    Position = new Vector2(128, 128),
                    Color = Vector4.One,
                    RampStart = 32,
                    RampEnd = 128,
                    RampMode = LightSourceRampMode.Linear
                },
                new LightSource {
                    Position = new Vector2(400, 128),
                    Color = Vector4.One,
                    RampStart = 32,
                    RampEnd = 128,
                    RampMode = LightSourceRampMode.Exponential
                },
                new LightSource {
                    Position = new Vector2(128, 400),
                    Color = Vector4.One,
                    RampStart = 32,
                    RampEnd = 128,
                    RampMode = LightSourceRampMode.Linear,
                    RampTexture = RampTexture
                },
                new LightSource {
                    Position = new Vector2(400, 400),
                    Color = Vector4.One,
                    RampStart = 32,
                    RampEnd = 128,
                    RampMode = LightSourceRampMode.Exponential,
                    RampTexture = RampTexture
                }
            });

            Renderer = new LightingRenderer(Game.Content, Game.RenderCoordinator, LightmapMaterials, Environment);
        }
Beispiel #13
0
        protected override void Initialize()
        {
            Materials = new DefaultMaterialSet(Services);

            base.Initialize();

            Alignment.Pressed += (s, e) => {
                Text2.Alignment = Text.Alignment = (HorizontalAlignment)(((int)Text.Alignment + 1) % 3);
            };
            CharacterWrap.Pressed += (s, e) => {
                Text2.CharacterWrap = Text.CharacterWrap = !Text.CharacterWrap;
            };
            WordWrap.Pressed += (s, e) => {
                Text2.WordWrap = Text.WordWrap = !Text.WordWrap;
            };
            FreeType.Pressed += (s, e) => {
                if (Text.GlyphSource == SpriteFont)
                {
                    Text2.GlyphSource = Text.GlyphSource = FallbackFont;
                }
                else
                {
                    Text2.GlyphSource = Text.GlyphSource = SpriteFont;
                }
            };
            Hinting.Pressed += (s, e) => {
                var ftf = (FreeTypeFont)LatinFont;
                ftf.Hinting = !ftf.Hinting;
                ftf.Invalidate();
                ftf         = (FreeTypeFont)UniFont;
                ftf.Hinting = !ftf.Hinting;
                ftf.Invalidate();
                Text.Invalidate();
                Text2.Invalidate();
            };
            Margin.Pressed += (s, e) => {
                var ftf = (FreeTypeFont)LatinFont;
                ftf.GlyphMargin = (ftf.GlyphMargin + 1) % 6;
                ftf.Invalidate();
                Text.Invalidate();
                Text2.Invalidate();
            };
        }
Beispiel #14
0
        public ImperativeRenderer(
            IBatchContainer container,
            DefaultMaterialSet materials,
            int layer = 0,
            RasterizerState rasterizerState     = null,
            DepthStencilState depthStencilState = null,
            BlendState blendState                     = null,
            SamplerState samplerState                 = null,
            bool worldSpace                           = true,
            bool useZBuffer                           = false,
            bool autoIncrementSortKey                 = false,
            bool autoIncrementLayer                   = false,
            bool lowPriorityMaterialOrdering          = false,
            Sorter <BitmapDrawCall> declarativeSorter = null,
            Tags tags = default(Tags)
            )
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (materials == null)
            {
                throw new ArgumentNullException("materials");
            }

            Container                   = container;
            Materials                   = materials;
            Layer                       = layer;
            RasterizerState             = rasterizerState;
            DepthStencilState           = depthStencilState;
            BlendState                  = blendState;
            SamplerState                = samplerState;
            UseZBuffer                  = useZBuffer;
            WorldSpace                  = worldSpace;
            AutoIncrementSortKey        = autoIncrementSortKey;
            AutoIncrementLayer          = autoIncrementLayer;
            NextSortKey                 = new DrawCallSortKey(tags, 0);
            Cache                       = new CachedBatches();
            LowPriorityMaterialOrdering = lowPriorityMaterialOrdering;
            DeclarativeSorter           = declarativeSorter;
        }
        protected override void LoadContent()
        {
            base.LoadContent();

            ScreenMaterials = new DefaultMaterialSet(Services)
            {
                ViewportScale    = new Vector2(1, 1),
                ViewportPosition = new Vector2(0, 0),
                ProjectionMatrix = Matrix.CreateOrthographicOffCenter(
                    0, GraphicsDevice.Viewport.Width,
                    GraphicsDevice.Viewport.Height, 0,
                    0, 1
                    )
            };

            foreach (var scene in Scenes)
            {
                scene.LoadContent();
            }
        }
Beispiel #16
0
        public ImperativeRenderer(
            IBatchContainer container,
            DefaultMaterialSet materials,
            int layer = 0,
            RasterizerState rasterizerState     = null,
            DepthStencilState depthStencilState = null,
            BlendState blendState     = null,
            SamplerState samplerState = null,
            bool worldSpace           = true,
            bool useZBuffer           = false,
            bool autoIncrementSortKey = false,
            bool autoIncrementLayer   = false
            )
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (materials == null)
            {
                throw new ArgumentNullException("materials");
            }

            Container            = container;
            Materials            = materials;
            Layer                = layer;
            RasterizerState      = rasterizerState;
            DepthStencilState    = depthStencilState;
            BlendState           = blendState;
            SamplerState         = samplerState;
            UseZBuffer           = useZBuffer;
            WorldSpace           = worldSpace;
            AutoIncrementSortKey = autoIncrementSortKey;
            AutoIncrementLayer   = autoIncrementLayer;
            NextSortKey          = 0;
            Cache                = new CachedBatches();
        }
Beispiel #17
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent () {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Load fonts
            hudFont = Content.Load<SpriteFont>("Fonts/Hud");

            // Load overlay textures
            winOverlay = Content.Load<Texture2D>("Overlays/you_win");
            loseOverlay = Content.Load<Texture2D>("Overlays/you_lose");
            diedOverlay = Content.Load<Texture2D>("Overlays/you_died");

            // Load materials used by threaded renderer
            materials = new DefaultMaterialSet(Content) {
                ProjectionMatrix = Matrix.CreateOrthographicOffCenter(
                    0.0f, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1
                )
            };

            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(Content.Load<Song>("Sounds/Music"));

            LoadNextLevel();
        }
 public ParticleRenderer (DefaultMaterialSet materials) {
     Materials = materials;
     Systems = new IParticleSystem[0];
 }
        protected override void Initialize()
        {
            Materials = new DefaultMaterialSet(Services);

            base.Initialize();
        }
        /// <summary>
        /// Synchronously renders a complete frame to the specified render target.
        /// Automatically sets up the device's viewport and the view transform of your materials and restores them afterwards.
        /// </summary>
        public void SynchronousDrawToRenderTarget(RenderTarget2D renderTarget, DefaultMaterialSet materials, Action<Frame> drawBehavior)
        {
            using (var frame = Manager.CreateFrame()) {
                lock (UseResourceLock) {
                    materials.PushViewTransform(ViewTransform.CreateOrthographic(renderTarget.Width, renderTarget.Height));

                    ClearBatch.AddNew(frame, int.MinValue, materials.Clear, clearColor: Color.Transparent);

                    drawBehavior(frame);

                    PrepareFrame(frame);

                    var oldRenderTargets = Device.GetRenderTargets();
                    var oldViewport = Device.Viewport;
                    try {
                        Device.SetRenderTarget(renderTarget);
                        Device.Viewport = new Viewport(0, 0, renderTarget.Width, renderTarget.Height);

                        RenderFrame(frame, false);
                    } finally {
                        Device.SetRenderTargets(oldRenderTargets);
                        materials.PopViewTransform();
                        Device.Viewport = oldViewport;
                    }
                }
            }
        }
Beispiel #21
0
        public override void LoadContent()
        {
            LightmapMaterials = new DefaultMaterialSet(Game.Services);

            LightingEnvironment.DefaultSubdivision = 512f;

            BackgroundEnvironment = new LightingEnvironment();
            ForegroundEnvironment = new LightingEnvironment();

            BackgroundRenderer = new LightingRenderer(Game.Content, LightmapMaterials, BackgroundEnvironment);
            ForegroundRenderer = new LightingRenderer(Game.Content, LightmapMaterials, ForegroundEnvironment);

            // Add a global sun
            AddAmbientLight(746, -300);

            // Add clipped suns for areas with weird shadowing behavior
            AddAmbientLight(746, 200, new Bounds(
                new Vector2(38, 33),
                new Vector2(678, 678)
            ));

            AddAmbientLight(740, 240, new Bounds(
                new Vector2(805, 34),
                new Vector2(1257, 546)
            ));

            AddAmbientLight(741, 750, new Bounds(
                new Vector2(0, 674),
                new Vector2(1257, 941)
            ));

            AddAmbientLight(741, 1025, new Bounds(
                new Vector2(0, 941),
                new Vector2(1257, 1250)
            ));

            AddTorch(102, 132);
            AddTorch(869, 132);
            AddTorch(102, 646);
            AddTorch(869, 645);

            GenerateObstructionsFromTiles();

            Layers[0] = Game.Content.Load<Texture2D>("layers_bg");
            Layers[1] = Game.Content.Load<Texture2D>("layers_fg");
            Layers[2] = Game.Content.Load<Texture2D>("layers_chars");
            Layers[3] = Game.Content.Load<Texture2D>("layers_torches");

            BricksLightMask = Game.Content.Load<Texture2D>("layers_bricks_lightmask");

            AdditiveBitmapMaterial = LightmapMaterials.ScreenSpaceBitmap.SetStates(blendState: BlendState.Additive);

            MaskedForegroundMaterial = LightmapMaterials.ScreenSpaceLightmappedBitmap.SetStates(blendState: BlendState.Additive);

            AOShadowMaterial = Game.ScreenMaterials.ScreenSpaceVerticalGaussianBlur5Tap.SetStates(blendState: RenderStates.SubtractiveBlend);

            ParticleRenderer = new ParticleRenderer(LightmapMaterials) {
                Viewport = new Bounds(Vector2.Zero, new Vector2(Width, Height))
            };

            Spark.Texture = Game.Content.Load<Texture2D>("spark");

            ParticleRenderer.Systems = new[] {
                Sparks = new ParticleSystem<Spark>(
                    new DotNetTimeProvider(),
                    BackgroundEnvironment
                )
            };
        }
Beispiel #22
0
        public override void LoadContent()
        {
            LightmapMaterials = new DefaultMaterialSet(Game.Services);

            LightingEnvironment.DefaultSubdivision = 512f;

            BackgroundEnvironment = new LightingEnvironment();
            ForegroundEnvironment = new LightingEnvironment();

            BackgroundRenderer = new LightingRenderer(Game.Content, Game.RenderCoordinator, LightmapMaterials, BackgroundEnvironment);
            ForegroundRenderer = new LightingRenderer(Game.Content, Game.RenderCoordinator, LightmapMaterials, ForegroundEnvironment);

            // Add a global sun
            AddAmbientLight(746, -300);

            // Add clipped suns for areas with weird shadowing behavior
            AddAmbientLight(746, 200, new Bounds(
                                new Vector2(38, 33),
                                new Vector2(678, 678)
                                ));

            AddAmbientLight(740, 240, new Bounds(
                                new Vector2(805, 34),
                                new Vector2(1257, 546)
                                ));

            AddAmbientLight(741, 750, new Bounds(
                                new Vector2(0, 674),
                                new Vector2(1257, 941)
                                ));

            AddAmbientLight(741, 1025, new Bounds(
                                new Vector2(0, 941),
                                new Vector2(1257, 1250)
                                ));

            GenerateObstructionsFromTiles();

            Layers[0] = Game.Content.Load <Texture2D>("layers_bg");
            Layers[1] = Game.Content.Load <Texture2D>("layers_fg");
            Layers[2] = Game.Content.Load <Texture2D>("layers_chars");
            Layers[3] = Game.Content.Load <Texture2D>("layers_torches");

            MaskedForegroundMaterial = LightmapMaterials.ScreenSpaceLightmappedBitmap.SetStates(blendState: BlendState.Additive);

            ParticleRenderer = new ParticleRenderer(LightmapMaterials)
            {
                Viewport = new Bounds(Vector2.Zero, new Vector2(Width, Height))
            };

            Spark.Texture = Game.Content.Load <Texture2D>("spark");

            ParticleRenderer.Systems = new[] {
                Sparks = new ParticleSystem <Spark>(
                    new DotNetTimeProvider(),
                    BackgroundEnvironment
                    )
            };

            SparkLights = new ParticleLightManager <Spark>(
                Sparks,
                new[] { BackgroundEnvironment, ForegroundEnvironment },
                UpdateSectorLight
                );
        }
Beispiel #23
0
        protected override void Initialize()
        {
            base.Initialize();

            Materials = new DefaultMaterialSet(RenderCoordinator);
        }
        protected override void Initialize()
        {
            Materials = new DefaultMaterialSet(Services);

            base.Initialize();
        }
Beispiel #25
0
        protected override void LoadContent()
        {
            // Load the default materials provided with the rendering library
            //  and provide a projection matrix
            Materials = new DefaultMaterialSet(Services) {
                ProjectionMatrix = Matrix.CreateOrthographicOffCenter(
                    0.0f, Width, Height, 0.0f, -1.0f, 1.0f
                )
            };

            // Attach a blend state setter to the geometry material so that we get alpha blending
            Materials.WorldSpaceGeometry = Materials.WorldSpaceGeometry.SetStates(blendState: BlendState.AlphaBlend);
        }
        /// <summary>
        /// Synchronously renders a complete frame to the specified render target.
        /// Automatically sets up the device's viewport and the view transform of your materials and restores them afterwards.
        /// </summary>
        public bool SynchronousDrawToRenderTarget(RenderTarget2D renderTarget, DefaultMaterialSet materials, Action<Frame> drawBehavior)
        {
            if (renderTarget.IsDisposed)
                return false;
            if (!SynchronousDrawsEnabled)
                throw new InvalidOperationException("Synchronous draws not available inside of Game.Draw");

            WaitForActiveDraw();

            var oldDrawIsActive = Interlocked.Exchange(ref _SynchronousDrawIsActive, 1);
            if (oldDrawIsActive != 0)
                throw new InvalidOperationException("A synchronous draw is already in progress");

            _SynchronousDrawFinishedSignal.Reset();

            WaitForActiveDraw();

            try {
                using (var frame = Manager.CreateFrame()) {
                    materials.PushViewTransform(ViewTransform.CreateOrthographic(renderTarget.Width, renderTarget.Height));

                    ClearBatch.AddNew(frame, int.MinValue, materials.Clear, clearColor: Color.Transparent);

                    drawBehavior(frame);

                    PrepareNextFrame(frame, false);

                    var oldRenderTargets = Device.GetRenderTargets();
                    var oldViewport = Device.Viewport;
                    try {
                        Device.SetRenderTarget(renderTarget);
                        RenderManager.ResetDeviceState(Device);
                        Device.Viewport = new Viewport(0, 0, renderTarget.Width, renderTarget.Height);

                        RenderFrameToDraw(false);
                    } finally {
                        Device.SetRenderTargets(oldRenderTargets);
                        materials.PopViewTransform();
                        Device.Viewport = oldViewport;
                    }
                }

                return true;
            } finally {
                _SynchronousDrawFinishedSignal.Set();
                Interlocked.Exchange(ref _SynchronousDrawIsActive, 0);
            }
        }
Beispiel #27
0
        protected override void LoadContent () {
            // Load the default materials provided with the rendering library
            //  and provide a projection matrix
            Materials = new DefaultMaterialSet(Content) {
                ProjectionMatrix = Matrix.CreateOrthographicOffCenter(
                    0.0f, Width, Height, 0.0f, -1.0f, 1.0f
                )
            };

            // Attach a blend state setter to the geometry material so that we get alpha blending
            Materials.WorldSpaceGeometry = new DelegateMaterial(
                Materials.WorldSpaceGeometry, 
                new Action<DeviceManager>[] { 
                    Material.MakeDelegate(BlendState.AlphaBlend)
                },
                new Action<DeviceManager>[0]
            );
        }
        public LightingRenderer(ContentManager content, RenderCoordinator coordinator, DefaultMaterialSet materials, LightingEnvironment environment)
        {
            Materials   = materials;
            Coordinator = coordinator;

            IlluminantMaterials = new IlluminantMaterials(materials);

            StoreScissorRect       = _StoreScissorRect;
            RestoreScissorRect     = _RestoreScissorRect;
            ShadowBatchSetup       = _ShadowBatchSetup;
            IlluminationBatchSetup = _IlluminationBatchSetup;

            IlluminantMaterials.ClearStencil = materials.Get(
                materials.Clear,
                rasterizerState: RasterizerState.CullNone,
                depthStencilState: new DepthStencilState {
                StencilEnable    = true,
                StencilMask      = StencilTrue,
                StencilWriteMask = StencilTrue,
                ReferenceStencil = StencilFalse,
                StencilFunction  = CompareFunction.Always,
                StencilPass      = StencilOperation.Replace,
                StencilFail      = StencilOperation.Replace,
            },
                blendState: BlendState.Opaque
                );

            materials.Add(
                IlluminantMaterials.DebugOutlines = materials.WorldSpaceGeometry.SetStates(
                    blendState: BlendState.AlphaBlend
                    )
                );

            // If stencil == false, paint point light at this location
            PointLightStencil = new DepthStencilState {
                DepthBufferEnable = false,
                StencilEnable     = true,
                StencilMask       = StencilTrue,
                StencilWriteMask  = StencilFalse,
                StencilFunction   = CompareFunction.Equal,
                StencilPass       = StencilOperation.Keep,
                StencilFail       = StencilOperation.Keep,
                ReferenceStencil  = StencilFalse
            };

            {
                var dBegin = new[] {
                    MaterialUtil.MakeDelegate(
                        rasterizerState: RenderStates.ScissorOnly, depthStencilState: PointLightStencil
                        )
                };
                var dEnd = new[] {
                    MaterialUtil.MakeDelegate(
                        rasterizerState: RasterizerState.CullNone, depthStencilState: DepthStencilState.None
                        )
                };

#if SDL2
                materials.Add(IlluminantMaterials.PointLightExponential = new DelegateMaterial(
                                  PointLightMaterialsInner[0]           = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightExponential"), "PointLightExponential"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinear = new DelegateMaterial(
                                  PointLightMaterialsInner[1]      = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightLinear"), "PointLightLinear"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightExponentialRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[2] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightExponentialRampTexture"), "PointLightExponentialRampTexture"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinearRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[3] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightLinearRampTexture"), "PointLightLinearRampTexture"
                                      ), dBegin, dEnd
                                  ));
#else
                materials.Add(IlluminantMaterials.PointLightExponential = new DelegateMaterial(
                                  PointLightMaterialsInner[0]           = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightExponential"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinear = new DelegateMaterial(
                                  PointLightMaterialsInner[1]      = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightLinear"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightExponentialRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[2] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightExponentialRampTexture"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinearRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[3] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightLinearRampTexture"
                                      ), dBegin, dEnd
                                  ));
#endif
            }

            // If stencil == false: set stencil to true.
            // If stencil == true: leave stencil alone, don't paint this pixel
            ShadowStencil = new DepthStencilState {
                DepthBufferEnable = false,
                StencilEnable     = true,
                StencilMask       = StencilTrue,
                StencilWriteMask  = StencilTrue,
                StencilFunction   = CompareFunction.NotEqual,
                StencilPass       = StencilOperation.Replace,
                StencilFail       = StencilOperation.Keep,
                ReferenceStencil  = StencilTrue
            };

            materials.Add(IlluminantMaterials.Shadow = new DelegateMaterial(
                              ShadowMaterialInner    = new Squared.Render.EffectMaterial(
#if SDL2
                                  content.Load <Effect>("Shadow"), "Shadow"
#else
                                  content.Load <Effect>("Illumination"), "Shadow"
#endif
                                  ),
                              new[] {
                MaterialUtil.MakeDelegate(
                    rasterizerState: RenderStates.ScissorOnly,
                    depthStencilState: ShadowStencil,
                    blendState: RenderStates.DrawNone
                    )
            },
                              new[] {
                MaterialUtil.MakeDelegate(
                    rasterizerState: RasterizerState.CullNone, depthStencilState: DepthStencilState.None
                    )
            }
                              ));

#if SDL2
            materials.Add(IlluminantMaterials.ScreenSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("ScreenSpaceGammaCompressedBitmap"), "ScreenSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("WorldSpaceGammaCompressedBitmap"), "WorldSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("ScreenSpaceToneMappedBitmap"), "ScreenSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("WorldSpaceToneMappedBitmap"), "WorldSpaceToneMappedBitmap"
                              ));
#else
            materials.Add(IlluminantMaterials.ScreenSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "ScreenSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "WorldSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "ScreenSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "WorldSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceRampBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("RampBitmap"), "ScreenSpaceRampBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceRampBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("RampBitmap"), "WorldSpaceRampBitmap"
                              ));
#endif

            Environment = environment;

            // Reduce garbage created by BufferPool<>.Allocate when creating cached sectors
            BufferPool <ShadowVertex> .MaxBufferSize = 1024 * 16;
            BufferPool <short> .MaxBufferSize        = 1024 * 32;
        }
 internal void SetContainer(DefaultMaterialSet materials, IBatchContainer container, int layer)
 {
     Container          = container;
     ImperativeRenderer = new ImperativeRenderer(container, materials, layer);
 }
Beispiel #30
0
 public ParticleRenderer(DefaultMaterialSet materials)
 {
     Materials = materials;
     Systems   = new IParticleSystem[0];
 }
Beispiel #31
0
        /// <summary>
        /// Draw everything in the level from background to foreground.
        /// </summary>
        public void Draw(GameTime gameTime, Frame frame, DefaultMaterialSet materials)
        {
            BitmapBatch bb;

            for (int i = 0; i <= EntityLayer; ++i)
                using (bb = BitmapBatch.New(frame, i, materials.ScreenSpaceBitmap))
                    bb.Add(new BitmapDrawCall(layers[i], Vector2.Zero));

            using (bb = BitmapBatch.New(frame, EntityLayer + 1, materials.ScreenSpaceBitmap))
                DrawTiles(bb);

            using (var entityBatch = BitmapBatch.New(frame, EntityLayer + 1, materials.ScreenSpaceBitmap)) {
                foreach (Gem gem in gems)
                    gem.Draw(gameTime, entityBatch);

                Player.Draw(gameTime, entityBatch);

                foreach (Enemy enemy in enemies)
                    enemy.Draw(gameTime, entityBatch);
            }

            for (int i = EntityLayer + 1; i < layers.Length; ++i)
                using (bb = BitmapBatch.New(frame, i + 1, materials.ScreenSpaceBitmap))
                    bb.Add(new BitmapDrawCall(layers[i], Vector2.Zero));
        }