Example #1
0
        private void ExecuteTechniqueDistortAndComputeDistance(ShadowCasterMap shadowCasterMap, LightSource light, RenderTarget2D destination, string techniqueName, Camera gameCamera)
        {
            graphicsDevice.SetRenderTarget(destination);
            graphicsDevice.Clear(Color.White);

            this.lightsFX.ResolveShadowsEffect.Parameters["lightRelativeZero"].SetValue(light.RelativeZeroHLSL(shadowCasterMap, gameCamera));

            Vector2 shadowCasterMapPortion = (light.Size * shadowCasterMap.PrecisionRatio) / shadowCasterMap.Size;
            this.lightsFX.ResolveShadowsEffect.Parameters["shadowCasterMapPortion"].SetValue(shadowCasterMapPortion);

            this.lightsFX.ResolveShadowsEffect.Parameters["InputTexture"].SetValue(shadowCasterMap.Map);

            this.lightsFX.ResolveShadowsEffect.CurrentTechnique = this.lightsFX.ResolveShadowsEffect.Techniques[techniqueName];

            this.lightsFX.ResolveShadowsEffect.CurrentTechnique.Passes[0].Apply();
            ShadowMapResolver.QuadRender.Render(this.graphicsDevice, Vector2.One * -1, Vector2.One);

            graphicsDevice.SetRenderTarget(null);
        }
Example #2
0
        private static void CreateCompounds(Map map, TileLayer terrainLayer, TileLayer wallLayer, TileLayer roofLayer, List<Compound> compounds, float[][] noise, float height, float distance, int minsize, LightingEngine lightingEngine, GraphicsDevice gd)
        {
            for (int y = 40; y < map.Width - 40; y++)
            {
                for (int x = 40; x < map.Height - 40; x++)
                {
                    if (noise[y][x] > height)
                    {
                        Vector2 thisLoc = new Vector2(x * map.TileWidth, y * map.TileHeight);

                        bool tooClose = false;
                        foreach (Compound c in compounds)
                            if ((c.Position - thisLoc).Length() < distance) tooClose = true;

                        if (!tooClose)
                        {
                            Rectangle bounds = new Rectangle(x, y, 1, 1);
                            bounds.Inflate(minsize, minsize);
                            bounds.Inflate(rand.Next(5) * 2, rand.Next(5) * 2);

                            bool tooBig = true;
                            while (tooBig)
                            {
                                if (GetTileIndex(map, terrainLayer, bounds.Left, bounds.Top) != GRASS ||
                                    GetTileIndex(map, terrainLayer, bounds.Right, bounds.Top) != GRASS ||
                                    GetTileIndex(map, terrainLayer, bounds.Left, bounds.Bottom) != GRASS ||
                                    GetTileIndex(map, terrainLayer, bounds.Right, bounds.Bottom) != GRASS)
                                {
                                    tooBig = true;
                                    bounds.Inflate(-1, -1);
                                }
                                else tooBig = false;
                            }

                            if (bounds.Width >= minsize && bounds.Height >= minsize)
                            {
                                Compound newCompound = new Compound() { Position = thisLoc };

                                for (int xx = bounds.Left; xx <= bounds.Right; xx++)
                                {
                                    for (int yy = bounds.Top; yy <= bounds.Bottom; yy++)
                                    {
                                        wallLayer.Tiles[xx, yy] = null;
                                        if (xx > bounds.Left + 2 && xx < bounds.Right - 2 && yy > bounds.Top + 2 && yy < bounds.Bottom - 2)
                                            terrainLayer.Tiles[xx, yy] = map.Tiles[MUD];
                                    }
                                }

                                Rectangle innerBounds = bounds;
                                innerBounds.Inflate(-3, -3);

                                // Outer walls
                                wallLayer.Tiles[innerBounds.Left, innerBounds.Top] = map.Tiles[WALL_TL];
                                wallLayer.Tiles[innerBounds.Right, innerBounds.Top] = map.Tiles[WALL_TR];
                                wallLayer.Tiles[innerBounds.Left, innerBounds.Bottom] = map.Tiles[WALL_BL];
                                wallLayer.Tiles[innerBounds.Right, innerBounds.Bottom] = map.Tiles[WALL_BR];

                                for (int xx = innerBounds.Left + 1; xx <= innerBounds.Right - 1; xx++)
                                {
                                    wallLayer.Tiles[xx, innerBounds.Top] = map.Tiles[WALL_EDGE_UP];
                                    wallLayer.Tiles[xx, innerBounds.Bottom] = map.Tiles[WALL_EDGE_DOWN];
                                }
                                for (int yy = innerBounds.Top + 1; yy <= innerBounds.Bottom - 1; yy++)
                                {
                                    wallLayer.Tiles[innerBounds.Left, yy] = map.Tiles[WALL_EDGE_LEFT];
                                    wallLayer.Tiles[innerBounds.Right,yy] = map.Tiles[WALL_EDGE_RIGHT];
                                }

                                newCompound.Bounds = bounds;
                                newCompound.InnerBounds = innerBounds;

                                // Exits
                                bool[] exits = new bool[4] { false, false, false, false };
                                for(int i=0;i<4;i++)
                                    exits[rand.Next(4)] = true;

                                bool carparkPlaced = false;
                                Building carpark = null;

                                if (exits[0])
                                {
                                    int doorx = rand.Next(innerBounds.Width - 7) + 3;
                                    for (int xx = innerBounds.Left + doorx; xx < (innerBounds.Left + doorx) + 4; xx++) wallLayer.Tiles[xx, innerBounds.Top] = null;
                                    if (!carparkPlaced && rand.Next(2) == 0)
                                    {
                                        carpark = new Building() { Type = BuildingType.Carpark, Rect = new Rectangle((innerBounds.Left + doorx), innerBounds.Top + 2, 4, 4) };
                                        newCompound.Buildings.Add(carpark);
                                        carparkPlaced = true;
                                    }
                                }
                                if (exits[1])
                                {
                                    int doorx = rand.Next(innerBounds.Width - 7) + 3;
                                    for (int xx = innerBounds.Left + doorx; xx < (innerBounds.Left + doorx) + 4; xx++) wallLayer.Tiles[xx, innerBounds.Bottom] = null;
                                    if (!carparkPlaced && rand.Next(2) == 0)
                                    {
                                        carpark = new Building() { Type = BuildingType.Carpark, Rect = new Rectangle((innerBounds.Left + doorx), innerBounds.Bottom - 6, 4, 4) };
                                        newCompound.Buildings.Add(carpark);
                                        carparkPlaced = true;
                                    }
                                }
                                if (exits[2])
                                {
                                    int doory = rand.Next(innerBounds.Height - 7) + 3;
                                    for (int yy = innerBounds.Top + doory; yy < (innerBounds.Top + doory) + 4; yy++) wallLayer.Tiles[innerBounds.Left,yy] = null;
                                    if (!carparkPlaced && rand.Next(2) == 0)
                                    {
                                        carpark = new Building() { Type = BuildingType.Carpark, Rect = new Rectangle(innerBounds.Left + 2, (innerBounds.Top + doory), 4, 4) };
                                        newCompound.Buildings.Add(carpark);
                                        carparkPlaced = true;
                                    }
                                }
                                if (exits[3])
                                {
                                    int doory = rand.Next(innerBounds.Height - 7) + 3;
                                    for (int yy = innerBounds.Top + doory; yy < (innerBounds.Top + doory) + 4; yy++) wallLayer.Tiles[innerBounds.Right, yy] = null;
                                    if (!carparkPlaced && rand.Next(2) == 0)
                                    {
                                        carpark = new Building() { Type = BuildingType.Carpark, Rect = new Rectangle(innerBounds.Right - 6, (innerBounds.Top + doory), 4, 4) };
                                        newCompound.Buildings.Add(carpark);
                                        carparkPlaced = true;
                                    }
                                }

                                int lightSpacing = 5;
                                for (int xx = innerBounds.Left + 4; xx <= innerBounds.Right - 4; xx++)
                                {
                                    lightSpacing++;
                                    if (lightSpacing == 6)
                                    {
                                        lightSpacing = 0;

                                        if (GetTileIndex(map, wallLayer, xx, innerBounds.Top) == WALL_EDGE_UP &&
                                            GetTileIndex(map, wallLayer, xx - 2, innerBounds.Top) == WALL_EDGE_UP &&
                                            GetTileIndex(map, wallLayer, xx + 2, innerBounds.Top) == WALL_EDGE_UP)
                                        {
                                            LightSource ls = new LightSource(gd, 300, LightAreaQuality.Low, Color.White, BeamStencilType.None, SpotStencilType.Half);
                                            ls.Rotation = MathHelper.PiOver2;
                                            ls.Position = new Vector2((xx * map.TileWidth) + (map.TileWidth / 2), ((innerBounds.Top + 1) * map.TileHeight));
                                            lightingEngine.LightSources.Add(ls);
                                        }

                                        if (GetTileIndex(map, wallLayer, xx, innerBounds.Bottom) == WALL_EDGE_DOWN &&
                                            GetTileIndex(map, wallLayer, xx - 2, innerBounds.Bottom) == WALL_EDGE_DOWN &&
                                            GetTileIndex(map, wallLayer, xx + 2, innerBounds.Bottom) == WALL_EDGE_DOWN)
                                        {
                                            LightSource ls = new LightSource(gd, 300, LightAreaQuality.Low, Color.White, BeamStencilType.None, SpotStencilType.Half);
                                            ls.Rotation = MathHelper.PiOver2 + MathHelper.Pi;
                                            ls.Position = new Vector2((xx * map.TileWidth) + (map.TileWidth / 2), ((innerBounds.Bottom) * map.TileHeight));
                                            lightingEngine.LightSources.Add(ls);
                                        }
                                    }
                                }
                                lightSpacing = 5;
                                for (int yy = innerBounds.Top + 4; yy <= innerBounds.Bottom - 4; yy++)
                                {
                                    lightSpacing++;
                                    if (lightSpacing == 6)
                                    {
                                        lightSpacing = 0;

                                        if (GetTileIndex(map, wallLayer, innerBounds.Left, yy) == WALL_EDGE_LEFT &&
                                            GetTileIndex(map, wallLayer, innerBounds.Left, yy - 2) == WALL_EDGE_LEFT &&
                                            GetTileIndex(map, wallLayer, innerBounds.Left, yy + 2) == WALL_EDGE_LEFT)
                                        {
                                            LightSource ls = new LightSource(gd, 300, LightAreaQuality.Low, Color.White, BeamStencilType.None, SpotStencilType.Half);
                                            //ls.Rotation = MathHelper.PiOver2;
                                            ls.Position = new Vector2(((innerBounds.Left+1) * map.TileWidth), (yy * map.TileHeight)+(map.TileHeight/2));
                                            lightingEngine.LightSources.Add(ls);
                                        }

                                        if (GetTileIndex(map, wallLayer, innerBounds.Right, yy) == WALL_EDGE_RIGHT &&
                                           GetTileIndex(map, wallLayer, innerBounds.Right, yy - 2) == WALL_EDGE_RIGHT &&
                                           GetTileIndex(map, wallLayer, innerBounds.Right, yy + 2) == WALL_EDGE_RIGHT)
                                        {
                                            LightSource ls = new LightSource(gd, 300, LightAreaQuality.Low, Color.White, BeamStencilType.None, SpotStencilType.Half);
                                            ls.Rotation = MathHelper.Pi;
                                            ls.Position = new Vector2(((innerBounds.Right) * map.TileWidth), (yy * map.TileHeight) + (map.TileHeight / 2));
                                            lightingEngine.LightSources.Add(ls);
                                        }
                                    }
                                }

                                //if (carpark!=null)
                                //    for (int xx = carpark.Rect.Left; xx < carpark.Rect.Right; xx++)
                                //        for (int yy = carpark.Rect.Top; yy < carpark.Rect.Bottom; yy++)
                                //             terrainLayer.Tiles[xx, yy] = map.Tiles[CARPARK];

                                MakeBuildings(map, wallLayer, terrainLayer, roofLayer, newCompound);

                                compounds.Add(newCompound);
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        public void ResolveShadows(ShadowCasterMap shadowCasterMap, LightSource resultLight, RenderTarget2D printRT, PostEffect postEffect, float distanceMod, Camera gameCamera)
        {
            BlendState backupBlendState = graphicsDevice.BlendState;
            graphicsDevice.BlendState = BlendState.Opaque;

            this.ExecuteTechniqueDistortAndComputeDistance(shadowCasterMap, resultLight, shadowMapDistorted, "DistortAndComputeDistances", gameCamera);

            // Horizontal reduction
            this.ApplyHorizontalReduction(shadowMapDistorted, this.shadowMapDigested);

            this.distanceMod = distanceMod;

            switch (postEffect)
            {
                case PostEffect.LinearAttenuation:
                    {
                        this.ExecuteTechniqueDrawShadows(printRT, "DrawShadowsLinearAttenuation", this.shadowMapDigested);
                    }
                    break;

                case PostEffect.LinearAttenuation_BlurLow:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyLow");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyLowLinearAttenuation");
                    }
                    break;

                case PostEffect.LinearAttenuation_BlurMid:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyMid");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyMidLinearAttenuation");
                    }
                    break;

                case PostEffect.LinearAttenuation_BlurHigh:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyHigh");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyHighLinearAttenuation");
                    }
                    break;

                case PostEffect.CurveAttenuation:
                    {
                        this.ExecuteTechniqueDrawShadows(printRT, "DrawShadowsCurveAttenuation", this.shadowMapDigested);
                    }
                    break;

                case PostEffect.CurveAttenuation_BlurLow:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyLow");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyLowCurveAttenuation");
                    }
                    break;

                case PostEffect.CurveAttenuation_BlurMid:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyMid");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyMidCurveAttenuation");
                    }
                    break;

                case PostEffect.CurveAttenuation_BlurHigh:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyHigh");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyHighCurveAttenuation");
                    }
                    break;

                case PostEffect.Only_BlurLow:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyLow");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyLowNoAttenuation");
                    }
                    break;

                case PostEffect.Only_BlurMid:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyMid");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyMidNoAttenuation");
                    }
                    break;

                case PostEffect.Only_BlurHigh:
                    {
                        this.ExecuteTechniqueDrawShadows(shadowsRT, "DrawShadowsNoAttenuationPreBlur", this.shadowMapDigested);
                        this.ExecuteTechniqueBlurH(shadowsRT, processedShadowsRT, "BlurHorizontallyHigh");
                        this.ExecuteTechniqueBlurV(processedShadowsRT, printRT, "BlurVerticallyHighNoAttenuation");
                    }
                    break;

                default: //NOFX
                    {
                        this.ExecuteTechniqueDrawShadows(printRT, "DrawShadowsNoAttenuation", this.shadowMapDigested);
                    }
                    break;
            }
            graphicsDevice.BlendState = backupBlendState;
        }
Example #4
0
 public void ResolveShadows(ShadowCasterMap shadowCasterMap, LightSource resultLight, RenderTarget2D printRT, PostEffect postEffect, Camera gameCamera)
 {
     this.ResolveShadows(shadowCasterMap, resultLight, printRT, postEffect, 1f, gameCamera);
 }
Example #5
0
 public void ResolveShadows(ShadowCasterMap shadowCasterMap, LightSource resultLight, RenderTarget2D printRT, PostEffect postEffect, float distanceMod, Vector2 newPosition, Camera gameCamera)
 {
     resultLight.Position = newPosition;
     this.ResolveShadows(shadowCasterMap, resultLight, printRT, postEffect, distanceMod, gameCamera);
 }
Example #6
0
 internal void Initialize(GraphicsDevice gd, LightingEngine le)
 {
     HeadTorch = new LightSource(gd, 500, LightAreaQuality.High, Color.White, BeamStencilType.Narrow, SpotStencilType.None);
     le.LightSources.Add(HeadTorch);
     base.Initialize();
 }
Example #7
0
 public void RemoveSource(LightSource ls)
 {
     LightSources.Remove(ls);
     ls = null;
 }