コード例 #1
0
    void BuildRenderers()
    {
        // Update the particle array.
        var mainModule = _target.main;
        var maxCount   = mainModule.maxParticles;

        if (_particleBuffer == null || _particleBuffer.Length != maxCount)
        {
            _particleBuffer = new ParticleSystem.Particle[maxCount];
        }

        var count = _target.GetParticles(_particleBuffer);

        // Update the input vertex array.
        var rendererModule = _target.GetComponent <ParticleSystemRenderer>();
        var template       = rendererModule.mesh;

        template.GetVertices(_vtx_in);
        template.GetNormals(_nrm_in);
        template.GetTangents(_tan_in);
        template.GetUVs(0, _uv0_in);
        template.GetIndices(_idx_in, 0);

        // Clear the output vertex array.
        _vtx_out.Clear();
        _nrm_out.Clear();
        _tan_out.Clear();
        _uv0_out.Clear();
        _idx_out.Clear();

        // Bake the particles.
        for (var i = 0; i < count; i++)
        {
            BakeParticle(i, mainModule.startRotation3D);

            // Flush the current vertex array into a temporary renderer when:
            // - This particle is the last one.
            // - Vertex count is going to go over the 64k limit.
            if (i == count - 1 || _vtx_out.Count + _vtx_in.Count > 65535)
            {
                // Build a mesh with the output vertex buffer.
                var mesh = new Mesh();
                mesh.hideFlags = HideFlags.HideAndDontSave;

                mesh.SetVertices(_vtx_out);
                mesh.SetNormals(_nrm_out);
                mesh.SetTangents(_tan_out);
                mesh.SetUVs(0, _uv0_out);
                mesh.SetTriangles(_idx_out, 0, true);

                // Allocate a temporary renderer and give the mesh.
                var renderer = TempRenderer.Allocate();
                renderer.SetTransform(transform);
                renderer.mesh     = mesh;
                renderer.material = rendererModule.sharedMaterial;

                _renderers.Push(renderer);

                // Clear the output vertex array.
                _vtx_out.Clear();
                _nrm_out.Clear();
                _tan_out.Clear();
                _uv0_out.Clear();
                _idx_out.Clear();
            }
        }
    }
コード例 #2
0
    void BuildMesh()
    {
        _vbuffer.Clear();
        _ibuffer.Clear();
        _uv.Clear();

        var hash = new Klak.Math.XXHash(1000);

        for (var i = 0; i < _triangleCount; i++)
        {
            var seed = (_randomSeed + Mathf.FloorToInt(i * 0.1f + _time)) * 10000;

            var i1 = i * 3;
            var i2 = i1 + 1;
            var i3 = i2 + 1;

            var v1 = RandomPoint(hash, i1 + seed);
            var v2 = RandomPoint(hash, i2 + seed);
            var v3 = RandomPoint(hash, i3 + seed);

            v2 = (v1 + (v2 - v1).normalized * _triangleExtent).normalized;
            v3 = (v1 + (v3 - v1).normalized * _triangleExtent).normalized;

            var l1 = Perlin.Noise(v1 * _noiseFrequency + _noiseOffset);
            var l2 = Perlin.Noise(v2 * _noiseFrequency + _noiseOffset);
            var l3 = Perlin.Noise(v3 * _noiseFrequency + _noiseOffset);

            l1 = Mathf.Abs(l1 * l1 * l1);
            l2 = Mathf.Abs(l2 * l2 * l2);
            l3 = Mathf.Abs(l3 * l3 * l3);

            v1 *= 1 + l1 * _noiseAmplitude;
            v2 *= 1 + l2 * _noiseAmplitude;
            v3 *= 1 + l3 * _noiseAmplitude;

            _vbuffer.Add(v1);
            _vbuffer.Add(v2);
            _vbuffer.Add(v3);

            _uv.Add(Vector2.zero);
            _uv.Add(Vector2.zero);
            _uv.Add(Vector2.zero);

            _ibuffer.Add(i1);
            _ibuffer.Add(i2);
            _ibuffer.Add(i3);
        }

        _mesh           = new Mesh();
        _mesh.hideFlags = HideFlags.DontSave;
        _mesh.SetVertices(_vbuffer);
        _mesh.SetUVs(0, _uv);
        _mesh.SetTriangles(_ibuffer, 0);
        _mesh.RecalculateNormals();

        _vbuffer.Clear();
        _ibuffer.Clear();

        _renderer          = TempRenderer.Allocate();
        _renderer.mesh     = _mesh;
        _renderer.material = _material;
    }