示例#1
0
    private void PositionParticles()
    {
        Quaternion  q         = Quaternion.Euler(surface.rotation);
        Quaternion  qInv      = Quaternion.Inverse(q);
        NoiseMethod method    = NoiseLibrary.noiseMethods[(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   = NoiseLibrary.Sum(method, point, surface.frequency, surface.octaves, surface.lacunarity, surface.persistence);
            sample            = surface.type == NoiseMethodType.Value ? (sample - 0.5f) : (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;
            }
        }
    }
示例#2
0
    public void FillTexture()
    {
        if (texture.width != resolution)
        {
            texture.Resize(resolution, resolution);
        }

        Vector3 point00 = transform.TransformPoint(new Vector3(-0.5f, -0.5f));
        Vector3 point10 = transform.TransformPoint(new Vector3(0.5f, -0.5f));
        Vector3 point01 = transform.TransformPoint(new Vector3(-0.5f, 0.5f));
        Vector3 point11 = transform.TransformPoint(new Vector3(0.5f, 0.5f));

        NoiseMethod method   = NoiseLibrary.noiseMethods[(int)type][dimensions - 1];
        float       stepSize = 1f / resolution;

        for (int y = 0; y < resolution; y++)
        {
            Vector3 point0 = Vector3.Lerp(point00, point01, (y + 0.5f) * stepSize);
            Vector3 point1 = Vector3.Lerp(point10, point11, (y + 0.5f) * stepSize);
            for (int x = 0; x < resolution; x++)
            {
                Vector3 point  = Vector3.Lerp(point0, point1, (x + 0.5f) * stepSize);
                float   sample = NoiseLibrary.Sum(method, point, frequency, octaves, lacunarity, persistence).value;
                if (type != NoiseMethodType.Value)
                {
                    sample = sample * 0.5f + 0.5f;
                }
                texture.SetPixel(x, y, coloring.Evaluate(sample));
            }
        }
        texture.Apply();
    }
示例#3
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    = NoiseLibrary.noiseMethods[(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 = NoiseLibrary.Sum(method, point, frequency, octaves, lacunarity, persistence);
                sample = type == NoiseMethodType.Value ? (sample - 0.5f) : (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;
                }
                normals[v] = new Vector3(-sample.derivative.x, 1f, -sample.derivative.y).normalized;
            }
        }
        mesh.vertices = vertices;
        mesh.colors   = colors;
        if (!analyticalDerivatives)
        {
            CalculateNormals();
        }
        mesh.normals = normals;
    }
示例#4
0
    private void PositionParticles()
    {
        morphOffset += Time.deltaTime * morphSpeed;
        if (morphOffset > 256f)
        {
            morphOffset -= 256f;
        }

        Quaternion  q         = Quaternion.Euler(rotation);
        Quaternion  qInv      = Quaternion.Inverse(q);
        NoiseMethod method    = NoiseLibrary.noiseMethods[(int)type][dimensions - 1];
        float       amplitude = damping ? strength / frequency : strength;

        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  = NoiseLibrary.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 = NoiseLibrary.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 = NoiseLibrary.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;
        }
    }