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;
            }
        }
Example #2
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);
        }
        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;
        }
        public LightingQuery(LightingEnvironment environment, bool parallelCreate)
        {
            Environment = environment;

            Update(parallelCreate);
        }
Example #5
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
                )
            };
        }