Пример #1
0
    private void DrawIllumination()
    {
        RaycastHit2D[] hits = null;
        _source.GetHits(ref hits);
        if (hits.Length == 0)
        {
            return;
        }

        // pre-calc some values
        int rayCount  = 4 * hits.Length;        // 4 rays per hit
        int vertCount = rayCount + 1;           // 1 vert per ray, +1 for centre
        var rays      = new LightRay[rayCount];
        var verts     = new Vector3[vertCount];
        var uv        = new Vector2[verts.Length];
        var indices   = new int[3 * rayCount];

        verts[0] = _source.Position;

        int ri = 0;

        for (int i = 0; i < hits.Length; i++)
        {
            var collider = hits[i].collider as CircleCollider2D;
            _source.GetRays(ref rays, ri, collider);
            ri += 4;
        }

        System.Array.Sort(rays, _comparer);

        for (int i = 0; i < rays.Length; i++)
        {
            verts[i + 1] = rays[i].end;
        }

        // verts[0] is mesh centre
        for (int i = 0; i < rayCount - 1; i++)
        {
            int ti = 3 * i;

            indices[ti]     = 0;
            indices[ti + 1] = i + 1;
            indices[ti + 2] = i + 2;
        }
        var li = indices.Length;

        indices[li - 3] = 0;
        indices[li - 2] = vertCount - 1;
        indices[li - 1] = 1;

        _mesh.Clear();
        _mesh.vertices  = verts;
        _mesh.triangles = indices;
        _mesh.RecalculateNormals();
        _mesh.RecalculateBounds();
        var size = _source.range * 2;

        for (int i = 0; i < verts.Length; i++)
        {
            var tv = (Vector2)verts[i] - _source.Position;

            uv[i].x = tv.x / size;
            uv[i].y = tv.y / size;
        }
        _mesh.uv = uv;
    }