Exemplo n.º 1
0
    public static void MakeNoise(NoiseItem noise)
    {
        GameObject n = new GameObject("Noise");

        n.AddComponent(typeof(Noise));
        n.GetComponent <Noise>().Init(noise);
    }
Exemplo n.º 2
0
        public override float GenerateLayer(Chunk chunk, int layerIndex, int x, int z, float heightSoFar, float strength)
        {
            LocalPools pools = Globals.WorkPool.GetPool(chunk.ThreadID);
            NoiseItem  ni    = pools.noiseItems[layerIndex];

            // Calculate height to add and sum it with the min height (because the height of this
            // layer should fluctuate between minHeight and minHeight+the max noise) and multiply
            // it by strength so that a fraction of the result that gets used can be decided
            float heightToAdd = ni.noiseGen.Interpolate(x, z, ni.lookupTable);

            heightToAdd += MinHeight;
            heightToAdd *= strength;

            // Absolute layers add from the minY and up but if the layer height is lower than
            // the existing terrain there's nothing to add so just return the initial value
            if (heightToAdd > heightSoFar)
            {
                SetBlocks(chunk, x, z, (int)heightSoFar, (int)heightToAdd, blockToPlace);

                //Return the height of this layer from minY as this is the new height of the column
                return(heightToAdd);
            }

            return(heightSoFar);
        }
Exemplo n.º 3
0
        public override void PostProcess(Chunk chunk, int layerIndex)
        {
            LocalPools pools = Globals.WorkPool.GetPool(chunk.ThreadID);
            NoiseItem  ni    = pools.noiseItems[layerIndex];

            pools.floatArrayPool.Push(ni.lookupTable);
        }
Exemplo n.º 4
0
        private NoiseItem PrepareLookupTable1D(FastNoise noise, NoiseItem ni)
        {
            // Generate a lookup table
            int i = 0;

            for (int z = 0; z < ni.noiseGen.Size; z++)
            {
                float zf = (z << ni.noiseGen.Step);
                ni.lookupTable[i++] = noise.SingleSimplex(0, 0, zf);
            }

            return(ni);
        }
Exemplo n.º 5
0
    public override float GetHeight(Chunk chunk, int layerIndex, int x, int z, float heightSoFar, float strength)
    {
        NoiseItem ni = chunk.pools.noiseItems[layerIndex];

        // Calculate height to add and sum it with the min height (because the height of this
        // layer should fluctuate between minHeight and minHeight+the max noise) and multiply
        // it by strength so that a fraction of the result that gets used can be decided
        float heightToAdd = ni.noiseGen.Interpolate(x, z, ni.lookupTable);

        heightToAdd += minHeight;
        heightToAdd  = heightToAdd * strength;

        return(heightSoFar + heightToAdd);
    }
Exemplo n.º 6
0
        public override void PreProcess(Chunk chunk, int layerIndex)
        {
            LocalPools pools = Globals.WorkPool.GetPool(chunk.ThreadID);
            NoiseItem  ni    = pools.noiseItems[layerIndex];

            ni.noiseGen.SetInterpBitStep(Env.CHUNK_SIZE_WITH_PADDING, 2);
            ni.lookupTable = pools.floatArrayPool.Pop((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1));

#if (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) && ENABLE_FASTSIMD
            float[] noiseSet = pools.floatArrayPool.Pop(ni.noiseGen.Size * ni.noiseGen.Size * ni.noiseGen.Size);

            // Generate SIMD noise
            int   offsetShift   = Env.CHUNK_POW - ni.noiseGen.Step;
            int   xStart        = (chunk.Pos.x * Env.CHUNK_SIZE) << offsetShift;
            int   yStart        = (chunk.Pos.y * Env.CHUNK_SIZE) << offsetShift;
            int   zStart        = (chunk.Pos.z * Env.CHUNK_SIZE) << offsetShift;
            float scaleModifier = 1 << ni.noiseGen.Step;
            noiseSIMD.Noise.FillNoiseSet(noiseSet, xStart, yStart, zStart, ni.noiseGen.Size, ni.noiseGen.Size, ni.noiseGen.Size, scaleModifier);

            // Generate a lookup table
            int i = 0;
            for (int z = 0; z < ni.noiseGen.Size; z++)
            {
                for (int x = 0; x < ni.noiseGen.Size; x++)
                {
                    ni.lookupTable[i++] = NoiseUtilsSIMD.GetNoise(noiseSet, ni.noiseGen.Size, x, 0, z, amplitude, noise.Gain);
                }
            }

            pools.floatArrayPool.Push(noiseSet);
#else
            int xOffset = chunk.Pos.x;
            int zOffset = chunk.Pos.z;

            // Generate a lookup table
            int i = 0;
            for (int z = 0; z < ni.noiseGen.Size; z++)
            {
                float zf = (z << ni.noiseGen.Step) + zOffset;

                for (int x = 0; x < ni.noiseGen.Size; x++)
                {
                    float xf = (x << ni.noiseGen.Step) + xOffset;
                    ni.lookupTable[i++] = NoiseUtils.GetNoise(noise.Noise, xf, 0, zf, 1f, amplitude, noise.Gain);
                }
            }
#endif
        }
Exemplo n.º 7
0
        public override float GenerateLayer(Chunk chunk, int layerIndex, int x, int z, float heightSoFar, float strength)
        {
            LocalPools pools = Globals.WorkPool.GetPool(chunk.ThreadID);
            NoiseItem  ni    = pools.noiseItems[layerIndex];

            // Calculate height to add and sum it with the min height (because the height of this
            // layer should fluctuate between minHeight and minHeight+the max noise) and multiply
            // it by strength so that a fraction of the result that gets used can be decided
            float heightToAdd = ni.noiseGen.Interpolate(x, z, ni.lookupTable);

            heightToAdd += MinHeight;
            heightToAdd *= strength;

            SetBlocks(chunk, x, z, (int)heightSoFar, (int)(heightSoFar + heightToAdd), blockToPlace);

            return(heightSoFar + heightToAdd);
        }
        public async Task <ActionResult> Post(NoiseItemModel noiseItemModel)
        {
            if (_noiseOptions.Value.Secret != noiseItemModel.SecretKey)
            {
                return(this.Forbid());
            }

            var noiseItem = new NoiseItem
            {
                ExecutionDate = noiseItemModel.Date,
                SoundLevel    = noiseItemModel.Db,
                IsTestItem    = noiseItemModel.IsTest
            };

            _context.NoiseItems.Add(noiseItem);
            await _context.SaveChangesAsync();

            return(this.Ok(noiseItem));
        }
Exemplo n.º 9
0
        private NoiseItem PrepareLookupTable3D(FastNoise noise, NoiseItem ni)
        {
            // Generate a lookup table
            int i = 0;

            for (int y = 0; y < ni.noiseGen.Size; y++)
            {
                float yf = (y << ni.noiseGen.Step);
                for (int z = 0; z < ni.noiseGen.Size; z++)
                {
                    float zf = (z << ni.noiseGen.Step);
                    for (int x = 0; x < ni.noiseGen.Size; x++)
                    {
                        float xf = (x << ni.noiseGen.Step);
                        ni.lookupTable[i++] = noise.SingleSimplex(0, xf, yf, zf);
                    }
                }
            }

            return(ni);
        }
Exemplo n.º 10
0
    public override float GetHeight(Chunk chunk, int layerIndex, int x, int z, float heightSoFar, float strength)
    {
        NoiseItem ni = chunk.pools.noiseItems[layerIndex];

        // Calculate height to add and sum it with the min height (because the height of this
        // layer should fluctuate between minHeight and minHeight+the max noise) and multiply
        // it by strength so that a fraction of the result that gets used can be decided
        float heightToAdd = ni.noiseGen.Interpolate(x, z, ni.lookupTable);

        heightToAdd += minHeight;
        heightToAdd  = heightToAdd * strength;

        // Absolute layers add from the minY and up but if the layer height is lower than
        // the existing terrain there's nothing to add so just return the initial value
        if (heightToAdd > heightSoFar)
        {
            //Return the height of this layer from minY as this is the new height of the column
            return(heightToAdd);
        }

        return(heightSoFar);
    }
Exemplo n.º 11
0
        void Benchmark_Noise_Dowsampling()
        {
            const int iters = 10;
            FastNoise noise = new FastNoise(0);

            Debug.Log("Bechmark - 1D, 2D, 3D noise downsampling");
            using (StreamWriter writer = File.CreateText("perf_noise_downsampling.txt"))
            {
                for (int i = 1; i <= 3; i++)
                {
                    NoiseItem ni = new NoiseItem {
                        noiseGen = new NoiseInterpolator()
                    };
                    ni.noiseGen.SetInterpBitStep(Env.CHUNK_SIZE, i);
                    ni.lookupTable = Helpers.CreateArray1D <float>(ni.noiseGen.Size + 1);

                    float[] number = { 0 };
                    double  t      = Clock.BenchmarkTime(
                        () =>
                    {
                        PrepareLookupTable1D(noise, ni);
                        for (int x = 0; x < Env.CHUNK_SIZE; x++)
                        {
                            number[0] += ni.noiseGen.Interpolate(x, ni.lookupTable);
                        }
                    }, iters);
                    t2     = t / iters;
                    output = string.Format("noise.Generate 1D\nout:{0}, downsample factor {1}\ntime:{2} | {3} ms", number[0], i,
                                           t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture));
                    Debug.Log(output);
                    foreach (string s in output.Split('\n'))
                    {
                        writer.WriteLine(s);
                    }
                }

                for (int i = 1; i <= 3; i++)
                {
                    NoiseItem ni = new NoiseItem {
                        noiseGen = new NoiseInterpolator()
                    };
                    ni.noiseGen.SetInterpBitStep(Env.CHUNK_SIZE, i);
                    ni.lookupTable = Helpers.CreateArray1D <float>((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1));

                    float[] number = { 0 };
                    double  t      = Clock.BenchmarkTime(
                        () =>
                    {
                        PrepareLookupTable2D(noise, ni);
                        for (int z = 0; z < Env.CHUNK_SIZE; z++)
                        {
                            for (int x = 0; x < Env.CHUNK_SIZE; x++)
                            {
                                number[0] += ni.noiseGen.Interpolate(x, z, ni.lookupTable);
                            }
                        }
                    }, iters);
                    t2     = t / iters;
                    output = string.Format("noise.Generate 2D\nout:{0}, downsample factor {1}\ntime:{2} | {3} ms", number[0], i,
                                           t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture));
                    Debug.Log(output);
                    foreach (string s in output.Split('\n'))
                    {
                        writer.WriteLine(s);
                    }
                }

                for (int i = 1; i <= 3; i++)
                {
                    NoiseItem ni = new NoiseItem {
                        noiseGen = new NoiseInterpolator()
                    };
                    ni.noiseGen.SetInterpBitStep(Env.CHUNK_SIZE, i);
                    ni.lookupTable = Helpers.CreateArray1D <float>((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1));

                    float[] number = { 0 };
                    double  t      = Clock.BenchmarkTime(
                        () =>
                    {
                        PrepareLookupTable3D(noise, ni);
                        for (int y = 0; y < Env.CHUNK_SIZE; y++)
                        {
                            for (int z = 0; z < Env.CHUNK_SIZE; z++)
                            {
                                for (int x = 0; x < Env.CHUNK_SIZE; x++)
                                {
                                    number[0] += ni.noiseGen.Interpolate(x, y, z, ni.lookupTable);
                                }
                            }
                        }
                    }, iters);
                    t2     = t / iters;
                    output = string.Format("noise.Generate 3D\nout:{0}, downsample factor {1}\ntime:{2} | {3} ms", number[0], i,
                                           t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture));
                    Debug.Log(output);
                    foreach (string s in output.Split('\n'))
                    {
                        writer.WriteLine(s);
                    }
                }
            }
        }
Exemplo n.º 12
0
        void Benchmark_Noise_Dowsampling()
        {
            const int iters = 10;
            FastNoise noise = new FastNoise(0);

            Debug.Log("Bechmark - 1D, 2D, 3D noise downsampling");
            using (StreamWriter writer = File.CreateText("perf_noise_downsampling.txt"))
            {
                for (int i = 1; i <= 3; i++)
                {
                    NoiseItem ni = new NoiseItem {
                        noiseGen = new NoiseInterpolator()
                    };
                    ni.noiseGen.SetInterpBitStep(Env.ChunkSize, i);
                    ni.lookupTable = Helpers.CreateArray1D <float>(ni.noiseGen.Size + 1);

                    float[] number = { 0 };
                    double  t      = Clock.BenchmarkTime(
                        () =>
                    {
                        PrepareLookupTable1D(noise, ni);
                        for (int x = 0; x < Env.ChunkSize; x++)
                        {
                            number[0] += ni.noiseGen.Interpolate(x, ni.lookupTable);
                        }
                    }, iters);
                    Debug.LogFormat("noise.Generate 1D -> out:{0}, time:{1} ms, downsample factor {2}", number[0],
                                    t.ToString(CultureInfo.InvariantCulture), i);
                    writer.WriteLine("noise.Generate 1D -> out:{0}, time:{1} ms, downsample factor {2}", number[0],
                                     t.ToString(CultureInfo.InvariantCulture), i);
                }

                for (int i = 1; i <= 3; i++)
                {
                    NoiseItem ni = new NoiseItem {
                        noiseGen = new NoiseInterpolator()
                    };
                    ni.noiseGen.SetInterpBitStep(Env.ChunkSize, i);
                    ni.lookupTable = Helpers.CreateArray1D <float>((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1));

                    float[] number = { 0 };
                    double  t      = Clock.BenchmarkTime(
                        () =>
                    {
                        PrepareLookupTable2D(noise, ni);
                        for (int z = 0; z < Env.ChunkSize; z++)
                        {
                            for (int x = 0; x < Env.ChunkSize; x++)
                            {
                                number[0] += ni.noiseGen.Interpolate(x, z, ni.lookupTable);
                            }
                        }
                    }, iters);
                    Debug.LogFormat("noise.Generate 2D -> out:{0}, time:{1} ms, downsample factor {2}", number[0],
                                    t.ToString(CultureInfo.InvariantCulture), i);
                    writer.WriteLine("noise.Generate 2D -> out:{0}, time:{1} ms, downsample factor {2}", number[0],
                                     t.ToString(CultureInfo.InvariantCulture), i);
                }

                for (int i = 1; i <= 3; i++)
                {
                    NoiseItem ni = new NoiseItem {
                        noiseGen = new NoiseInterpolator()
                    };
                    ni.noiseGen.SetInterpBitStep(Env.ChunkSize, i);
                    ni.lookupTable = Helpers.CreateArray1D <float>((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1));

                    float[] number = { 0 };
                    double  t      = Clock.BenchmarkTime(
                        () =>
                    {
                        PrepareLookupTable3D(noise, ni);
                        for (int y = 0; y < Env.ChunkSize; y++)
                        {
                            for (int z = 0; z < Env.ChunkSize; z++)
                            {
                                for (int x = 0; x < Env.ChunkSize; x++)
                                {
                                    number[0] += ni.noiseGen.Interpolate(x, y, z, ni.lookupTable);
                                }
                            }
                        }
                    }, iters);
                    Debug.LogFormat("noise.Generate 3D -> out:{0}, time:{1} ms, downsample factor {2}", number[0],
                                    t.ToString(CultureInfo.InvariantCulture), i);
                    writer.WriteLine("noise.Generate 3D -> out:{0}, time:{1} ms, downsample factor {2}", number[0],
                                     t.ToString(CultureInfo.InvariantCulture), i);
                }
            }
        }
Exemplo n.º 13
0
    public override void PostProcess(Chunk chunk, int layerIndex)
    {
        NoiseItem ni = chunk.pools.noiseItems[layerIndex];

        chunk.pools.FloatArrayPool.Push(ni.lookupTable);
    }