Exemplo n.º 1
0
        public static NoiseSample Noise2D(Vector2 point, float frequency, ENormal normalType)
        {
            var noiseStart = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            point *= frequency;
            int ix0 = (int)math.floor(point.x);
            int iy0 = (int)math.floor(point.y);


            float tx0 = point.x - ix0;
            float ty0 = point.y - iy0;

            float tx1 = tx0 - 1f;
            float ty1 = ty0 - 1f;


            ix0 &= MASK;
            iy0 &= MASK;

            int ix1 = ix0 + 1;
            int iy1 = iy0 + 1;

            int h0 = PERMUTATION[ix0];
            int h1 = PERMUTATION[ix1];

            Vector3 g00 = GRADIENT_2D[PERMUTATION[h0 + iy0] & MASK2D];
            Vector3 g10 = GRADIENT_2D[PERMUTATION[h1 + iy0] & MASK2D];
            Vector3 g01 = GRADIENT_2D[PERMUTATION[h0 + iy1] & MASK2D];
            Vector3 g11 = GRADIENT_2D[PERMUTATION[h1 + iy1] & MASK2D];


            float v00 = Dot(g00, tx0, ty0);
            float v10 = Dot(g10, tx1, ty0);
            float v01 = Dot(g01, tx0, ty1);
            float v11 = Dot(g11, tx1, ty1);

            float tx = Smooth(tx0);
            float ty = Smooth(ty0);

            float a = v00;
            float b = v10 - v00;
            float c = v01 - v00;
            float d = v11 - v01 - v10 + v00;

            NoiseSample sample;

            sample.m_Value      = MapTo1(a + b * tx + (c + d * tx) * ty);
            sample.m_ColorValue = sample.m_Value;
            sample.m_Derivative = Vector3.zero;


            if (normalType == ENormal.Analytical)
            {
                var   startDerivative = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                float dtx             = SmoothDerivative(tx0);
                float dty             = SmoothDerivative(ty0);

                Vector2 da = g00;
                Vector2 db = g10 - g00;
                Vector2 dc = g01 - g00;
                Vector2 dd = g11 - g01 - g10 + g00;

                sample.m_Derivative              = da + db * tx + (dc + dd * tx) * ty;
                sample.m_Derivative.x           += (b + d * ty) * dtx;
                sample.m_Derivative.y           += (c + d * tx) * dty;
                sample.m_Derivative.z            = 0f;
                m_DerivativeCalculationDuration += System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - startDerivative;
            }
            else if (normalType == ENormal.GeometricDinamic)
            {
                var startGeometrical = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

                var x1 = GetValue(new Vector2(point.x + 0.1f, point.y));
                var x2 = GetValue(new Vector2(point.x - 0.1f, point.y));
                var y1 = GetValue(new Vector2(point.x, point.y + 0.1f));
                var y2 = GetValue(new Vector2(point.x, point.y - 0.1f));

                sample.m_Derivative.x = (x1 - x2) * 0.5f;
                sample.m_Derivative.y = (y1 - x2) * 0.5f;

                m_GeometricalDerivativeDuration += System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - startGeometrical;
            }
            sample.m_Derivative        *= frequency;
            m_NoiseCalculationDuration += System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - noiseStart;
            return(sample);
        }
Exemplo n.º 2
0
        public static NoiseSample Sample(Vector2 pos, float frequency, int octaves, float strength, float lacunarity, float persistance, float amplitude, float damping, float strColoring, ENormal normalType)
        {
            bool bDamping  = math.abs(damping - 1f) < 0.0001f;
            bool bColoring = math.abs(strColoring - 1f) < 0.0001f;



            float amp         = bDamping ? strength / frequency : strength;
            var   noiseSample = SumNoise(pos, frequency, octaves, lacunarity, persistance, amplitude, normalType);

            noiseSample.m_ColorValue = math.saturate(bColoring ? noiseSample.m_Value * strength * amplitude : noiseSample.m_Value);
            noiseSample.m_Value     *= strength * amp;
            noiseSample.m_Derivative = Vector3.Normalize(noiseSample.m_Derivative * strength * amp);

            return(noiseSample);
        }
Exemplo n.º 3
0
        public static NoiseSample SumNoise(Vector2 pos, float frequency, int octaves, float lacunarity, float persistance, float amplitude, ENormal normalType)
        {
            var   noiseSample = Noise2D(pos, frequency, normalType);
            float range       = amplitude;

            for (int i = 0; i < octaves; ++i)
            {
                frequency   *= lacunarity;
                amplitude   *= persistance;
                range       += amplitude;
                noiseSample += Noise2D(pos, frequency, normalType) * amplitude;
            }

            return(noiseSample * (1f / range));
        }