Example #1
0
        public TerrainChunk(TerrainChunkSettings settings, INoiseProvider noiseProvider, float seed, List <IFeatureGenerator> featureGenerator, Material material)
        {
            this.settings         = settings;
            this.noiseProvider    = noiseProvider;
            this.seed             = seed;
            this.featureGenerator = featureGenerator;
            this.material         = material;

            cube = (GameObject)Resources.Load("Cube", typeof(GameObject));
        }
        private void Awake()
        {
            settings         = new TerrainChunkSettings(129, 200, 0.1f);
            noiseProvider1   = new SeussNoise0(true);
            noiseProvider2   = new SeussNoise0(false);
            featureGenerator = new List <IFeatureGenerator>();

            seed = Random.Range(0.0f, 100000.0f);

            featureGenerator.Add(new TreePlacer(settings.length));

            CreateTerrainChunk();
        }
        public void GenerateWorldMap()
        {
            //float[,] islandmask = TerrainUtils.GetIslandMask(MAP_SIZE);

            this.InitWorldmapData();
            //worldperlin generates the overall worldmap for sampling heights from for biomes
            float SCALE          = 20.0f; //scaling the perlin to be larger (low res)
            float moisutreOffset = UnityEngine.Random.Range(-10000, 10000);

            for (int x = 0; x < MAP_SIZE; x++)
            {
                for (int z = 0; z < MAP_SIZE; z++)
                {
                    float biomeX = x / SCALE;
                    float biomeZ = z / SCALE;
                    float moistX = biomeX + moisutreOffset;
                    float moistZ = biomeZ + moisutreOffset;

                    INoiseProvider provider = WorldMapProvider as INoiseProvider;
                    ElevationData[x, z] = ((float)(provider.GetValue(biomeX, biomeZ)) + 2f / 0.5f);// * islandmask[x,z];
                    //track the max and min values for normalisation later
                    if (ElevationData[x, z] > this.ElevationMaxValue)
                    {
                        ElevationMaxValue = ElevationData[x, z];
                    }
                    if (ElevationData[x, z] < this.ElevationMinValue)
                    {
                        ElevationMinValue = ElevationData[x, z];
                    }
                    MoistureData[x, z] = (float)(provider.GetValue(moistX, moistZ)) + 2f / 0.5f;
                    if (MoistureData[x, z] > this.MoisutreMaxValue)
                    {
                        MoisutreMaxValue = MoistureData[x, z];
                    }
                    if (MoistureData[x, z] < this.MoisutreMinValue)
                    {
                        MoisutreMinValue = MoistureData[x, z];
                    }
                }
            }

            //set the random chunks to put features in <STATIC FOR DEV>
            //ChunkFeatures.Add(new Vector2i(0, 0),  FeatureType.DungeonEntrance);
            //ChunkFeatures.Add(new Vector2i(-1, -1), FeatureType.DungeonEntrance);
            //ChunkFeatures.Add(new Vector2i(5, 5),  FeatureType.DungeonEntrance);
            PlaceDungeonsInWorld();
            this.IsGenerated = true;
            Debug.Log("World Map is Generated");
        }
Example #4
0
        // Use this for initialization
        public void Generate(float seed, INoiseProvider noiseProvider, TerrainChunkSettings settings)
        {
            trees = new GameObject("Trees");

            for (float i = -length / 2; i < length / 2; i += spacing)
            {
                for (float j = -length / 2; j < length / 2; j += spacing)
                {
                    if (Random.value > noiseThreshold)
                    {
                        // calculate tree position
                        float xOffset   = Random.Range(0, maxDiplacement);
                        float zOffset   = Random.Range(0, maxDiplacement);
                        float xPosition = i + xOffset;
                        float zPosition = j + zOffset;

                        if (noiseProvider.GetValue(xPosition, zPosition, seed) < 0.2f)
                        {
                            continue;
                        }

                        // find y position by choosing lowest point from 4 nearest heightmap points
                        float radius        = 0.45f;
                        float minHeight     = noiseProvider.GetValue(xPosition - radius, zPosition - radius, seed);
                        float compareHeight = noiseProvider.GetValue(xPosition - radius, zPosition + radius, seed);
                        if (compareHeight < minHeight)
                        {
                            minHeight = compareHeight;
                        }
                        compareHeight = noiseProvider.GetValue(xPosition + radius, zPosition - radius, seed);
                        if (compareHeight < minHeight)
                        {
                            minHeight = compareHeight;
                        }
                        compareHeight = noiseProvider.GetValue(xPosition + radius, zPosition + radius, seed);
                        if (compareHeight < minHeight)
                        {
                            minHeight = compareHeight;
                        }

                        // create and place tree
                        Vector3 treePosition = new Vector3(xPosition, minHeight, zPosition);
                        MonoBehaviour.Instantiate(tree, treePosition, Quaternion.identity, trees.transform);
                    }
                }
            }
        }
            public FractionalBrownianMotionFunction(INoiseProvider provider, Random rng, double scale, int octaves, double persistence = 0.5, double lacunarity = 2.0, Func<double, double> octaveModifier = null, double? xPeriod = null, double? yPeriod = null, SampleMode mode = SampleMode.Sample2D)
            {
                _octaveModifier = octaveModifier;
                _octaves = new Tuple<double, IFunction2D>[octaves];

                bool tiling = xPeriod != null || yPeriod != null;
                if (tiling && xPeriod == null)
                {
                    xPeriod = yPeriod;
                }
                if (tiling && yPeriod == null)
                {
                    yPeriod = xPeriod;
                }

                _maxAmplitude = 0.0;
                var amplitude = 1.0;
                var frequency = scale;

                for (var i = 0; i < octaves; i++)
                {
                    IFunction2D noise;
                    switch (mode)
                    {
                        case SampleMode.Sample2D:
                            noise = provider.Create2D(rng, frequency);
                            break;
                        case SampleMode.Slice3D:
                            noise = provider.Slice3D(rng, frequency);
                            break;
                        case SampleMode.Slice4D:
                            noise = provider.Slice4D(rng, frequency);
                            break;
                        case SampleMode.Tileable2D:
                            noise = provider.Create2D(rng, frequency, xPeriod.Value, yPeriod.Value);
                            break;
                        default:
                            throw new InvalidOperationException();
                    }
                    _octaves[i] = Tuple.Create(amplitude, noise);

                    _maxAmplitude += amplitude;
                    amplitude *= persistence;
                    frequency *= lacunarity;
                }
            }
        private void GenerateImage(INoiseProvider provider, PictureBox pictureBox, TextBox diagnosticInfo, Button saveButton)
        {
            var rng = new Random();
            Bitmap bitmap;
            try
            {
                var function = CreateNoiseFunction(provider, rng);
                var heightMap = new double[_size, _size];
                var watch = new Stopwatch();
                watch.Start();
                function.FillHeightMap(heightMap);
                watch.Stop();
               
                var seconds = watch.Elapsed.TotalSeconds;
                var perPoint = (int)Math.Floor((seconds / (_octaves * _size * _size)) * 1e9);
                var min = Double.MaxValue;
                var max = Double.MinValue;
                var total = 0.0;
                for (var x = 0; x < _size; x++)
                {
                    for (var y = 0; y < _size; y++)
                    {
                        var value = heightMap[x, y];
                        total += value;
                        min = Math.Min(value, min);
                        max = Math.Max(value, max);
                    }
                }
                var average = total / (_size * _size);
                var diagnostic = String.Format("Range: {0:f4} -> {1:f4}  Avg: {2:f4}\r\n Time: {3:f4}s      Per Point: {4}ns", min, max, average, seconds, perPoint);

                if (_normalize)
                {
                    var change = max - min;
                    var offset = min + 1.0;
                    for (var x = 0; x < _size; x++)
                    {
                        for (var y = 0; y < _size; y++)
                        {
                            var value = heightMap[x, y];
                            var magnitude = (value + 1.0 - offset) / change;
                            heightMap[x, y] = magnitude * 2.0 - 1.0;
                        }
                    }
                }

                bitmap = heightMap.RenderBitmap();
                Action setBitmap = () => { pictureBox.Image = bitmap; saveButton.Enabled = true; saveButton.Tag = bitmap; };
                Invoke(setBitmap);

                Action setText = () => { diagnosticInfo.Text = diagnostic; };
                Invoke(setText);
            }
            catch
            {
                Action setText = () => { diagnosticInfo.Text = "Error generating image!"; };
                Invoke(setText);
            }

        }
        private IFunction2D CreateNoiseFunction(INoiseProvider provider, Random rng)
        {
            if (_octaves == 1 && _power == 1.0 && !_absoluteValue)
            {
                // Simple case that doesn't need fBm, so we can just return a basic noise function
                // to remove some overhead for benchmarking.

                switch (_mode)
                {
                    case SampleMode.Sample2D:
                        return provider.Create2D(rng, _scale);
                    case SampleMode.Slice3D:
                        return provider.Slice3D(rng, _scale);
                    case SampleMode.Slice4D:
                        return provider.Slice4D(rng, _scale);
                    case SampleMode.Tileable2D:
                        return provider.Create2D(rng, _scale, _size, _size);
                    default:
                        throw new InvalidOperationException();
                }
            }

            Func<double, double> modifier = null;

            if (_absoluteValue)
            {
                if (_power == 1.0)
                {
                    modifier = input => (1.0 - Math.Abs(input)) * 2.0 - 1.0;
                }
                else
                {
                    modifier = input => Math.Pow(1.0 - Math.Abs(input), _power) * 2.0 - 1.0;
                }
            }
            else if (_power != 1.0)
            {
                modifier = input => input < 0 ? -Math.Pow(-input, _power) : Math.Pow(input, _power);
            }

            return provider.FractionalBrownianMotion(rng, _scale, _octaves, _persistence, _lacunarity, modifier, _size, _size, _mode);
        }
Example #8
0
 public ChunkGenerator(INoiseProvider noiseProvider) => this.noiseProvider = noiseProvider;
Example #9
0
 /// <summary>
 /// Creates a new instance of the <see cref="LsbStorageProvider" /> class.
 /// </summary>
 /// <param name="noiseProvider">The noise provider used to disguise the end of the data in an image.</param>
 public LsbStorageProvider(INoiseProvider noiseProvider)
 {
     NoiseProvider = noiseProvider;
 }
Example #10
0
 /// <summary>
 /// Creates a new instance of the <see cref="LsbStorageProvider" /> class.
 /// </summary>
 public LsbStorageProvider()
 {
     NoiseProvider = new RandomNoiseProvider();
 }