示例#1
0
        public ColorGradient(Color first, Color last, int numStops)
        {
            this.Stops = new List <ColorStop>();
            Vector4 color1 = new Vector4((float)first.R / 255.0f, (float)first.G / 255.0f, (float)first.B / 255.0f, (float)first.A / 255.0f);
            Vector4 color2 = new Vector4((float)last.R / 255.0f, (float)last.G / 255.0f, (float)last.B / 255.0f, (float)last.A / 255.0f);

            Vector4 norm   = color2 - color1;
            float   length = norm.Length();
            float   dStop  = length / numStops;

            norm.Normalize();

            float currentStop = 0.0f;

            for (int i = 0; i < numStops; i++)
            {
                ColorStop stop     = new ColorStop();
                Vector4   colorVec = color1 + currentStop * norm;
                stop.m_color = new Color(colorVec.X, colorVec.Y, colorVec.Y, colorVec.W);
                currentStop += dStop;
                Stops.Add(stop);
            }
        }
示例#2
0
        public static void TextureFromHeightMap(string displayMode,
                                                MapData[,] map,
                                                ScalarFieldType type,
                                                int width, int height,
                                                Mutex imageMutex,
                                                Color[] worldData,
                                                Texture2D worldMap, float sealevel)
        {
            if (JetGradient == null)
            {
                List <ColorStop> stops = new List <ColorStop>();
                ColorStop        first = new ColorStop
                {
                    m_color    = new Color(0, 255, 255),
                    m_position = 0.0f
                };

                ColorStop second = new ColorStop
                {
                    m_color    = new Color(0, 0, 255),
                    m_position = 0.2f
                };


                ColorStop third = new ColorStop
                {
                    m_color    = new Color(255, 255, 0),
                    m_position = 0.4f
                };

                ColorStop fourth = new ColorStop
                {
                    m_color    = new Color(255, 0, 0),
                    m_position = 0.8f
                };

                ColorStop fifth = new ColorStop
                {
                    m_color    = new Color(255, 255, 255),
                    m_position = 1.0f
                };

                stops.Add(first);
                stops.Add(second);
                stops.Add(third);
                stops.Add(fourth);
                stops.Add(fifth);


                JetGradient = new ColorGradient(stops);
            }


            int    stepX = map.GetLength(0) / width;
            int    stepY = map.GetLength(1) / height;
            string index = "";

            for (int tx = 0; tx < width; tx++)
            {
                for (int ty = 0; ty < height; ty++)
                {
                    int x = tx * stepX;
                    int y = ty * stepY;

                    float h1    = map[x, y].GetValue(type);
                    var   biome = Map[x, y].Biome;
                    if (h1 < 0.1f)
                    {
                        index = "Sea";
                    }
                    else if (h1 >= 0.1f && h1 <= sealevel)
                    {
                        index = "Water";
                    }
                    else if (displayMode == "Biomes")
                    {
                        index = "Biome";
                    }
                    else if (displayMode == "Height")
                    {
                        if (h1 >= 0.2f && h1 < 0.21f)
                        {
                            index = "Shore";
                        }
                        else if (h1 >= 0.21f && h1 < 0.4f)
                        {
                            index = "Lowlands";
                        }
                        else if (h1 >= 0.4f && h1 < 0.6f)
                        {
                            index = "Highlands";
                        }
                        else if (h1 >= 0.6f && h1 < 0.9f)
                        {
                            index = "Mountains";
                        }
                        else
                        {
                            index = "Peaks";
                        }
                    }

                    if (displayMode == "Gray")
                    {
                        Color toDraw = JetGradient.GetColor(h1);
                        worldData[y * width + x] = toDraw;
                    }
                    else if (displayMode == "Factions")
                    {
                        float h2           = map[x, y].Height;
                        byte  factionColor = map[x, y].Faction;


                        Color ci = Color.DarkBlue;

                        if (factionColor > 0 && factionColor <= NativeFactions.Count)
                        {
                            bool inside = x > 0 && x < width - 1 && y > 0 && y < height - 1;
                            ci = NativeFactions[factionColor - 1].PrimaryColor;
                            if (inside &&
                                (map[x + 1, y].Faction != factionColor ||
                                 map[x - 1, y].Faction != factionColor ||
                                 map[x, y - 1].Faction != factionColor ||
                                 map[x, y + 1].Faction != factionColor ||
                                 map[x + 1, y + 1].Faction != factionColor ||
                                 map[x - 1, y - 1].Faction != factionColor ||
                                 map[x + 1, y - 1].Faction != factionColor ||
                                 map[x - 1, y + 1].Faction != factionColor))
                            {
                                ci = NativeFactions[factionColor - 1].SecondaryColor;
                            }
                        }
                        else if (h2 > sealevel)
                        {
                            ci = Color.Gray;
                        }

                        Color toDraw = new Color((float)(ci.R) * (h2 + 0.5f) / 255.0f, (float)(ci.G * (h2 + 0.5f)) / 255.0f, (float)(ci.B * (h2 + 0.5f)) / 255.0f);
                        worldData[ty * width + tx] = toDraw;
                    }
                    else
                    {
                        Color ci = Color.Black;
                        if (displayMode == "Biomes" && index != "Water" && index != "Sea")
                        {
                            if ((int)biome < BiomeLibrary.Biomes.Count)
                            {
                                ci = BiomeLibrary.Biomes[biome].MapColor;
                            }
                        }
                        else
                        {
                            ci = HeightColors[index];
                        }

                        Color toDraw = new Color((float)(ci.R) * (h1 + 0.5f) / 255.0f, (float)(ci.G * (h1 + 0.5f)) / 255.0f, (float)(ci.B * (h1 + 0.5f)) / 255.0f);
                        worldData[ty * width + tx] = toDraw;
                    }
                }
            }

            if (imageMutex != null)
            {
                imageMutex.WaitOne();
            }

            GameState.Game.GraphicsDevice.Textures[0] = null;
            worldMap.SetData(worldData);

            if (imageMutex != null)
            {
                imageMutex.ReleaseMutex();
            }
        }
示例#3
0
        public static void TextureFromHeightMap(string displayMode,
            MapData[,] map,
            ScalarFieldType type,
            int width, int height,
            Mutex imageMutex,
            Color[] worldData,
            Texture2D worldMap, float sealevel)
        {
            if(JetGradient == null)
            {
                List<ColorStop> stops = new List<ColorStop>();
                ColorStop first = new ColorStop
                {
                    m_color = new Color(0, 255, 255),
                    m_position = 0.0f
                };

                ColorStop second = new ColorStop
                {
                    m_color = new Color(0, 0, 255),
                    m_position = 0.2f
                };

                ColorStop third = new ColorStop
                {
                    m_color = new Color(255, 255, 0),
                    m_position = 0.4f
                };

                ColorStop fourth = new ColorStop
                {
                    m_color = new Color(255, 0, 0),
                    m_position = 0.8f
                };

                ColorStop fifth = new ColorStop
                {
                    m_color = new Color(255, 255, 255),
                    m_position = 1.0f
                };

                stops.Add(first);
                stops.Add(second);
                stops.Add(third);
                stops.Add(fourth);
                stops.Add(fifth);

                JetGradient = new ColorGradient(stops);
            }

            int stepX = map.GetLength(0) / width;
            int stepY = map.GetLength(1) / height;
            string index = "";
            for(int tx = 0; tx < width; tx++)
            {
                for(int ty = 0; ty < height; ty++)
                {
                    int x = tx * stepX;
                    int y = ty * stepY;

                    float h1 = map[x, y].GetValue(type);
                    Biome biome = Map[x, y].Biome;
                    if(h1 < 0.1f)
                    {
                        index = "Sea";
                    }
                    else if(h1 >= 0.1f && h1 <= sealevel)
                    {
                        index = "Water";
                    }
                    else if(displayMode == "Biomes")
                    {
                        index = "Biome";
                    }
                    else if(displayMode == "Height")
                    {
                        if(h1 >= 0.2f && h1 < 0.21f)
                        {
                            index = "Shore";
                        }
                        else if(h1 >= 0.21f && h1 < 0.4f)
                        {
                            index = "Lowlands";
                        }
                        else if(h1 >= 0.4f && h1 < 0.6f)
                        {
                            index = "Highlands";
                        }
                        else if(h1 >= 0.6f && h1 < 0.9f)
                        {
                            index = "Mountains";
                        }
                        else
                        {
                            index = "Peaks";
                        }
                    }

                    if(displayMode == "Gray")
                    {
                        Color toDraw = JetGradient.GetColor(h1);
                        worldData[y * width + x] = toDraw;
                    }
                    else if (displayMode == "Factions")
                    {
                        float h2 = map[x, y].Height;
                        byte factionColor = map[x, y].Faction;

                        Color ci = Color.DarkBlue;

                        if (factionColor > 0)
                        {
                            bool inside = x > 0 && x < width - 1 && y > 0 && y < height - 1;
                            ci = NativeFactions[factionColor - 1].PrimaryColor;
                           if(inside &&
                               (map[x + 1, y].Faction != factionColor ||
                               map[x - 1, y].Faction != factionColor ||
                               map[x, y - 1].Faction != factionColor ||
                               map[x, y + 1].Faction != factionColor ||
                               map[x + 1, y + 1].Faction != factionColor ||
                               map[x - 1, y - 1].Faction != factionColor ||
                               map[x + 1, y - 1].Faction != factionColor ||
                               map[x - 1, y + 1].Faction != factionColor))
                           {
                                    ci = NativeFactions[factionColor - 1].SecondaryColor;
                           }
                        }
                        else if (h2 > sealevel)
                        {
                            ci = Color.Gray;
                        }

                        Color toDraw = new Color((float)(ci.R) * (h2 + 0.5f) / 255.0f, (float)(ci.G * (h2 + 0.5f)) / 255.0f, (float)(ci.B * (h2 + 0.5f)) / 255.0f);
                        worldData[ty * width + tx] = toDraw;
                    }
                    else
                    {
                        Color ci = displayMode == "Biomes"  && index != "Water" && index != "Sea" ? BiomeLibrary.Biomes[biome].MapColor : HeightColors[index];
                        Color toDraw = new Color((float) (ci.R) * (h1 + 0.5f) / 255.0f, (float) (ci.G * (h1 + 0.5f)) / 255.0f, (float) (ci.B * (h1 + 0.5f)) / 255.0f);
                        worldData[ty * width + tx] = toDraw;
                    }
                }
            }

            if(imageMutex != null)
            {
                imageMutex.WaitOne();
            }

            GameState.Game.GraphicsDevice.Textures[0] = null;
            worldMap.SetData(worldData);

            if(imageMutex != null)
            {
                imageMutex.ReleaseMutex();
            }
        }
示例#4
0
        public ColorGradient(Color first, Color last, int numStops)
        {
            this.Stops = new List<ColorStop>();
            Vector4 color1 = new Vector4((float) first.R / 255.0f, (float) first.G / 255.0f, (float) first.B / 255.0f, (float) first.A / 255.0f);
            Vector4 color2 = new Vector4((float) last.R / 255.0f, (float) last.G / 255.0f, (float) last.B / 255.0f, (float) last.A / 255.0f);

            Vector4 norm = color2 - color1;
            float length = norm.Length();
            float dStop = length / numStops;
            norm.Normalize();

            float currentStop = 0.0f;
            for(int i = 0; i < numStops; i++)
            {
                ColorStop stop = new ColorStop();
                Vector4 colorVec = color1 + currentStop * norm;
                stop.m_color = new Color(colorVec.X, colorVec.Y, colorVec.Y, colorVec.W);
                currentStop += dStop;
                Stops.Add(stop);
            }
        }