Esempio n. 1
0
        private void PositionParticles()
        {
            Quaternion  q         = Quaternion.Euler(surface.rotation);
            Quaternion  qInv      = Quaternion.Inverse(q);
            NoiseMethod method    = Noise.methods[(int)surface.type][surface.dimensions - 1];
            float       amplitude = surface.damping ? surface.strength / surface.frequency : surface.strength;

            for (int i = 0; i < particles.Length; i++)
            {
                Vector3     position = particles[i].position;
                Vector3     point    = q * new Vector3(position.x, position.z) + surface.offset;
                NoiseSample sample   = Noise.Sum(method, point, surface.frequency, surface.octaves, surface.lacunarity, surface.persistence);
                sample            = sample * 0.5f;
                sample           *= amplitude;
                sample.derivative = qInv * sample.derivative;
                Vector3 curl = new Vector3(sample.derivative.y, 0f, -sample.derivative.x);
                position             += curl * Time.deltaTime * flowStrength;
                position.y            = sample.value + system.startSize;
                particles[i].position = position;
                if (position.x < -0.5f || position.x > 0.5f || position.z < -0.5f || position.z > 0.5f)
                {
                    particles[i].remainingLifetime = 0f;
                }
            }
        }
Esempio n. 2
0
        public static NoiseSample Simplex3D(Vector3 point, float frequency)
        {
            point *= frequency;
            float       skew   = (point.x + point.y + point.z) * (1f / 3f);
            float       sx     = point.x + skew;
            float       sy     = point.y + skew;
            float       sz     = point.z + skew;
            int         ix     = Mathf.FloorToInt(sx);
            int         iy     = Mathf.FloorToInt(sy);
            int         iz     = Mathf.FloorToInt(sz);
            NoiseSample sample = Simplex3DPart(point, ix, iy, iz);

            sample += Simplex3DPart(point, ix + 1, iy + 1, iz + 1);
            float x = sx - ix;
            float y = sy - iy;
            float z = sz - iz;

            if (x >= y)
            {
                if (x >= z)
                {
                    sample += Simplex3DPart(point, ix + 1, iy, iz);
                    if (y >= z)
                    {
                        sample += Simplex3DPart(point, ix + 1, iy + 1, iz);
                    }
                    else
                    {
                        sample += Simplex3DPart(point, ix + 1, iy, iz + 1);
                    }
                }
                else
                {
                    sample += Simplex3DPart(point, ix, iy, iz + 1);
                    sample += Simplex3DPart(point, ix + 1, iy, iz + 1);
                }
            }
            else
            {
                if (y >= z)
                {
                    sample += Simplex3DPart(point, ix, iy + 1, iz);
                    if (x >= z)
                    {
                        sample += Simplex3DPart(point, ix + 1, iy + 1, iz);
                    }
                    else
                    {
                        sample += Simplex3DPart(point, ix, iy + 1, iz + 1);
                    }
                }
                else
                {
                    sample += Simplex3DPart(point, ix, iy, iz + 1);
                    sample += Simplex3DPart(point, ix, iy + 1, iz + 1);
                }
            }
            sample.derivative *= frequency;
            return(sample * simplexScale3D);
        }
Esempio n. 3
0
        public static NoiseSample Simplex1D(Vector3 point, float frequency)
        {
            point *= frequency;
            int         ix     = Mathf.FloorToInt(point.x);
            NoiseSample sample = Simplex1DPart(point, ix);

            sample            += Simplex1DPart(point, ix + 1);
            sample.derivative *= frequency;
            return(sample * (64f / 27f));
        }
Esempio n. 4
0
        public static NoiseSample SimplexValue1D(Vector3 point, float frequency)
        {
            point *= frequency;
            int         ix     = Mathf.FloorToInt(point.x);
            NoiseSample sample = SimplexValue1DPart(point, ix);

            sample            += SimplexValue1DPart(point, ix + 1);
            sample.derivative *= frequency;
            return(sample * (2f / hashMask) - 1f);
        }
Esempio n. 5
0
        public void Refresh()
        {
            if (resolution != currentResolution)
            {
                CreateGrid();
            }
            Quaternion q       = Quaternion.Euler(rotation);
            Quaternion qInv    = Quaternion.Inverse(q);
            Vector3    point00 = q * new Vector3(-0.5f, -0.5f) + offset;
            Vector3    point10 = q * new Vector3(0.5f, -0.5f) + offset;
            Vector3    point01 = q * new Vector3(-0.5f, 0.5f) + offset;
            Vector3    point11 = q * new Vector3(0.5f, 0.5f) + offset;

            NoiseMethod method    = Noise.methods[(int)type][dimensions - 1];
            float       stepSize  = 1f / resolution;
            float       amplitude = damping ? strength / frequency : strength;

            for (int v = 0, y = 0; y <= resolution; y++)
            {
                Vector3 point0 = Vector3.Lerp(point00, point01, y * stepSize);
                Vector3 point1 = Vector3.Lerp(point10, point11, y * stepSize);
                for (int x = 0; x <= resolution; x++, v++)
                {
                    Vector3     point  = Vector3.Lerp(point0, point1, x * stepSize);
                    NoiseSample sample = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence);
                    sample = sample * 0.5f;
                    if (coloringForStrength)
                    {
                        colors[v] = coloring.Evaluate(sample.value + 0.5f);
                        sample   *= amplitude;
                    }
                    else
                    {
                        sample   *= amplitude;
                        colors[v] = coloring.Evaluate(sample.value + 0.5f);
                    }
                    vertices[v].y     = sample.value;
                    sample.derivative = qInv * sample.derivative;
                    if (analyticalDerivatives)
                    {
                        normals[v] = new Vector3(-sample.derivative.x, 1f, -sample.derivative.y).normalized;
                    }
                }
            }
            mesh.vertices = vertices;
            mesh.colors   = colors;
            if (!analyticalDerivatives)
            {
                CalculateNormals();
            }
            mesh.normals = normals;
        }
Esempio n. 6
0
        private static NoiseSample SimplexValue1DPart(Vector3 point, int ix)
        {
            float       x      = point.x - ix;
            float       f      = 1f - x * x;
            float       f2     = f * f;
            float       f3     = f * f2;
            float       h      = hash[ix & hashMask];
            NoiseSample sample = new NoiseSample();

            sample.value        = h * f3;
            sample.derivative.x = -6f * h * x * f2;
            return(sample);
        }
Esempio n. 7
0
        private static NoiseSample Simplex1DPart(Vector3 point, int ix)
        {
            float       x      = point.x - ix;
            float       f      = 1f - x * x;
            float       f2     = f * f;
            float       f3     = f * f2;
            float       g      = gradients1D[hash[ix & hashMask] & gradientsMask1D];
            float       v      = g * x;
            NoiseSample sample = new NoiseSample();

            sample.value        = v * f3;
            sample.derivative.x = g * f3 - 6f * v * x * f2;
            return(sample);
        }
Esempio n. 8
0
        public static NoiseSample Sum(NoiseMethod method, Vector3 point, float frequency, int octaves, float lacunarity, float persistence)
        {
            NoiseSample sum       = method(point, frequency);
            float       amplitude = 1f;
            float       range     = 1f;

            for (int o = 1; o < octaves; o++)
            {
                frequency *= lacunarity;
                amplitude *= persistence;
                range     += amplitude;
                sum       += method(point, frequency) * amplitude;
            }
            return(sum * (1f / range));
        }
Esempio n. 9
0
        private static NoiseSample SimplexValue2DPart(Vector3 point, int ix, int iy)
        {
            float       unskew = (ix + iy) * squaresToTriangles;
            float       x      = point.x - ix + unskew;
            float       y      = point.y - iy + unskew;
            float       f      = 0.5f - x * x - y * y;
            NoiseSample sample = new NoiseSample();

            if (f > 0f)
            {
                float f2   = f * f;
                float f3   = f * f2;
                float h    = hash[hash[ix & hashMask] + iy & hashMask];
                float h6f2 = -6f * h * f2;
                sample.value        = h * f3;
                sample.derivative.x = h6f2 * x;
                sample.derivative.y = h6f2 * y;
            }
            return(sample);
        }
Esempio n. 10
0
        private static NoiseSample Simplex2DPart(Vector3 point, int ix, int iy)
        {
            float       unskew = (ix + iy) * squaresToTriangles;
            float       x      = point.x - ix + unskew;
            float       y      = point.y - iy + unskew;
            float       f      = 0.5f - x * x - y * y;
            NoiseSample sample = new NoiseSample();

            if (f > 0f)
            {
                float   f2   = f * f;
                float   f3   = f * f2;
                Vector2 g    = gradients2D[hash[hash[ix & hashMask] + iy & hashMask] & gradientsMask2D];
                float   v    = Dot(g, x, y);
                float   v6f2 = -6f * v * f2;
                sample.value        = v * f3;
                sample.derivative.x = g.x * f3 + v6f2 * x;
                sample.derivative.y = g.y * f3 + v6f2 * y;
            }
            return(sample);
        }
Esempio n. 11
0
        public static NoiseSample Simplex2D(Vector3 point, float frequency)
        {
            point *= frequency;
            float       skew   = (point.x + point.y) * trianglesToSquares;
            float       sx     = point.x + skew;
            float       sy     = point.y + skew;
            int         ix     = Mathf.FloorToInt(sx);
            int         iy     = Mathf.FloorToInt(sy);
            NoiseSample sample = Simplex2DPart(point, ix, iy);

            sample += Simplex2DPart(point, ix + 1, iy + 1);
            if (sx - ix >= sy - iy)
            {
                sample += Simplex2DPart(point, ix + 1, iy);
            }
            else
            {
                sample += Simplex2DPart(point, ix, iy + 1);
            }
            sample.derivative *= frequency;
            return(sample * simplexScale2D);
        }
Esempio n. 12
0
        private static NoiseSample SimplexValue3DPart(Vector3 point, int ix, int iy, int iz)
        {
            float       unskew = (ix + iy + iz) * (1f / 6f);
            float       x      = point.x - ix + unskew;
            float       y      = point.y - iy + unskew;
            float       z      = point.z - iz + unskew;
            float       f      = 0.5f - x * x - y * y - z * z;
            NoiseSample sample = new NoiseSample();

            if (f > 0f)
            {
                float f2   = f * f;
                float f3   = f * f2;
                float h    = hash[hash[hash[ix & hashMask] + iy & hashMask] + iz & hashMask];
                float h6f2 = -6f * h * f2;
                sample.value        = h * f3;
                sample.derivative.x = h6f2 * x;
                sample.derivative.y = h6f2 * y;
                sample.derivative.z = h6f2 * z;
            }
            return(sample);
        }
Esempio n. 13
0
        private static NoiseSample Simplex3DPart(Vector3 point, int ix, int iy, int iz)
        {
            float       unskew = (ix + iy + iz) * (1f / 6f);
            float       x      = point.x - ix + unskew;
            float       y      = point.y - iy + unskew;
            float       z      = point.z - iz + unskew;
            float       f      = 0.5f - x * x - y * y - z * z;
            NoiseSample sample = new NoiseSample();

            if (f > 0f)
            {
                float   f2   = f * f;
                float   f3   = f * f2;
                Vector3 g    = simplexGradients3D[hash[hash[hash[ix & hashMask] + iy & hashMask] + iz & hashMask] & simplexGradientsMask3D];
                float   v    = Dot(g, x, y, z);
                float   v6f2 = -6f * v * f2;
                sample.value        = v * f3;
                sample.derivative.x = g.x * f3 + v6f2 * x;
                sample.derivative.y = g.y * f3 + v6f2 * y;
                sample.derivative.z = g.z * f3 + v6f2 * z;
            }
            return(sample);
        }
Esempio n. 14
0
        private void PositionParticles()
        {
            Quaternion  q         = Quaternion.Euler(rotation);
            Quaternion  qInv      = Quaternion.Inverse(q);
            NoiseMethod method    = Noise.methods[(int)type][dimensions - 1];
            float       amplitude = damping ? strength / frequency : strength;

            morphOffset += Time.deltaTime * morphSpeed;

            if (morphOffset > 256f)
            {
                morphOffset -= 256f;
            }

            for (int i = 0; i < particles.Length; i++)
            {
                Vector3     position = particles[i].position;
                Vector3     point    = q * new Vector3(position.z, position.y, position.x + morphOffset) + offset;
                NoiseSample sampleX  = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence);
                sampleX           *= amplitude;
                sampleX.derivative = qInv * sampleX.derivative;
                point = q * new Vector3(position.x + 100f, position.z, position.y + morphOffset) + offset;
                NoiseSample sampleY = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence);
                sampleY           *= amplitude;
                sampleY.derivative = qInv * sampleY.derivative;
                point = q * new Vector3(position.y, position.x + 100f, position.z + morphOffset) + offset;
                NoiseSample sampleZ = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence);
                sampleZ           *= amplitude;
                sampleZ.derivative = qInv * sampleZ.derivative;
                Vector3 curl;
                curl.x = sampleZ.derivative.x - sampleY.derivative.y;
                curl.y = sampleX.derivative.x - sampleZ.derivative.y + (1f / (1f + position.y));
                curl.z = sampleY.derivative.x - sampleX.derivative.y;
                particles[i].velocity = curl;
            }
        }