public List <Result> Generate()
        {
            List <Result> items = new List <Result>();

            LibNoise.FastNoise noise = new LibNoise.FastNoise(10);
            noise.Frequency = 1;

            for (int i = 0; i < referenceImage.width; i++)
            {
                for (int j = 0; j < referenceImage.height; j++)
                {
                    Color c       = referenceImage.GetPixel(i, j);
                    float density = (c.r + c.g + c.b) / 3.0f;

                    if (parameters.minDensity > 0.0f && density < parameters.minDensity)
                    {
                        continue;
                    }

                    Vector2 pos2d          = new Vector2(((float)i - referenceImage.width / 2.0f), ((float)j - referenceImage.height / 2.0f));
                    Vector3 sectorPosition = new Vector3(pos2d.x * parameters.sectorSize, pos2d.y * parameters.sectorSize, 0.0f);
                    float   radiusMod      = (pos2d.magnitude > 0.0f) ? (1.0f / pos2d.sqrMagnitude) : 1.0f;
                    int     starCount      = (int)(density * parameters.maxStarCountPerSector * radiusMod);

                    while (starCount > 0)
                    {
                        Vector3 starPosition = sectorPosition + UnityEngine.Random.insideUnitSphere * parameters.sectorSize;

                        starPosition.Set(starPosition.x, starPosition.y, starPosition.z * parameters.galaxySize.z);

                        float nm = (float)noise.GetValue((double)starPosition.x, (double)starPosition.y, (double)starPosition.z);

                        starPosition.Set(starPosition.x, starPosition.y, starPosition.z * nm * radiusMod);

                        starCount--;

                        Result result = new Result();
                        result.type     = UnityEngine.Random.Range(0, parameters.maxStarTypes);
                        result.position = starPosition;
                        items.Add(result);
                    }
                }
            }

            Debug.Log("stars created=" + items.Count);

            return(items);
        }
        public List<Result> Generate()
        {
            List<Result> items = new List<Result>();

            LibNoise.FastNoise noise = new LibNoise.FastNoise(10);
            noise.Frequency = 1;

            for (int i = 0; i < referenceImage.width; i++)
            {
                for (int j = 0; j < referenceImage.height; j++)
                {
                    Color c = referenceImage.GetPixel(i, j);
                    float density = (c.r + c.g + c.b)/3.0f;

                    if (parameters.minDensity > 0.0f && density < parameters.minDensity)
                        continue;

                    Vector2 pos2d = new Vector2(((float)i - referenceImage.width / 2.0f), ((float)j - referenceImage.height / 2.0f));
                    Vector3 sectorPosition = new Vector3(pos2d.x * parameters.sectorSize, pos2d.y * parameters.sectorSize, 0.0f);
                    float radiusMod = (pos2d.magnitude > 0.0f) ? (1.0f / pos2d.sqrMagnitude) : 1.0f;
                    int starCount = (int)(density * parameters.maxStarCountPerSector * radiusMod);

                    while (starCount > 0)
                    {
                        Vector3 starPosition = sectorPosition + UnityEngine.Random.insideUnitSphere * parameters.sectorSize;

                        starPosition.Set(starPosition.x, starPosition.y , starPosition.z * parameters.galaxySize.z);

                        float nm = (float)noise.GetValue((double)starPosition.x, (double)starPosition.y, (double)starPosition.z);

                        starPosition.Set(starPosition.x, starPosition.y, starPosition.z * nm * radiusMod);

                        starCount--;

                        Result result = new Result();
                        result.type = UnityEngine.Random.Range(0, parameters.maxStarTypes);
                        result.position = starPosition;
                        items.Add(result);
                    }
                }
            }

            Debug.Log("stars created=" + items.Count);

            return items;
        }
示例#3
0
        private void CreatePlanet()
        {
            var seed = 42601423;

            var fastNoise01 = new LibNoise.FastNoise(seed);
            fastNoise01.Frequency = 0.1;
            fastNoise01.OctaveCount = 2;

            var fastNoise02 = new LibNoise.FastNoise(seed);
            fastNoise02.Frequency = 0.25;
            fastNoise02.OctaveCount = 4;

            var noiseModule =
                new LibNoise.Modifiers.Add(
                    new LibNoise.Modifiers.ScaleOutput(fastNoise01, 0.3),
                    new LibNoise.Modifiers.ScaleOutput(fastNoise02, 0.05));

            // Create a new planet.
            int terrainWidth = 1000;
            int terrainHeight = 1000;

            var terrainData = new sbyte[terrainWidth + 1, terrainHeight + 1];

            for (int x = 0; x < terrainWidth; ++x)
            {
                for (int y = 0; y < terrainHeight; ++y)
                {
                    var position = new Vector2(x, y);
                    var center = new Vector2(terrainWidth / 2, terrainHeight / 2);
                    var distance = (position - center).Length();

                    var noise = (float)noiseModule.GetValue((double)(position.X / 5.0f), (double)(position.Y / 5.0f), 10.0);
                    var density = (1.2f / 500.0f * distance) - 1.0f - ((noise + 1.5f) * 0.1f);

                    terrainData[x, y] =  (sbyte)(density > 0.0f ? 1 : -1);
                }
            }

            m_planet = new Entities.Terrain(Vector2.One * 100.0f, new Entities.MemoryTerrainSource(terrainData, 1000), "Terrain");
            m_planet.Position = new Vector2(0.0f, -100.0f);

            m_world.Add(m_planet);

            var atmosphere = new Movables.Atmosphere(70.0f);
            m_planet.SceneNode.Attach(atmosphere);
            m_miscLayer.AddRenderable(atmosphere);
        }